subsys-build-module.md 19.8 KB
Newer Older
1 2 3
# 模块
## 模块配置规则

4
编译子系统通过模块、部件和产品三层配置来实现编译和打包。模块就是编译子系统的一个目标,包括(动态库、静态库、配置文件、预编译模块等)。模块要定义属于哪个部件,一个模块只能归属于一个部件。OpenHarmony使用定制化的Gn模板来配置模块规则,Gn语法相关的基础知识请参考[官网手册](https://gn.googlesource.com/gn/+/main/docs/reference.md)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

以下是常用的模块配置规则:

```
# C/C++模板
ohos_shared_library
ohos_static_library
ohos_executable
ohos_source_set

# 预编译模板:
ohos_prebuilt_executable
ohos_prebuilt_shared_library
ohos_prebuilt_static_library

#hap模板
ohos_hap
ohos_app_scope
ohos_js_assets
ohos_resources

#其他常用模板
#配置文件
28
ohos_prebuilt_etc
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 62 63 64

#sa配置
ohos_sa_profile
```

ohos开头的模板与内建模板的差异主要在于:推荐使用ohos定制模板。

### C/C++模板示例

ohos开头的模板对应的.gni文件路径在:openharmony/build/templates/cxx/cxx.gni。

ohos_shared_library示例

```shell
import("//build/ohos.gni")
ohos_shared_library("helloworld") {
  sources = ["file"]
  include_dirs = []             # 如有重复头文件定义,优先使用前面路径头文件
  cflags = []                   # 如重复冲突定义,后面的参数优先生效,也就是该配置项中优先生效
  cflags_c = []
  cflags_cc = []
  ldflags = []                  # 如重复冲突定义,前面参数优先生效,也就是ohos_template中预制参数优先生效
  configs = []
  deps = []                     # 部件内模块依赖

  external_deps = [             # 跨部件模块依赖定义,
  "part_name:module_name",      # 定义格式为 "部件名:模块名称"
  ]                             # 这里依赖的模块必须是依赖的部件声明在inner_kits中的模块

  output_name = [string]        # 模块输出名
  output_extension = []         # 模块名后缀
  module_install_dir = []       # 缺省在/system/lib64或/system/lib下, 模块安装路径,模块安装路径,从system/,vendor/后开始指定
  relative_install_dir = []     # 模块安装相对路径,相对于/system/lib64或/system/lib;如果有module_install_dir配置时,该配置不生效

  part_name = [string]          # 必选,所属部件名称
  output_dir
H
Haryslee 已提交
65

66 67 68
  # Sanitizer配置,每项都是可选的,默认为false/空
  sanitize = {
    # 各个Sanitizer开关
H
Haryslee 已提交
69 70 71 72 73
    cfi = [boolean]               # 控制流完整性检测
    integer_overflow = [boolean]  # 整数溢出检测
    boundary_sanitize = [boolean] # 边界检测
    ubsan = [boolean]             # 部分ubsan选项
    all_ubsan = [boolean]         # 全量ubsan选项
74 75 76 77 78
    ...

    debug = [boolean]             # 调测模式
    blocklist = [string]          # 屏蔽名单路径
  }
H
Haryslee 已提交
79

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  testonly = [boolean]
  license_as_sources = []
  license_file = []             # 后缀名是.txt的文件
  remove_configs = []
  no_default_deps = []
  install_images = []
  install_enable = [boolean]
  symlink_target_name = []
  version_script = []
  use_exceptions = []
}
```

ohos_static_library示例

```shell
import("//build/ohos.gni")
ohos_static_library("helloworld") {
  sources = ["file"]            # 后缀名是.c的相关文件
  include_dirs = ["dir"]        # 包含目录
  configs = []                  # 配置
  deps = []                     # 部件内模块依赖
  part_name = [string]          # 部件名称
  subsystem_name = [string]     # 子系统名称
  cflags = []

  external_deps = [             # 跨部件模块依赖定义,
  "part_name:module_name",      # 定义格式为 "部件名:模块名称"
  ]                             # 这里依赖的模块必须是依赖的部件声明在inner_kits中的模块

  lib_dirs = []
  public_configs = []
H
Haryslee 已提交
112

113 114 115
  # Sanitizer配置,每项都是可选的,默认为false/空
  sanitize = {
    # 各个Sanitizer开关
H
Haryslee 已提交
116 117 118 119 120
    cfi = [boolean]               # 控制流完整性检测
    integer_overflow = [boolean]  # 整数溢出检测
    boundary_sanitize = [boolean] # 边界检测
    ubsan = [boolean]             # 部分ubsan选项
    all_ubsan = [boolean]         # 全量ubsan选项
121 122 123 124 125
    ...

    debug = [boolean]             # 调测模式
    blocklist = [string]          # 屏蔽名单路径
  }
H
Haryslee 已提交
126

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  remove_configs = []
  no_default_deps = []
  license_file = []             # 后缀名是.txt的文件
  license_as_sources = []
  use_exceptions = []
}
```

ohos_executable示例

```shell
import("//build/ohos.gni")
ohos_executable("helloworld") {
  configs = []                       # 配置  
  part_name = [string]               # 部件名称 
  subsystem_name = [string]          # 子系统名称
  deps = []                          # 部件内模块依赖

  external_deps = [                  # 跨部件模块依赖定义,
  "part_name:module_name",           # 定义格式为 "部件名:模块名称"
  ]                                  # 这里依赖的模块必须是依赖的部件声明在inner_kits中的模块
  ohos_test = []
  test_output_dir = []
H
Haryslee 已提交
150

151 152 153
  # Sanitizer配置,每项都是可选的,默认为false/空
  sanitize = {
    # 各个Sanitizer开关
H
Haryslee 已提交
154 155 156 157 158
    cfi = [boolean]               # 控制流完整性检测
    integer_overflow = [boolean]  # 整数溢出检测
    boundary_sanitize = [boolean] # 边界检测
    ubsan = [boolean]             # 部分ubsan选项
    all_ubsan = [boolean]         # 全量ubsan选项
159 160 161 162 163
    ...

    debug = [boolean]             # 调测模式
    blocklist = [string]          # 屏蔽名单路径
  }
H
Haryslee 已提交
164

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
  testonly = [boolean]
  license_as_sources = []
  license_file = []                  # 后缀名是.txt的文件
  remove_configs = []
  static_link = []
  install_images = []
  module_install_dir = []            # 模块安装路径,从system/,vendor/后开始指定
  relative_install_dir = []
  symlink_target_name = []
  output_dir = [directory]           # 存放输出文件的目录
  install_enable = [boolean]
  version_script = []
  use_exceptions = []
}
```

ohos_source_set示例

```shell
import("//build/ohos.gni")
ohos_source_set("helloworld") {
  sources = ["file"]              # 后缀名是.c的相关文件
  include_dirs = []               # 包含目录
  configs = []                    # 配置
  public = []                     # .h类型头文件
  defines = []
  public_configs = []
  part_name = [string]            # 部件名称
  subsystem_name = [string]       # 子系统名称
  deps = []  # 部件内模块依赖

  external_deps = [               # 跨部件模块依赖定义,
  "part_name:module_name",        # 定义格式为 "部件名:模块名称"
  ]                               # 这里依赖的模块必须是依赖的部件声明在inner_kits中的模块
H
Haryslee 已提交
199

200 201 202
  # Sanitizer配置,每项都是可选的,默认为false/空
  sanitize = {
    # 各个Sanitizer开关
H
Haryslee 已提交
203 204 205 206 207
    cfi = [boolean]               # 控制流完整性检测
    integer_overflow = [boolean]  # 整数溢出检测
    boundary_sanitize = [boolean] # 边界检测
    ubsan = [boolean]             # 部分ubsan选项
    all_ubsan = [boolean]         # 全量ubsan选项
208 209 210 211 212
    ...

    debug = [boolean]             # 调测模式
    blocklist = [string]          # 屏蔽名单路径
  }
H
Haryslee 已提交
213

214 215 216 217 218 219 220 221 222 223 224
  testonly = [boolean]
  license_as_sources = []
  license_file = []
  remove_configs = []
  no_default_deps = []
  license_file = []               # 后缀名是.txt的文件
  license_as_sources = []
  use_exceptions = []
}
```

225 226 227
![icon-note.gif](public_sys-resources/icon-note.gif)**注意**
  - 只有sources和part_name是必选,其他都是可选的;
  - Sanitizer配置详见:[Sanitizer使用说明](subsys-build-reference.md#Sanitizer使用说明)
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530

### 预编译模板示例

预编译模板的.gni相关文件路径在:openharmony/build/templates/cxx/prebuilt.gni。

ohos_prebuilt_executable示例

```shell
import("//build/ohos.gni")
ohos_prebuilt_executable("helloworld") {
  sources = ["file"]                      # 源
  output = []
  install_enable = [boolean]         

  deps = []                               # 部件内模块依赖
  public_configs = []
  subsystem_name = [string]               # 子系统名
  part_name = [string]                    # 部件名

  testonly = [boolean]
  visibility = []

  install_images = []
  module_install_dir = []                 # 模块安装路径,从system/,vendor/后开始指定
  relative_install_dir = []               # 模块安装相对路径,相对于system/etc;如果有module_install_dir配置时,该配置不生效
  symlink_target_name = []


  license_file = []                       # 后缀名是.txt的文件
  license_as_sources = []
}
```

ohos_prebuilt_shared_library示例

```shell
import("//build/ohos.gni")
ohos_prebuilt_shared_library("helloworld") {
  sources = ["file"]                   # 一般是后缀为.so的文件
  output = []
  install_enable = [boolean]

  deps = []                            # 部件内模块依赖
  public_configs = []
  subsystem_name = [string]            # 子系统名
  part_name = [string]                 # 部件名

  testonly = [boolean]
  visibility = []

  install_images = []
  module_install_dir = []              # 模块安装路径,从system/,vendor/后开始指定
  relative_install_dir = []            # 模块安装相对路径,相对于system/etc;如果有module_install_dir配置时,该配置不生效
  symlink_target_name = [string]


  license_file = [string]              # 后缀名是.txt的文件
  license_as_sources = []
}
```

ohos_prebuilt_static_library示例

```shell
import("//build/ohos.gni")
ohos_prebuilt_static_library("helloworld") {
  sources = ["file"]               # 一般是后缀为.so的文件
  output = []

  deps = []                        # 部件内模块依赖
  public_configs = []
  subsystem_name = [string]        # 子系统名
  part_name = [string]             # 部件名

  testonly = [boolean]
  visibility = []

  license_file = [string]          # 后缀名是.txt的文件
  license_as_sources = []
}
```

![icon-note.gif](public_sys-resources/icon-note.gif)**注意**:只有sources和part_name是必选,其他都是可选的。

### Hap模板

hap模板详见:[ HAP编译构建指导](https://gitee.com/openharmony/docs/blob/master/zh-cn/device-dev/subsystems/subsys-build-gn-hap-compilation-guide.md)



### 其他常用模板

ohos_prebuilt_etc示例:

```shell
import("//build/ohos.gni")
ohos_prebuilt_etc("helloworld") {
  # ohos_prebuilt_etc模板最常用属性:
  sources = ["file"]
  module_install_dir = []                  # 模块安装路径,从system/,vendor/后开始指定
  subsystem_name = [string]                # 子系统名
  part_name = [string]                     # 必选,所属部件名称
  install_images = []
  relative_install_dir = []                # 模块安装相对路径,相对于system/etc;如果有module_install_dir配置时,该配置不生效
  
  # ohos_prebuilt_etc模板不常用属性:
  deps = []                                # 部件内模块依赖
  testonly = [boolean]
  visibility = []
  public_configs = []
  symlink_target_name = [string]
  license_file = [string]
  license_as_sources = []
}
```

ohos_sa_profile示例:

```shell
import("//build/ohos.gni")
ohos_sa_profile("helloworld") {
  sources = [".xml"]                   # xml文件
  part_name = [string]                 # 部件名
  subsystem_name = [string]            # 子系统名
}
```

![icon-note.gif](public_sys-resources/icon-note.gif)**注意**:只有sources和part_name是必选,其他都是可选的。

## 新增并编译模块

新建模块可以分为以下三种情况。主要的添加逻辑如下面的流程图所示,若没有子系统则需新建子系统并在该子系统的部件下添加模块,若没有部件则需新建部件并在其中添加模块,否则直接在原有部件中添加模块即可,需要注意的是芯片解决方案作为特殊部件是没有对应子系统的。

- 在原有部件中添加一个模块

- 新建部件并在其中添加模块

- 新建子系统并在该子系统的部件下添加模块

  ![模块添加流程](figures/module_addition_process.png)

**在原有部件中添加一个模块**

1. 在模块目录下配置BUILD.gn,根据模板类型选择对应的gn模板。 

2. 修改bundle.json配置文件。

   ```shell
   {
      "name": "@ohos/<component_name>",                         # HPM部件英文名称,格式"@组织/部件名称"
      "description": "xxxxxxxxxxxxxxxxxxx",                     # 部件功能一句话描述
      "version": "3.1",                                         # 版本号,版本号与OpenHarmony版本号一致
      "license": "MIT",                                         # 部件License
      "publishAs": "code-segment",                              # HPM包的发布方式,当前默认都为code-segment
      "segment": {
          "destPath": "third_party/nghttp2"
      },                                                        # 发布类型为code-segment时为必填项,定义发布类型code-segment的代码还原路径(源码路径)
      "dirs": {},                                               # HPM包的目录结构,字段必填内容可以留空
      "scripts": {},                                            # HPM包定义需要执行的脚本,字段必填,值非必填
      "licensePath": "COPYING",
      "readmePath": {
          "en": "README.rst"
      },
      "component": {                                            # 部件属性
          "name": "<component_name>",                           # 部件名称
          "subsystem": ,                                        # 部件所属子系统
          "syscap": [],                                         # 部件为应用提供的系统能力
          "features": [],                                       # 部件对外的可配置特性列表,一般与build中的sub_component对应,可供产品配置
          "adapted_system_type": [],                            # 轻量(mini)小型(small)和标准(standard),可以是多个
          "rom": "xxxKB"                                        # ROM基线,没有基线写当前值
          "ram": "xxxKB",                                       # RAM基线,没有基线写当前值
          "deps": {
              "components": [],                                 # 部件依赖的其他部件
              "third_party": []                                 # 部件依赖的三方开源软件
          },
       
          "build": {                                            # 编译相关配置
              "sub_component": [
                  "//foundation/arkui/napi:napi_packages",      # 原有模块1
                  "//foundation/arkui/napi:napi_packages_ndk"   # 原有模块2
                  "//foundation/arkui/napi:new"                 # 新增模块new
              ],                                                # 部件编译入口,模块在此处配置
              "inner_kits": [],                                 # 部件间接口
              "test": []                                        # 部件测试用例编译入口
          }
      }
   }
   ```

   ![icon-note.gif](public_sys-resources/icon-note.gif)**注意**:无论哪种方式该bundle.json文件均在对应子系统所在文件夹下。

3. 成功添加验证:编译完成后打包到image中去,生成对应的so文件或者二进制文件。

**新建部件并在其中添加一个模块**

1. 在模块目录下配置BUILD.gn,根据模板类型选择对应的gn模板。这一步与在原有部件中添加一个模块的方法基本一致,只需注意该模块对应BUILD.gn文件中的part_name为新建部件的名称即可。

2. 新建一个bundle.json文件,bundle.json文件均在对应子系统所在文件夹下。

3. 在vendor/{product_company}/{product-name}/config.json中添加对应的部件,直接添加到原有部件后即可。

   ```shell
    "subsystems": [
         {
           "subsystem": "部件所属子系统名",
           "components": [
             { "component": "部件名1", "features":[] },         # 子系统下的原有部件1
             { "component": "部件名2", "features":[] },         # 子系统下的原有部件2
             { "component": "部件名new", "features":[] }        # 子系统下的新增部件new
           ]
         },
         .
    ]
   ```

4. 成功添加验证:编译完成后打包到image中去,生成对应的so文件或者二进制文件。
   

**新建子系统并在该子系统的部件下添加模块**

1. 在模块目录下配置BUILD.gn,根据模板类型选择对应的gn模板。这一步与新建部件并在其中添加模块中对应的步骤并无区别。

2. 在新建的子系统目录下每个部件对应的文件夹下创建bundle.json文件,定义部件信息。这一步与新建部件并在其中添加模块中对应的步骤并无区别。

3. 修改build目录下的subsystem_config.json文件。

   ```shell
   {
    "子系统名1": {                     # 原有子系统1
      "path": "子系统目录1",
      "name": "子系统名1"
    },
     "子系统名2": {                    # 原有子系统2
      "path": "子系统目录2",
      "name": "子系统名2"
    },
    "子系统名new": {                   # 新增子系统new
      "path": "子系统目录new",
      "name": "子系统名new"
    },
   
   }
   ```

   该文件定义了有哪些子系统以及这些子系统所在文件夹路径,添加子系统时需要说明子系统path与name,分别表示子系统路径和子系统名。

4. 在vendor/{product_company}/{product-name}目录下的产品配置如product-name是hispark_taurus_standard时,在config.json中添加对应的部件,直接添加到原有部件后即可。

   ```shell
   "subsystems": [
     {
       "subsystem": "arkui",                      # 原有的子系统名
       "components": [                            # 单个子系统下的所有部件集合
         {
           "component": "ace_engine_standard",    # 原有的部件名
           "features": []
         },
         {
           "component": "napi",                   # 原有的部件名
           "features": []
         }
          {
           "component": "component_new1",         # 原有子系统新增的的部件名component_new1
           "features": []
         }
      ]
     },
     {
       "subsystem": "subsystem_new",              #  新增的子系统名
       "components": [
         {
           "component": "component_new2",         # 新增子系统新增的的部件名component_new2
           "features": []
         }
       ]
     },
    
    ]
   ```

4. 成功添加验证:编译完成后打包到image中去,生成对应的so文件或者二进制文件。


**编译模块**

主要有两种编译方式,[命令行方式和hb方式](subsys-build-all.md#编译命令),这里以命令行方式为例。

   模块可以使用“--build-target 模块名"单独编译,编译命令如下:

   ```shell
   ./build.sh --build-target 模块名
   ```

   也可以编译相应产品,以编译hispark_taurus_standard为例,编译命令如下:

   ```shell
   ./build.sh --product-name hispark_taurus_standard --build-target 模块名 --ccache
   ```

   还可以编译模块所在的部件:

   ```shell
   ./build.sh --product-name hispark_taurus_standard --build-target musl --build-target 模块名 --ccache
531
   ```