OpenHarmony添加自定义的子系统、部件、模块
  EzIAYXiV4scN 2023年11月02日 41 0

这里我们将OpenHarmony的两个例子添加到源码中并使其编译到系统中,这里有一点需要注意的是不可以将自定义的子系统放到其他已经定义的子系统目录下,vendor下的product文件夹也被认为成一个子系统,比如

/vendor/hihope/rk3568/就作为product_hihope子系统存在的,其定义在/vendor/hihope/rk3568/ohos.build中如下:

{
  "parts": {
    "product_rk3568": {
      "module_list": [
        "//vendor/hihope/rk3568/default_app_config:default_app_config",
        "//vendor/hihope/rk3568/image_conf:custom_image_conf",
        "//vendor/hihope/rk3568/preinstall-config:preinstall-config",
        "//vendor/hihope/rk3568/resourceschedule:resourceschedule",
        "//vendor/hihope/rk3568/etc:product_etc_conf"
      ]
    }
  },
  "subsystem": "product_hihope"
}

1.添加子系统

我们将所有改动放到vendor/guide文件夹下方便后期移植

1)创建一个guide_common_subsystems用来存放所有自定义的子系统

2)创建子系统sample,创建一个vendor/guide/guide_common_subsystems/sample文件夹

3)在build/subsystem_config.json中加入子系统

"sample": {
    "path": "vendor/guide/guide_common_subsystems/sample",
    "name": "sample"
  }

2.添加一个单模块的部件

这里我们参考https://docs.openharmony.cn/pages/v3.2Beta/zh-cn/device-dev/quick-start/quickstart-ide-3568-helloworld.md/添加一个hello部件,并添加一个helloworld的模块

1)创建目录,编写业务代码

在上述创建的sample文件夹下新建hello/src/helloworld.c目录及文件内容如下:

//vendor/guide/guide_common_subsystems/sample/hello/src/helloworld.c
#include <stdio.h>
#include "helloworld.h"

int main(int argc, char **argv)
{
    HelloPrint();
    return 0;
}

void HelloPrint()
{
    printf("\n\n");
    printf("\n\t\tHello World!\n");
    printf("\n\n");
}

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

//vendor/guide/guide_common_subsystems/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)新建编译组织文件

创建 BUILD.gn内容如下所示:

//vendor/guide/guide_common_subsystems/sample/hello/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  # 是否默认安装(缺省默认不安装),可选
}

3)新建部件配置规则文件

新建sample/hello/bundle.json文件,添加sample部件描述,bundle.json内容如下所示。

//vendor/guide/guide_common_subsystems/sample/hello/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": []
        }
    }
}

4)修改产品配置文件

在//vendor/guide/zp37a/config.json文件中添加如下配置:

"subsystems":[
    ....
	{
      "subsystem": "sample",
      "components": [
        {
          "component": "hello",
          "features": []
        }
      ]
    }
]

编译后即可在开机后的设备目录 system/bin/中找到helloworld的可执行文件。

3.添加多模块的部件

参考https://docs.openharmony.cn/pages/v3.2Beta/zh-cn/device-dev/subsystems/subsys-build-component.md/ 添加部件partA,包含三个模块

1)添加部件。示例部件partA的完整目录结构如下:

vendor/guide/guide_common_subsystems/sample/partA
├── BUILD.gn
├── bundle.json
├── feature1
│   ├── BUILD.gn
│   ├── include
│   │   └── helloworld1.h
│   └── src
│       └── helloworld1.cpp
├── feature2
│   ├── BUILD.gn
│   ├── include
│   │   └── helloworld2.h
│   └── src
│       └── helloworld2.cpp
└── feature3
    ├── BUILD.gn
    └── src
        └── config.conf

2)编写动态库gn脚本vendor/guide/guide_common_subsystems/sample/partA/feature1/BUILD.gn如下:

//vendor/guide/guide_common_subsystems/sample/partA/feature1/BUILD.gn
import("//build/ohos.gni")

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"
}

