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 Compute Library](#build-the-compute-library)
6 - [Build Google's Protobuf library](#build-google-s-protobuf-library)
7 - [Build Arm NN](#build-armnn)
8 - [Build Standalone Sample Dynamic Backend](#build-standalone-sample-dynamic-backend)
9 - [Run the Arm NN unit tests on an Android device](#run-the-armnn-unit-tests-on-an-android-device)
13 These are step by step instructions for using the Android NDK to build Arm NN.
14 They have been tested on a clean install of Ubuntu 18.04, and should also work with other OS versions.
15 The instructions show how to build the Arm NN core library.
16 Building protobuf is optional. We have given steps should the user wish to build it (i.e. as an Onnx dependency).
17 All downloaded or generated files will be saved inside the `$HOME/armnn-devenv` directory.
19 ## Download the Android NDK and make a standalone toolchain
21 * Download the Android NDK from [the official website](https://developer.android.com/ndk/downloads/index.html):
23 mkdir -p $HOME/armnn-devenv/
24 cd $HOME/armnn-devenv/
25 # For Mac OS, change the NDK download link accordingly.
26 wget https://dl.google.com/android/repository/android-ndk-r20b-linux-x86_64.zip
27 unzip android-ndk-r20b-linux-x86_64.zip
28 export NDK=$HOME/armnn-devenv/android-ndk-r20b
29 export NDK_TOOLCHAIN_ROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64
30 export PATH=$NDK_TOOLCHAIN_ROOT/bin/:$PATH
32 # You may want to append the above export variables commands to your `~/.bashrc` (or `~/.bash_profile` in Mac OS).
35 * 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.
37 ## Build the Compute Library
38 * Clone the Compute Library:
40 (Requires Git if not previously installed: `sudo apt install git`)
43 git clone https://github.com/ARM-software/ComputeLibrary.git
46 * Checkout ComputeLibrary release tag:
49 git checkout <tag_name>
51 For example, if you want to checkout release tag of 21.02:
58 (Requires SCons if not previously installed: `sudo apt install scons`)
60 scons arch=arm64-v8a neon=1 opencl=1 embed_kernels=1 extra_cxx_flags="-fPIC" \
61 benchmark_tests=0 validation_tests=0 os=android -j16
64 ## Build Google's Protobuf library (Optional)
68 mkdir $HOME/armnn-devenv/google
69 cd $HOME/armnn-devenv/google
70 git clone https://github.com/google/protobuf.git
72 git checkout -b v3.12.0 v3.12.0
75 * Build a native (x86) version of the protobuf libraries and compiler (protoc):
77 (Requires cUrl, autoconf, llibtool, and other build dependencies if not previously installed: `sudo apt install curl autoconf libtool build-essential g++`)
82 ../configure --prefix=$HOME/armnn-devenv/google/x86_pb_install
87 * Build the arm64 version of the protobuf libraries:
91 CC=aarch64-linux-android<Android_API>-clang \
92 CXX=aarch64-linux-android<Android_API>-clang++ \
93 CFLAGS="-fPIE -fPIC" \
94 LDFLAGS="-llog -lz -lc++_static" \
95 ../configure --host=aarch64-linux-android \
96 --prefix=$HOME/armnn-devenv/google/arm64_pb_install \
97 --enable-cross-compile \
98 --with-protoc=$HOME/armnn-devenv/google/x86_pb_install/bin/protoc
103 Note: The ANDROID_API variable should be set to the Android API version number you are using. E.g. "30" for Android R.
107 * Clone Arm NN source code:
109 cd $HOME/armnn-devenv/
110 git clone https://github.com/ARM-software/armnn.git
113 * Checkout Arm NN branch:
116 git checkout <branch_name>
120 For example, if you want to checkout release branch of 21.02:
122 git checkout branches/armnn_21_02
128 (Requires CMake if not previously installed: `sudo apt install cmake`)
130 mkdir $HOME/armnn-devenv/armnn/build
131 cd $HOME/armnn-devenv/armnn/build
132 CXX=aarch64-linux-android<Android_API>-clang++ \
133 CC=aarch64-linux-android<Android_API>-clang \
134 CXX_FLAGS="-fPIE -fPIC" \
136 -DCMAKE_ANDROID_NDK=$NDK \
137 -DCMAKE_SYSTEM_NAME=Android \
138 -DCMAKE_SYSTEM_VERSION=<Android_API> \
139 -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \
140 -DCMAKE_EXE_LINKER_FLAGS="-pie -llog -lz" \
141 -DARMCOMPUTE_ROOT=$HOME/armnn-devenv/ComputeLibrary/ \
142 -DARMCOMPUTE_BUILD_DIR=$HOME/armnn-devenv/ComputeLibrary/build \
143 -DARMCOMPUTENEON=1 -DARMCOMPUTECL=1 -DARMNNREF=1 \
144 -DPROTOBUF_ROOT=$HOME/armnn-devenv/google/arm64_pb_install/
147 To include standalone sample dynamic backend tests, add the argument to enable the tests and the dynamic backend path to the CMake command:
150 -DSAMPLE_DYNAMIC_BACKEND=1 \
151 -DDYNAMIC_BACKEND_PATHS=$SAMPLE_DYNAMIC_BACKEND_PATH
152 # Where $SAMPLE_DYNAMIC_BACKEND_PATH is the path where libArm_SampleDynamic_backend.so library file is pushed
160 ## Build Standalone Sample Dynamic Backend
161 * The sample dynamic backend is located in armnn/src/dynamic/sample
167 * 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:
170 CXX=aarch64-linux-android<Android_API>-clang++ \
171 CC=aarch64-linux-android<Android_API>-clang \
172 CXX_FLAGS="-fPIE -fPIC" \
174 -DCMAKE_C_COMPILER_WORKS=TRUE \
175 -DCMAKE_CXX_COMPILER_WORKS=TRUE \
176 -DCMAKE_ANDROID_NDK=$NDK \
177 -DCMAKE_SYSTEM_NAME=Android \
178 -DCMAKE_SYSTEM_VERSION=$ANDROID_API \
179 -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \
180 -DCMAKE_SYSROOT=$HOME/armnn-devenv/android-ndk-r20b/toolchains/llvm/prebuilt/linux-x86_64/sysroot \
181 -DCMAKE_CXX_FLAGS=--std=c++14 \
182 -DCMAKE_EXE_LINKER_FLAGS="-pie -llog" \
183 -DCMAKE_MODULE_LINKER_FLAGS="-llog" \
184 -DARMNN_PATH=$HOME/armnn-devenv/armnn/build/libarmnn.so ..
192 ## Run the Arm NN unit tests on an Android device
195 * Push the build results to an Android device and make symbolic links for shared libraries:
196 Currently adb version we have used for testing is 1.0.41.
198 adb push libarmnn.so /data/local/tmp/
199 adb push libtimelineDecoder.so /data/local/tmp/
200 adb push UnitTests /data/local/tmp/
201 adb push $NDK/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_shared.so /data/local/tmp/
202 adb push $HOME/armnn-devenv/google/arm64_pb_install/lib/libprotobuf.so /data/local/tmp/libprotobuf.so.23.0.0
203 adb shell 'ln -s libprotobuf.so.23.0.0 /data/local/tmp/libprotobuf.so.23'
204 adb shell 'ln -s libprotobuf.so.23.0.0 /data/local/tmp/libprotobuf.so'
207 * Push the files needed for the unit tests (they are a mix of files, directories and symbolic links):
209 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/testSharedObject
210 adb push -p $HOME/armnn-devenv/armnn/build/src/backends/backendsCommon/test/testSharedObject/* /data/local/tmp/src/backends/backendsCommon/test/testSharedObject/
212 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/testDynamicBackend
213 adb push -p $HOME/armnn-devenv/armnn/build/src/backends/backendsCommon/test/testDynamicBackend/* /data/local/tmp/src/backends/backendsCommon/test/testDynamicBackend/
215 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath1
216 adb push -p $HOME/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath1/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath1/
218 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2
219 adb push -p $HOME/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/
220 adb shell ln -s Arm_CpuAcc_backend.so /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so.1
221 adb shell ln -s Arm_CpuAcc_backend.so.1 /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so.1.2
222 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
223 adb push -p $HOME/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath2/Arm_GpuAcc_backend.so /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/
224 adb shell ln -s nothing /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/Arm_no_backend.so
226 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath3
228 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath5
229 adb push -p $HOME/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath5/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath5/
231 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath6
232 adb push -p $HOME/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath6/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath6/
234 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath7
236 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath9
237 adb push -p $HOME/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath9/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath9/
239 adb shell mkdir -p /data/local/tmp/src/backends/dynamic/reference
240 adb push -p $HOME/armnn-devenv/armnn/build/src/backends/dynamic/reference/Arm_CpuRef_backend.so /data/local/tmp/src/backends/dynamic/reference/
242 # 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.
243 # This is the example when $SAMPLE_DYNAMIC_BACKEND_PATH is specified as /data/local/tmp/dynamic/sample/:
245 adb shell mkdir -p /data/local/tmp/dynamic/sample/
246 adb push -p $HOME/armnn-devenv/armnn/src/dynamic/sample/build/libArm_SampleDynamic_backend.so /data/local/tmp/dynamic/sample/
249 * Run Arm NN unit tests:
251 adb shell 'LD_LIBRARY_PATH=/data/local/tmp:/vendor/lib64:/vendor/lib64/egl /data/local/tmp/UnitTests'
253 If libarmnnUtils.a is present in `$HOME/armnn-devenv/armnn/build/` and the unit tests run without failure then the build was successful.