鸿蒙OpenHarmony【标准系统编写“Hello World”程序】 (基于RK3568开发板)

电子说

1.2w人已加入

描述

编写“Hello World”程序

下方将展示如何在单板上运行第一个应用程序,其中包括新建应用程序、编译、烧写、运行等步骤,最终输出“Hello World!”。

前提条件

已参考[创建工程并获取源码],创建RK3568开发板的源码工程。

示例目录

拉取openharmony项目代码,在代码根目录创建sample子系统文件夹,在子系统目录下创建hello部件文件夹,hello文件夹中创建hello源码目录,构建文件BUILD.gn及部件配置文件bundle.json。 示例完整目录如下。

HarmonyOS与OpenHarmony鸿蒙文档籽料:mau123789是v直接拿

sample/hello
│── BUILD.gn
│── include
│   └── helloworld.h
│── src
│   └── helloworld.c
├── bundle.json
build
└── subsystem_config.json
vendor/hihope
└── rk3568
    └── config.json

开发步骤

鸿蒙开发指导文档:[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md]

HarmonyOS

请在源码目录中通过以下步骤创建“Hello World”应用程序。

  1. 创建目录,编写业务代码。
    新建sample/hello/src/helloworld.c目录及文件,代码如下所示,用户可以自定义修改打印内容(例如:修改World为OHOS)。其中helloworld.h包含字符串打印函数HelloPrint的声明。当前应用程序可支持标准C及C++的代码开发。

    #include < stdio.h >
    #include "helloworld.h"
    
    int main(int argc, char **argv)
    {
        HelloPrint();
        return 0;
    }
    
    void HelloPrint()
    {
        printf("nn");
        printf("nttHello World!n");
        printf("nn");
    }
    

    再添加头文件sample/hello/include/helloworld.h,代码如下所示。

    #ifndef HELLOWORLD_H
    #define HELLOWORLD_H
    #ifdef __cplusplus
    #if __cplusplus
    extern "C" {
    #endif
    #endif
    
    void HelloPrint();
    
    #ifdef __cplusplus
    #if __cplusplus
    }
    #endif
    #endif
    #endif // HELLOWORLD_H
    
  2. 新建编译组织文件。
    新建sample/hello/BUILD.gn,创建方法可参考:[模块配置规则]。

模块

模块配置规则

编译子系统通过模块、部件和产品三层配置来实现编译和打包。模块就是编译子系统的一个目标,包括(动态库、静态库、配置文件、预编译模块等)。模块要定义属于哪个部件,一个模块只能归属于一个部件。OpenHarmony使用定制化的Gn模板来配置模块规则,Gn语法相关的基础知识请参考[官网手册]

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

# 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

#rust模板
ohos_rust_executable
ohos_rust_shared_library
ohos_rust_static_library
ohos_rust_proc_macro
ohos_rust_shared_ffi
ohos_rust_static_ffi
ohos_rust_cargo_crate
ohos_rust_systemtest
ohos_rust_unittest
ohos_rust_fuzztest

#其他常用模板
#配置文件
ohos_prebuilt_etc

#sa配置
ohos_sa_profile

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

C/C++模板示例

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

ohos_shared_library示例

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 = ""                # 必选,所属部件名称
  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示例

import("//build/ohos.gni")
ohos_static_library("helloworld") {
  sources = ["file"]            # 后缀名是.c的相关文件
  include_dirs = ["dir"]        # 包含目录
  configs = []                  # 配置
  deps = []                     # 部件内模块依赖
  part_name = ""                # 部件名称
  subsystem_name = ""           # 子系统名称
  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示例

import("//build/ohos.gni")
ohos_executable("helloworld") {
  configs = []                       # 配置  
  part_name = ""                     # 部件名称 
  subsystem_name = ""                # 子系统名称
  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]
  version_script = []
  use_exceptions = []
}

ohos_source_set示例