对应的代码文件如下:

//vendor/guide/guide_common_subsystems/sample/partA/feature1/include/helloworld1.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

//vendor/guide/guide_common_subsystems/sample/partA/feature1/src/helloworld1.cpp
#include <stdio.h>
#include "helloworld1.h"

void HelloPrint()
{
    printf("\n\n");
    printf("\n\t\tHello World!\n");
    printf("\n\n");
}

3)编写可执行文件gn脚本vendor/guide/guide_common_subsystems/sample/partA/feature2/BUILD.gn,示例如下:

//vendor/guide/guide_common_subsystems/sample/partA/feature2/BUILD.gn
import("//build/ohos.gni")

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"
}

对应的代码文件如下:

//vendor/guide/guide_common_subsystems/sample/partA/feature2/include/helloworld2.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

//vendor/guide/guide_common_subsystems/sample/partA/feature2/src/helloworld2.cpp
#include <stdio.h>
#include "helloworld2.h"

int main(int argc, char **argv)
{
    HelloPrint();
    return 0;
}

void HelloPrint()
{
    printf("\n\n");
    printf("\n\t\tHello World!\n");
    printf("\n\n");
}

4)编写etc模块gn脚本vendor/guide/guide_common_subsystems/sample/partA/feature3/BUILD.gn,示例如下:

//vendor/guide/guide_common_subsystems/sample/partA/feature3/BUILD.gn
import("//build/ohos.gni")

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

对应的文件如下:

//vendor/guide/guide_common_subsystems/sample/partA/feature3/src/config.conf
var_a=xxx
var_b=xxx

5)在部件的BUILD.gn中添加模块配置,文件内容如下:

//vendor/guide/guide_common_subsystems/sample/partA/BUILD.gn
import("//build/ohos.gni")

group("partA_target"){
    deps = [
        "feature1:helloworld_lib", #这里声明对应的文件夹和模块名
        "feature2:helloworld_bin",
        "feature3:feature3_etc"
    ]
}

6)在部件的bundle.json中添加模块配置

//vendor/guide/guide_common_subsystems/sample/partA/bundle.json
{
    "name": "@guideIr/partA",
    "description": "partA compent example.",
    "version": "3.1",
    "license": "Apache License 2.0",
    "publishAs": "code-segment",
    "segment": {
        "destPath": "//vendor/guide/guide_common_subsystems/sample/partA"
    },
    "dirs": {},
    "scripts": {},
    "component": {
        "name": "partA",
        "subsystem": "sample",
        "syscap": [],
        "features": [],
        "adapted_system_type": [ "mini", "small", "standard" ],
        "rom": "10KB",
        "ram": "10KB",
        "deps": {
            "components": [],
            "third_party": []
        },
        "build": {
            "sub_component": [
                "//vendor/guide/guide_common_subsystems/sample/partA:partA_target" #上面的build.gn中声明的group
            ],
            "inner_kits": [],
            "test": []
        }
    }
}

7)将部件添加到产品配置中.在产品的配置中添加部件,产品对应的配置文件://vendor/{product_company}/{product-name}/config.json。

在产品配置文件中添加 “subsystem_examples:partA”,表示该产品中会编译并打包partA到版本中。改动如下:

"subsystems":[
    ....
	{
      "subsystem": "sample",
      "components": [
        {
          "component": "hello",
          "features": []
        },
        //新增内容
        {
          "component": "partA",
          "features": []
        }
      ]
    }
]

编译后可以在设备的system/bin/下看到helloworld_bin 可执行文件,在system/lib64中看到libhelloworld_lib.z.so文件,在system/etc/init/目录下看到config.conf文件,使用cat也能看到如下内容:

# cat config.conf
var_a=xxx
var_b=xxx
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  KObryig2cZt5   2023年12月23日   104   0   0 UserUserjsonJSONjavajava
  6x82OSIkB82a   2023年12月23日   53   0   0 jsonJSONjavajava
EzIAYXiV4scN
最新推荐 更多