# Compilation and Building Subsystem - [Overview](#section11660541593) - [Directory Structure](#section1464106163817) - [Constraints](#section1718733212019) - [Usage](#section641210360552) - [Repositories Involved](#section6299103515474) ## Overview The compilation and building subsystem provides a compilation and building framework based on GN and Ninja. Using this subsystem, you can perform the following operations: 1. Build products with different chip platforms. For example, you can build IP camera products on the Hi3518EV300 and Hi3516DV300 development boards and WLAN module product on the Hi3861 development board. 2. Build a customized product generated by HPM configurations. ## Directory Structure ``` build/lite # Compile and build a primary directory. ├── config # Compile related configuration items. │ ├── boards # Define variables related to a development board, including its name, target architecture, and target CPU. │ ├── component # Define templates related to OpenHarmony components, including static libraries, dynamic libraries, extended components, and simulator libraries. │ ├── kernel # Define compilation variables of OpenHarmony kernel and configuration parameters. │ └── subsystem # OpenHarmony subsystem lists ├── ndk # Define NDK-related compilation scripts and configuration parameters. ├── platform # Platform-related configuration files │ ├── hi3516dv300_liteos_a # HI3516DV300 and LiteOS_A platforms, including the platform full configuration table and boot file. │ ├── hi3518ev300_liteos_a # HI3518EV300 and LiteOS_A platforms, including the platform full configuration table and boot file. │ └── hi3861v100_liteos_riscv # HI3861V100 and LiteOS_RISCV platforms, including the platform full configuration table and boot file. ├── product # Product full configuration table, including the configuration unit, subsystem list, and compiler ├── toolchain # Compilation toolchain, including the compiler path, compilation options, and link options. └── tools # Tools on which compilation and building depend, including mkfs. ``` ## Constraints You must preinstall GN and Ninja and add them to environment variables. ## Usage - Compile an existing product. ``` # Compile the WLAN platform. python build.py wifiiot # Compile the IP camera based on Hi3518EV300. python build.py ipcamera_hi3518ev300 # Compile the IP camera based on Hi3516DV300. python build.py ipcamera_hi3516dv300 ``` - Compile components. This section uses a customized component as an example to describe how to compile a component, a library, and an executable file. The example component consists of two functions: **feature1** and **feature2**. **feature1** is a dynamic library, and **feature2** is an executable file. The complete directory structure of the example component is as follows: ``` example # Customized component ├── BUILD.gn # GN script of a customized component. BUILD.gn is a fixed name. ├── feature1 # Customized unit 1 │ ├── BUILD.gn # GN script of the customized unit 1. BUILD.gn is a fixed name. │ ├── include # Header file folder │ │ └── helloworld1.h # Header file 1 │ └── src # Source file folder │ └── helloworld1.c # Source file 1 ├── feature2 # Customized unit 2 │ ├── BUILD.gn # GN script of the customized unit 2. BUILD.gn is a fixed name. │ ├── include # Header file folder │ │ └── helloworld2.h # Header file 2 │ └── src # Source file folder │ └── helloworld2.c # Source file 2 ├── build.sh # build.sh script of the customized component, which is optional └── Makefile # Makefile script of the customized component, which is optional ``` Step 1: Compile the **BUILD.gn** script of a dynamic library in the **example/feature1** directory. The **lite\_library** template can be used to compile dynamic and static libraries. The following is an example: ``` # Example of compiling the helloworld dynamic library # Build.gn file of helloworld lite_library("helloworld_lib") { target_type = "shared_library" # Compile a dynamic library. #target_type = "static_library" # Compile a static library. sources = [ "src/helloworld1.c" ] include_dirs = [ "include", "../feature2_example/include" # If feature2_example is required, add it to this include. ] } ``` Step 2: Compile the **BUILD.gn** script of an executable file in the **example/feature2** directory. The executable template provided by GN can be used to compile an executable file. The following is an example: ``` # Compile the executable .bin file. executable("hello_world_bin") { sources = [ "src/helloworld.c" ] include_dirs = [ "include", #"../feature2_example/include" # If feature2_example is required, add it to this include. ] # If feature1_example is required, add it to this deps. #deps = [ # "../feature1_example:helloworld1" #] } ``` Step 3: Compile the **BUILD.gn** script of a component in the **example/BUILD.gn** directory. ``` import("//build/lite/config/component/lite_component.gni") # Use the BUILD.gn script to compile the entire project. lite_component("example_gn") { features = [ "feature_example1:helloworld_lib", "feature_example2:hello_world_bin", ] } # Integrate the built-in build.sh or Makefile project and use the gn script to call the hybrid compilation. build_ext_component("example_mk") { exec_path = rebase_path(rebase_path(".", root_build_dir)) outdir = rebase_path(get_path_info(".", "out_dir")) prebuilts = "sh build.sh" command = "make clean && make" } ``` - Available compilation and building variables The variables that can be referenced globally are defined in **//build/lite/ohos\_var.gni**. [Table 1](#table3296103714372) describes the common variables of users. **Table 1**