1 # How to use the Android NDK to build ArmNN
3 * [Introduction](#introduction)
4 * [Download the Android NDK and make a standalone toolchain](#downloadNDK)
5 * [Build the Boost C++ libraries](#buildBoost)
6 * [Build the Compute Library](#buildCL)
7 * [Build Google's Protobuf library](#buildProtobuf)
8 * [Download TensorFlow](#downloadTF)
9 * [Build ArmNN](#buildArmNN)
10 * [Run ArmNN UnitTests on an Android device](#runArmNNUnitTests)
13 #### <a name="introduction">Introduction</a>
14 These are step by step instructions for using the Android NDK to build ArmNN.
15 They have been tested on a clean install of Ubuntu 18.04, and should also work with other OS versions.
16 The instructions show how to build the ArmNN core library and the optional TensorFlow parser.
17 All downloaded or generated files will be saved inside the `~/armnn-devenv` directory.
19 #### <a name="downloadNDK">Download the Android NDK and make a standalone toolchain</a>
21 * Download the Android NDK from [the official website](https://developer.android.com/ndk/downloads/index.html):
24 mkdir -p ~/armnn-devenv/toolchains
25 cd ~/armnn-devenv/toolchains
26 # For Mac OS, change the NDK download link accordingly.
27 wget https://dl.google.com/android/repository/android-ndk-r16b-linux-x86_64.zip
28 unzip android-ndk-r16b-linux-x86_64.zip
29 export NDK=~/armnn-devenv/toolchains/android-ndk-r16b
32 You may want to append `export NDK=~/armnn-devenv/toolchains/android-ndk-r16b` to your `~/.bashrc` (or `~/.bash_profile` in Mac OS).
34 * Make a standalone toolchain:
36 (Requires python if not previously installed: `sudo apt install python`)
39 # Create an arm64 API 26 libc++ toolchain.
40 $NDK/build/tools/make_standalone_toolchain.py \
44 --install-dir=$HOME/armnn-devenv/toolchains/aarch64-android-r16b
45 export PATH=$HOME/armnn-devenv/toolchains/aarch64-android-r16b/bin:$PATH
48 You may want to append `export PATH=$HOME/armnn-devenv/toolchains/aarch64-android-r16b/bin:$PATH` to your `~/.bashrc` (or `~/.bash_profile` in Mac OS).
50 #### <a name="buildBoost">Build the Boost C++ libraries</a>
52 * Download Boost version 1.64:
55 mkdir ~/armnn-devenv/boost
56 cd ~/armnn-devenv/boost
57 wget https://dl.bintray.com/boostorg/release/1.64.0/source/boost_1_64_0.tar.bz2
58 tar xvf boost_1_64_0.tar.bz2
63 (Requires gcc if not previously installed: `sudo apt install gcc`)
65 echo "using gcc : arm : aarch64-linux-android-clang++ ;" > $HOME/armnn-devenv/boost/user-config.jam
66 cd ~/armnn-devenv/boost/boost_1_64_0
67 ./bootstrap.sh --prefix=$HOME/armnn-devenv/boost/install
68 ./b2 install --user-config=$HOME/armnn-devenv/boost/user-config.jam \
69 toolset=gcc-arm link=static cxxflags=-fPIC --with-filesystem \
70 --with-test --with-log --with-program_options -j16
73 #### <a name="buildCL">Build the Compute Library</a>
74 * Clone the Compute Library:
76 (Requires Git if not previously installed: `sudo apt install git`)
80 git clone https://github.com/ARM-software/ComputeLibrary.git
85 (Requires SCons if not previously installed: `sudo apt install scons`)
88 scons arch=arm64-v8a neon=1 opencl=1 embed_kernels=1 extra_cxx_flags="-fPIC" \
89 benchmark_tests=0 validation_tests=0 os=android -j16
92 #### <a name="buildProtobuf">Build Google's Protobuf library</a>
96 mkdir ~/armnn-devenv/google
97 cd ~/armnn-devenv/google
98 git clone https://github.com/google/protobuf.git
100 git checkout -b v3.5.2 v3.5.2
103 * Build a native (x86) version of the protobuf libraries and compiler (protoc):
105 (Requires cUrl, autoconf, llibtool, and other build dependencies if not previously installed: `sudo apt install curl autoconf libtool build-essential g++`)
111 ../configure --prefix=$HOME/armnn-devenv/google/x86_pb_install
116 * Build the arm64 version of the protobuf libraries:
121 CC=aarch64-linux-android-clang \
122 CXX=aarch64-linux-android-clang++ \
123 CFLAGS="-fPIE -fPIC" LDFLAGS="-pie -llog" \
124 ../configure --host=aarch64-linux-android \
125 --prefix=$HOME/armnn-devenv/google/arm64_pb_install \
126 --with-protoc=$HOME/armnn-devenv/google/x86_pb_install/bin/protoc
131 #### <a name="downloadTF">Download TensorFlow</a>
132 * Clone TensorFlow source code:
135 cd ~/armnn-devenv/google/
136 git clone https://github.com/tensorflow/tensorflow.git
139 #### <a name="buildArmNN">Build ArmNN</a>
141 * Clone ArmNN source code:
145 git clone https://github.com/ARM-software/armnn.git
148 * Generate TensorFlow protobuf definitions:
151 cd ~/armnn-devenv/google/tensorflow
152 ~/armnn-devenv/armnn/scripts/generate_tensorflow_protobuf.sh \
153 $HOME/armnn-devenv/google/tf_pb $HOME/armnn-devenv/google/x86_pb_install
158 (Requires CMake if not previously installed: `sudo apt install cmake`)
161 mkdir ~/armnn-devenv/armnn/build
162 cd ~/armnn-devenv/armnn/build
163 CXX=aarch64-linux-android-clang++ \
164 CC=aarch64-linux-android-clang \
165 CXX_FLAGS="-fPIE -fPIC" \
167 -DCMAKE_SYSTEM_NAME=Android \
168 -DCMAKE_EXE_LINKER_FLAGS="-pie -llog" \
169 -DARMCOMPUTE_ROOT=$HOME/armnn-devenv/ComputeLibrary/ \
170 -DARMCOMPUTE_BUILD_DIR=$HOME/armnn-devenv/ComputeLibrary/build \
171 -DBOOST_ROOT=$HOME/armnn-devenv/boost/install/ \
172 -DARMCOMPUTENEON=1 -DARMCOMPUTECL=1 \
173 -DTF_GENERATED_SOURCES=$HOME/armnn-devenv/google/tf_pb/ -DBUILD_TF_PARSER=1 \
174 -DPROTOBUF_ROOT=$HOME/armnn-devenv/google/arm64_pb_install/
178 #### <a name="runArmNNUnitTests">Run the ArmNN unit tests on an Android device</a>
181 * Push the build results to an Android device and make symbolic links for shared libraries:
184 adb push libarmnnTfParser.so /data/local/tmp/
185 adb push libarmnn.so /data/local/tmp/
186 adb push UnitTests /data/local/tmp/
187 adb push $NDK/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_shared.so /data/local/tmp/
188 adb push $HOME/armnn-devenv/google/arm64_pb_install/lib/libprotobuf.so /data/local/tmp/libprotobuf.so.15.0.1
189 adb shell 'ln -s libprotobuf.so.15.0.1 /data/local/tmp/libprotobuf.so.15'
190 adb shell 'ln -s libprotobuf.so.15.0.1 /data/local/tmp/libprotobuf.so'
193 * Run ArmNN unit tests:
196 adb shell 'LD_LIBRARY_PATH=/data/local/tmp /data/local/tmp/UnitTests'
199 If libarmnnUtils.a is present in `~/armnn-devenv/armnn/build/` and the unit tests run without failure then the build was successful.