1 # How to use the Android NDK to build Arm NN
3 - [Introduction](#introduction)
4 - [Download the Android NDK and make a standalone toolchain](#download-the-android-ndk-and-make-a-standalone-toolchain)
5 - [Build the Boost C++ libraries](#build-the-boost-c---libraries)
6 - [Build the Compute Library](#build-the-compute-library)
7 - [Build Google's Protobuf library](#build-google-s-protobuf-library)
8 - [Download TensorFlow](#download-tensorflow)
9 - [Build Arm NN](#build-armnn)
10 - [Build Standalone Sample Dynamic Backend](#build-standalone-sample-dynamic-backend)
11 - [Run the Arm NN unit tests on an Android device](#run-the-armnn-unit-tests-on-an-android-device)
15 These are step by step instructions for using the Android NDK to build Arm NN.
16 They have been tested on a clean install of Ubuntu 16.04, and should also work with other OS versions.
17 The instructions show how to build the Arm NN core library and the optional TensorFlow parser.
18 All downloaded or generated files will be saved inside the `~/armnn-devenv` directory.
20 #####Note: We are currently in the process of removing boost as a dependency to Arm NN. This process is finished for everything apart from our unit tests. This means you don't need boost to build and use Arm NN but you need it to execute our unit tests. Boost will soon be removed from Arm NN entirely. We also are deprecating support for Tensorflow parsers in 21.02. This will be removed in 21.05.
22 ## Download the Android NDK and make a standalone toolchain
24 * Download the Android NDK from [the official website](https://developer.android.com/ndk/downloads/index.html):
26 mkdir -p ~/armnn-devenv/toolchains
27 cd ~/armnn-devenv/toolchains
28 # For Mac OS, change the NDK download link accordingly.
29 wget https://dl.google.com/android/repository/android-ndk-r20b-linux-x86_64.zip
30 unzip android-ndk-r20b-linux-x86_64.zip
31 export NDK=~/armnn-devenv/android-ndk-r20b
32 export NDK_TOOLCHAIN_ROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64
33 export PATH=$NDK_TOOLCHAIN_ROOT/bin/:$PATH
35 # You may want to append the above export variables commands to your `~/.bashrc` (or `~/.bash_profile` in Mac OS).
38 * With the android ndk-20b, you don't need to use the make_standalone_toolchain script to create a toolchain for a specific version of android. Android's current preference is for you to just specify the architecture and operating system while setting the compiler and just use the ndk directory.
40 ## Build the Boost C++ libraries
42 * Download Boost version 1.64:
44 mkdir ~/armnn-devenv/boost
45 cd ~/armnn-devenv/boost
46 wget https://dl.bintray.com/boostorg/release/1.64.0/source/boost_1_64_0.tar.bz2
47 tar xvf boost_1_64_0.tar.bz2
52 (Requires clang if not previously installed: `sudo apt-get install clang`)
54 Note: You can specify the 'Android_API' version you want. For example, if your ANDROID_API is 27 then the compiler will be aarch64-linux-android27-clang++.
56 echo "using clang : arm : aarch64-linux-android<Android_API>-clang++ ;" > $HOME/armnn-devenv/boost/user-config.jam
57 cd ~/armnn-devenv/boost/boost_1_64_0
58 ./bootstrap.sh --prefix=$HOME/armnn-devenv/boost/install
59 ./b2 install --user-config=$HOME/armnn-devenv/boost/user-config.jam \
60 toolset=clang-arm link=static cxxflags=-fPIC \
61 --with-test --with-log --with-program_options -j16
64 ## Build the Compute Library
65 * Clone the Compute Library:
67 (Requires Git if not previously installed: `sudo apt install git`)
70 git clone https://github.com/ARM-software/ComputeLibrary.git
73 * Checkout ComputeLibrary branch:
76 git checkout <branch_name>
79 For example, if you want to checkout release branch of 20.02:
81 git checkout branches/arm_compute_20_02
87 (Requires SCons if not previously installed: `sudo apt install scons`)
89 scons arch=arm64-v8a neon=1 opencl=1 embed_kernels=1 extra_cxx_flags="-fPIC" \
90 benchmark_tests=0 validation_tests=0 os=android -j16
93 ## Build Google's Protobuf library
97 mkdir ~/armnn-devenv/google
98 cd ~/armnn-devenv/google
99 git clone https://github.com/google/protobuf.git
101 git checkout -b v3.12.0 v3.12.0
104 * Build a native (x86) version of the protobuf libraries and compiler (protoc):
106 (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:
120 CC=aarch64-linux-android<Android_API>-clang \
121 CXX=aarch64-linux-android<Android_API>-clang++ \
122 CFLAGS="-fPIE -fPIC" \
123 LDFLAGS="-llog -lz -lc++_static" \
124 ../configure --host=aarch64-linux-android \
125 --prefix=$HOME/armnn-devenv/google/arm64_pb_install \
126 --enable-cross-compile \
127 --with-protoc=$HOME/armnn-devenv/google/x86_pb_install/bin/protoc
132 ## Download TensorFlow
133 * Clone TensorFlow source code:
135 cd ~/armnn-devenv/google/
136 git clone https://github.com/tensorflow/tensorflow.git
138 git checkout fcc4b966f1265f466e82617020af93670141b009
143 * Clone Arm NN source code:
146 git clone https://github.com/ARM-software/armnn.git
149 * Checkout Arm NN branch:
152 git checkout <branch_name>
156 For example, if you want to checkout release branch of 20.02:
158 git checkout branches/armnn_20_02
162 * Generate TensorFlow protobuf definitions:
164 cd ~/armnn-devenv/google/tensorflow
165 ~/armnn-devenv/armnn/scripts/generate_tensorflow_protobuf.sh \
166 $HOME/armnn-devenv/google/tf_pb $HOME/armnn-devenv/google/x86_pb_install
171 (Requires CMake if not previously installed: `sudo apt install cmake`)
173 mkdir ~/armnn-devenv/armnn/build
174 cd ~/armnn-devenv/armnn/build
175 CXX=aarch64-linux-android<Android_API>-clang++ \
176 CC=aarch64-linux-android<Android_API>-clang \
177 CXX_FLAGS="-fPIE -fPIC" \
179 -DCMAKE_ANDROID_NDK=$NDK \
180 -DCMAKE_SYSTEM_NAME=Android \
181 -DCMAKE_SYSTEM_VERSION=<Android_API> \
182 -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \
183 -DCMAKE_EXE_LINKER_FLAGS="-pie -llog -lz" \
184 -DARMCOMPUTE_ROOT=$HOME/armnn-devenv/ComputeLibrary/ \
185 -DARMCOMPUTE_BUILD_DIR=$HOME/armnn-devenv/ComputeLibrary/build \
186 -DBOOST_ROOT=$HOME/armnn-devenv/boost/install/ \
187 -DARMCOMPUTENEON=1 -DARMCOMPUTECL=1 -DARMNNREF=1 \
188 -DTF_GENERATED_SOURCES=$HOME/armnn-devenv/google/tf_pb/ -DBUILD_TF_PARSER=1 \
189 -DPROTOBUF_ROOT=$HOME/armnn-devenv/google/arm64_pb_install/
192 To include standalone sample dynamic backend tests, add the argument to enable the tests and the dynamic backend path to the CMake command:
195 -DSAMPLE_DYNAMIC_BACKEND=1 \
196 -DDYNAMIC_BACKEND_PATHS=$SAMPLE_DYNAMIC_BACKEND_PATH
197 # Where $SAMPLE_DYNAMIC_BACKEND_PATH is the path where libArm_SampleDynamic_backend.so library file is pushed
205 ## Build Standalone Sample Dynamic Backend
206 * The sample dynamic backend is located in armnn/src/dynamic/sample
212 * Use CMake to configure the build environment, update the following script and run it from the armnn/src/dynamic/sample/build directory to set up the Arm NN build:
215 CXX=aarch64-linux-android<Android_API>-clang++ \
216 CC=aarch64-linux-android<Android_API>-clang \
217 CXX_FLAGS="-fPIE -fPIC" \
219 -DCMAKE_SYSTEM_NAME=Android \
220 -DCMAKE_CXX_FLAGS=--std=c++14 \
221 -DCMAKE_EXE_LINKER_FLAGS="-pie -llog" \
222 -DCMAKE_MODULE_LINKER_FLAGS="-llog" \
223 -DBOOST_ROOT=$HOME/armnn-devenv/boost/install \
224 -DBoost_SYSTEM_LIBRARY=$HOME/armnn-devenv/boost/install/lib/libboost_system.a \
225 -DARMNN_PATH=$HOME/armnn-devenv/armnn/build/libarmnn.so ..
233 ## Run the Arm NN unit tests on an Android device
236 * Push the build results to an Android device and make symbolic links for shared libraries:
237 Currently adb version we have used for testing is 1.0.41.
239 adb push libarmnnTfParser.so /data/local/tmp/
240 adb push libarmnn.so /data/local/tmp/
241 adb push libtimelineDecoder.so /data/local/tmp/
242 adb push UnitTests /data/local/tmp/
243 adb push $NDK/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_shared.so /data/local/tmp/
244 adb push $HOME/armnn-devenv/google/arm64_pb_install/lib/libprotobuf.so /data/local/tmp/libprotobuf.so.24.0.0
245 adb shell 'ln -s libprotobuf.so.24.0.0 /data/local/tmp/libprotobuf.so.24'
246 adb shell 'ln -s libprotobuf.so.24.0.0 /data/local/tmp/libprotobuf.so'
249 * Push the files needed for the unit tests (they are a mix of files, directories and symbolic links):
251 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/testSharedObject
252 adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/testSharedObject/* /data/local/tmp/src/backends/backendsCommon/test/testSharedObject/
254 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/testDynamicBackend
255 adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/testDynamicBackend/* /data/local/tmp/src/backends/backendsCommon/test/testDynamicBackend/
257 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath1
258 adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath1/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath1/
260 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2
261 adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/
262 adb shell ln -s Arm_CpuAcc_backend.so /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so.1
263 adb shell ln -s Arm_CpuAcc_backend.so.1 /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so.1.2
264 adb shell ln -s Arm_CpuAcc_backend.so.1.2 /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so.1.2.3
265 adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath2/Arm_GpuAcc_backend.so /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/
266 adb shell ln -s nothing /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/Arm_no_backend.so
268 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath3
270 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath5
271 adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath5/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath5/
273 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath6
274 adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath6/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath6/
276 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath7
278 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath9
279 adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath9/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath9/
281 adb shell mkdir -p /data/local/tmp/src/backends/dynamic/reference
282 adb push -p ~/armnn-devenv/armnn/build/src/backends/dynamic/reference/Arm_CpuRef_backend.so /data/local/tmp/src/backends/dynamic/reference/
284 # If the standalone sample dynamic tests are enabled, also push libArm_SampleDynamic_backend.so library file to the folder specified as $SAMPLE_DYNAMIC_BACKEND_PATH when Arm NN is built.
285 # This is the example when $SAMPLE_DYNAMIC_BACKEND_PATH is specified as /data/local/tmp/dynamic/sample/:
287 adb shell mkdir -p /data/local/tmp/dynamic/sample/
288 adb push -p ${WORKING_DIR}/armnn/src/dynamic/sample/build/libArm_SampleDynamic_backend.so /data/local/tmp/dynamic/sample/
291 * Run Arm NN unit tests:
293 adb shell 'LD_LIBRARY_PATH=/data/local/tmp:/vendor/lib64:/vendor/lib64/egl /data/local/tmp/UnitTests'
295 If libarmnnUtils.a is present in `~/armnn-devenv/armnn/build/` and the unit tests run without failure then the build was successful.