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 Google's Protobuf library](#build-google-s-protobuf-library)
6 - [Download Arm NN](#download-arm-nn)
7 - [Build Arm Compute Library](#build-arm-compute-library)
8 - [Build Arm NN](#build-arm-nn)
9 - [Build Standalone Sample Dynamic Backend](#build-standalone-sample-dynamic-backend)
10 - [Run the Arm NN unit tests on an Android device](#run-the-armnn-unit-tests-on-an-android-device)
14 These are step by step instructions for using the Android NDK to build Arm NN.
15 They have been tested on a clean install of Ubuntu 18.04 and 20.04, and should also work with other OS versions.
16 The instructions show how to build the Arm NN core library.
17 Building protobuf is optional. We have given steps should the user wish to build it (i.e. as an Onnx dependency).
18 All downloaded or generated files will be saved inside the `$HOME/armnn-devenv` directory.
20 ## Download the Android NDK and make a standalone toolchain
22 * Download the Android NDK from [the official website](https://developer.android.com/ndk/downloads/index.html):
24 mkdir -p $HOME/armnn-devenv/
25 cd $HOME/armnn-devenv/
26 # For Mac OS, change the NDK download link accordingly.
27 wget https://dl.google.com/android/repository/android-ndk-r20b-linux-x86_64.zip
28 unzip android-ndk-r20b-linux-x86_64.zip
29 export NDK=$HOME/armnn-devenv/android-ndk-r20b
30 export NDK_TOOLCHAIN_ROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64
31 export PATH=$NDK_TOOLCHAIN_ROOT/bin/:$PATH
33 # You may want to append the above export variables commands to your `~/.bashrc` (or `~/.bash_profile` in Mac OS).
36 * 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.
38 ## Build Google's Protobuf library (Optional)
41 (Requires Git if not previously installed: `sudo apt install git`)
43 mkdir $HOME/armnn-devenv/google
44 cd $HOME/armnn-devenv/google
45 git clone https://github.com/google/protobuf.git
47 git checkout -b v3.12.0 v3.12.0
50 * Build a native (x86) version of the protobuf libraries and compiler (protoc):
51 (Requires cUrl, autoconf, llibtool, and other build dependencies if not previously installed: `sudo apt install curl autoconf libtool build-essential g++`)
56 ../configure --prefix=$HOME/armnn-devenv/google/x86_pb_install
61 * Build the arm64 version of the protobuf libraries:
65 CC=aarch64-linux-android<Android_API>-clang \
66 CXX=aarch64-linux-android<Android_API>-clang++ \
67 CFLAGS="-fPIE -fPIC" \
68 LDFLAGS="-llog -lz -lc++_static" \
69 ../configure --host=aarch64-linux-android \
70 --prefix=$HOME/armnn-devenv/google/arm64_pb_install \
71 --enable-cross-compile \
72 --with-protoc=$HOME/armnn-devenv/google/x86_pb_install/bin/protoc
77 Note: The ANDROID_API variable should be set to the Android API version number you are using. E.g. "30" for Android R.
81 (Requires Git if not previously installed: `sudo apt install git`)
85 git clone https://github.com/ARM-software/armnn.git
88 * Checkout Arm NN branch:
91 git checkout <branch_name>
95 For example, if you want to check out the 21.11 release branch:
97 git checkout branches/armnn_21_11
101 ## Build Arm Compute Library
102 * Clone Arm Compute Library:
105 cd $HOME/armnn-devenv
106 git clone https://github.com/ARM-software/ComputeLibrary.git
108 * Checkout Arm Compute Library release tag:
111 git checkout <tag_name>
113 For example, if you want to checkout the 21.11 release tag:
118 Arm NN and Arm Compute Library are developed closely together. If you would like to use a particular release of Arm NN you will need the same release tag of ACL too.
120 Arm NN provides a script that downloads the version of Arm Compute Library that Arm NN was tested with:
122 git checkout $(../armnn/scripts/get_compute_library.sh -p)
124 * the Arm Compute Library:
125 (Requires SCons if not previously installed: `sudo apt install scons`)
127 scons arch=arm64-v8a neon=1 opencl=1 embed_kernels=1 extra_cxx_flags="-fPIC" \
128 benchmark_tests=0 validation_tests=0 os=android -j16
134 (Requires CMake if not previously installed: `sudo apt install cmake`)
136 mkdir $HOME/armnn-devenv/armnn/build
137 cd $HOME/armnn-devenv/armnn/build
138 CXX=aarch64-linux-android<Android_API>-clang++ \
139 CC=aarch64-linux-android<Android_API>-clang \
140 CXX_FLAGS="-fPIE -fPIC" \
142 -DCMAKE_ANDROID_NDK=$NDK \
143 -DCMAKE_SYSTEM_NAME=Android \
144 -DCMAKE_SYSTEM_VERSION=<Android_API> \
145 -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \
146 -DCMAKE_EXE_LINKER_FLAGS="-pie -llog -lz" \
147 -DARMCOMPUTE_ROOT=$HOME/armnn-devenv/ComputeLibrary/ \
148 -DARMCOMPUTE_BUILD_DIR=$HOME/armnn-devenv/ComputeLibrary/build \
149 -DARMCOMPUTENEON=1 -DARMCOMPUTECL=1 -DARMNNREF=1 \
150 -DPROTOBUF_ROOT=$HOME/armnn-devenv/google/arm64_pb_install/
153 To include standalone sample dynamic backend tests, add the argument to enable the tests and the dynamic backend path to the CMake command:
156 -DSAMPLE_DYNAMIC_BACKEND=1 \
157 -DDYNAMIC_BACKEND_PATHS=$SAMPLE_DYNAMIC_BACKEND_PATH
158 # Where $SAMPLE_DYNAMIC_BACKEND_PATH is the path where libArm_SampleDynamic_backend.so library file is pushed
166 ## Build Standalone Sample Dynamic Backend
167 * The sample dynamic backend is located in armnn/src/dynamic/sample
173 * 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:
176 CXX=aarch64-linux-android<Android_API>-clang++ \
177 CC=aarch64-linux-android<Android_API>-clang \
178 CXX_FLAGS="-fPIE -fPIC" \
180 -DCMAKE_C_COMPILER_WORKS=TRUE \
181 -DCMAKE_CXX_COMPILER_WORKS=TRUE \
182 -DCMAKE_ANDROID_NDK=$NDK \
183 -DCMAKE_SYSTEM_NAME=Android \
184 -DCMAKE_SYSTEM_VERSION=$ANDROID_API \
185 -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \
186 -DCMAKE_SYSROOT=$HOME/armnn-devenv/android-ndk-r20b/toolchains/llvm/prebuilt/linux-x86_64/sysroot \
187 -DCMAKE_CXX_FLAGS=--std=c++14 \
188 -DCMAKE_EXE_LINKER_FLAGS="-pie -llog" \
189 -DCMAKE_MODULE_LINKER_FLAGS="-llog" \
190 -DARMNN_PATH=$HOME/armnn-devenv/armnn/build/libarmnn.so ..
198 ## Run the Arm NN unit tests on an Android device
201 * Push the build results to an Android device and make symbolic links for shared libraries:
202 Currently adb version we have used for testing is 1.0.41.
204 adb push libarmnn.so /data/local/tmp/
205 adb push libtimelineDecoder.so /data/local/tmp/
206 adb push UnitTests /data/local/tmp/
207 adb push $NDK/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_shared.so /data/local/tmp/
208 adb push $HOME/armnn-devenv/google/arm64_pb_install/lib/libprotobuf.so /data/local/tmp/libprotobuf.so.23.0.0
209 adb shell 'ln -s libprotobuf.so.23.0.0 /data/local/tmp/libprotobuf.so.23'
210 adb shell 'ln -s libprotobuf.so.23.0.0 /data/local/tmp/libprotobuf.so'
213 * Push the files needed for the unit tests (they are a mix of files, directories and symbolic links):
215 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/testSharedObject
216 adb push -p $HOME/armnn-devenv/armnn/build/src/backends/backendsCommon/test/testSharedObject/* /data/local/tmp/src/backends/backendsCommon/test/testSharedObject/
218 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/testDynamicBackend
219 adb push -p $HOME/armnn-devenv/armnn/build/src/backends/backendsCommon/test/testDynamicBackend/* /data/local/tmp/src/backends/backendsCommon/test/testDynamicBackend/
221 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath1
222 adb push -p $HOME/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath1/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath1/
224 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2
225 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/
226 adb shell ln -s Arm_CpuAcc_backend.so /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so.1
227 adb shell ln -s Arm_CpuAcc_backend.so.1 /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so.1.2
228 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
229 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/
230 adb shell ln -s nothing /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/Arm_no_backend.so
232 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath3
234 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath5
235 adb push -p $HOME/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath5/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath5/
237 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath6
238 adb push -p $HOME/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath6/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath6/
240 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath7
242 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath9
243 adb push -p $HOME/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath9/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath9/
245 adb shell mkdir -p /data/local/tmp/src/backends/dynamic/reference
246 adb push -p $HOME/armnn-devenv/armnn/build/src/backends/dynamic/reference/Arm_CpuRef_backend.so /data/local/tmp/src/backends/dynamic/reference/
248 # 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.
249 # This is the example when $SAMPLE_DYNAMIC_BACKEND_PATH is specified as /data/local/tmp/dynamic/sample/:
251 adb shell mkdir -p /data/local/tmp/dynamic/sample/
252 adb push -p $HOME/armnn-devenv/armnn/src/dynamic/sample/build/libArm_SampleDynamic_backend.so /data/local/tmp/dynamic/sample/
255 * Run Arm NN unit tests:
257 adb shell 'LD_LIBRARY_PATH=/data/local/tmp:/vendor/lib64:/vendor/lib64/egl /data/local/tmp/UnitTests'
259 If libarmnnUtils.a is present in `$HOME/armnn-devenv/armnn/build/` and the unit tests run without failure then the build was successful.