diff options
author | 2016-06-15 00:05:51 -0800 | |
---|---|---|
committer | 2016-06-15 01:18:12 -0700 | |
commit | 5b7d0716f7fad28bcf5490d2b6406be651036b14 (patch) | |
tree | 51a2736b9e1b34083de6443950f80ac8289a8312 | |
parent | cc40cd3b0a8b83f5ee071b7ee32c17b56815a89c (diff) |
Fix android build of protobuf with android_ndk r10e
Change: 124926049
-rw-r--r-- | tensorflow/contrib/makefile/.gitignore | 2 | ||||
-rwxr-xr-x | tensorflow/contrib/makefile/compile_android_protobuf.sh | 182 |
2 files changed, 155 insertions, 29 deletions
diff --git a/tensorflow/contrib/makefile/.gitignore b/tensorflow/contrib/makefile/.gitignore new file mode 100644 index 0000000000..3ff6e609f4 --- /dev/null +++ b/tensorflow/contrib/makefile/.gitignore @@ -0,0 +1,2 @@ +gen/ +downloads/ diff --git a/tensorflow/contrib/makefile/compile_android_protobuf.sh b/tensorflow/contrib/makefile/compile_android_protobuf.sh index 4be5b2868e..167e4a1b38 100755 --- a/tensorflow/contrib/makefile/compile_android_protobuf.sh +++ b/tensorflow/contrib/makefile/compile_android_protobuf.sh @@ -18,30 +18,146 @@ # tensorflow/contrib/makefile/compile_android_protobuf.sh \ # ${HOME}/toolchains/clang-21-stl-gnu -if [[ $# -ne 1 ]] +# Pass ANDROID_API_VERSION as an environment variable to support +# a different version of API. +android_api_version="${ANDROID_API_VERSION:-21}" +# Pass cc prefix to set the prefix for cc (e.g. ccache) +cc_prefix="${CC_PREFIX}" + +usage() { + echo "Usage: $(basename "$0") [-a:c]" + echo "-a [Architecture] Architecture of target android [default=armeabi-v7a] \ +(supported archtecture list: \ +arm64-v8a armeabi armeabi-v7a armeabi-v7a-hard mips mips64 x86 x86_64)" + echo "-c Clean before building protobuf for target" + echo "\"NDK_ROOT\" should be defined as an environment variable." + exit 1 +} + +SCRIPT_DIR=$(dirname $0) +ARCHITECTURE=armeabi-v7a + +# debug options +while getopts "a:c" opt_name; do + case "$opt_name" in + a) ARCHITECTURE=$OPTARG;; + c) clean=true;; + *) usage;; + esac +done +shift $((OPTIND - 1)) + +if [[ -z "${NDK_ROOT}" ]] then - echo "You need to pass in the Android NDK as the first argument, e.g:" - echo "tensorflow/contrib/makefile/compile_android_protobuf.sh \ - ${HOME}/toolchains/clang-21-stl-gnu" + echo "You need to pass in the Android NDK location as the environment \ +variable" + echo "e.g. NDK_ROOT=${HOME}/android_ndk/android-ndk-rXXx \ +tensorflow/contrib/makefile/compile_android_protobuf.sh" exit 1 fi -cd tensorflow/contrib/makefile +if [[ ! -f "${SCRIPT_DIR}/Makefile" ]]; then + echo "Makefile not found in ${SCRIPT_DIR}" 1>&2 + exit 1 +fi + +cd "${SCRIPT_DIR}" +if [ $? -ne 0 ] +then + echo "cd to ${SCRIPT_DIR} failed." 1>&2 + exit 1 +fi -GENDIR=`pwd`/gen/protobuf/ -LIBDIR=${GENDIR}lib -mkdir -p ${LIBDIR} +GENDIR="$(pwd)/gen/protobuf" +HOST_GENDIR="$(pwd)/gen/protobuf-host" +mkdir -p "${GENDIR}" +mkdir -p "${HOST_GENDIR}" + +if [[ ! -f "./downloads/protobuf/autogen.sh" ]]; then + echo "You need to download dependencies before running this script." 1>&2 + echo "tensorflow/contrib/makefile/download_dependencies.sh" 1>&2 + exit 1 +fi -export NDK=$1 -export PATH=${NDK}/bin:$PATH -export SYSROOT=${NDK}/sysroot -export CC="arm-linux-androideabi-gcc --sysroot $SYSROOT" -export CXX="arm-linux-androideabi-g++ --sysroot $SYSROOT" -export CXXSTL=$NDK/sources/cxx-stl/gnu-libstdc++/4.6 - cd downloads/protobuf -mkdir build +PROTOC_PATH="${HOST_GENDIR}/bin/protoc" + +if [[ ! -f "${PROTOC_PATH}" || ${clean} == true ]]; then + # Try building compatible protoc first on host + echo "protoc not found at ${PROTOC_PATH}. Build it first." + ./autogen.sh + if [ $? -ne 0 ] + then + echo "./autogen.sh command failed." + exit 1 + fi + make clean + rm -rf "${HOST_GENDIR}" + mkdir -p "${HOST_GENDIR}" + ./configure --disable-shared --prefix="${HOST_GENDIR}" + make + if [ $? -ne 0 ]; then + echo "make failed" + exit 1 + fi + make install +else + echo "protoc found. Skip building host tools." +fi + + +echo $OSTYPE | grep -q "darwin" && os_type="darwin" || os_type="linux" +if [[ ${ARCHITECTURE} == "arm64-v8a" ]]; then + toolchain="aarch64-linux-android-4.9" + sysroot_arch="arm64" + bin_prefix="aarch64-linux-android" +elif [[ ${ARCHITECTURE} == "armeabi" ]]; then + toolchain="arm-linux-androideabi-4.9" + sysroot_arch="arm" + bin_prefix="arm-linux-androideabi" +elif [[ ${ARCHITECTURE} == "armeabi-v7a" ]]; then + toolchain="arm-linux-androideabi-4.9" + sysroot_arch="arm" + bin_prefix="arm-linux-androideabi" + march_option="-march=armv7-a" +elif [[ ${ARCHITECTURE} == "armeabi-v7a-hard" ]]; then + toolchain="arm-linux-androideabi-4.9" + sysroot_arch="arm" + bin_prefix="arm-linux-androideabi" + march_option="-march=armv7-a" +elif [[ ${ARCHITECTURE} == "mips" ]]; then + toolchain="mipsel-linux-android-4.9" + sysroot_arch="mips" + bin_prefix="mipsel-linux-android" +elif [[ ${ARCHITECTURE} == "mips64" ]]; then + toolchain="mips64el-linux-android-4.9" + sysroot_arch="mips64" + bin_prefix="mips64el-linux-android" +elif [[ ${ARCHITECTURE} == "x86" ]]; then + toolchain="x86-4.9" + sysroot_arch="x86" + bin_prefix="i686-linux-android" +elif [[ ${ARCHITECTURE} == "x86_64" ]]; then + toolchain="x86_64-4.9" + sysroot_arch="x86_64" + bin_prefix="x86_64-linux-android" +else + echo "archtecture ${arcitecture} is not supported." 1>&2 + usage + exit 1 +fi + +echo "Android api version = ${android_api_version} cc_prefix = ${cc_prefix}" + +export PATH=\ +"${NDK_ROOT}/toolchains/${toolchain}/prebuilt/${os_type}-x86_64/bin:$PATH" +export SYSROOT=\ +"${NDK_ROOT}/platforms/android-${android_api_version}/arch-${sysroot_arch}" +export CC="${cc_prefix} ${bin_prefix}-gcc --sysroot ${SYSROOT}" +export CXX="${cc_prefix} ${bin_prefix}-g++ --sysroot ${SYSROOT}" +export CXXSTL=\ +"${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ARCHITECTURE}" ./autogen.sh if [ $? -ne 0 ] @@ -49,21 +165,32 @@ then echo "./autogen.sh command failed." exit 1 fi - -./configure --prefix=$(pwd)/build \ ---host=arm-linux-androideabi \ ---with-sysroot=$SYSROOT \ + +./configure --prefix="${GENDIR}" \ +--host="${bin_prefix}" \ +--with-sysroot="${SYSROOT}" \ --disable-shared \ --enable-cross-compile \ ---with-protoc=protoc \ -CFLAGS="-march=armv7-a" \ -CXXFLAGS="-march=armv7-a -I$CXXSTL/include -I$CXXSTL/libs/armeabi-v7a/include" +--with-protoc="${PROTOC_PATH}" \ +CFLAGS="${march_option}" \ +CXXFLAGS="-frtti -fexceptions ${march_option} \ +-I${NDK_ROOT}/sources/android/support/include \ +-I${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.9/include \ +-I${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ARCHITECTURE}/include" \ +LDFLAGS="-L${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ARCHITECTURE}" \ +LIBS="-lz -lgnustl_static" + if [ $? -ne 0 ] then echo "./configure command failed." exit 1 fi +if [[ ${clean} == true ]]; then + echo "clean before build" + make clean +fi + make if [ $? -ne 0 ] then @@ -71,9 +198,6 @@ then exit 1 fi -cp src/.libs/* ${LIBDIR} -if [ $? -ne 0 ] -then - echo "cp command failed." - exit 1 -fi +make install + +echo "$(basename $0) finished successfully!!!" |