当前位置: 首页 > news >正文

Openharmony的编译构建--进阶篇1

上一篇中说明了Openharmony V3.1的编译构建流程,如何在标准系统即L2设备添加一个模块呢,在Openharmony上如何编译与运行HelloWorld此篇中有所提及,此篇对此进行详细的说明。

一、标准系统添加一个模块

在Openharmony中添加模块可以分以下三种情况,对原有的配置文件时行不同程度的修改

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

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

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

以下分三种方式对添加一个模块的过程来分别分析。

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

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

支持的模板类型:

ohos_executable		#可执行程序
ohos_shared_library #动态库
ohos_static_library #静态库

# 预编译模板
ohos_prebuilt_executable 		#预编译可执行程序
ohos_prebuilt_shared_library 	#预编译动态库
ohos_prebuilt_etc				#预编译配置

以下例子中对BUILD.gn文件中各模板的属性及配置项的注释说明

例子:

ohos_shared_library示例

import("//build/ohos.gni")
ohos_shared_library("helloworld") {
  source = ["",""]		# 包含的C或C++文件
  include_dir = ["",""] # 包含的头文件
  cflags = []			# 编译选项
  cflags_c = []
  cflags_CC = []
  ldflags = []
  configs = []
  deps = []				# 部件内模块依赖
  
  external_deps = [
    "part_name:module_name",
  ]
  # 跨部件模块依赖定义
  # 定义格式为 "部件名:模块名称"
  # 这里依赖的模块必须是部件声明在inner_kits中的模块
  
  defines += ["",""]	# 宏定义
  
  output_name = "" 		# 可选,模块输出名
  output_extension = "" # 可选,模块名后缀
  module_install_dir = ""	# 可选,缺省在/system/lib64或/system/lib下,模块安装路径,模块安装路径,从system/,vendor/后开始指定
  relative_install_dir = ""	# 可选,模块安装相对路径,相对于/system/lib64或/system/lib;如果有module_install_dir配置时,该配置项不生效
  
  part_name = ""		# 必选,所属部件名称
  subsystem_name = ""	# 可选,子系统名称
}

ohos_executable示例

ohos_executable模板属性和ohos_shared_library基本一致。

ohos_executable("helloworld") {
  source = ["",""]		# 包含的C或C++文件
  include_dir = ["",""] # 包含的头文件
...
  install_enable = true	# 编译后的镜像烧录后,可执行模块默认是不安装在开发板内,如果要安装需要指定install_enable为true.
}

ohos_prebuilt_etc示例

ohos_prebuilt_etc就是将预编译的文件安装在开发板内,一般会是配置文件,例如xml,cfg等。

ohos_prebuilt_etc("helloworld") {
  source = ""		
  deps = [] 	# 部件内模块依赖
  module_install_dir = ""	# 可选,模块安装路径,从system/,vendor/后开始指定
  relative_install_dir = ""	# 可选,模块安装相对路径,相对于/system/etc;如果有module_install_dir配置时,该配置项不生效
  part_name = ""		# 必选,所属部件名称
}

2.修改包含该模块所属部件的bundle.json

以base/account/os_account/bundle.json为例:

此模块的BUILD.gn文件所在目录为//base/account/os_account/,

则在“sub_component”中添加"//base/account/os_account:helloworld"。

{
  "name": "@ohos/os_account",
  "description": "Allowing apps to use OS account-related functions",
  "version": "3.1",
  "license": "Apache License 2.0",
  "publishAs": "code-segment",
  "segment": {
    "destPath": "base/account/os_account"
  },
...
  "component": {
    "name": "os_account_standard",
    "subsystem": "account",
...
    "build": {
      "sub_component": [
        "//base/account/os_account/services:services_target",
        "//base/account/os_account/services/accountmgr/src/appaccount:app_account_service_core",
        "//base/account/os_account/frameworks/appaccount/native:app_account_innerkits",
        "//base/account/os_account/frameworks/osaccount/core:os_account_core",
        "//base/account/os_account/frameworks/common:common_target",
        "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits",
        "//base/account/os_account/interfaces/kits/napi/appaccount:appaccount",
        "//base/account/os_account/interfaces/kits/napi/distributedaccount:distributedaccount",
        "//base/account/os_account/interfaces/kits/napi/osaccount:osaccount",
        "//base/account/os_account/sa_profile:account_sa_profile",
        "//base/account/os_account/tools:os_account_tools",
        "//base/account/os_account:helloworld"
      ],
...
   }
  }
}