import("//build/ohos.gni")
ohos_source_set("helloworld") {
  sources = ["file"]              # 后缀名是.c的相关文件
  include_dirs = []               # 包含目录
  configs = []                    # 配置
  public = []                     # .h类型头文件
  defines = []
  public_configs = []
  part_name = ""                  # 部件名称
  subsystem_name = ""             # 子系统名称
  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 = []
}

注意

  • 只有sources和part_name是必选,其他都是可选的;
  • Sanitizer配置详见:[Sanitizer使用说明]

预编译模板示例

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

ohos_prebuilt_executable示例

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

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

  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示例

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

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

  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示例

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

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

  testonly = [boolean]
  visibility = []

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

注意 :只有sources和part_name是必选,其他都是可选的。

Hap模板

hap模板详见:[ HAP编译构建指导]

Rust模板

rust模板详见:[ Rust模块配置规则和指导]

其他常用模板

ohos_prebuilt_etc示例:

import("//build/ohos.gni")
ohos_prebuilt_etc("helloworld") {
  # ohos_prebuilt_etc模板最常用属性:
  source = "file"                          # 指定单个原文件
  module_install_dir = ""                  # 模块安装路径,从system/,vendor/后开始指定
  subsystem_name = ""                      # 子系统名
  part_name = ""                           # 必选,所属部件名称
  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示例:

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

注意 :只有sources和part_name是必选,其他都是可选的。

新增并编译模块

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

  • 在原有部件中添加一个模块
  • 新建部件并在其中添加模块
  • 新建子系统并在该子系统的部件下添加模块
    HarmonyOS

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

  1. 在模块目录下配置BUILD.gn,根据模板类型选择对应的gn模板。
  2. 修改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": []                                        # 部件测试用例编译入口
           }
       }
    }
    

注意 :无论哪种方式该bundle.json文件均在对应子系统所在文件夹下。

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

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

  1. 在模块目录下配置BUILD.gn,根据模板类型选择对应的gn模板。这一步与在原有部件中添加一个模块的方法基本一致,只需注意该模块对应BUILD.gn文件中的part_name为新建部件的名称即可。
  2. 新建一个bundle.json文件,bundle.json文件均在对应子系统所在文件夹下。
  3. 在vendor/{product_company}/{product-name}/config.json中添加对应的部件,直接添加到原有部件后即可。
    "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文件。

    {
     "子系统名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中添加对应的部件,直接添加到原有部件后即可。

    "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": []
          }
        ]
      },
    
     ]
    
  5. 成功添加验证:编译完成后打包到image中去,生成对应的so文件或者二进制文件。

编译模块

主要有两种编译方式,[命令行方式和hb方式],这里以命令行方式为例。

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

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

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

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

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

./build.sh --product-name hispark_taurus_standard --build-target musl --build-target 模块名 --ccache
创建 BUILD.gn内容如下所示:

import("//build/ohos.gni") # 导入编译模板
ohos_executable("helloworld") { # 可执行模块
sources = [ # 模块源码
"src/helloworld.c"
]
include_dirs = [ # 模块依赖头文件目录
"include"
]
cflags = []
cflags_c = []
cflags_cc = []
ldflags = []
configs = []
deps =[] # 部件内部依赖
part_name = "hello" # 所属部件名称,必选
install_enable = true # 是否默认安装(缺省默认不安装),可选
}


  1. 新建部件配置规则文件
    新建sample/hello/bundle.json文件,添加sample部件描述,创建方法可参考:[部件配置规则]

部件配置规则

部件的bundle.json放在部件源码的根目录下。以泛sensor子系统的sensor服务部件为例,部件属性定义描述文件字段说明如下:

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

注意 :lite上旧的部件在build/lite/components目录下对应子系统的json文件中,路径规则为: {领域}/{子系统}/{部件} ,部件目录树规则如下:

component
├── interfaces
│   ├── innerkits  # 系统内接口,部件间使用
│   └── kits       # 应用接口,应用开发者使用
├── frameworks     # framework实现
├── services       # service实现
└── BUILD.gn       # 部件编译脚本

