openharmony-basics-module configuration rules

The compilation subsystem implements compilation and packaging through three-layer configuration of modules, components and products. A module is a target of the compilation subsystem, including (dynamic libraries, static libraries, configuration files, precompiled modules, etc.). The module needs to define which component it belongs to, and a module can only belong to one component.

The following are commonly used module configuration rules:

# C/C++ template
ohos_shared_library #dynamic library
ohos_static_library #static library
ohos_executable #executable file
ohos_source_set
#Precompiled template:
ohos_prebuilt_executable
ohos_prebuilt_shared_library
ohos_prebuilt_static_library
#hap template
ohos_hap
ohos_app_scope
ohos_js_assets
ohos_resources
#Other commonly used templates
#Configuration File
ohos_prebuilt_etc
#sa configuration
ohos_sa_profile

c/c++ template example

The .gni file path corresponding to the template starting with ohos is: openharmony/build/templates/cxx/cxx.gni

ohos_shared_library example:

import("//build/ohos.gni")
ohos_shared_library("helloworld") {
  sources = ["file"]            # 包含的C或C++文件,如:["",""]
  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

  # Sanitizer配置,每项都是可选的,默认为false/空
  sanitize = {
    # 各个Sanitizer开关
    cfi = [boolean]               # 控制流完整性检测
    cfi_cross_dso = [boolean]     # 开启跨so调用的控制流完整性检测
    integer_overflow = [boolean]  # 整数溢出检测
    boundary_sanitize = [boolean] # 边界检测
    ubsan = [boolean]             # 部分ubsan选项
    all_ubsan = [boolean]         # 全量ubsan选项
    ...

    debug = [boolean]             # 调测模式
    blocklist = [string]          # 屏蔽名单路径
  }

  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 example

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 = []

  # Sanitizer配置,每项都是可选的,默认为false/空
  sanitize = {
    # 各个Sanitizer开关
    cfi = [boolean]               # 控制流完整性检测
    cfi_cross_dso = [boolean]     # 开启跨so调用的控制流完整性检测
    integer_overflow = [boolean]  # 整数溢出检测
    boundary_sanitize = [boolean] # 边界检测
    ubsan = [boolean]             # 部分ubsan选项
    all_ubsan = [boolean]         # 全量ubsan选项
    ...

    debug = [boolean]             # 调测模式
    blocklist = [string]          # 屏蔽名单路径
  }

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

ohos_executable example

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 = []

  # Sanitizer配置,每项都是可选的,默认为false/空
  sanitize = {
    # 各个Sanitizer开关
    cfi = [boolean]               # 控制流完整性检测
    cfi_cross_dso = [boolean]     # 开启跨so调用的控制流完整性检测
    integer_overflow = [boolean]  # 整数溢出检测
    boundary_sanitize = [boolean] # 边界检测
    ubsan = [boolean]             # 部分ubsan选项
    all_ubsan = [boolean]         # 全量ubsan选项
    ...

    debug = [boolean]             # 调测模式
    blocklist = [string]          # 屏蔽名单路径
  }

  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]         # 编译后的镜像烧录后,可执行模块默认是不安装在开发板内,如果要安装需要指定install_enable为true.
  version_script = []
  use_exceptions = []
}

ohos_source_set example

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中的模块

  # Sanitizer配置,每项都是可选的,默认为false/空
  sanitize = {
    # 各个Sanitizer开关
    cfi = [boolean]               # 控制流完整性检测
    cfi_cross_dso = [boolean]     # 开启跨so调用的控制流完整性检测
    integer_overflow = [boolean]  # 整数溢出检测
    boundary_sanitize = [boolean] # 边界检测
    ubsan = [boolean]             # 部分ubsan选项
    all_ubsan = [boolean]         # 全量ubsan选项
    ...

    debug = [boolean]             # 调测模式
    blocklist = [string]          # 屏蔽名单路径
  }

  testonly = [boolean]
  license_as_sources = []
  license_file = []
  remove_configs = []
  no_default_deps = []
  license_file = []               # 后缀名是.txt的文件
  license_as_sources = []
  use_exceptions = []
}

Notice:

  • Only sources and part_name are required, the others are optional;

  • For details on Sanitizer configuration, see: Sanitizer Instructions for Use.

Precompiled template example

The .gni related file path of the precompiled template is: openharmony/build/templates/cxx/prebuilt.gni.

ohos_prebuilt_executable example:

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 = []
}

Note : Only sources and part_name are required, others are optional.

Add module and compile

Divided into three cases:

  • Add a module to the original component

  • Create a new widget and add modules to it

  • Create a new subsystem and add modules under the subsystem's components.

Add a module to the original component.

  1. Configure BUILD.gn in the module directory and select the corresponding gn template according to the template type.

  1. Modify bundle.json

{
   "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": []                                        # 部件测试用例编译入口
       }
   }
}

Note : Either way, the bundle.json file is in the folder where the corresponding subsystem is located.

Create a new widget and add a module to it

1. Configure BUILD.gn in the module directory and select the corresponding gn template according to the template type. The method of adding a template to the original component is basically the same. Just note that the part_name in the BUILD.gn file corresponding to the template is the name of the new component.

2. Create a new bundle.json file

3. Add the corresponding components to vendor/{product_company}/{product-name}/config.json and add them directly to the original components. For example, ~/vendor/hihope/rk3568/config.json.

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

Create a new subsystem and add modules under the components of the subsystem

1. Configure BUILD.gn in the module directory.

2. Create bundle.json in the folder corresponding to each component of the subsystem.

3. Modify subsystem_config.json in the build/ directory.

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

}

4. Add new corresponding components to vendor/{product_company}/{product-name}/config.json, for example, ~/vendor/hihope/rk3568/config.json.

"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": []
      }
    ]
  },
 
 ]

Module compilation

1. Modules can be compiled separately using "--build-target module name". The compilation command is as follows:

./build.sh --build-target 模块名 或者 ./build.sh -T  模块名

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

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

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

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

Guess you like

Origin blog.csdn.net/zhoudidong/article/details/129684830