【OpenHarmony】L0L1 添加子系统/组件

环境与设备说明

当前文档记录时,使用的是OpenHarmony4.0版本。
使用的L0设备是Hi3861,对应编译类型是wifiiot_hispark_pegasus,内核是liteos-m。
而使用的L1设备是Hi3516,对应编译类型是ipcamera_hispark_taurus和ipcamera_hispark_taurus_linux,内核分别是liteos-a和linux。

自定义子系统目录结构

在openharmony项目代码里,可暂时随意位置存放你创建的子系统代码。写完新增子系统的代码后,需要在新增的代码目录下添加BUILD.gn文件。这里创建叫做"myhello"的子系统,组件名叫"hello_test"。
注意:build/lite/components编译组件已弃用,被弃用的编译组件是OpenHarmony3.0前使用。

项目自定义存放路径/myhello
├── src 			  # 源文件存放目录
│   └── my_hello.c
│   └── main.c
├── include			  # 头文件存放目录
│   └── my_hello.h
├── BUILD.gn          # 编译配置脚本
├── bundle.json       # 元数据声明文件

编译说明

在BUILD.gn编译脚本中,配置好子系统代码的编译关系。BUILD.gn作为模块编译的入口,需要在bundle.json中声明,被OpenHarmony编译模块发现,加入到集成编译中。

创建BUILD

BUILD.gn内容如下:

import("//build/lite/config/component/lite_component.gni")

if (ohos_kernel_type == "liteos_m") {
  # 静态库
  static_library("hello_lib") {
    sources = [ "src/my_hello.c" ]
    include_dirs = [ "include" ]
    defines = []
    cflags_c = []
    ldflags = []
    deps = []
  }
} else {
  # 动态库
  shared_library("hello_lib") {
    sources = [ "src/my_hello.c" ]
    include_dirs = [ "include" ]
    defines = []
    cflags_c = []
    ldflags = []
    deps = []
  }
}

# 执行文件
executable("hello_bin") {
  sources = [ "src/main.c" ]
  include_dirs = [ "include" ]
  defines = []
  cflags_c = []
  ldflags = []
  deps = [ ":hello_lib" ]
}

lite_component("my_hello") {
  features = [ ":hello_bin" ]
}

BUILD.gn标准格式化

OpenHarmony对BUILD.gn的格式有要求,并提供了gn格式化的命令。在工程的根目录下执行。另外要注意,BUILD.gn最后要留空行。

cat 自己路径/BUILD.gn | prebuilts/build-tools/linux-x86/bin/gn format --stdin > FORMAT_RESULT.gn;cp -f FORMAT_RESULT.gn 自己路径/BUILD.gn;rm FORMAT_RESULT.gn

创建bundle.json

bundle.json内容如下:

{
  "name": "@ohos/hello_test",
  "version": "4.0",
  "description": "",
  "license": "Apache License 2.0",
  "domain": "os",
  "publishAs": "",
  "private": false,
  "scripts": {},
  "dirs": [],
  "segment": {
    "destPath": "项目自定义存放路径/myhello"
  },
  "component": {
    "name": "hello_test",
    "subsystem": "myhello",
    "syscap": [],
    "features": [],
    "adapted_system_type": [
      "liteos_m",
      "liteos_a",
      "linux"
    ],
    "rom": "3072KB",
    "ram": "~64KB",
    "deps": {
      "components": [
        "samgr_lite",
        "ipc_lite",
        "kal_timer",
        "hilog"
      ],
      "third_party": [
        "bounds_checking_function",
        "cjson",
        "mbedtls"
      ]
    },
    "build": {
      "sub_component": [
        "//项目自定义存放路径/myhello:my_hello"
      ],
      "inner_kits": [],
      "test": []
    }
  }
}

修改build/subsystem_config.json

在build/subsystem_config.json中添加子系统或组件。

  "myhello": {
    "path": "项目自定义存放路径/myhello",
    "name": "myhello"
  }

修改产品配置文件config.json

各产品对应的配置文件路径如下:
wifiiot_hispark_pegasus:vendor/hisilicon/hispark_pegasus/config.json
ipcamera_hispark_taurus:vendor/hisilicon/ipcamera_hispark_taurus/config.json
ipcamera_hispark_taurus_linux:vendor/hisilicon/ipcamera_hispark_taurus_linux/config.json

在"subsystems":[]里面添加以下代码,注意逗号

{
    "subsystem": "myhello",
    "components": [
        { "component": "hello_test", "features":[] }
    ]
}

以上就是L0L1新增子系统/组件的方法。L2的类似,后续更新。


build/lite/components编译方式(已弃用)

创建了自定义子系统的目录后,在build/lite/components路径下,新建对应子系统名称的json文件。如果子系统已存在,则在对应子系统名称的json文件下补充新增组件的信息。不使用build/subsystem_config.json

创建myhello.json

在路径:build/lite/components,新建json文件
myhello.json内容如下:

{
	"components": [
	{
		"component": "my_hello_app",
		"description": "",
		"optional": "true",
		"dirs": [ "//base/myhello" ],
		"targets": [ "//base/myhello:my_hello" ],
		"rom": "2.6M",
		"ram": "~1.8M",
		"adapted_board": [
			"liteos_m",
			"liteos_a",
			"linux",
		],
		"adapted_kernel": [
			"liteos_m",
			"liteos_a",
			"linux",
		],
		"features": [],
		"deps": {
			"components": [
				"utils_base",
				"samgr_lite",
				"peripheral_input",
				"ipc_lite",
				"aafwk_lite",
				"appexecfwk_lite",
				"hilog_lite",
				"wms",
				"kal_timer",
			],
			"third_party": [
				"bounds_checking_function",
				"cjson"
			]
		}
	}]
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值