此时再在Openharmony里执行

./build.sh --product-name rk3568

编译完成后,在//out/rk3568/account/os_account_standard/目录下可以找到编译完成的可执行文件与动态库。

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

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

此步与在原部件中添加一个模块的方法基本上一致,只需要注意该模块对应BUILD.gn文件中的part_name为新建部件的名称即可。

2.修改或者新建bundle.json

在原有子系统中添加一个新的部件,有两种方法,一种就是如第二点所述,在原有的bundle.json文件中添加该部件。另一种就是新建一个。注意无论哪种方式该bundle.json文件均在对应子系统所在的文件目录下。除非你修改了//build/subsystem_config.json中关于此子系统对应的路径信息。参考Openharmony的编译构建--基础篇。

以account子系统为例,添加新的子部件os_account_test.

  • 首先base/account下创建目录os_account_test,此目录下新建bundle.json。内容如下

{
  "name": "@ohos/os_account",
  "description": "Allowing apps to use OS account-related functions",
  "version": "3.1",
  "license": "Apache License 2.0",
  "publishAs": "code-segment",
  "segment": {
    "destPath": "base/account/os_account"
  },
...
  "component": {
    "name": "os_account_test",		# 新建的部件名称
    "subsystem": "account",			# 所属的子系统
...
    },
    "build": {
      "sub_component": [			# 部件包含的模块的gn目标
...                                  
      ],
      "inner_kits": [
...
     ],
     "test": [
...
     ]
   }
  }
}
  • 添加BUILD.gn

如果在base/account/os_account_test新建BUILD.gn,则sub_component添加如下

"//base/account/os_account_test:helloworld"
  • inner_kits与test

假如有提供给其它部件的接口,需要在inner_kits说明,假如有测试用例,需要在test中说明,inner_kits与test不是必选项,可以不添加。

3.//productdefine/common/products/rk3568.json文件添加部件"account:os_account_test":{}

{
  "product_name": "rk3568",
  "product_company": "hihope",
  "product_device": "rk3568",
  "version": "2.0",
  "type": "standard",
  "product_build_path": "device/hihope/build",
  "parts":{
    "ace:ace_engine_standard":{},
    "ace:napi":{},
    "account:os_account_standard":{},
    "account:os_account_test":{},
    "barrierfree:accessibility":{},
......
    }
}

至此,重新编译。在//out/rk3568/account/os_account_test/目录下会得到新增的模块下可执行文件或动态库。

后续更精彩

1.Openharmony编译构建--进阶篇2

相关文章:

  • 每天一道大厂SQL题【Day02】电商场景TopK统计
  • EMT4J详细介绍与使用,帮你找到Java版本升级带来的问题,让你在项目jdk升级不在头疼
  • 第2章:使用CSS定义样式
  • 【数据结构】动图详解单向链表
  • MySQL基础篇笔记
  • Vue3现状—必然趋势?
  • uniapp获取支付宝user_id - 支付宝提现 - 登录授权 - APP支付宝登陆 - H5支付宝授权
  • Promise详解与手写实现
  • 【C++】类型转换
  • 关于栈和队列
  • 网络知识详解之:网络攻击与安全防护
  • Java快速上手Properties集合类
  • leetcode:43. 字符串相乘(附加一些C++string其他小练习)
  • 游戏SDK(三)架构设计之代码实现1
  • 射频识别技术|期末考试知识点|重点题目|第1讲_RFID
  • C++中拷贝构造函数、拷贝赋值运算符、析构函数、移动构造函数、移动赋值运算符(三/五法则)
  • MVC和MVVM的区别
  • Python(17):Numpy之array数组的排序
  • Pipenv使用指南:轻量级虚拟环境管理工具详解
  • 【Python】request.session的cookie如何导入aiohttp._cookie_jar
  • https://app.hackthebox.com/machines/Inject
  • Spring —— Spring简单的读取和存储对象 Ⅱ
  • 渗透测试之冰蝎实战
  • Mybatis、TKMybatis对比
  • Microsoft Office 2019(2022年10月批量许可版)图文教程
  • 《谷粒商城基础篇》分布式基础环境搭建
  • 哈希表题目:砖墙
  • Vue 3.0 选项 生命周期钩子
  • 【车载嵌入式开发】AutoSar架构入门介绍篇
  • 【计算机视觉 | 目标检测】DETR风格的目标检测框架解读