• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br>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:
51
52```shell
53component
54├── interfaces
55│   ├── innerkits  # APIs exposed internally among components
56│   └── kits       # APIs provided for application developers
57├── frameworks     # Framework implementation
58├── services       # Service implementation
59├── BUILD.gn       # Build script
60```
61
62You 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.
63
64When 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.
65
66### Adding and Building a Component
67
681. Add a component.
69
70   The following use a custom component as an example to describe how to compile a library, executable file, and configuration file.
71
72   In this example, **partA** consists of **feature1**, **feature2**, and **feature3**, which represent a dynamic library, an executable file, and an etc configuration file, respectively.
73
74   Add **partA** to a subsystem, for example, **subsystem_examples** (defined in the **test/examples/** directory).
75
76   The directory structure of **partA** is as follows:
77
78   ```shell
79   test/examples/partA
80   ├── feature1
81   │   ├── BUILD.gn
82   │   ├── include
83   │   │   └── helloworld1.h
84   │   └── src
85   │       └── helloworld1.cpp
86   ├── feature2
87   │   ├── BUILD.gn
88   │   ├── include
89   │   │   └── helloworld2.h
90   │   └── src
91   │       └── helloworld2.cpp
92   └── feature3
93       ├── BUILD.gn
94       └── src
95           └── config.conf
96   ```
97
98   (a) Configure **test/examples/partA/feature1/BUILD.gn** for the dynamic library.
99
100   ```shell
101   config("helloworld_lib_config") {
102    include_dirs = [ "include" ]
103   }
104
105   ohos_shared_library("helloworld_lib") {
106     sources = [
107       "include/helloworld1.h",
108       "src/helloworld1.cpp",
109     ]
110     public_configs = [ ":helloworld_lib_config" ]
111     part_name = "partA"
112   }
113   ```
114
115   (b) Configure **test/examples/partA/feature2/BUILD.gn** for the executable file.
116
117   ```shell
118   ohos_executable("helloworld_bin") {
119     sources = [
120       "src/helloworld2.cpp"
121     ]
122     include_dirs = [ "include" ]
123     deps = [                                # Dependent modules in the component
124       "../feature1:helloworld_lib"
125     ]
126     external_deps = [ "partB:module1" ]     # (Optional) Dependent modules of another component are named in the Component name:Module name format.
127     install_enable = true                   # By default, executable programs are not installed. Set this parameter to true if an executable program needs to be installed.
128     part_name = "partA"
129   }
130   ```
131
132   (c) Configure **test/examples/partA/feature3/BUILD.gn** for the etc module.
133
134   ```shell
135   ohos_prebuilt_etc("feature3_etc") {
136     source = "src/config.conf"
137     relative_install_dir = "init"    # (Optional) Relative directory for installing the module. The default installation directory is **/system/etc**.
138     part_name = "partA"
139   }
140   ```
141
142   (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).
143
1442. Add the component to **//vendor/{*product_company*}/{*product_name*}/config.json**.
145
146   For example, add "subsystem_examples:partA" to the product **config.json** file. Then, **partA** will be built and packaged into the distribution.
147
1483. Start the build.
149
150   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:
151
152   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:
153
154   ```
155   ./build.sh --product-name hispark_taurus_standard --build-target musl --ccache
156   ```
157
158   You can also build a product. For example, to build hispark_taurus_standard, run the following command:
159
160   ```shell
161   ./build.sh --product-name hispark_taurus_standard --ccache
162   ```
163
1644. Obtain the build result.
165
166   You can obtain the generated files from the **out/hispark_taurus/** directory and the image in the **out/hispark_taurus/packages/phone/images/** directory.
167