• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# How to use the Android NDK to build Arm NN
2
3- [Introduction](#introduction)
4- [Prerequisites](#prerequisites)
5- [Download Arm NN](#download-arm-nn)
6- [Build Arm Compute Library](#build-arm-compute-library)
7- [Build Arm NN](#build-arm-nn)
8- [Build Arm NN Support Library](#build-arm-nn-support-library)
9- [Build Arm NN Shim](#build-arm-nn-shim)
10
11
12## Introduction
13These are step by step instructions for building the Arm NN shim and support library for NNAPI.
14This work is currently in an experimental phase.
15
16## Prerequisites
17
18The following are required to build the Arm NN support library
19* Android NDK r25
20  * Detailed setup can be found in [BuildGuideAndroidNDK.md](../BuildGuideAndroidNDK.md)
21* Flatbuffer version 2.0.6
22  * Detailed setup can be found in [BuildGuideCrossCompilation.md](../BuildGuideCrossCompilation.md)
23
24The following is required to build the Arm NN shim
25* AOSP Source (Android Open Source Project)
26  * Download the source from the [official website](https://source.android.com/setup/build/downloading)
27  * This guide will use release tag `android12-s1-release`
28
29
30Set environment variables
31```bash
32export WORKING_DIR=<path to where the Arm NN source code, clframework and aosp repos will be cloned>
33export AOSP_ROOT=<path to the root of Android tree where the shim will be built>
34export AOSP_MODULES_ROOT=<path to where AOSP modules will be cloned i.e. $WORKING_DIR/aosp>
35export ARMNN_BUILD_DIR=<path to the Arm NN build directory i.e. $WORKING_DIR/build>
36export NDK=<path to>android-ndk-r25
37export NDK_TOOLCHAIN_ROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64
38export PATH=$NDK_TOOLCHAIN_ROOT/bin/:$PATH
39export FLATBUFFERS_ANDROID_BUILD=<path to flatbuffers target android build>
40export FLATBUFFERS_X86_BUILD=<path to flatbuffers host build-x86_64>
41```
42
43## Download Arm NN
44If the user only wishes to build the Support Library with the NDK, the Arm NN repo can be cloned into any folder.
45If the user also wishes to build the Arm NN Shim, for this the Arm NN repo will need to reside within
46the Android tree in order for armnn/shim/Android.bp to be picked up by the Soong (Android) build system.
47For example $AOSP_ROOT/vendor/arm/armnn
48
49
50* Clone Arm NN:
51  (Requires Git if not previously installed: `sudo apt install git`)
52
53```bash
54cd $WORKING_DIR
55git clone https://github.com/ARM-software/armnn.git
56```
57
58## Build Arm Compute Library
59
60Arm NN provides a script that downloads the version of Arm Compute Library that Arm NN was tested with:
61```bash
62${WORKING_DIR}/armnn/scripts/get_compute_library.sh
63```
64* Build the Arm Compute Library:
65  (Requires SCons if not previously installed: `sudo apt install scons`)
66```bash
67cd ${WORKING_DIR}/clframework
68
69scons arch=arm64-v8a \
70toolchain_prefix=aarch64-linux-android- \
71compiler_prefix=aarch64-linux-android29- \
72neon=1 opencl=1 \
73embed_kernels=1 \
74build_dir=android-arm64v8a \
75extra_cxx_flags="-Wno-parentheses-equality -Wno-missing-braces -fPIC" \
76Werror=0 embed_kernels=1 examples=0 \
77validation_tests=0 benchmark_tests=0 benchmark_examples=0 os=android -j16
78```
79
80## Build Arm NN and Serializer
81
82* Build Arm NN:
83  (Requires CMake if not previously installed: `sudo apt install cmake`)
84```bash
85cd $ARMNN_BUILD_DIR
86CXX=aarch64-linux-android29-clang++ \
87CC=aarch64-linux-android29-clang \
88CXX_FLAGS="-fPIE -fPIC" cmake ${WORKING_DIR}/armnn \
89-DCMAKE_ANDROID_NDK=$NDK \
90-DCMAKE_SYSTEM_NAME=Android \
91-DCMAKE_SYSTEM_VERSION=29 \
92-DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \
93-DCMAKE_EXE_LINKER_FLAGS="-pie -llog -lz" \
94-DARMCOMPUTE_ROOT=$WORKING_DIR/clframework/ \
95-DARMCOMPUTE_BUILD_DIR=$WORKING_DIR/clframework/build/android-arm64v8a/ \
96-DARMCOMPUTENEON=1 -DARMCOMPUTECL=1 -DARMNNREF=1 \
97-DFLATBUFFERS_ROOT=$FLATBUFFERS_ANDROID_BUILD \
98-DFLATC_DIR=$FLATBUFFERS_X86_BUILD \
99-DBUILD_ARMNN_SERIALIZER=1 -DBUILD_GATORD_MOCK=0 -DBUILD_BASE_PIPE_SERVER=0
100```
101
102 * Run the build
103```bash
104make -j16
105```
106
107## Build Arm NN Support Library
108
109Building the support library requires building some AOSP libraries via the NDK.
110It should be possible to use $AOSP_ROOT instead of $AOSP_MODULES_ROOT.
111
112However this example will instead clone the necessary AOSP repos outside of the Android tree and apply some minor patches
113which were required to get it to build with the Android version used in this guide.
114
115```bash
116# Call a script which will clone the necessary AOSP repos (do not clone them into Android tree)
117${WORKING_DIR}/armnn/shim/sl/scripts/clone_aosp_libs.sh $AOSP_MODULES_ROOT
118
119# Modify the repos by applying patches
120${WORKING_DIR}/armnn/shim/sl/scripts/modify_aosp_libs.sh $AOSP_MODULES_ROOT
121
122# Build the Support Library
123CMARGS="$CMARGS \
124-DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake \
125-DANDROID_ABI=arm64-v8a \
126-DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \
127-DCMAKE_ANDROID_NDK=$NDK \
128-DANDROID_PLATFORM=android-29 \
129-DAOSP_MODULES_ROOT=$AOSP_MODULES_ROOT \
130-DARMNN_SOURCE_DIR=$WORKING_DIR/armnn \
131-DArmnn_DIR=$ARMNN_BUILD_DIR "
132
133mkdir ${WORKING_DIR}/armnn/shim/sl/build
134cd ${WORKING_DIR}/armnn/shim/sl/build
135
136CXX=aarch64-linux-android29-clang++ \
137CC=aarch64-linux-android29-clang \
138cmake $CMARGS ../
139make
140```
141
142## Build Arm NN Shim
143
144By default the Arm NN shim Android.bp.off is not enabled.
145It is enabled below by renaming it to Android.bp
146
147```bash
148cd ${WORKING_DIR}/armnn/shim
149mv Android.bp.off Android.bp
150
151cd $AOSP_ROOT
152source build/envsetup.sh
153lunch <device>-eng
154cd vendor/arm/armnn/shim
155export ARMNN_ANDROID_MK_ENABLE=0
156mm
157```
158
159The built libraries and manifest file can be found here:
160$AOSP_ROOT/out/target/product/<device>/vendor/lib64/libarmnn_support_library.so
161$AOSP_ROOT/out/target/product/<device>/vendor/bin/hw/android.hardware.neuralnetworks-shim-service-armnn
162$AOSP_ROOT/out/target/product/<device>/vendor/etc/vintf/manifest/android.hardware.neuralnetworks-shim-service-armnn.xml
163
164Currently the Arm NN libraries are shared libraries and therefore will need to be pushed to the device:
165$ARMNN_BUILD_DIR/libarmnn.so
166