7a1fa1cc710b7fc06036ceb478d546ae5b3397a3
[platform/core/ml/nnfw.git] / docs / howto / how-to-build-compiler.md
1 # How to Build Compiler
2
3 This document is based on the system where Ubuntu Desktop Linux 18.04 LTS is installed with default
4 settings, and can be applied in other environments without much difference. For reference, the
5 development of our project started in the Ubuntu Desktop Linux 16.04 LTS environment.
6 As of now, to build in 16.04, please use gcc 7.x or above.
7
8 ## Build Requires
9
10 If you are going to build this project, the following modules must be installed on your system:
11
12 - CMake
13 - Boost C++ libraries
14
15 In the Ubuntu, you can easily install it with the following command.
16
17 ```
18 $ sudo apt-get install cmake libboost-all-dev
19 ```
20
21 If your linux system does not have the basic development configuration, you will need to install
22 more packages. A list of all packages needed to configure the development environment can be found
23 in the https://github.com/Samsung/ONE/blob/master/infra/docker/Dockerfile.1804 file.
24
25 Here is a summary of it
26
27 ```
28 $ sudo apt-get install \
29 build-essential \
30 clang-format-8 \
31 cmake \
32 doxygen \
33 git \
34 hdf5-tools \
35 lcov \
36 libatlas-base-dev \
37 libboost-all-dev \
38 libgflags-dev \
39 libgoogle-glog-dev \
40 libgtest-dev \
41 libhdf5-dev \
42 libprotobuf-dev \
43 protobuf-compiler \
44 pylint \
45 python3 \
46 python3-pip \
47 python3-venv \
48 python3.8 \
49 python3.8-dev \
50 python3.8-venv \
51 scons \
52 software-properties-common \
53 unzip \
54 wget
55
56 $ mkdir /tmp/gtest
57 $ cd /tmp/gtest
58 $ cmake /usr/src/gtest
59 $ make
60 $ sudo mv *.a /usr/lib
61
62 $ pip install yapf==0.22.0 numpy
63 ```
64
65 ## Build for Ubuntu
66
67 In a typical linux development environment, including Ubuntu, you can build the compiler with a
68 simple command like this:
69
70 ```
71 $ git clone https://github.com/Samsung/ONE.git one
72 $ cd one
73 $ ./nncc configure
74 $ ./nncc build
75 ```
76 Build artifacts will be placed in `build` folder.
77
78 To run unit tests:
79 ```
80 $ ./nncc test
81 ```
82
83 Above steps will build all the modules in the compiler folder. There are modules that are currently
84 not active. To build only as of now active modules of the compiler, we provide a preset of modules
85 to build with below command:
86 ```
87 $ ./nnas create-package --prefix $HOME/.local
88 ```
89
90 With this command, `~/.local` folder will contain all files in release.
91 If you have added `~/.local/bin` in PATH, then you will now have latest compiler binaries.
92
93 ### Build for debug and release separately
94
95 Build target folder can be customized by `NNCC_WORKSPACE` environment, as we may want to separate
96 debug and release builds.
97
98 ```
99 $ NNCC_WORKSPACE=build/debug ./nncc configure
100 $ ./nncc build
101 ```
102 will build debug version in `build/debug` folder, and
103
104 ```
105 $ NNCC_WORKSPACE=build/release ./nncc configure -DCMAKE_BUILD_TYPE=Release
106 $ ./nncc build
107 ```
108 will build release version in `build/release` folder.
109
110 ### Trouble shooting
111
112 If you are using python3.8, as there is no TensorFlow1.13.2 package for python3.8, build may fail.
113 Please install python3.7 or lower versions as default python3.
114
115 ## Build for Windows
116
117 To build for Windows, we use MinGW(Minimalist GNU for Windows). [Here](https://github.com/git-for-windows/build-extra/releases) you can download a tool that includes it.
118
119 ```
120 $ git clone https://github.com/Samsung/ONE.git one
121 $ cd one
122 $ NNAS_BUILD_PREFIX=build ./nnas create-package --preset 20200731_windows --prefix install
123 ```
124
125 - `NNAS_BUILD_PREFIX` is the path to directory where compiler-build-artifacts will be stored.
126 - `--preset` is the one that specifies a version you will install. You can see `infra/packaging/preset/` directory for more details and getting latest version.
127 - `--prefix` is the install directory.
128
129 ## Cross build for Ubuntu/ARM32 (experimental)
130
131 Some modules are availble to run in Ubuntu/ARM32 through cross building.
132
133 While configuring the build, some modules need to execute tools for generating
134 test materials and they need to execute in the host(x86-64). So some modules
135 are needed to build the tools for host before cross building.
136
137 Cross build overall steps are like, (1) configure for host
138 (2) build tools for host (3) configure for ARM32 target (4) and then build
139 for ARM32 target.
140
141 Unit tests can also run in target device.
142 But value test needs to run TensorFlow lite to get expected results,
143 and it would be a task to do this so the data files from host execution
144 are used instead.
145
146 Thus to run the unit tests in the target, running in host is needed in prior.
147
148 ### Prepare root file system
149
150 You should prepare Ubuntu/ARM32 root file system for cross compilation.
151 Please refer
152 [how-to-cross-build-runtime-for-arm.md](how-to-cross-build-runtime-for-arm.md)
153 for preparation.
154
155 You can set `ROOTFS_ARM` environment variable if you have in alternative
156 folder.
157
158 ### Clean existing external source for patches
159
160 Some external projects from source are not "cross compile ready with CMake"
161 projects. This experimental project prepared some patches for this.
162 Just remove the source and stamp file like below and the `make` will prepare
163 patch applied source codes.
164 ```
165 rm -rf externals/HDF5
166 rm -rf externals/PROTOBUF
167 rm externals/HDF5.stamp
168 rm externals/PROTOBUF.stamp
169 ```
170
171 ### Build
172
173 To cross build, `infra/nncc/Makefile.arm32` file is provided as an example to
174 work with `make` command.
175 ```
176 make -f infra/nncc/Makefile.arm32 cfg
177 make -f infra/nncc/Makefile.arm32 debug
178 ```
179 First `make` will run above steps (1), (2) and (3). Second `make` will run (4).
180
181 ### Test
182
183 Preprequisite for testing in ARM32 device.
184 ```
185 # numpy is required for value match in ARM32 target device
186 sudo apt-get install python3-pip
187 python3 -m pip install numpy
188 ```
189
190 You can also run unit tests in ARM32 Ubuntu device with cross build results.
191 First you need to run the test in host to prepare files that are currently
192 complicated in target device.
193 ```
194 # run this in x86-64 host
195 make -f infra/nncc/Makefile.arm32 test_prep
196
197 # run this in ARM32 target device
198 make -f infra/nncc/Makefile.arm32 test
199 ```
200
201 NOTE: this assumes
202 - host and target have same directoy structure
203 - should copy `build` folder to target or
204 - mounting `ONE` folder with NFS on the target would be simple