1# Building the Standard System<a name="EN-US_TOPIC_0000001076490572"></a> 2 3## Overview<a name="section17466112012244"></a> 4 5The compilation and building subsystem provides a framework based on Generate Ninja \(GN\) and Ninja. This subsystem allows you to: 6 7- Build products based on different chipset platforms, for example, Hi3516D V300. 8 9- Package capabilities required by a product by assembling modules based on the product configuration. 10 11### Basic Concepts<a name="section445513507246"></a> 12 13It is considered best practice to learn the following basic concepts before you start building: 14 15- **Platform** 16 17 A platform is a combination of development boards and kernels. 18 19 Supported subsystems and modules vary according to the platform. 20 21- **Subsystems** 22 23 OpenHarmony is designed with a layered architecture, which from bottom to top consists of the kernel layer, system service layer, framework layer, and application layer. System functions are expanded by levels, from system to subsystem, and further to module. In a multi-device deployment scenario, unnecessary subsystems and modules can be excluded from the system as required. A subsystem is a logical concept and is a flexible combination of functions. 24 25- **Module** 26 27 A module is a reusable software binary unit that contains source code, configuration files, resource files, and build scripts. A module can be built independently, integrated in binary mode, and then tested independently. 28 29- **GN** 30 31 GN is short for Generate Ninja, which is used to generate Ninja files. 32 33- **Ninja** 34 35 Ninja is a small high-speed build system. 36 37 38### Working Principles<a name="section12541217142510"></a> 39 40The process to build OpenHarmony is as follows: 41 42- Parsing commands: Parse the name of the product to build and load related configurations. 43- Running GN: Configure toolchains and global options based on the parsed product name and compilation type. 44- Running Ninja: Start building and generate a product distribution. 45 46### Limitations and Constraints<a name="section886933762513"></a> 47 48- You must download the source code using method 3 described in [Source Code Acquisition](../get-code/sourcecode-acquire.md). 49- The build environment must be Ubuntu 18.04 or later. 50- You must install the software package required for build. 51 52 The installation command is as follows: 53 54 ``` 55 sudo apt-get install binutils git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip m4 56 ``` 57 58 59## Compilation and Building Guidelines<a name="section16901215262"></a> 60 61### Directory Structure<a name="section109065332264"></a> 62 63``` 64/build # Primary directory 65├── config # Build configuration items 66├── core 67│ └── gn # Build entry BUILD.gn configuration 68├── loader # Loader of module configuration, which also generates a template for the module 69├── ohos # Configuration of the process for building and packaging OpenHarmony 70│ ├── kits # Build and packaging templates and processing flow for kits 71│ ├── ndk # NDK template and processing flow 72│ ├── notice # Notice template and processing flow 73│ ├── packages # Distribution packaging template and processing flow 74│ ├── sa_profile # SA template and processing flow 75│ ├── sdk # SDK template and processing flow, which contains the module configuration in the SDK 76│ └── testfwk # Processing flow related to the test 77├── scripts # Build-related Python script 78├── templates # C/C++ build templates 79└── toolchain # Toolchain configuration 80``` 81 82### Build Command<a name="section123265539266"></a> 83 84- Run the following command in the root directory of the source code to build the full distribution: 85 86 ``` 87 ./build.sh --product-name {product_name} 88 ``` 89 90 **product\_name** indicates the product supported by the current distribution, for example, Hi3516D V300. 91 92 The image generated after build is stored in the **out/{device_name}/packages/phone/images/** directory. 93 94- The build command supports the following options: 95 96 ``` 97 --product-name # (Mandatory) Name of the product to build, for example, Hi3516D V300 98 --build-target # (Optional) One or more build targets 99 --gn-args # (Optional) One or more gn parameters 100 --ccache # (Optional) Use of Ccache for build. This option takes effect only when Ccache is installed on the local PC. 101 ``` 102 103 104### How to Develop<a name="section591084422719"></a> 105 1061. Add a module. 107 108 The following steps use a custom module as an example to describe how to build the module, including build a library, an executable file, and a configuration file. 109 110 The example module **partA** consists of **feature1**, **feature2**, and **feature3**. The target is a dynamic library for **feature1**, an executable file for **feature2**, and an etc configuration file for **feature3**. 111 112 Add **partA** to a subsystem, for example, **subsystem\_examples** \(defined in the **test/examples/** directory\). 113 114 The complete directory structure of **partA** is as follows: 115 116 ``` 117 test/examples/partA 118 ├── feature1 119 │ ├── BUILD.gn 120 │ ├── include 121 │ │ └── helloworld1.h 122 │ └── src 123 │ └── helloworld1.cpp 124 ├── feature2 125 │ ├── BUILD.gn 126 │ ├── include 127 │ │ └── helloworld2.h 128 │ └── src 129 │ └── helloworld2.cpp 130 └── feature3 131 ├── BUILD.gn 132 └── src 133 └── config.conf 134 ``` 135 136 Example 1: GN script \(**test/examples/partA/feature1/BUILD.gn**\) for building a dynamic library 137 138 ``` 139 config("helloworld_lib_config") { 140 include_dirs = [ "include" ] 141 } 142 143 ohos_shared_library("helloworld_lib") { 144 sources = [ 145 "include/helloworld1.h", 146 "src/helloworld1.cpp", 147 ] 148 public_configs = [ ":helloworld_lib_config" ] 149 part_name = "partA" 150 } 151 ``` 152 153 Example 2: GN script \(**test/examples/partA/feature2/BUILD.gn**\) for building an executable file 154 155 ``` 156 ohos_executable("helloworld_bin") { 157 sources = [ 158 "src/helloworld2.cpp" 159 ] 160 include_dirs = [ "include" ] 161 deps = [ # Dependent submodule 162 "../feature1:helloworld_lib" 163 ] 164 external_deps = [ "partB:module1" ] # (Optional) If there is a cross-module dependency, the format is "module name: submodule name" 165 install_enable = true # By default, the executable file is not installed. You need to set this parameter to true for installation. 166 part_name = "partA" 167 } 168 ``` 169 170 Example 3: GN script \(**test/examples/partA/feature3/BUILD.gn**\) for building the etc configuration file \(submodule\). 171 172 ``` 173 ohos_prebuilt_etc("feature3_etc") { 174 source = "src/config.conf" 175 relative_install_dir = "init" # (Optional) Directory for installing the submodule, which is relative to the default installation directory (/system/etc) 176 part_name = "partA" 177 } 178 ``` 179 180 Example 4: Adding the module configuration file **test/examples/ohos.build** to the **ohos.build** file of this subsystem. Each subsystem has an **ohos.build** file in its root directory. Example: 181 182 ``` 183 "partA": { 184 "module_list": [ 185 "//test/examples/partA/feature1:helloworld_lib", 186 "//test/examples/partA/feature2:helloworld_bin", 187 "//test/examples/partA/feature3:feature3_etc", 188 ], 189 "inner_kits": [ 190 191 ], 192 "system_kits": [ 193 194 ], 195 "test_list": [ 196 197 ] 198 } 199 ``` 200 201 The declaration of a module contains the following parts: 202 203 - **module\_list**: submodule list of the module 204 - **inner\_kits**: APIs for other modules that depend on this module through **external\_deps** 205 - **system\_kits**: APIs for developers 206 - **test\_list**: test cases for the submodules of the module 207 2082. Add the module to the product configuration file. 209 210 Add the module to the product configuration file **productdefine/common/products/\{product-name\}.json**. 211 212 Add "subsystem\_examples:partA" to the product configuration file. **partA** will be built and packaged into the distribution. 213 2143. Build the module. 215 216 For example, run the following command to build Hi3516D V300: 217 218 ``` 219 ./build.sh --product-name Hi3516DV300 --ccache 220 ``` 221 2224. Obtain the build result. 223 224 Files generated during the build process are stored in the **out/hi3516dv300/** directory, and the generated image is stored in the **out/hi3516dv300/packages/phone/images/** directory. 225 226 227