ArmNN
 22.02
BuildGuideAndroidNDK.md
Go to the documentation of this file.
1 # How to use the Android NDK to build Arm NN
2 
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)
11 
12 
13 ## Introduction
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.
19 
20 ## Download the Android NDK and make a standalone toolchain
21 
22 * Download the Android NDK from [the official website](https://developer.android.com/ndk/downloads/index.html):
23  ```bash
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
32 
33  # You may want to append the above export variables commands to your `~/.bashrc` (or `~/.bash_profile` in Mac OS).
34  ```
35 
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.
37 
38 ## Build Google's Protobuf library (Optional)
39 
40 * Clone protobuf:
41  (Requires Git if not previously installed: `sudo apt install git`)
42 ```bash
43 mkdir $HOME/armnn-devenv/google
44 cd $HOME/armnn-devenv/google
45 git clone https://github.com/google/protobuf.git
46 cd protobuf
47 git checkout -b v3.12.0 v3.12.0
48 ```
49 
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++`)
52 ```bash
53 ./autogen.sh
54 mkdir x86_build
55 cd x86_build
56 ../configure --prefix=$HOME/armnn-devenv/google/x86_pb_install
57 make install -j16
58 cd ..
59 ```
60 
61 * Build the arm64 version of the protobuf libraries:
62 ```bash
63 mkdir arm64_build
64 cd arm64_build
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
73 make install -j16
74 cd ..
75 ```
76 
77 Note: The ANDROID_API variable should be set to the Android API version number you are using. E.g. "30" for Android R.
78 
79 ## Download Arm NN
80 * Clone Arm NN:
81  (Requires Git if not previously installed: `sudo apt install git`)
82 
83 ```bash
84 cd $HOME/armnn-devenv
85 git clone https://github.com/ARM-software/armnn.git
86 ```
87 
88 * Checkout Arm NN branch:
89 ```bash
90 cd armnn
91 git checkout <branch_name>
92 git pull
93 ```
94 
95 For example, if you want to check out the 21.11 release branch:
96 ```bash
97 git checkout branches/armnn_21_11
98 git pull
99 ```
100 
101 ## Build Arm Compute Library
102 * Clone Arm Compute Library:
103 
104 ```bash
105 cd $HOME/armnn-devenv
106 git clone https://github.com/ARM-software/ComputeLibrary.git
107 ```
108 * Checkout Arm Compute Library release tag:
109 ```bash
110 cd ComputeLibrary
111 git checkout <tag_name>
112 ```
113 For example, if you want to checkout the 21.11 release tag:
114 ```bash
115 git checkout v21.11
116 ```
117 
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.
119 
120 Arm NN provides a script that downloads the version of Arm Compute Library that Arm NN was tested with:
121 ```bash
122 git checkout $(../armnn/scripts/get_compute_library.sh -p)
123 ```
124 * the Arm Compute Library:
125  (Requires SCons if not previously installed: `sudo apt install scons`)
126 ```bash
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
129 ```
130 
131 ## Build Arm NN
132 
133 * Build Arm NN:
134  (Requires CMake if not previously installed: `sudo apt install cmake`)
135 ```bash
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" \
141 cmake .. \
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/
151 ```
152 
153 To include standalone sample dynamic backend tests, add the argument to enable the tests and the dynamic backend path to the CMake command:
154 
155 ```bash
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
159 ```
160 
161  * Run the build
162 ```bash
163 make -j16
164 ```
165 
166 ## Build Standalone Sample Dynamic Backend
167 * The sample dynamic backend is located in armnn/src/dynamic/sample
168 ```bash
169 mkdir build
170 cd build
171 ```
172 
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:
174 ```bash
175 #!/bin/bash
176 CXX=aarch64-linux-android<Android_API>-clang++ \
177 CC=aarch64-linux-android<Android_API>-clang \
178 CXX_FLAGS="-fPIE -fPIC" \
179 cmake \
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 ..
191 ```
192 
193 * Run the build
194 ```bash
195 make
196 ```
197 
198 ## Run the Arm NN unit tests on an Android device
199 
200 
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.
203 ```bash
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'
211 ```
212 
213 * Push the files needed for the unit tests (they are a mix of files, directories and symbolic links):
214 ```bash
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/
217 
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/
220 
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/
223 
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
231 
232 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath3
233 
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/
236 
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/
239 
240 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath7
241 
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/
244 
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/
247 
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/:
250 
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/
253 ```
254 
255 * Run Arm NN unit tests:
256 ```bash
257 adb shell 'LD_LIBRARY_PATH=/data/local/tmp:/vendor/lib64:/vendor/lib64/egl /data/local/tmp/UnitTests'
258 ```
259 If libarmnnUtils.a is present in `$HOME/armnn-devenv/armnn/build/` and the unit tests run without failure then the build was successful.