ArmNN
 21.05
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 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 - [Build Arm NN](#build-armnn)
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 16.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 `~/armnn-devenv` directory.
19 
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.
21 
22 ## Download the Android NDK and make a standalone toolchain
23 
24 * Download the Android NDK from [the official website](https://developer.android.com/ndk/downloads/index.html):
25  ```bash
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
34 
35  # You may want to append the above export variables commands to your `~/.bashrc` (or `~/.bash_profile` in Mac OS).
36  ```
37 
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.
39 
40 ## Build the Boost C++ libraries
41 
42 * Download Boost version 1.64:
43 ```bash
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
48 ```
49 
50 * Build:
51 
52  (Requires clang if not previously installed: `sudo apt-get install clang`)
53 
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++.
55 ```bash
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
62 ```
63 
64 ## Build the Compute Library
65 * Clone the Compute Library:
66 
67  (Requires Git if not previously installed: `sudo apt install git`)
68 ``` bash
69 cd ~/armnn-devenv
70 git clone https://github.com/ARM-software/ComputeLibrary.git
71 ```
72 
73 * Checkout ComputeLibrary branch:
74 ```bash
75 cd ComputeLibrary
76 git checkout <branch_name>
77 git pull
78 ```
79 For example, if you want to checkout release branch of 20.02:
80 ```bash
81 git checkout branches/arm_compute_20_02
82 git pull
83 ```
84 
85 * Build:
86 
87  (Requires SCons if not previously installed: `sudo apt install scons`)
88 ```bash
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
91 ```
92 
93 ## Build Google's Protobuf library (Optional)
94 
95 * Clone protobuf:
96 ```bash
97 mkdir ~/armnn-devenv/google
98 cd ~/armnn-devenv/google
99 git clone https://github.com/google/protobuf.git
100 cd protobuf
101 git checkout -b v3.12.0 v3.12.0
102 ```
103 
104 * Build a native (x86) version of the protobuf libraries and compiler (protoc):
105 
106  (Requires cUrl, autoconf, llibtool, and other build dependencies if not previously installed: `sudo apt install curl autoconf libtool build-essential g++`)
107 ```bash
108 ./autogen.sh
109 mkdir x86_build
110 cd x86_build
111 ../configure --prefix=$HOME/armnn-devenv/google/x86_pb_install
112 make install -j16
113 cd ..
114 ```
115 
116 * Build the arm64 version of the protobuf libraries:
117 ```bash
118 mkdir arm64_build
119 cd arm64_build
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
128 make install -j16
129 cd ..
130 ```
131 
132 ## Build Arm NN
133 
134 * Clone Arm NN source code:
135 ```bash
136 cd ~/armnn-devenv/
137 git clone https://github.com/ARM-software/armnn.git
138 ```
139 
140 * Checkout Arm NN branch:
141 ```bash
142 cd armnn
143 git checkout <branch_name>
144 git pull
145 ```
146 
147 For example, if you want to checkout release branch of 20.02:
148 ```bash
149 git checkout branches/armnn_20_02
150 git pull
151 ```
152 
153 * Build Arm NN:
154 
155  (Requires CMake if not previously installed: `sudo apt install cmake`)
156 ```bash
157 mkdir ~/armnn-devenv/armnn/build
158 cd ~/armnn-devenv/armnn/build
159 CXX=aarch64-linux-android<Android_API>-clang++ \
160 CC=aarch64-linux-android<Android_API>-clang \
161 CXX_FLAGS="-fPIE -fPIC" \
162 cmake .. \
163  -DCMAKE_ANDROID_NDK=$NDK \
164  -DCMAKE_SYSTEM_NAME=Android \
165  -DCMAKE_SYSTEM_VERSION=<Android_API> \
166  -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \
167  -DCMAKE_EXE_LINKER_FLAGS="-pie -llog -lz" \
168  -DARMCOMPUTE_ROOT=$HOME/armnn-devenv/ComputeLibrary/ \
169  -DARMCOMPUTE_BUILD_DIR=$HOME/armnn-devenv/ComputeLibrary/build \
170  -DBOOST_ROOT=$HOME/armnn-devenv/boost/install/ \
171  -DARMCOMPUTENEON=1 -DARMCOMPUTECL=1 -DARMNNREF=1 \
172  -DPROTOBUF_ROOT=$HOME/armnn-devenv/google/arm64_pb_install/
173 ```
174 
175 To include standalone sample dynamic backend tests, add the argument to enable the tests and the dynamic backend path to the CMake command:
176 
177 ```bash
178 -DSAMPLE_DYNAMIC_BACKEND=1 \
179 -DDYNAMIC_BACKEND_PATHS=$SAMPLE_DYNAMIC_BACKEND_PATH
180 # Where $SAMPLE_DYNAMIC_BACKEND_PATH is the path where libArm_SampleDynamic_backend.so library file is pushed
181 ```
182 
183  * Run the build
184 ```bash
185 make -j16
186 ```
187 
188 ## Build Standalone Sample Dynamic Backend
189 * The sample dynamic backend is located in armnn/src/dynamic/sample
190 ```bash
191 mkdir build
192 cd build
193 ```
194 
195 * 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:
196 ```bash
197 #!/bin/bash
198 CXX=aarch64-linux-android<Android_API>-clang++ \
199 CC=aarch64-linux-android<Android_API>-clang \
200 CXX_FLAGS="-fPIE -fPIC" \
201 cmake \
202 -DCMAKE_SYSTEM_NAME=Android \
203 -DCMAKE_CXX_FLAGS=--std=c++14 \
204 -DCMAKE_EXE_LINKER_FLAGS="-pie -llog" \
205 -DCMAKE_MODULE_LINKER_FLAGS="-llog" \
206 -DBOOST_ROOT=$HOME/armnn-devenv/boost/install \
207 -DBoost_SYSTEM_LIBRARY=$HOME/armnn-devenv/boost/install/lib/libboost_system.a \
208 -DARMNN_PATH=$HOME/armnn-devenv/armnn/build/libarmnn.so ..
209 ```
210 
211 * Run the build
212 ```bash
213 make
214 ```
215 
216 ## Run the Arm NN unit tests on an Android device
217 
218 
219 * Push the build results to an Android device and make symbolic links for shared libraries:
220  Currently adb version we have used for testing is 1.0.41.
221 ```bash
222 adb push libarmnn.so /data/local/tmp/
223  adb push libtimelineDecoder.so /data/local/tmp/
224 adb push UnitTests /data/local/tmp/
225 adb push $NDK/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_shared.so /data/local/tmp/
226 adb push $HOME/armnn-devenv/google/arm64_pb_install/lib/libprotobuf.so /data/local/tmp/libprotobuf.so.23.0.0
227 adb shell 'ln -s libprotobuf.so.23.0.0 /data/local/tmp/libprotobuf.so.23'
228 adb shell 'ln -s libprotobuf.so.23.0.0 /data/local/tmp/libprotobuf.so'
229 ```
230 
231 * Push the files needed for the unit tests (they are a mix of files, directories and symbolic links):
232 ```bash
233 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/testSharedObject
234 adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/testSharedObject/* /data/local/tmp/src/backends/backendsCommon/test/testSharedObject/
235 
236 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/testDynamicBackend
237 adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/testDynamicBackend/* /data/local/tmp/src/backends/backendsCommon/test/testDynamicBackend/
238 
239 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath1
240 adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath1/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath1/
241 
242 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2
243 adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/
244 adb shell ln -s Arm_CpuAcc_backend.so /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so.1
245 adb shell ln -s Arm_CpuAcc_backend.so.1 /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so.1.2
246 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
247 adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath2/Arm_GpuAcc_backend.so /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/
248 adb shell ln -s nothing /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/Arm_no_backend.so
249 
250 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath3
251 
252 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath5
253 adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath5/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath5/
254 
255 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath6
256 adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath6/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath6/
257 
258 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath7
259 
260 adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath9
261 adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath9/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath9/
262 
263 adb shell mkdir -p /data/local/tmp/src/backends/dynamic/reference
264 adb push -p ~/armnn-devenv/armnn/build/src/backends/dynamic/reference/Arm_CpuRef_backend.so /data/local/tmp/src/backends/dynamic/reference/
265 
266 # 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.
267 # This is the example when $SAMPLE_DYNAMIC_BACKEND_PATH is specified as /data/local/tmp/dynamic/sample/:
268 
269 adb shell mkdir -p /data/local/tmp/dynamic/sample/
270 adb push -p ${WORKING_DIR}/armnn/src/dynamic/sample/build/libArm_SampleDynamic_backend.so /data/local/tmp/dynamic/sample/
271 ```
272 
273 * Run Arm NN unit tests:
274 ```bash
275 adb shell 'LD_LIBRARY_PATH=/data/local/tmp:/vendor/lib64:/vendor/lib64/egl /data/local/tmp/UnitTests'
276 ```
277 If libarmnnUtils.a is present in `~/armnn-devenv/armnn/build/` and the unit tests run without failure then the build was successful.