pytorch/scripts/build_android.sh
Pieter Noordhuis 9575364d30 Update protobuf detection
Summary:
The scripts/build_local.sh script would always build protoc from the
third_party protobuf tree and override the PROTOBUF_PROTOC_EXECUTABLE
CMake variable. This variable is used by the protobuf CMake files, so
it doesn't let us detect whether the protoc was specified by the user
or by the protobuf CMake files (e.g. an existing installation). This
in turn led to a problem where system installed headers would be
picked up while using protoc built from third_party. This only works
if the system installed version matches the version included in the
Caffe2 tree. Therefore, this commit changes the variable to specify a
custom protoc executable to CAFFE2_CUSTOM_PROTOC_EXECUTABLE, and
forces the use of the bundled libprotobuf when it is specified.

The result is that we now EITHER specify a custom protoc (as required
for cross-compilation where protoc must be compiled for the host and
libprotobuf for the target architecture) and use libprotobuf from the
Caffe2 tree, OR use system protobuf.

If system protobuf cannot be found, we fall back to building protoc
and libprotobuf in tree and packaging it as part of the Caffe2 build
artifacts.
Closes https://github.com/caffe2/caffe2/pull/1328

Differential Revision: D6032836

Pulled By: pietern

fbshipit-source-id: b75f8dd88412f02c947dc81ca43f7b2788da51e5
2017-10-12 11:48:50 -07:00

77 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
##############################################################################
# Example command to build the android target.
##############################################################################
#
# This script shows how one can build a Caffe2 binary for the Android platform
# using android-cmake. A few notes:
#
# (1) This build also does a host build for protobuf. You will need autoconf
# to carry out this. If autoconf is not possible, you will need to provide
# a pre-built protoc binary that is the same version as the protobuf
# version under third_party.
# If you are building on Mac, you might need to install autotool and
# libtool. The easiest way is via homebrew:
# brew install automake
# brew install libtool
# (2) You will need to have android ndk installed. The current script assumes
# that you set ANDROID_NDK to the location of ndk.
# (3) The toolchain and the build target platform can be specified with the
# cmake arguments below. For more details, check out android-cmake's doc.
CAFFE2_ROOT="$( cd "$(dirname "$0")"/.. ; pwd -P)"
echo "Caffe2 codebase root is: $CAFFE2_ROOT"
if [ -z "$ANDROID_NDK" ]; then
echo "Did you set ANDROID_NDK variable?"
exit 1
fi
if [ -d "$ANDROID_NDK" ]; then
echo "Using Android ndk at $ANDROID_NDK"
else
echo "Cannot find ndk: did you install it under $ANDROID_NDK?"
exit 1
fi
# We are going to build the target into build_android.
BUILD_ROOT=$CAFFE2_ROOT/build_android
mkdir -p $BUILD_ROOT
echo "Build Caffe2 Android into: $BUILD_ROOT"
# Build protobuf from third_party so we have a host protoc binary.
echo "Building protoc"
$CAFFE2_ROOT/scripts/build_host_protoc.sh || exit 1
# Now, actually build the android target.
echo "Building caffe2"
cd $BUILD_ROOT
cmake .. \
-DCMAKE_TOOLCHAIN_FILE=../third_party/android-cmake/android.toolchain.cmake \
-DCMAKE_INSTALL_PREFIX=../install \
-DANDROID_NDK=$ANDROID_NDK \
-DCMAKE_BUILD_TYPE=Release \
-DANDROID_ABI="armeabi-v7a with NEON FP16" \
-DANDROID_NATIVE_API_LEVEL=21 \
-DUSE_CUDA=OFF \
-DBUILD_TEST=OFF \
-DUSE_LMDB=OFF \
-DUSE_LEVELDB=OFF \
-DBUILD_PYTHON=OFF \
-DCAFFE2_CUSTOM_PROTOC_EXECUTABLE=$CAFFE2_ROOT/build_host_protoc/bin/protoc \
-DCMAKE_VERBOSE_MAKEFILE=1 \
-DUSE_MPI=OFF \
-DUSE_OPENMP=OFF \
-DBUILD_SHARED_LIBS=OFF \
-DCMAKE_CXX_FLAGS_RELEASE=-s \
-DUSE_OPENCV=OFF \
$@ \
|| exit 1
# Cross-platform parallel build
if [ "$(uname)" = 'Darwin' ]; then
cmake --build . -- "-j$(sysctl -n hw.ncpu)"
else
cmake --build . -- "-j$(nproc)"
fi