ArmNN
 22.11
shim/BuildGuideShimSupportLibrary.md
Go to the documentation of this file.
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
13 These are step by step instructions for building the Arm NN shim and support library for NNAPI.
14 This work is currently in an experimental phase.
15 
16 ## Prerequisites
17 
18 The 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 
24 The 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 
30 Set environment variables
31 ```bash
32 export WORKING_DIR=<path to where the Arm NN source code, clframework and aosp repos will be cloned>
33 export AOSP_ROOT=<path to the root of Android tree where the shim will be built>
34 export AOSP_MODULES_ROOT=<path to where AOSP modules will be cloned i.e. $WORKING_DIR/aosp>
35 export ARMNN_BUILD_DIR=<path to the Arm NN build directory i.e. $WORKING_DIR/build>
36 export NDK=<path to>android-ndk-r25
37 export NDK_TOOLCHAIN_ROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64
38 export PATH=$NDK_TOOLCHAIN_ROOT/bin/:$PATH
39 export FLATBUFFERS_ANDROID_BUILD=<path to flatbuffers target android build>
40 export FLATBUFFERS_X86_BUILD=<path to flatbuffers host build-x86_64>
41 ```
42 
43 ## Download Arm NN
44 If the user only wishes to build the Support Library with the NDK, the Arm NN repo can be cloned into any folder.
45 If the user also wishes to build the Arm NN Shim, for this the Arm NN repo will need to reside within
46 the Android tree in order for armnn/shim/Android.bp to be picked up by the Soong (Android) build system.
47 For 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
54 cd $WORKING_DIR
55 git clone https://github.com/ARM-software/armnn.git
56 ```
57 
58 ## Build Arm Compute Library
59 
60 Arm 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
67 cd ${WORKING_DIR}/clframework
68 
69 scons arch=arm64-v8a \
70 toolchain_prefix=aarch64-linux-android- \
71 compiler_prefix=aarch64-linux-android29- \
72 neon=1 opencl=1 \
73 embed_kernels=1 \
74 build_dir=android-arm64v8a \
75 extra_cxx_flags="-Wno-parentheses-equality -Wno-missing-braces -fPIC" \
76 Werror=0 embed_kernels=1 examples=0 \
77 validation_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
85 cd $ARMNN_BUILD_DIR
86 CXX=aarch64-linux-android29-clang++ \
87 CC=aarch64-linux-android29-clang \
88 CXX_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
104 make -j16
105 ```
106 
107 ## Build Arm NN Support Library
108 
109 Building the support library requires building some AOSP libraries via the NDK.
110 It should be possible to use $AOSP_ROOT instead of $AOSP_MODULES_ROOT.
111 
112 However this example will instead clone the necessary AOSP repos outside of the Android tree and apply some minor patches
113 which 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
123 CMARGS="$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 
133 mkdir ${WORKING_DIR}/armnn/shim/sl/build
134 cd ${WORKING_DIR}/armnn/shim/sl/build
135 
136 CXX=aarch64-linux-android29-clang++ \
137 CC=aarch64-linux-android29-clang \
138 cmake $CMARGS ../
139 make
140 ```
141 
142 ## Build Arm NN Shim
143 
144 By default the Arm NN shim Android.bp.off is not enabled.
145 It is enabled below by renaming it to Android.bp
146 
147 ```bash
148 cd ${WORKING_DIR}/armnn/shim
149 mv Android.bp.off Android.bp
150 
151 cd $AOSP_ROOT
152 source build/envsetup.sh
153 lunch <device>-eng
154 cd vendor/arm/armnn/shim
155 export ARMNN_ANDROID_MK_ENABLE=0
156 mm
157 ```
158 
159 The 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 
164 Currently the Arm NN libraries are shared libraries and therefore will need to be pushed to the device:
165 $ARMNN_BUILD_DIR/libarmnn.so