aboutsummaryrefslogtreecommitdiff
path: root/BuildGuideAndroidNDK.md
blob: 94a23cb51ee97cf9f7e4bb43ec88656f22223aeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# How to use the Android NDK to build ArmNN

- [Introduction](#introduction)
- [Download the Android NDK and make a standalone toolchain](#download-the-android-ndk-and-make-a-standalone-toolchain)
- [Build the Boost C++ libraries](#build-the-boost-c---libraries)
- [Build the Compute Library](#build-the-compute-library)
- [Build Google's Protobuf library](#build-google-s-protobuf-library)
- [Download TensorFlow](#download-tensorflow)
- [Build ArmNN](#build-armnn)
- [Build Standalone Sample Dynamic Backend](#build-standalone-sample-dynamic-backend)
- [Run the ArmNN unit tests on an Android device](#run-the-armnn-unit-tests-on-an-android-device)


## Introduction
These are step by step instructions for using the Android NDK to build ArmNN.
They have been tested on a clean install of Ubuntu 18.04, and should also work with other OS versions.
The instructions show how to build the ArmNN core library and the optional TensorFlow parser.
All downloaded or generated files will be saved inside the `~/armnn-devenv` directory.

## Download the Android NDK and make a standalone toolchain

* Download the Android NDK from [the official website](https://developer.android.com/ndk/downloads/index.html):
 ```bash
 mkdir -p ~/armnn-devenv/toolchains
 cd ~/armnn-devenv/toolchains
 # For Mac OS, change the NDK download link accordingly.
 wget https://dl.google.com/android/repository/android-ndk-r20b-linux-x86_64.zip
 unzip android-ndk-r20b-linux-x86_64.zip
 export NDK=~/armnn-devenv/android-ndk-r20b
 export NDK_TOOLCHAIN_ROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64
 export PATH=$NDK_TOOLCHAIN_ROOT/bin/:$PATH

 # You may want to append the above export variables commands to your `~/.bashrc` (or `~/.bash_profile` in Mac OS).
 ```

* 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.

## Build the Boost C++ libraries

* Download Boost version 1.64:
```bash
mkdir ~/armnn-devenv/boost
cd ~/armnn-devenv/boost
wget https://dl.bintray.com/boostorg/release/1.64.0/source/boost_1_64_0.tar.bz2
tar xvf boost_1_64_0.tar.bz2
```

* Build:

	(Requires clang if not previously installed: `sudo apt-get install clang`)
    
    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++.
```bash
echo "using clang : arm : aarch64-linux-android<Android_API>-clang++ ;" > $HOME/armnn-devenv/boost/user-config.jam
cd ~/armnn-devenv/boost/boost_1_64_0
./bootstrap.sh --prefix=$HOME/armnn-devenv/boost/install
./b2 install --user-config=$HOME/armnn-devenv/boost/user-config.jam \
 toolset=clang-arm link=static cxxflags=-fPIC \
 --with-test --with-log --with-program_options -j16
```

## Build the Compute Library
* Clone the Compute Library:

	(Requires Git if not previously installed: `sudo apt install git`)
``` bash
cd ~/armnn-devenv
git clone https://github.com/ARM-software/ComputeLibrary.git
```

* Checkout ComputeLibrary branch:
```bash
cd ComputeLibrary
git checkout <branch_name>
git pull
```
For example, if you want to checkout release branch of 20.02:
```bash
git checkout branches/arm_compute_20_02
git pull
```

* Build:

	(Requires SCons if not previously installed: `sudo apt install scons`)
```bash
scons arch=arm64-v8a neon=1 opencl=1 embed_kernels=1 extra_cxx_flags="-fPIC" \
 benchmark_tests=0 validation_tests=0 os=android -j16
```

## Build Google's Protobuf library

* Clone protobuf:
```bash
mkdir ~/armnn-devenv/google
cd ~/armnn-devenv/google
git clone https://github.com/google/protobuf.git
cd protobuf
git checkout -b v3.12.0 v3.12.0
```

* Build a native (x86) version of the protobuf libraries and compiler (protoc):

	(Requires cUrl, autoconf, llibtool, and other build dependencies if not previously installed: `sudo apt install curl autoconf libtool build-essential g++`)
```bash
./autogen.sh
mkdir x86_build
cd x86_build
../configure --prefix=$HOME/armnn-devenv/google/x86_pb_install
make install -j16
cd ..
```

* Build the arm64 version of the protobuf libraries:
```bash
mkdir arm64_build
cd arm64_build
CC=aarch64-linux-android<Android_API>-clang \
CXX=aarch64-linux-android<Android_API>-clang++ \
CFLAGS="-fPIE -fPIC" \
    LDFLAGS="-llog -lz -lc++_static" \
   ../configure --host=aarch64-linux-android \
   --prefix=$HOME/armnn-devenv/google/arm64_pb_install \
   --enable-cross-compile \
   --with-protoc=$HOME/armnn-devenv/google/x86_pb_install/bin/protoc
make install -j16
cd ..
```

## Download TensorFlow
* Clone TensorFlow source code:
```bash
cd ~/armnn-devenv/google/
git clone https://github.com/tensorflow/tensorflow.git
    cd tensorflow/
    git checkout fcc4b966f1265f466e82617020af93670141b009
```

## Build ArmNN

* Clone ArmNN source code:
```bash
cd ~/armnn-devenv/
git clone https://github.com/ARM-software/armnn.git
```

* Checkout ArmNN branch:
```bash
cd armnn
git checkout <branch_name>
git pull
```

For example, if you want to checkout release branch of 20.02:
```bash
git checkout branches/armnn_20_02
git pull
```

* Generate TensorFlow protobuf definitions:
```bash
cd ~/armnn-devenv/google/tensorflow
~/armnn-devenv/armnn/scripts/generate_tensorflow_protobuf.sh \
 $HOME/armnn-devenv/google/tf_pb $HOME/armnn-devenv/google/x86_pb_install
```

* Build ArmNN:

 	(Requires CMake if not previously installed: `sudo apt install cmake`)
```bash
mkdir ~/armnn-devenv/armnn/build
cd ~/armnn-devenv/armnn/build
CXX=aarch64-linux-android<Android_API>-clang++ \
CC=aarch64-linux-android<Android_API>-clang \
CXX_FLAGS="-fPIE -fPIC" \
cmake .. \
    -DCMAKE_ANDROID_NDK=$NDK \
    -DCMAKE_SYSTEM_NAME=Android \
    -DCMAKE_SYSTEM_VERSION=<Android_API> \
    -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \
    -DCMAKE_EXE_LINKER_FLAGS="-pie -llog -lz" \
    -DARMCOMPUTE_ROOT=$HOME/armnn-devenv/ComputeLibrary/ \
    -DARMCOMPUTE_BUILD_DIR=$HOME/armnn-devenv/ComputeLibrary/build \
    -DBOOST_ROOT=$HOME/armnn-devenv/boost/install/ \
    -DARMCOMPUTENEON=1 -DARMCOMPUTECL=1 -DARMNNREF=1 \
    -DTF_GENERATED_SOURCES=$HOME/armnn-devenv/google/tf_pb/ -DBUILD_TF_PARSER=1 \
    -DPROTOBUF_ROOT=$HOME/armnn-devenv/google/arm64_pb_install/
```

To include standalone sample dynamic backend tests, add the argument to enable the tests and the dynamic backend path to the CMake command:

```bash
-DSAMPLE_DYNAMIC_BACKEND=1 \
-DDYNAMIC_BACKEND_PATHS=$SAMPLE_DYNAMIC_BACKEND_PATH
# Where $SAMPLE_DYNAMIC_BACKEND_PATH is the path where libArm_SampleDynamic_backend.so library file is pushed
```

 * Run the build
```bash
make -j16
```

## Build Standalone Sample Dynamic Backend
* The sample dynamic backend is located in armnn/src/dynamic/sample
```bash
mkdir build
cd build
```

* 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 armNN build:
```bash
#!/bin/bash
CXX=aarch64-linux-android<Android_API>-clang++ \
CC=aarch64-linux-android<Android_API>-clang \
CXX_FLAGS="-fPIE -fPIC" \
cmake \
-DCMAKE_SYSTEM_NAME=Android \
-DCMAKE_CXX_FLAGS=--std=c++14 \
-DCMAKE_EXE_LINKER_FLAGS="-pie -llog" \
-DCMAKE_MODULE_LINKER_FLAGS="-llog" \
-DBOOST_ROOT=$HOME/armnn-devenv/boost/install \
-DBoost_SYSTEM_LIBRARY=$HOME/armnn-devenv/boost/install/lib/libboost_system.a \
-DARMNN_PATH=$HOME/armnn-devenv/armnn/build/libarmnn.so ..
```

* Run the build
```bash
make
```

## Run the ArmNN unit tests on an Android device


* Push the build results to an Android device and make symbolic links for shared libraries:
  Currently adb version we have used for testing is 1.0.41.
```bash
adb push libarmnnTfParser.so /data/local/tmp/
adb push libarmnn.so /data/local/tmp/
    adb push libtimelineDecoder.so /data/local/tmp/
adb push UnitTests /data/local/tmp/
adb push $NDK/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_shared.so /data/local/tmp/
adb push $HOME/armnn-devenv/google/arm64_pb_install/lib/libprotobuf.so /data/local/tmp/libprotobuf.so.24.0.0
adb shell 'ln -s libprotobuf.so.24.0.0 /data/local/tmp/libprotobuf.so.24'
adb shell 'ln -s libprotobuf.so.24.0.0 /data/local/tmp/libprotobuf.so'
```

* Push the files needed for the unit tests (they are a mix of files, directories and symbolic links):
```bash
adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/testSharedObject
adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/testSharedObject/* /data/local/tmp/src/backends/backendsCommon/test/testSharedObject/

adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/testDynamicBackend
adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/testDynamicBackend/* /data/local/tmp/src/backends/backendsCommon/test/testDynamicBackend/

adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath1
adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath1/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath1/

adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2
adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/
adb shell ln -s Arm_CpuAcc_backend.so /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so.1
adb shell ln -s Arm_CpuAcc_backend.so.1 /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/Arm_CpuAcc_backend.so.1.2
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
adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath2/Arm_GpuAcc_backend.so /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/
adb shell ln -s nothing /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath2/Arm_no_backend.so

adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath3

adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath5
adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath5/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath5/

adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath6
adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath6/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath6/

adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath7

adb shell mkdir -p /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath9
adb push -p ~/armnn-devenv/armnn/build/src/backends/backendsCommon/test/backendsTestPath9/* /data/local/tmp/src/backends/backendsCommon/test/backendsTestPath9/

adb shell mkdir -p /data/local/tmp/src/backends/dynamic/reference
adb push -p ~/armnn-devenv/armnn/build/src/backends/dynamic/reference/Arm_CpuRef_backend.so /data/local/tmp/src/backends/dynamic/reference/

# 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 ArmNN is built.
# This is the example when $SAMPLE_DYNAMIC_BACKEND_PATH is specified as /data/local/tmp/dynamic/sample/:

adb shell mkdir -p /data/local/tmp/dynamic/sample/
adb push -p ${WORKING_DIR}/armnn/src/dynamic/sample/build/libArm_SampleDynamic_backend.so /data/local/tmp/dynamic/sample/
```

* Run ArmNN unit tests:
```bash
adb shell 'LD_LIBRARY_PATH=/data/local/tmp:/vendor/lib64:/vendor/lib64/egl /data/local/tmp/UnitTests'
```
If libarmnnUtils.a is present in `~/armnn-devenv/armnn/build/` and the unit tests run without failure then the build was successful.