1 #!/usr/bin/env bash 2 # Copyright 2015 The TensorFlow Authors. All Rights Reserved. 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 # ============================================================================== 16 17 set -e 18 19 copy_lib() { 20 FILE=$1 21 TARGET_DIR=${OUT_DIR}/native/$(basename $FILE)/${CPU} 22 mkdir -p ${TARGET_DIR} 23 echo "Copying ${FILE} to ${TARGET_DIR}" 24 cp ${FILE} ${TARGET_DIR} 25 } 26 27 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 28 source "${SCRIPT_DIR}/builds_common.sh" 29 configure_android_workspace 30 31 CPUS=armeabi-v7a,arm64-v8a,x86,x86_64 32 33 OUT_DIR="$(pwd)/out/" 34 AAR_LIB_TMP="$(pwd)/aar_libs" 35 36 rm -rf ${OUT_DIR} 37 rm -rf ${AAR_LIB_TMP} 38 39 # Build all relevant native libraries for each architecture. 40 for CPU in ${CPUS//,/ } 41 do 42 echo "========== Building native libs for Android ${CPU} ==========" 43 bazel build -c opt --config=monolithic --cpu=${CPU} \ 44 --crosstool_top=//external:android/crosstool \ 45 --host_crosstool_top=@bazel_tools//tools/cpp:toolchain \ 46 //tensorflow/core:android_tensorflow_lib \ 47 //tensorflow/contrib/android:libtensorflow_inference.so \ 48 //tensorflow/examples/android:libtensorflow_demo.so \ 49 //tensorflow/tools/benchmark:benchmark_model 50 51 copy_lib bazel-bin/tensorflow/core/libandroid_tensorflow_lib.lo 52 copy_lib bazel-bin/tensorflow/contrib/android/libtensorflow_inference.so 53 copy_lib bazel-bin/tensorflow/examples/android/libtensorflow_demo.so 54 copy_lib bazel-bin/tensorflow/tools/benchmark/benchmark_model 55 56 mkdir -p ${AAR_LIB_TMP}/jni/${CPU} 57 cp bazel-bin/tensorflow/contrib/android/libtensorflow_inference.so ${AAR_LIB_TMP}/jni/${CPU} 58 done 59 60 # Build Jar and also demo containing native libs for all architectures. 61 # Enable sandboxing so that zip archives don't get incorrectly packaged 62 # in assets/ dir (see https://github.com/bazelbuild/bazel/issues/2334) 63 # TODO(gunan): remove extra flags once sandboxing is enabled for all builds. 64 echo "========== Building TensorFlow Android Jar and Demo ==========" 65 bazel --bazelrc=/dev/null build -c opt --config=monolithic --fat_apk_cpu=${CPUS} \ 66 --spawn_strategy=sandboxed --genrule_strategy=sandboxed \ 67 //tensorflow/contrib/android:android_tensorflow_inference_java \ 68 //tensorflow/contrib/android:android_tensorflow_inference_java.aar \ 69 //tensorflow/examples/android:tensorflow_demo 70 71 echo "Copying demo, AAR and Jar to ${OUT_DIR}" 72 cp bazel-bin/tensorflow/examples/android/tensorflow_demo.apk \ 73 bazel-bin/tensorflow/contrib/android/libandroid_tensorflow_inference_java.jar ${OUT_DIR} 74 75 cp bazel-bin/tensorflow/contrib/android/android_tensorflow_inference_java.aar \ 76 ${OUT_DIR}/tensorflow.aar 77 78 # TODO(andrewharp): build native libs into AAR directly once 79 # https://github.com/bazelbuild/bazel/issues/348 is resolved. 80 echo "Adding native libs to AAR" 81 chmod +w ${OUT_DIR}/tensorflow.aar 82 pushd ${AAR_LIB_TMP} 83 zip -ur ${OUT_DIR}/tensorflow.aar $(find jni -name *.so) 84 popd 85 rm -rf ${AAR_LIB_TMP} 86 87 # Test Makefile build just to make sure it still works. 88 if [ -z "$NDK_ROOT" ]; then 89 export NDK_ROOT=${ANDROID_NDK_HOME} 90 fi 91 92 echo "========== Benchmark Makefile Build Test ==========" 93 tensorflow/contrib/makefile/build_all_android.sh 94 95 echo "========== Demo Makefile Build Test ==========" 96 tensorflow/contrib/makefile/build_all_android.sh \ 97 -s $(pwd)/tensorflow/contrib/makefile/sub_makefiles/android/Makefile.in \ 98 -t "libtensorflow_inference.so libtensorflow_demo.so" 99 100 # Test Makefile build for tensorflow runtime with hexagon. 101 # -b ... build only, -p ... use prebuilt binaries 102 # This uses prebuilt binaries for hexagon dependencies because Building 103 # hexagon binaries from source code requires qualcomm sdk. 104 echo "========== Hexagon Build Test ==========" 105 tensorflow/contrib/makefile/samples/build_and_run_inception_hexagon.sh -bp 106