subsys-build-standard-large.md 9.0 KB
Newer Older
A
annie_wangli 已提交
1
# Building a Standard System<a name="EN-US_TOPIC_0000001076490572"></a>
M
mamingshuai 已提交
2

D
duangavin123 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
## Overview<a name="section17466112012244"></a>

The compilation and building subsystem provides a framework based on Generate Ninja \(GN\) and Ninja. This subsystem allows you to:

-   Build products based on different chipset platforms, for example, Hi3516D V300.

-   Package capabilities required by a product by assembling modules based on the product configuration.

### Basic Concepts<a name="section445513507246"></a>

It is considered best practice to learn the following basic concepts before you start building:

-   **Platform**

    A platform is a combination of development boards and kernels.

    Supported subsystems and modules vary according to the platform.

-   **Subsystems**

    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.

-   **Module**

    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.

-   **GN**

    GN is short for Generate Ninja, which is used to generate Ninja files.

-   **Ninja**

    Ninja is a small high-speed build system.


### Working Principles<a name="section12541217142510"></a>

The process to build OpenHarmony is as follows:

-   Parsing commands: Parse the name of the product to build and load related configurations.
-   Running GN: Configure toolchains and global options based on the parsed product name and compilation type.
-   Running Ninja: Start building and generate a product distribution.

### Limitations and Constraints<a name="section886933762513"></a>

-   You must download the source code using method 3 described in  [Source Code Acquisition](../get-code/sourcecode-acquire.md).
-   The build environment must be Ubuntu 18.04 or later.
-   You must install the software package required for build.

    The installation command is as follows:

    ```
    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
    ```


## Compilation and Building Guidelines<a name="section16901215262"></a>

### Directory Structure<a name="section109065332264"></a>
M
mamingshuai 已提交
62 63 64 65 66

```
/build                               # Primary directory
├── config                        # Build configuration items
├── core
D
duangavin123 已提交
67
│   └── gn                       # Build entry BUILD.gn configuration
M
mamingshuai 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81
├── loader                        # Loader of module configuration, which also generates a template for the module
├── ohos                          # Configuration of the process for building and packaging OpenHarmony
│   ├── kits                     # Build and packaging templates and processing flow for kits
│   ├── ndk                      # NDK template and processing flow
│   ├── notice                   # Notice template and processing flow
│   ├── packages                 # Distribution packaging template and processing flow
│   ├── sa_profile               # SA template and processing flow
│   ├── sdk                      # SDK template and processing flow, which contains the module configuration in the SDK
│   └── testfwk                   # Processing flow related to the test
├── scripts                      # Build-related Python script
├── templates                    # C/C++ build templates
└── toolchain                    # Toolchain configuration
```

D
duangavin123 已提交
82
### Build Command<a name="section123265539266"></a>
M
mamingshuai 已提交
83 84 85 86 87 88 89 90 91

-   Run the following command in the root directory of the source code to build the full distribution:

    ```
    ./build.sh --product-name {product_name}
    ```

    **product\_name**  indicates the product supported by the current distribution, for example, Hi3516D V300.

W
weichaox 已提交
92
    The image generated after build is stored in the  **out/{device_name}/packages/phone/images/**  directory.
M
mamingshuai 已提交
93 94 95 96 97 98 99 100 101 102 103

-   The build command supports the following options:

    ```
      --product-name    # (Mandatory) Name of the product to build, for example, Hi3516D V300
      --build-target    # (Optional) One or more build targets
      --gn-args         # (Optional) One or more gn parameters
      --ccache          # (Optional) Use of Ccache for build. This option takes effect only when Ccache is installed on the local PC.
    ```


D
duangavin123 已提交
104
### How to Develop<a name="section591084422719"></a>
M
mamingshuai 已提交
105 106 107 108 109

1.  Add a module.

    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.

D
duangavin123 已提交
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**.
M
mamingshuai 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

    Add  **partA**  to a subsystem, for example,  **subsystem\_examples**  \(defined in the  **test/examples/**  directory\).

    The complete directory structure of  **partA**  is as follows:

    ```
    test/examples/partA
    ├── feature1
    │   ├── BUILD.gn
    │   ├── include
    │   │   └── helloworld1.h
    │   └── src
    │       └── helloworld1.cpp
    ├── feature2
    │   ├── BUILD.gn
    │   ├── include
    │   │   └── helloworld2.h
    │   └── src
    │       └── helloworld2.cpp
    └── feature3
        ├── BUILD.gn
        └── src
            └── config.conf
    ```

    Example 1: GN script \(**test/examples/partA/feature1/BUILD.gn**\) for building a dynamic library

    ```
    config("helloworld_lib_config") {
     include_dirs = [ "include" ]
    }
    
    ohos_shared_library("helloworld_lib") {
      sources = [
        "include/helloworld1.h",
        "src/helloworld1.cpp",
      ]
      public_configs = [ ":helloworld_lib_config" ]
      part_name = "partA"
    }
    ```

    Example 2: GN script \(**test/examples/partA/feature2/BUILD.gn**\) for building an executable file

    ```
    ohos_executable("helloworld_bin") {
      sources = [
        "src/helloworld2.cpp"
      ]
      include_dirs = [ "include" ]
      deps = [                                # Dependent submodule
        "../feature1:helloworld_lib"
      ]
      external_deps = [ "partB:module1" ]     # (Optional) If there is a cross-module dependency, the format is "module name: submodule name"
      install_enable = true                   # By default, the executable file is not installed. You need to set this parameter to true for installation.
      part_name = "partA"
    }
    ```

    Example 3: GN script \(**test/examples/partA/feature3/BUILD.gn**\) for building the etc configuration file \(submodule\).

    ```
    ohos_prebuilt_etc("feature3_etc") {
      source = "src/config.conf"
      relative_install_dir = "init"    # (Optional) Directory for installing the submodule, which is relative to the default installation directory (/system/etc)
      part_name = "partA"
    }
    ```

D
duangavin123 已提交
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:
M
mamingshuai 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

    ```
    "partA": {
        "module_list": [
          "//test/examples/partA/feature1:helloworld_lib",
          "//test/examples/partA/feature2:helloworld_bin",
          "//test/examples/partA/feature3:feature3_etc",
        ],
        "inner_kits": [
    
        ],
        "system_kits": [
    
        ],
        "test_list": [
    
        ]
      }
    ```

    The declaration of a module contains the following parts:

    -   **module\_list**: submodule list of the module
    -   **inner\_kits**: APIs for other modules that depend on this module through  **external\_deps**
    -   **system\_kits**: APIs for developers
    -   **test\_list**: test cases for the submodules of the module

2.  Add the module to the product configuration file.

D
duangavin123 已提交
210 211 212
    Add the module to the product configuration file  **productdefine/common/products/\{product-name\}.json**.

    Add "subsystem\_examples:partA" to the product configuration file.  **partA**  will be built and packaged into the distribution.
M
mamingshuai 已提交
213

D
duangavin123 已提交
214
3.  Build the module.
M
mamingshuai 已提交
215 216 217 218 219 220 221 222 223

    For example, run the following command to build Hi3516D V300:

    ```
    ./build.sh --product-name Hi3516DV300 --ccache
    ```

4.  Obtain the build result.

W
weichaox 已提交
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.
M
mamingshuai 已提交
225 226