Unverified Commit 91792109 authored by Luca Boccassi's avatar Luca Boccassi Committed by GitHub

Merge pull request #3827 from sappo/master

Improve android build handling
parents d5bd1642 1fc4f513
This diff is collapsed.
LIBZMQ ANDROID COMPILATION STEPS # Android Build
================================
To launch the docker build:
$ docker build . ## Prerequisites
To launch the android build, you have to first install the android-ndk first in your HOME directory: You need the Android Native Development Kit (NDK) installed. See
[here](https://developer.android.com/ndk) to download it.
$ cd ~ This project is tested against Android NDK version r20.
$ export ANDROID_NDK_VERSION="r10e"
$ wget -q https://dl.google.com/android/ndk/android-ndk-${ANDROID_NDK_VERSION}-linux-x86_64.bin -O android-ndk-${ANDROID_NDK_VERSION}-linux-x86_64.bin
$ #check the hashes before executing this binary
$ md5sum android-ndk-r10e-linux-x86_64.bin
# 19af543b068bdb7f27787c2bc69aba7f android-ndk-r10e-linux-x86_64.bin
$ sha256sum android-ndk-r10e-linux-x86_64.bin
# 102d6723f67ff1384330d12c45854315d6452d6510286f4e5891e00a5a8f1d5a android-ndk-r10e-linux-x86_64.bin
$ chmod +x android-ndk-${ANDROID_NDK_VERSION}-linux-x86_64.bin
$ ./android-ndk-r10e-linux-x86_64.bin
$ export ANDROID_NDK_ROOT=${HOME}/android-ndk-r10e
$ export TOOLCHAIN_NAME=arm-linux-androideabi-4.9
$ export TOOLCHAIN_HOST=arm-linux-androideabi
$ export TOOLCHAIN_PATH=${ANDROID_NDK_ROOT}/toolchains/${TOOLCHAIN_NAME}/prebuilt/linux-x86_64/bin
$ export TOOLCHAIN_ARCH=arm
Then you can launch the build: If you installed version r20 all you have to do is to expose the NDK root
directory as environment variable, e.g:
$ cd ~/libzmq/builds/android export ANDROID_NDK_ROOT=$HOME/android-ndk-r20
$ ./build.sh
Cloning into 'libsodium'...
remote: Counting objects: 15246, done.
remote: Compressing objects: 100% (182/182), done.
Receiving objects: 16% (2440/15246)
[...]
A successful build should finish with the following message and give you back your shell prompt: If you installed another version you have to expose the NDK root directory as
well as the NDK version, e.g:
[...] export ANDROID_NDK_ROOT=$HOME/android-ndk-r17c
make[2]: Leaving directory '/tmp/android_build/arm-linux-androideabi-4.9/libzmq' export NDK_VERSION=android-ndk-r17c
make[1]: Leaving directory '/tmp/android_build/arm-linux-androideabi-4.9/libzmq'
libzmq android build succeeded
$
You will then be able to see libzmq.so compiled in the prefix/ directory: To specify the minimum sdk version set the environment variable below:
$ cd ~/libzmq/builds/android/prefix/arm-linux-androideabi-4.9/lib export MIN_SDK_VERSION=21 # Default value if unset
$ ls
libsodium.a libsodium.la libsodium.so libzmq.a libzmq.la libzmq.so pkgconfig
You can then triple check that they are ARM libs: ## Build
$ cd ~/libzmq/builds/android/prefix/arm-linux-androideabi-4.9/lib In the android directory, run:
$ file libzmq.so
libzmq.so: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /system/bin/linker, not stripped
./build.sh [ arm | arm64 | x86 | x86_64 ]
COMPILE WITH DOCKERFILE
=======================
To launch the docker build with a tagged image as a result:
$ cd ~/libzmq/builds/android
$ docker build -t libzmq-android:`date +"%y%m%d-%H%M%S"` .
$ docker build -t libzmq-android:latest .
If it is successful, it will end by "libzmq android build succeeded" followed by the ContainerID "e53db616aff4":
[...]
make[2]: Leaving directory `/tmp/android_build/arm-linux-androideabi-4.9/libzmq'
make[1]: Leaving directory `/tmp/android_build/arm-linux-androideabi-4.9/libzmq'
libzmq android build succeeded
---> e53db616aff4
Removing intermediate container 8a5f3e34f3da
Successfully built e53db616aff4
$
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
libzmq-android 151218-101411 e53db616aff4 18 hours ago 5.495 GB
If you want to collect the artifacts on the build, you can specify a directory in your HOME directory, such as "$HOME/libzmq-android-bins" for example:
$ mkdir -pv $HOME/libzmq-android-bins
$ docker run -v $HOME/libzmq-android-bins:/data libzmq-android:latest "cp -v /home/zmq/libzmq/builds/android/prefix/arm-linux-androideabi-4.9/lib/* /data"
...@@ -69,6 +69,14 @@ function android_build_check_fail { ...@@ -69,6 +69,14 @@ function android_build_check_fail {
fi fi
} }
function android_build_arch {
export TOOLCHAIN_PATH="${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/${HOST_PLATFORM}/bin"
export TOOLCHAIN_HOST=$1
export TOOLCHAIN_COMP=$2
export TOOLCHAIN_CXXSTL=$3
export TOOLCHAIN_ARCH=$4
}
function android_build_env { function android_build_env {
## ##
# Check that necessary environment variables are set # Check that necessary environment variables are set
...@@ -128,7 +136,7 @@ function android_build_env { ...@@ -128,7 +136,7 @@ function android_build_env {
ANDROID_BUILD_FAIL+=(" ${ANDROID_BUILD_SYSROOT}") ANDROID_BUILD_FAIL+=(" ${ANDROID_BUILD_SYSROOT}")
fi fi
ANDROID_BUILD_PREFIX="${ANDROID_BUILD_DIR}/prefix/${TOOLCHAIN_NAME}" ANDROID_BUILD_PREFIX="${ANDROID_BUILD_DIR}/prefix/${TOOLCHAIN_ARCH}"
mkdir -p "$ANDROID_BUILD_PREFIX" || { mkdir -p "$ANDROID_BUILD_PREFIX" || {
ANDROID_BUILD_FAIL+=("Failed to make ANDROID_BUILD_PREFIX directory") ANDROID_BUILD_FAIL+=("Failed to make ANDROID_BUILD_PREFIX directory")
...@@ -201,7 +209,7 @@ function android_build_opts { ...@@ -201,7 +209,7 @@ function android_build_opts {
_android_build_opts_process_binaries _android_build_opts_process_binaries
local LIBS="-lc -lgcc -ldl -lc++_shared" local LIBS="-lc -lgcc -ldl -lm -llog -lc++_shared"
local LDFLAGS="-L${ANDROID_BUILD_PREFIX}/lib" local LDFLAGS="-L${ANDROID_BUILD_PREFIX}/lib"
LDFLAGS+=" -L${ANDROID_NDK_ROOT}/sources/cxx-stl/llvm-libc++/libs/${TOOLCHAIN_CXXSTL}" LDFLAGS+=" -L${ANDROID_NDK_ROOT}/sources/cxx-stl/llvm-libc++/libs/${TOOLCHAIN_CXXSTL}"
CFLAGS+=" -D_GNU_SOURCE -D_REENTRANT -D_THREAD_SAFE" CFLAGS+=" -D_GNU_SOURCE -D_REENTRANT -D_THREAD_SAFE"
......
#!/usr/bin/env bash #!/usr/bin/env bash
function usage {
echo "Usage ./build.sh [ arm | arm64 | x86 | x86_64 ]"
}
# Use directory of current script as the build directory and working directory # Use directory of current script as the build directory and working directory
cd "$( dirname "${BASH_SOURCE[0]}" )" cd "$( dirname "${BASH_SOURCE[0]}" )"
ANDROID_BUILD_DIR="$(pwd)" ANDROID_BUILD_DIR="$(pwd)"
...@@ -10,12 +14,73 @@ source ${ANDROID_BUILD_DIR}/android_build_helper.sh ...@@ -10,12 +14,73 @@ source ${ANDROID_BUILD_DIR}/android_build_helper.sh
# Choose a C++ standard library implementation from the ndk # Choose a C++ standard library implementation from the ndk
ANDROID_BUILD_CXXSTL="gnustl_shared_49" ANDROID_BUILD_CXXSTL="gnustl_shared_49"
BUILD_ARCH=$1
if [ -z $BUILD_ARCH ]; then
usage
exit 1
fi
case $(uname | tr '[:upper:]' '[:lower:]') in
linux*)
export HOST_PLATFORM=linux-x86_64
;;
darwin*)
export HOST_PLATFORM=darwin-x86_64
;;
*)
echo "Unsupported platform"
exit 1
;;
esac
# Set default values used in ci builds
export NDK_VERSION=${NDK_VERSION:-android-ndk-r20}
# With NDK r20, the minimum SDK version range is [16, 29].
# SDK version 21 is the minimum version for 64-bit builds.
export MIN_SDK_VERSION=${MIN_SDK_VERSION:-21}
# Set variables for each architecture
HOST_ARM="arm-linux-androideabi"
HOST_ARM64="aarch64-linux-android"
HOST_X86="i686-linux-android"
HOST_X86_64="x86_64-linux-android"
COMP_ARM="armv7a-linux-androideabi${MIN_SDK_VERSION}"
COMP_ARM64="aarch64-linux-android${MIN_SDK_VERSION}"
COMP_X86="i686-linux-android${MIN_SDK_VERSION}"
COMP_X86_64="x86_64-linux-android${MIN_SDK_VERSION}"
CXXSTL_ARM="armeabi-v7a"
CXXSTL_ARM64="arm64-v8a"
CXXSTL_X86="x86"
CXXSTL_X86_64="x86_64"
ARCH_ARM="arm"
ARCH_ARM64="arm64"
ARCH_X86="x86"
ARCH_X86_64="x86_64"
if [ $BUILD_ARCH == "arm" ]; then
android_build_arch $HOST_ARM $COMP_ARM $CXXSTL_ARM $ARCH_ARM
elif [ $BUILD_ARCH == "x86" ]; then
android_build_arch $HOST_X86 $COMP_X86 $CXXSTL_X86 $ARCH_X86
elif [ $BUILD_ARCH == "arm64" ]; then
android_build_arch $HOST_ARM64 $COMP_ARM64 $CXXSTL_ARM64 $ARCH_ARM64
elif [ $BUILD_ARCH == "x86_64" ]; then
android_build_arch $HOST_X86_64 $COMP_X86_64 $CXXSTL_X86_64 $ARCH_X86_64
else
usage
exit 1
fi
# Set up android build environment and set ANDROID_BUILD_OPTS array # Set up android build environment and set ANDROID_BUILD_OPTS array
android_build_env android_build_env
android_build_opts android_build_opts
# Use a temporary build directory # Use a temporary build directory
cache="/tmp/android_build/${TOOLCHAIN_NAME}" cache="/tmp/android_build/${TOOLCHAIN_ARCH}"
rm -rf "${cache}" rm -rf "${cache}"
mkdir -p "${cache}" mkdir -p "${cache}"
......
#!/usr/bin/env bash #!/usr/bin/env bash
NDK_VERSION=android-ndk-r20 export NDK_VERSION=android-ndk-r20
NDK_ABI_VERSION=4.9 export ANDROID_NDK_ROOT="/tmp/${NDK_VERSION}"
if [ $TRAVIS_OS_NAME == "linux" ] case $(uname | tr '[:upper:]' '[:lower:]') in
then linux*)
HOST_PLATFORM=linux-x86_64 HOST_PLATFORM=linux-x86_64
elif [ $TRAVIS_OS_NAME == "osx" ] ;;
then darwin*)
HOST_PLATFORM=darwin-x86_64 HOST_PLATFORM=darwin-x86_64
else ;;
echo "Unsupported platform $TRAVIS_OS_NAME" *)
echo "Unsupported platform"
exit 1 exit 1
fi ;;
esac
if [ ! -d "/tmp/${NDK_VERSION}" ] ; then if [ ! -d "${ANDROID_NDK_ROOT}" ]; then
export FILENAME=$NDK_VERSION-$HOST_PLATFORM.zip export FILENAME=$NDK_VERSION-$HOST_PLATFORM.zip
(cd '/tmp' \ (cd '/tmp' \
&& wget http://dl.google.com/android/repository/$FILENAME -O $FILENAME \ && wget http://dl.google.com/android/repository/$FILENAME -O $FILENAME &> /dev/null \
&& unzip -q $FILENAME) || exit 1 && unzip -q $FILENAME) || exit 1
unset FILENAME unset FILENAME
fi fi
function _build_arch { ./build.sh "arm"
export ANDROID_NDK_ROOT="/tmp/${NDK_VERSION}" ./build.sh "arm64"
export TOOLCHAIN_PATH="${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/${HOST_PLATFORM}/bin" ./build.sh "x86"
export TOOLCHAIN_HOST=$1 ./build.sh "x86_64"
export TOOLCHAIN_COMP=$2
export TOOLCHAIN_CXXSTL=$3
export TOOLCHAIN_ARCH=$4
export TOOLCHAIN_NAME="${TOOLCHAIN_HOST}-${NDK_ABI_VERSION}"
source ./build.sh
}
# Define the minimum Android API level for the library to run.
# With NDK r20, the minimum SDK version range is [16, 29]
export MIN_SDK_VERSION="21"
HOST_ARM="arm-linux-androideabi"
HOST_ARM64="aarch64-linux-android"
HOST_X86="i686-linux-android"
HOST_X86_64="x86_64-linux-android"
COMP_ARM="armv7a-linux-androideabi${MIN_SDK_VERSION}"
COMP_ARM64="aarch64-linux-android${MIN_SDK_VERSION}"
COMP_X86="i686-linux-android${MIN_SDK_VERSION}"
COMP_X86_64="x86_64-linux-android${MIN_SDK_VERSION}"
CXXSTL_ARM="armeabi-v7a"
CXXSTL_ARM64="arm64-v8a"
CXXSTL_X86="x86"
CXXSTL_X86_64="x86_64"
ARCH_ARM="arm"
ARCH_ARM64="arm64"
ARCH_X86="x86"
ARCH_X86_64="x86_64"
_build_arch $HOST_ARM $COMP_ARM $CXXSTL_ARM $ARCH_ARM
_build_arch $HOST_X86 $COMP_X86 $CXXSTL_X86 $ARCH_X86
if [[ $MIN_SDK_VERSION -ge 21 ]] ; then
_build_arch $HOST_ARM64 $COMP_ARM64 $CXXSTL_ARM64 $ARCH_ARM64
_build_arch $HOST_X86_64 $COMP_X86_64 $CXXSTL_X86_64 $ARCH_X86_64
fi
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment