1# 部件
2### 部件配置规则
3
4部件的bundle.json放在部件源码的根目录下。以泛sensor子系统的sensor服务部件为例,部件属性定义描述文件字段说明如下:
5
6```shell
7{
8    "name": "@ohos/sensor_lite",		                                 # HPM部件英文名称,格式"@组织/部件名称"
9    "description": "Sensor services",		                             # 部件功能一句话描述
10    "version": "3.1",			                                         # 版本号,版本号与OpenHarmony版本号一致
11    "license": "MIT",			                                         # 部件License
12    "publishAs": "code-segment",		                                 # HPM包的发布方式,当前默认都为code-segment
13    "segment": {
14        "destPath": ""
15    },					                                                 # 发布类型为code-segment时为必填项,定义发布类型code-segment的代码还原路径(源码路径)
16    "dirs": {"base/sensors/sensor_lite"},	                             # HPM包的目录结构,字段必填内容可以留空
17    "scripts": {},			                                             # HPM包定义需要执行的脚本,字段必填,值非必填
18    "licensePath": "COPYING",
19    "readmePath": {
20        "en": "README.rst"
21    },
22    "component": {			                                             # 部件属性
23        "name": "sensor_lite",			                                 # 部件名称
24        "subsystem": "",		                                         # 部件所属子系统
25        "syscap": [],				                                     # 部件为应用提供的系统能力
26        "features": [],                                                  # 部件对外的可配置特性列表,一般与build中的sub_component对应,可供产品配置
27        "adapted_system_type": [],		                                 # 轻量(mini)小型(small)和标准(standard),可以是多个
28        "rom": "92KB",                                                   # 部件ROM值
29        "ram": "~200KB",                                                 # 部件RAM估值
30        "deps": {
31        "components": [                                                  # 部件依赖的其他部件
32          "samgr_lite",
33          "ipc_lite"
34        ],
35        "third_party": [                                                 # 部件依赖的三方开源软件
36          "bounds_checking_function"
37        ],
38        "hisysevent_config": []                                          # 部件HiSysEvent打点配置文件编译入口
39      }
40        "build": {				                                         # 编译相关配置
41            "sub_component": [
42                ""//base/sensors/sensor_lite/services:sensor_service"",  # 部件编译入口
43            ],			                                                 # 部件编译入口,模块在此处配置
44            "inner_kits": [],						                     # 部件间接口
45            "test": []							                         # 部件测试用例编译入口
46        }
47    }
48 }
49```
50
51> **注意**:lite上旧的部件在build/lite/components目录下对应子系统的json文件中,路径规则为:**{领域}/{子系统}/{部件}**,部件目录树规则如下:
52
53```shell
54component
55├── interfaces
56│   ├── innerkits  # 系统内接口,部件间使用
57│   └── kits       # 应用接口,应用开发者使用
58├── frameworks     # framework实现
59├── services       # service实现
60└── BUILD.gn       # 部件编译脚本
61```
62
63部件配置中需要配置部件的名称、源码路径、功能简介、是否必选、编译目标、RAM、ROM、编译输出、已适配的内核、可配置的特性和依赖等属性定义。
64
65> **注意**:部件配置中HiSysEvent打点配置文件使用说明,请参考文档[HiSysEvent打点配置](subsys-dfx-hisysevent-logging-config.md)。
66
67新增部件时需要在对应子系统json文件中添加相应的部件定义。产品所配置的部件必须在某个子系统中被定义过,否则会校验失败。
68
69### 新增并编译部件
70
711. 添加部件。 本节以添加一个自定义的部件为例,描述如何编译部件,编译库、编译可执行文件等。
72
73   示例部件partA由feature1、feature2和feature3组成,feature1的编译目标为一个动态库,feature2的目标为一个可执行程序,feature3的目标为一个etc配置文件。
74
75   示例部件partA的配置需要添加到一个子系统中,本次示例将添加到subsystem_examples子系统中(subsystem_examples子系统定义在test/examples/目录)。
76
77   示例部件partA的完整目录结构如下:
78
79   ```shell
80   test/examples/partA
81   ├── feature1
82   │   ├── BUILD.gn
83   │   ├── include
84   │   │   └── helloworld1.h
85   │   └── src
86   │       └── helloworld1.cpp
87   ├── feature2
88   │   ├── BUILD.gn
89   │   ├── include
90   │   │   └── helloworld2.h
91   │   └── src
92   │       └── helloworld2.cpp
93   └── feature3
94       ├── BUILD.gn
95       └── src
96           └── config.conf
97   ```
98
99   示例1:编写动态库gn脚本test/examples/partA/feature1/BUILD.gn,示例如下:
100
101   ```shell
102   config("helloworld_lib_config") {
103    include_dirs = [ "include" ]
104   }
105
106   ohos_shared_library("helloworld_lib") {
107     sources = [
108       "include/helloworld1.h",
109       "src/helloworld1.cpp",
110     ]
111     public_configs = [ ":helloworld_lib_config" ]
112     part_name = "partA"
113   }
114   ```
115
116   示例2:编写可执行文件gn脚本test/examples/partA/feature2/BUILD.gn,示例如下:
117
118   ```shell
119   ohos_executable("helloworld_bin") {
120     sources = [
121       "src/helloworld2.cpp"
122     ]
123     include_dirs = [ "include" ]
124     deps = [                                # 依赖部件内模块
125       "../feature1:helloworld_lib"
126     ]
127     external_deps = [ "partB:module1" ]     # (可选)如果有跨部件的依赖,格式为“部件名:模块名”
128     install_enable = true                   # 可执行程序缺省不安装,需要安装时需要指定
129     part_name = "partA"
130   }
131   ```
132
133   示例3:编写etc模块gn脚本test/examples/partA/feature3/BUILD.gn,示例如下:
134
135   ```shell
136   ohos_prebuilt_etc("feature3_etc") {
137     source = "src/config.conf"
138     relative_install_dir = "init"    #可选,模块安装相对路径,相对于默认安装路径;默认在/system/etc目录
139     part_name = "partA"
140   }
141   ```
142
143   示例4:在部件的bundle.json中添加模块配置:test/examples/bundle.json。每个部件都有一个bundle.json配置文件,在部件的根目录下。示例见:[部件的bundle.json](subsys-build-component.md#部件配置规则)
144
1452. 将部件添加到产品配置中。 在产品的配置中添加部件,产品对应的配置文件://vendor/{product_company}/{product-name}/config.json。下面以vendor/hisilicon/hispark_taurus_standard/config.json为例:
146
147    ```shell
148      {
149        "product_name": "hispark_taurus_standard",
150        "device_company": "hisilicon",
151        "device_build_path": "device/board/hisilicon/hispark_taurus/linux",
152        "target_cpu": "arm",
153        "type": "standard",
154        "version": "3.0",
155        "board": "hispark_taurus",
156        "inherit": [ "productdefine/common/base/standard_system.json",
157                    "productdefine/common/inherit/ipcamera.json"
158        ],
159        "enable_ramdisk": true,
160        "subsystems": [
161          {
162            "subsystem": "subsystem_examples",                              # 部件所属子系统
163            "components": [
164              {
165                "component": "partA",                                       # 部件名称
166                "features": []                                              # 部件对外的可配置特性列表
167              }
168            ]
169          },
170        ······
171      }
172    ```
173
174    从中可以看出产品名称、芯片厂家等;inherit指出依赖的通用组件;subsystems指出通用组件以外的部件。
175
176    在产品配置文件中添加 "subsystem_examples:partA",表示该产品中会编译并打包partA到版本中。
177
1783. 编译。 主要有两种编译方式,[命令行方式和hb方式](subsys-build-all.md#编译命令),下面以命令行方式为例:
179
180   部件可以使用"--build-target 部件名"进行单独编译,以编译产品hispark_taurus_standard的musl部件为例,编译命令如下:
181
182   ```
183   ./build.sh --product-name hispark_taurus_standard --build-target musl --ccache
184   ```
185
186   也可以编译相应产品,以编译hispark_taurus_standard为例,编译命令如下:
187
188   ```shell
189   ./build.sh --product-name hispark_taurus_standard --ccache
190   ```
191
1924. 编译输出。 编译所生成的文件都归档在out/hispark_taurus/目录下,结果镜像输出在 out/hispark_taurus/packages/phone/images/ 目录下。