Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / docs / howto / how-to-cross-build-runtime-for-arm.md
1 # How to Cross-build Runtime for ARM
2
3 ## Prepare ARM Ubuntu RootFS
4
5 Install required packages
6
7 ```
8 $ sudo apt-get install qemu qemu-user-static binfmt-support debootstrap
9 ```
10
11 Use `install_rootfs.sh` script to prepare Root File System. You should have `sudo`
12
13 ```
14 $ sudo ./tools/cross/install_rootfs.sh arm
15 ```
16 - supports `arm`(default) and `aarch64` architecutre for now
17 - supports `focal`(default), `bionic`, and `jammy` release
18
19 To see the options,
20 ```
21 $ ./tools/cross/install_rootfs.sh -h
22 ```
23
24 RootFS will be prepared at `tools/cross/rootfs/arm` or `tools/cross/rootfs/aarch64` folder.
25
26 ***\* CAUTION: The OS version of rootfs must match the OS version of execution target device. On the other hand, you need to match the Ubuntu version of the development PC with the Ubuntu version of rootfs to be used for cross-build. Otherwise, unexpected build errors may occur.***
27
28 If you are using Ubuntu 20.04 LTS, select `focal`, if you are using Ubuntu 22.04 LTS, select `jammy`. You can check your Ubuntu code name in the following way.
29
30 ```
31 $ cat /etc/lsb-release
32 DISTRIB_ID=Ubuntu
33 DISTRIB_RELEASE=22.04
34 DISTRIB_CODENAME=jammy
35 DISTRIB_DESCRIPTION="Ubuntu 22.04.3 LTS"
36 ```
37
38 If a build error occurs because the version of the development system and the target system do not match, and if you can't replace your development system for any reason, you can consider [cross-build using the docker image](how-to-build-runtime-using-prebuilt-docker-image.md).
39
40 ### Prepare RootFS at alternative folder
41
42 Use `ROOTFS_DIR` to a full path to prepare at alternative path.
43
44 ```
45 $ ROOTFS_DIR=/home/user/rootfs/arm-bionic sudo -E ./tools/cross/install_rootfs.sh arm
46 ```
47
48 ### Using proxy
49
50 If you need to use proxy server while building the rootfs, use `--setproxy` option.
51
52 ```
53 # for example,
54 $ sudo ./tools/cross/install_rootfs.sh arm --setproxy="1.2.3.4:8080"
55 # or
56 $ sudo ./tools/cross/install_rootfs.sh arm --setproxy="proxy.server.com:8888"
57 ```
58
59 This will put `apt` proxy settings in `rootfs/etc/apt/apt.conf.d/90proxy` file
60 for `http`, `https` and `ftp` protocol.
61
62 ## Install ARM Cross Toolchain
63
64 We recommend you have g++ >= 6.1 installed on your system because NN generated tests require it (c++14).
65
66 ### Ubuntu 20.04 LTS
67
68 On Ubuntu 20.04 LTS, you can install using `apt-get`.
69
70 Choose g++ version whatever you prefer: 9 (default) or 10. We are officially testing on default g++ version,
71 so we don't confirm build on different version.
72
73 ```
74 $ sudo apt-get install g++-{9,10}-arm-linux-gnueabihf
75 ```
76
77 If you select specific version, update symbolic link for build toolchain.
78
79 Otherwise, you should set your custom cmake crossbuild toolchain. You can find cmake toolchain files in `infra/nnfw/cmake/buildtool/cross/`.
80
81 ```
82 $ update-alternatives --install /usr/bin/arm-linux-gnueabihf-gcc arm-linux-gnueabihf-gcc /usr/bin/arm-linux-gnueabihf-gcc-10 80 \
83     --slave /usr/bin/arm-linux-gnueabihf-g++ arm-linux-gnueabihf-g++ /usr/bin/arm-linux-gnueabihf-g++-10 \
84     --slave /usr/bin/arm-linux-gnueabihf-gcov arm-linux-gnueabihf-gcov /usr/bin/arm-linux-gnueabihf-gcov-10
85 ```
86
87 ### Ubuntu 22.04 LTS
88
89 Same with Ubuntu 20.04 LTS. (except g++ version)
90
91 ## Build and install ARM Compute Library
92
93 Mostly you only need once of ACL (ARM Compute Library) build.
94
95 To build ACL, you need to install scons
96
97 ```
98 $ sudo apt-get install scons
99 ```
100
101 ACL source will be automatically installed in `externals/ARMCOMPUTE` when you build runtime without any changes.
102
103 You can check ACL source information in `infra/cmake/packages/ARMComputeSourceConfig.cmake`
104
105 ## Cross build for ARM by using Makefile.template
106
107 Give `TARGET_ARCH` variable to set the target architecture.
108
109 If you used `ROOTFS_DIR` to prepare in alternative folder, you should also give this to makefile.
110
111 ```
112 $ CROSS_BUILD=1 TARGET_ARCH=armv7l make -f Makefile.template
113
114 # If ROOTFS_DIR is in alternative folder
115 $ ROOTFS_DIR=/path/to/your/rootfs/arm \
116 CROSS_BUILD=1 TARGET_ARCH=armv7l make
117 ```
118
119 You can also omit the `CROSS_BUILD=1` option if you explicitly pass `ROOTFS_DIR`. In that case, if
120 the `TARGET_ARCH` are differs from the hostarchitecture, the make script automatically applies
121 `CROSS_BUILD=1`. So, if you set `ROOTFS_DIR` as an environment variable, you can simply perform
122 normal build and cross build as follows.
123
124 ```
125 $ export ROOTFS_DIR=xxx
126 ...
127 $ make -f Makefile.template                     # do normal build
128 $ TARGET_ARCH=armv7l make -f Makefile.template  # do cross build
129 ```
130
131 Makefile.template will pass crossbuild toolchain setting to cmake automatically by parsing variables.
132
133 ### Run test
134
135 To run and test the cross-compiled runtime, you need to install library packages and copy the compiled output to the target device of the architecture in which it is executable.
136
137 1. Install hdf5 and boost library package
138
139 ```
140 $ sudo apt install libhdf5-dev libboost-system-dev libboost-program-options-dev
141 ```
142
143 2. Copy all artifacts under the `./Product/armv7l-linux.<BUILD_TYPE>` folder to the target device, Odroid-XU4 for example, as a whole.
144
145 ```
146 $ ssh odroid mkdir -p one/Product
147 sjlee@odroid's password:
148 $ scp -rp ./Product/armv7l-linux.debug odroid:one/Product
149 sjlee@odroid's password:
150 FillFrom_runner                                                                                 100%  224KB 223.6KB/s   00:00
151 benchmark_nnapi.sh                                                                              100% 7464     7.3KB/s   00:00
152 common.sh                                                                                       100% 2084     2.0KB/s   00:00
153 test_framework.sh                                                                               100% 3154     3.1KB/s   00:00
154 test-driver.sh
155 ...
156 ```
157
158 3. Log in to the target device, go to the copied path, and reestore the symbolic link settings of the `Product` directory.
159
160 ```
161 $ ssh odroid
162 sjlee@odroid's password:
163 ...
164 $ cd ~/one/Product
165 $ ln ${PWD}/armv7l-linux.debug/out out
166 $ cd ..
167 $ ls -la Product
168 drwxrwxr-x  5 sjlee sjlee 4096 Jun  4 20:55 armv7l-linux.debug
169 lrwxrwxrwx  1 sjlee sjlee   51 Jun  4 20:55 out -> /home/sjlee/one/Product/armv7l-linux.debug/out
170 ```
171
172 Now you can test the compilation result in the same way as the native build. Please refer to the following document for details on the test procedure.
173
174 - [Testing neural network model inference using the runtime](./how-to-build-runtime.md#run-test)