部件配置中需要配置部件的名称、源码路径、功能简介、是否必选、编译目标、RAM、ROM、编译输出、已适配的内核、可配置的特性和依赖等属性定义。

注意 :部件配置中HiSysEvent打点配置文件使用说明,请参考文档[HiSysEvent打点配置]。

新增部件时需要在对应子系统json文件中添加相应的部件定义。产品所配置的部件必须在某个子系统中被定义过,否则会校验失败。

新增并编译部件

  1. 添加部件。 本节以添加一个自定义的部件为例,描述如何编译部件,编译库、编译可执行文件等。
    示例部件partA由feature1、feature2和feature3组成,feature1的编译目标为一个动态库,feature2的目标为一个可执行程序,feature3的目标为一个etc配置文件。
    示例部件partA的配置需要添加到一个子系统中,本次示例将添加到subsystem_examples子系统中(subsystem_examples子系统定义在test/examples/目录)。
    示例部件partA的完整目录结构如下:

    test/examples/partA
    ├── feature1
    │   ├── BUILD.gn
    │   ├── include
    │   │   └── helloworld1.h
    │   └── src
    │       └── helloworld1.cpp
    ├── feature2
    │   ├── BUILD.gn
    │   ├── include
    │   │   └── helloworld2.h
    │   └── src
    │       └── helloworld2.cpp
    └── feature3
        ├── BUILD.gn
        └── src
            └── config.conf
    

    示例1:编写动态库gn脚本test/examples/partA/feature1/BUILD.gn,示例如下:

    config("helloworld_lib_config") {
     include_dirs = [ "include" ]
    }
    
    ohos_shared_library("helloworld_lib") {
      sources = [
        "include/helloworld1.h",
        "src/helloworld1.cpp",
      ]
      public_configs = [ ":helloworld_lib_config" ]
      part_name = "partA"
    }
    

    示例2:编写可执行文件gn脚本test/examples/partA/feature2/BUILD.gn,示例如下:

    ohos_executable("helloworld_bin") {
      sources = [
        "src/helloworld2.cpp"
      ]
      include_dirs = [ "include" ]
      deps = [                                # 依赖部件内模块
        "../feature1:helloworld_lib"
      ]
      external_deps = [ "partB:module1" ]     # (可选)如果有跨部件的依赖,格式为“部件名:模块名”
      install_enable = true                   # 可执行程序缺省不安装,需要安装时需要指定
      part_name = "partA"
    }
    

    示例3:编写etc模块gn脚本test/examples/partA/feature3/BUILD.gn,示例如下:

    ohos_prebuilt_etc("feature3_etc") {
      source = "src/config.conf"
      relative_install_dir = "init"    #可选,模块安装相对路径,相对于默认安装路径;默认在/system/etc目录
      part_name = "partA"
    }
    

    示例4:在部件的bundle.json中添加模块配置:test/examples/bundle.json。每个部件都有一个bundle.json配置文件,在部件的根目录下。示例见:[部件的bundle.json]

  2. 将部件添加到产品配置中。 在产品的配置中添加部件,产品对应的配置文件://vendor/{product_company}/{product-name}/config.json。下面以vendor/hisilicon/hispark_taurus_standard/config.json为例:

    {
        "product_name": "hispark_taurus_standard",
        "device_company": "hisilicon",
        "device_build_path": "device/board/hisilicon/hispark_taurus/linux",
        "target_cpu": "arm",
        "type": "standard",
        "version": "3.0",
        "board": "hispark_taurus",
        "inherit": [ "productdefine/common/base/standard_system.json",
                    "productdefine/common/inherit/ipcamera.json"
        ],
        "enable_ramdisk": true,
        "subsystems": [
          {
            "subsystem": "subsystem_examples",                              # 部件所属子系统
            "components": [
              {
                "component": "partA",                                       # 部件名称
                "features": []                                              # 部件对外的可配置特性列表
              }
            ]
          },
        ······
      }
    

    从中可以看出产品名称、芯片厂家等;inherit指出依赖的通用组件;subsystems指出通用组件以外的部件。

    在产品配置文件中添加 "subsystem_examples:partA",表示该产品中会编译并打包partA到版本中。

  3. 编译。 主要有两种编译方式,[命令行方式和hb方式],下面以命令行方式为例:
    部件可以使用"--build-target 部件名"进行单独编译,以编译产品hispark_taurus_standard的musl部件为例,编译命令如下:

    ./build.sh --product-name hispark_taurus_standard --build-target musl --ccache
    

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

    ./build.sh --product-name hispark_taurus_standard --ccache
    
  4. 编译输出。 编译所生成的文件都归档在out/hispark_taurus/目录下,结果镜像输出在 out/hispark_taurus/packages/phone/images/ 目录下。
    bundle.json内容如下所示。

    {
        "name": "@ohos/hello",
        "description": "Hello world example.",
        "version": "3.1",
        "license": "Apache License 2.0",
        "publishAs": "code-segment",
        "segment": {
            "destPath": "sample/hello"
        },
        "dirs": {},
        "scripts": {},
        "component": {
            "name": "hello",
            "subsystem": "sample",
            "syscap": [],
            "features": [],
            "adapted_system_type": [ "mini", "small", "standard" ],
            "rom": "10KB",
            "ram": "10KB",
            "deps": {
                "components": [],
                "third_party": []
            },
            "build": {
                "sub_component": [
                    "//sample/hello:helloworld"
                ],
                "inner_kits": [],
                "test": []
            }
        }
    }
    

    bundle.json文件包含两个部分,第一部分描述该部件所属子系统的信息,第二部分component则定义该部件构建相关配置。添加的时候需要指明该部件包含的模块sub_component,假如有提供给其它部件的接口,需要在inner_kits中说明,假如有测试用例,需要在test中说明,inner_kits与test没有也可以不添加。

  5. 修改子系统配置文件。
    在build/subsystem_config.json中添加新建的子系统的配置。修改方法可参考:[子系统配置规则]

子系统配置规则

通过build仓下的subsystem_config.json可以查看所有子系统的配置规则。

{
  "arkui": {
    "path": "foundation/arkui",      # 路径
    "name": "arkui"                  # 子系统名
  },
  "ai": {
    "path": "foundation/ai",
    "name": "ai"
  },
  "account": {
    "path": "base/account",
    "name": "account"
  },
  "distributeddatamgr": {
    "path": "foundation/distributeddatamgr",
    "name": "distributeddatamgr"
  },
  "security": {
    "path": "base/security",
    "name": "security"
  },
  ...
}

子系统的配置规则主要是在build/subsystem_config.json中指定子系统的路径和子系统名称。

新增子系统的配置如下所示。

"sample": {
"path": "sample",
"name": "sample"
},


  1. 修改产品配置文件。

    说明: OpenHarmony-v3.2-Beta2之前版本,RK3568的产品配置文件为productdefine/common/products/rk3568.json;从OpenHarmony-v3.2-Beta2版本开始,RK3568的产品配置文件为vendor/hihope/rk3568/config.json。

    • 3.2-Beta2之前版本
      在productdefine/common/products/rk3568.json中添加对应的hello部件,直接添加到原有部件后即可。
      "usb:usb_manager_native":{},
      "applications:prebuilt_hap":{},
      "sample:hello":{},
      "wpa_supplicant-2.9:wpa_supplicant-2.9":{},
      
    • 3.2-Beta2及之后版本
      在vendor/hihope/rk3568/config.json中添加对应的hello部件,直接添加到原有部件后即可。
      {
        "subsystem": "sample",
        "components": [
          {
            "component": "hello",
            "features": []
          }
        ]
      },
      

审核编辑 黄宇

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分