1 # How to use the Android NDK to build Arm NN
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)
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.
18 The following are required to build the Arm NN support library
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)
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`
30 Set environment variables
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>
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
51 (Requires Git if not previously installed: `sudo apt install git`)
55 git clone https://github.com/ARM-software/armnn.git
58 ## Build Arm Compute Library
60 Arm NN provides a script that downloads the version of Arm Compute Library that Arm NN was tested with:
62 ${WORKING_DIR}/armnn/scripts/get_compute_library.sh
64 * Build the Arm Compute Library:
65 (Requires SCons if not previously installed: `sudo apt install scons`)
67 cd ${WORKING_DIR}/clframework
69 scons arch=arm64-v8a \
70 toolchain_prefix=aarch64-linux-android- \
71 compiler_prefix=aarch64-linux-android29- \
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
80 ## Build Arm NN and Serializer
83 (Requires CMake if not previously installed: `sudo apt install cmake`)
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
107 ## Build Arm NN Support Library
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.
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.
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
119 # Modify the repos by applying patches
120 ${WORKING_DIR}/armnn/shim/sl/scripts/modify_aosp_libs.sh $AOSP_MODULES_ROOT
122 # Build the Support Library
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 "
133 mkdir ${WORKING_DIR}/armnn/shim/sl/build
134 cd ${WORKING_DIR}/armnn/shim/sl/build
136 CXX=aarch64-linux-android29-clang++ \
137 CC=aarch64-linux-android29-clang \
144 By default the Arm NN shim Android.bp.off is not enabled.
145 It is enabled below by renaming it to Android.bp
148 cd ${WORKING_DIR}/armnn/shim
149 mv Android.bp.off Android.bp
152 source build/envsetup.sh
154 cd vendor/arm/armnn/shim
155 export ARMNN_ANDROID_MK_ENABLE=0
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
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