add install step and docs for Android build (#17298)

Summary:
This commit did below enhancements:
1, add doc for build_android.sh;
2, add install step for build_android.sh, thus the headers and libraries can be collected together for further usage conveniently;
3, change the default INSTALL_PREFIX from $PYTORCH_ROOT/install to $PYTORCH_ROOT/build_android/install to make the project directory clean.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/17298

Differential Revision: D14149709

Pulled By: soumith

fbshipit-source-id: a3a38cb41f26377e21aa89e49e57e8f21c9c1a39
This commit is contained in:
Gemfield 2019-02-20 06:59:31 -08:00 committed by Facebook Github Bot
parent 1b3315ec17
commit c3a23379ea
2 changed files with 37 additions and 5 deletions

23
scripts/README.md Normal file
View File

@ -0,0 +1,23 @@
This directory contains the usful tools.
## build_android.sh
This script is to build PyTorch/Caffe2 library for Android. Take following steps to start the build:
- set ANDROID_NDK to the location of ndk
```bash
export ANDROID_NDK=YOUR_NDK_PATH
```
- run build_android.sh
```bash
#in your PyTorch root directory
bash scripts/build_android.sh
```
If succeeded, the libraries and headers would be generated to build_android/install directory. You can then copy these files from build_android/install to your Android project for further usage.
You can also override the cmake flags via command line, e.g., following command will also compile the executable binary files:
```bash
bash scripts/build_android.sh -DBUILD_BINARY=ON
```

View File

@ -21,8 +21,12 @@
set -e
CAFFE2_ROOT="$( cd "$(dirname "$0")"/.. ; pwd -P)"
# Android specific flags
ANDROID_ABI="armeabi-v7a with NEON"
ANDROID_NATIVE_API_LEVEL="21"
echo "Build with ANDROID_ABI[$ANDROID_ABI], ANDROID_NATIVE_API_LEVEL[$ANDROID_NATIVE_API_LEVEL]"
CAFFE2_ROOT="$( cd "$(dirname "$0")"/.. ; pwd -P)"
if [ -z "$ANDROID_NDK" ]; then
echo "ANDROID_NDK not set; please set it to the Android NDK directory"
exit 1
@ -43,6 +47,7 @@ $CAFFE2_ROOT/scripts/build_host_protoc.sh
# Now, actually build the Android target.
BUILD_ROOT=${BUILD_ROOT:-"$CAFFE2_ROOT/build_android"}
INSTALL_PREFIX=${BUILD_ROOT}/install
mkdir -p $BUILD_ROOT
cd $BUILD_ROOT
@ -74,7 +79,6 @@ CMAKE_ARGS+=("-DUSE_LMDB=OFF")
CMAKE_ARGS+=("-DUSE_LEVELDB=OFF")
CMAKE_ARGS+=("-DUSE_MPI=OFF")
CMAKE_ARGS+=("-DUSE_OPENMP=OFF")
# Only toggle if VERBOSE=1
if [ "${VERBOSE:-}" == '1' ]; then
CMAKE_ARGS+=("-DCMAKE_VERBOSE_MAKEFILE=1")
@ -82,15 +86,15 @@ fi
# Android specific flags
CMAKE_ARGS+=("-DANDROID_NDK=$ANDROID_NDK")
CMAKE_ARGS+=("-DANDROID_ABI=armeabi-v7a with NEON")
CMAKE_ARGS+=("-DANDROID_NATIVE_API_LEVEL=21")
CMAKE_ARGS+=("-DANDROID_ABI=$ANDROID_ABI")
CMAKE_ARGS+=("-DANDROID_NATIVE_API_LEVEL=$ANDROID_NATIVE_API_LEVEL")
CMAKE_ARGS+=("-DANDROID_CPP_FEATURES=rtti exceptions")
# Use-specified CMake arguments go last to allow overridding defaults
CMAKE_ARGS+=($@)
cmake "$CAFFE2_ROOT" \
-DCMAKE_INSTALL_PREFIX=../install \
-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX \
-DCMAKE_BUILD_TYPE=Release \
"${CMAKE_ARGS[@]}"
@ -103,3 +107,8 @@ if [ -z "$MAX_JOBS" ]; then
fi
fi
cmake --build . -- "-j${MAX_JOBS}"
# copy headers and libs to install directory
echo "Will install headers and libs to $INSTALL_PREFIX for further Android project usage."
make install
echo "Installation completed, now you can copy the headers/libs from $INSTALL_PREFIX to your Android project directory."