1# Component 2### Configuration Rules 3 4The **bundle.json** file of a component is stored in the root directory of the component source code. The following example shows how to configure the sensor service component of the pan-sensor subsystem: 5 6```shell 7{ 8 "name": "@ohos/sensor_lite", # OpenHarmony Package Manager (HPM) component name, in the "@Organization/Component name" format. 9 "description": "Sensor services", # Description of the component function. 10 "version": "3.1", # Version, which must be the same as the version of OpenHarmony. 11 "license": "MIT", # Component license. 12 "publishAs": "code-segment", # HPM package release mode. The default value is code-segment. 13 "segment": { 14 "destPath": "" 15 }, # Code restoration path (source code path) set when publishAs is code-segment. 16 "dirs": {"base/sensors/sensor_lite"}, # Directory structure of the HPM package. This field is mandatory and can be left empty. 17 "scripts": {}, # Scripts to be executed. This field is mandatory and can be left empty. 18 "licensePath": "COPYING", 19 "readmePath": { 20 "en": "README.rst" 21 }, 22 "component": { # Component attributes. 23 "name": "sensor_lite", # Component name. 24 "subsystem": "", # Subsystem to which the component belongs. 25 "syscap": [], # System capabilities provided by the component for applications. 26 "features": [], # List of external configurable features of the component. Generally, this parameter corresponds to sub_component in build. 27 "adapted_system_type": [], # Types of adapted systems, which can be mini, small, standard, or their combinations. 28 "rom": "92KB", # Component ROM size. 29 "ram": "~200KB", # Component RAM size. 30 "deps": { 31 "components": [ # Other components on which this component depends. 32 "samgr_lite", 33 "ipc_lite" 34 ], 35 "third_party": [ # Third-party open-source software on which this component depends. 36 "bounds_checking_function" 37 ] 38 } 39 "build": { # Build-related configurations. 40 "sub_component": [ 41 ""//base/sensors/sensor_lite/services:sensor_service"", # Component build entry. 42 ], # Component build entry. Configure modules here. 43 "inner_kits": [], # APIs between components. 44 "test": [] # Entry for building the component's test cases. 45 } 46 } 47 } 48``` 49 50> **CAUTION** 51> 52> Existing components on the LiteOS are configured in the JSON file of the corresponding subsystem in the **build/lite/components** directory. The directory is named in the **{Domain}/{Subsystem}/{Component}** format. The component directory structure is as follows: 53 54```shell 55component 56├── interfaces 57│ ├── innerkits # APIs exposed internally among components 58│ └── kits # APIs provided for application developers 59├── frameworks # Framework implementation 60├── services # Service implementation 61├── BUILD.gn # Build script 62``` 63 64You need to configure the component name, source code path, function description, mandatory or not, build target, RAM, ROM, output, adapted kernel, configurable features, and dependencies. 65 66When adding a component, you must add the component definition to the JSON file of the corresponding subsystem. The component configured for a product must have been defined in a subsystem. Otherwise, the verification will fail. 67 68### Adding and Building a Component 69 701. Add a component. 71 72 The following use a custom component as an example to describe how to compile a library, executable file, and configuration file. 73 74 In this example, **partA** consists of **feature1**, **feature2**, and **feature3**, which represent a dynamic library, an executable file, and an etc configuration file, respectively. 75 76 Add **partA** to a subsystem, for example, **subsystem_examples** (defined in the **test/examples/** directory). 77 78 The directory structure of **partA** is as follows: 79 80 ```shell 81 test/examples/partA 82 ├── feature1 83 │ ├── BUILD.gn 84 │ ├── include 85 │ │ └── helloworld1.h 86 │ └── src 87 │ └── helloworld1.cpp 88 ├── feature2 89 │ ├── BUILD.gn 90 │ ├── include 91 │ │ └── helloworld2.h 92 │ └── src 93 │ └── helloworld2.cpp 94 └── feature3 95 ├── BUILD.gn 96 └── src 97 └── config.conf 98 ``` 99 100 (a) Configure **test/examples/partA/feature1/BUILD.gn** for the dynamic library. 101 102 ```shell 103 config("helloworld_lib_config") { 104 include_dirs = [ "include" ] 105 } 106 107 ohos_shared_library("helloworld_lib") { 108 sources = [ 109 "include/helloworld1.h", 110 "src/helloworld1.cpp", 111 ] 112 public_configs = [ ":helloworld_lib_config" ] 113 part_name = "partA" 114 } 115 ``` 116 117 (b) Configure **test/examples/partA/feature2/BUILD.gn** for the executable file. 118 119 ```shell 120 ohos_executable("helloworld_bin") { 121 sources = [ 122 "src/helloworld2.cpp" 123 ] 124 include_dirs = [ "include" ] 125 deps = [ # Dependent modules in the component 126 "../feature1:helloworld_lib" 127 ] 128 external_deps = [ "partB:module1" ] # (Optional) Dependent modules of another component are named in the Component name:Module name format. 129 install_enable = true # By default, executable programs are not installed. Set this parameter to true if an executable program needs to be installed. 130 part_name = "partA" 131 } 132 ``` 133 134 (c) Configure **test/examples/partA/feature3/BUILD.gn** for the etc module. 135 136 ```shell 137 ohos_prebuilt_etc("feature3_etc") { 138 source = "src/config.conf" 139 relative_install_dir = "init" # (Optional) Relative directory for installing the module. The default installation directory is /system/etc. 140 part_name = "partA" 141 } 142 ``` 143 144 (d) Add the module configuration **test/examples/bundle.json** to the **bundle.json** file of the component. Each component has a **bundle.json** file in the root directory of the component. For details, see the [component bundle.json file](subsys-build-component.md#configuration-rules). 145 1462. Add the component to the product configuration file. 147 148 Add the component to **//vendor/{*product_company*}/{*product_name*}/config.json**. 149 150 The following uses **vendor/hisilicon/hispark_taurus_standard/config.json** as an example: 151 152 ```shell 153 { 154 "product_name": "hispark_taurus_standard", 155 "device_company": "hisilicon", 156 "device_build_path": "device/board/hisilicon/hispark_taurus/linux", 157 "target_cpu": "arm", 158 "type": "standard", 159 "version": "3.0", 160 "board": "hispark_taurus", 161 "inherit": [ "productdefine/common/base/standard_system.json", 162 "productdefine/common/inherit/ipcamera.json" 163 ], 164 "enable_ramdisk": true, 165 "subsystems": [ 166 { 167 "subsystem": "subsystem_examples", # Subsystem to which the component belongs. 168 "components": [ 169 { 170 "component": "partA", # Component name. 171 "features": [] # Configurable features of the component. 172 } 173 ] 174 }, 175 ··· 176 } 177 ``` 178 179 The configuration file contains information about the the product name and chip vendor. **inherit** specifies the dependent, and **subsystems** specifies the components other than the common components. 180 181 For example, add "subsystem_examples:partA" to the product **config.json** file. Then, **partA** will be built and packaged into the distribution. 182 1833. Start the build. 184 185 You can start the build by using the [CLI or hb tool](subsys-build-all.md#build-commands). The following uses the CLI as an example: 186 187 You can run '**--build-target *componentName***' to build a component separately. For example, to build the musl component of hispark_taurus_standard, run the following command: 188 189 ``` 190 ./build.sh --product-name hispark_taurus_standard --build-target musl --ccache 191 ``` 192 193 You can also build a product. For example, to build hispark_taurus_standard, run the following command: 194 195 ```shell 196 ./build.sh --product-name hispark_taurus_standard --ccache 197 ``` 198 1994. Obtain the build result. 200 201 You can obtain the generated files from the **out/hispark_taurus/** directory and the image in the **out/hispark_taurus/packages/phone/images/** directory. 202