1#!/bin/bash -e 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# Builds protobuf 3 for Android. Pass in the location of your NDK as the first 17# argument to the script, for example: 18# tensorflow/contrib/makefile/compile_android_protobuf.sh \ 19# ${HOME}/toolchains/clang-21-stl-gnu 20 21# Pass ANDROID_API_VERSION as an environment variable to support 22# a different version of API. 23android_api_version="${ANDROID_API_VERSION:-21}" 24# Pass cc prefix to set the prefix for cc (e.g. ccache) 25cc_prefix="${CC_PREFIX}" 26 27usage() { 28 echo "Usage: $(basename "$0") [-a:c]" 29 echo "-a [Architecture] Architecture of target android [default=armeabi-v7a] \ 30(supported architecture list: \ 31arm64-v8a armeabi armeabi-v7a armeabi-v7a-hard mips mips64 x86 x86_64)" 32 echo "-c Clean before building protobuf for target" 33 echo "\"NDK_ROOT\" should be defined as an environment variable." 34 exit 1 35} 36 37SCRIPT_DIR=$(dirname $0) 38ARCHITECTURE=armeabi-v7a 39 40# debug options 41while getopts "a:c" opt_name; do 42 case "$opt_name" in 43 a) ARCHITECTURE=$OPTARG;; 44 c) clean=true;; 45 *) usage;; 46 esac 47done 48shift $((OPTIND - 1)) 49 50source "${SCRIPT_DIR}/build_helper.subr" 51JOB_COUNT="${JOB_COUNT:-$(get_job_count)}" 52 53if [[ -z "${NDK_ROOT}" ]] 54then 55 echo "You need to pass in the Android NDK location as the environment \ 56variable" 57 echo "e.g. NDK_ROOT=${HOME}/android_ndk/android-ndk-rXXx \ 58tensorflow/contrib/makefile/compile_android_protobuf.sh" 59 exit 1 60fi 61 62if [[ ! -f "${SCRIPT_DIR}/Makefile" ]]; then 63 echo "Makefile not found in ${SCRIPT_DIR}" 1>&2 64 exit 1 65fi 66 67cd "${SCRIPT_DIR}" 68if [ $? -ne 0 ] 69then 70 echo "cd to ${SCRIPT_DIR} failed." 1>&2 71 exit 1 72fi 73 74GENDIR="$(pwd)/gen/protobuf_android" 75HOST_GENDIR="$(pwd)/gen/protobuf-host" 76mkdir -p "${GENDIR}" 77mkdir -p "${GENDIR}/${ARCHITECTURE}" 78 79if [[ ! -f "./downloads/protobuf/autogen.sh" ]]; then 80 echo "You need to download dependencies before running this script." 1>&2 81 echo "tensorflow/contrib/makefile/download_dependencies.sh" 1>&2 82 exit 1 83fi 84 85cd downloads/protobuf 86 87PROTOC_PATH="${HOST_GENDIR}/bin/protoc" 88if [[ ! -f "${PROTOC_PATH}" || ${clean} == true ]]; then 89 # Try building compatible protoc first on host 90 echo "protoc not found at ${PROTOC_PATH}. Build it first." 91 make_host_protoc "${HOST_GENDIR}" 92else 93 echo "protoc found. Skip building host tools." 94fi 95 96 97echo $OSTYPE | grep -q "darwin" && os_type="darwin" || os_type="linux" 98if [[ ${ARCHITECTURE} == "arm64-v8a" ]]; then 99 toolchain="aarch64-linux-android-4.9" 100 sysroot_arch="arm64" 101 bin_prefix="aarch64-linux-android" 102elif [[ ${ARCHITECTURE} == "armeabi" ]]; then 103 toolchain="arm-linux-androideabi-4.9" 104 sysroot_arch="arm" 105 bin_prefix="arm-linux-androideabi" 106elif [[ ${ARCHITECTURE} == "armeabi-v7a" ]]; then 107 toolchain="arm-linux-androideabi-4.9" 108 sysroot_arch="arm" 109 bin_prefix="arm-linux-androideabi" 110 march_option="-march=armv7-a" 111elif [[ ${ARCHITECTURE} == "armeabi-v7a-hard" ]]; then 112 toolchain="arm-linux-androideabi-4.9" 113 sysroot_arch="arm" 114 bin_prefix="arm-linux-androideabi" 115 march_option="-march=armv7-a" 116elif [[ ${ARCHITECTURE} == "mips" ]]; then 117 toolchain="mipsel-linux-android-4.9" 118 sysroot_arch="mips" 119 bin_prefix="mipsel-linux-android" 120elif [[ ${ARCHITECTURE} == "mips64" ]]; then 121 toolchain="mips64el-linux-android-4.9" 122 sysroot_arch="mips64" 123 bin_prefix="mips64el-linux-android" 124elif [[ ${ARCHITECTURE} == "x86" ]]; then 125 toolchain="x86-4.9" 126 sysroot_arch="x86" 127 bin_prefix="i686-linux-android" 128elif [[ ${ARCHITECTURE} == "x86_64" ]]; then 129 toolchain="x86_64-4.9" 130 sysroot_arch="x86_64" 131 bin_prefix="x86_64-linux-android" 132else 133 echo "architecture ${ARCHITECTURE} is not supported." 1>&2 134 usage 135 exit 1 136fi 137 138echo "Android api version = ${android_api_version} cc_prefix = ${cc_prefix}" 139 140export PATH=\ 141"${NDK_ROOT}/toolchains/${toolchain}/prebuilt/${os_type}-x86_64/bin:$PATH" 142export SYSROOT=\ 143"${NDK_ROOT}/platforms/android-${android_api_version}/arch-${sysroot_arch}" 144export CC="${cc_prefix} ${bin_prefix}-gcc --sysroot ${SYSROOT}" 145export CXX="${cc_prefix} ${bin_prefix}-g++ --sysroot ${SYSROOT}" 146export CXXSTL=\ 147"${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ARCHITECTURE}" 148 149./autogen.sh 150if [ $? -ne 0 ] 151then 152 echo "./autogen.sh command failed." 153 exit 1 154fi 155 156./configure --prefix="${GENDIR}/${ARCHITECTURE}" \ 157--host="${bin_prefix}" \ 158--with-sysroot="${SYSROOT}" \ 159--disable-shared \ 160--enable-cross-compile \ 161--with-protoc="${PROTOC_PATH}" \ 162CFLAGS="${march_option}" \ 163CXXFLAGS="-frtti -fexceptions ${march_option} \ 164-I${NDK_ROOT}/sources/android/support/include \ 165-I${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.9/include \ 166-I${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ARCHITECTURE}/include" \ 167LDFLAGS="-L${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ARCHITECTURE}" \ 168LIBS="-llog -lz -lgnustl_static" 169 170if [ $? -ne 0 ] 171then 172 echo "./configure command failed." 173 exit 1 174fi 175 176if [[ ${clean} == true ]]; then 177 echo "clean before build" 178 make clean 179fi 180 181make -j"${JOB_COUNT}" 182if [ $? -ne 0 ] 183then 184 echo "make command failed." 185 exit 1 186fi 187 188make install 189 190echo "$(basename $0) finished successfully!!!" 191