这篇来聊聊OpenHarmony的编译构建,经过前面的实践,再来看编译构建。
编译构建概述
在官网中提到了,OpenHarmony编译子系统是以GN和Ninja构建为基座,对构建和配置粒度进行部件化抽象、对内建模块进行功能增强、对业务模块进行功能扩展的系统,该系统提供以下基本功能:
- 以部件为最小粒度拼装产品和独立编译。
- 支持轻量、小型、标准三种系统的解决方案级版本构建,以及用于支撑应用开发者使用IDE开发的SDK开发套件的构建。
- 支持芯片解决方案厂商的灵活定制和独立编译。
hb、GN、Ninja
回想我们在OpenHarmony搭建编译环境的时候,进行了编译操作是怎么进行的了吗?首先是hb set 选择了wifiiot_hispark_pegasus,然后进行了全量编译操作hb build -f 。
▍ hb set
选择产品或者说选择一个编译的目录,我们可以自己创建自己的产品,哪怕他只有一个hello,world的功能。而其他的产品或者说代码都不会参与编译,这也解释了什么是最小的产品独立编译。编译什么是我们手动选择的,功能可大可小。


▍hb build
编译指定的产品(代码),根据指定的产品开发板,读取开发板config.gni文件的内容,主要是一些编译工具链和编译的配置选项。
我们也可以用-T修饰命令,让他只编译某一个源文件
hb build -T 路径:目标
▍BUILD.gn

这个文件应该说很熟悉了,每一个案例都要去写这个gn文件,gn是Generate ninja的缩写,用于产生ninja文件。在我们之前简单案例的开发中,如"hello,world",gn文件就是一个编译脚本。

我们对nijia的印象不是很深,因为他是自动执行的,我们作为开发者没有去人工干涉他。
▍编译小总结
总结来说,hb就是OpenHarmony的命令行工具,用来执行编译命令。gn生成nijia文件,nijia是一个专注于速度的小型编译构建系统。他们三者在整个编译中的流程如下图所示:

整个编译构建的流程图如下:

OpenHarmony系统

OpenHarmony整体遵从分层设计,系统功能按照"系统 > 子系统 > 组件"逐级展开,在多设备部署场景下,支持根据实际需求裁剪某些非必要的子系统或部件,非常的灵活,高内聚低耦合。
配置规则
▍组件配置规则
遵循:{领域(子系统集)}/{子系统}/{组件}的一个规则,从下面的源码中可以看出:

###▍组件定义
组件定义在build/lite/components/下:

定义就是一个JSON文件,由一个总的components数组包含每一个component对象,对象中包含了组件的所有属性。

至此,我们知道怎么去定义组件,定义在哪里,也就能新建组件了。但是新出现的组件,怎么能后加入到编译中呢,targets参数其实已经说明清楚了,下面通过Wifi组件的案例做具体解释。
▍WiFi组件

我们可以根据targets参数追踪到目录中/foundation/communication/wifi/BUILD.gn文件中的wifi

$WIFI_ROOT_DIR表示/foundation/communication/wifi,之后继续跟踪,这些dependences,完成相应BUILD.gn脚本的执行,也就让组件被编译系统所识别,完成组件的编译了。

▍组件总结

▍芯片解决方案配置规则
芯片解决方案的路径如下图所示:

芯片解决方案组件会随产品选择的开发板默认编译。
▍产品解决方案配置规则
产品解决方案的路径如下图所示:

产品解决方案,在config.json文件中进行配置:

-
"product_name": 产品名称,指定为"wifiiot_hispark_pegasus"。
-
"type": 产品类型,被标记为"mini"。
-
"version": 产品版本号,标记为"3.0"。
-
"ohos_version": 操作系统版本,使用的是OpenHarmony 1.0。
-
"device_company": 设备制造公司,此产品由"hisilicon"制造。
-
"device_build_path": 设备构建路径,指定为"device/board/hisilicon/hispark_pegasus"。
-
"board": 开发板名称,被标记为"hispark_pegasus"。
-
"kernel_type": 内核类型,使用的是"liteos_m"。
-
"kernel_is_prebuilt": 内核是否预构建,被标记为true。
-
"kernel_version": 内核版本号,此处为空。
-
"subsystems": 子系统列表,包含了产品的不同子系统及其组件信息。
-
"component": 组件名称,表示不同的功能组件。
-
"features": 特性列表,描述了组件的不同特性。
-
"subsystem": 子系统名称,表示不同的功能区域。
-
"components": 组件列表,表示在该子系统中使用的组件及其特性。
-
"third_party_dir": 第三方库路径,指定为"//device/soc/hisilicon/hi3861v100/sdk_liteos/third_party"。
-
"product_adapter_dir": 产品适配层路径,指定为"//vendor/hisilicon/hispark_pegasus/hals"。
最后,也就能看到我们的hb set从顶层,选择vendor下的产品解决方案,通过方案中的各个子系统集,子系统,组件,进行编译。

新增自己的产品解决方案
▍组件定义

1.首先,在application/sample下创建一个myComponent等如下目录

2.完成组件功能的编写component.c
#include <stdio.h>
#include "ohos_init.h"
void entry(void){
printf("test component!"); // 哪怕这个解决方案是个hello,world呢
}
SYS_RUN(entry);
static_library(test){
sources = [
"component.c"
]
include_dirs = [
"//commonlibrary/utils_lite/include"
]
}
3.定义组件在build/lite/components/创建application1.json编写如下代码:
{
"components": [
{
"component": "myComponent",
"description": "a test component",
"optional": "true",
"dirs": [
"applications/sample/myComponent"
],
"targets": [
"//applications/sample/myComponent:test"
],
"adapted_kernel": [ "liteos_m" ]
}
]
}
我们可以使用 -T 修饰我们的编译命令,实现指定文件编译
hb build -f -T //applications/sample/myComponent:test

说明我们的组件编写没什么问题
▍ 解决方案定义

1.创建如下目录,并编写config.json配置文件

config.json
{
"product_name": "product",
"type": "mini",
"version": "3.0",
"ohos_version": "OpenHarmony 3.2",
"device_company": "hisilicon",
"device_build_path": "device/board/hisilicon/hispark_pegasus",
"board": "hispark_pegasus",
"kernel_type": "liteos_m",
"kernel_is_prebuilt": true,
"kernel_version": "",
"subsystems": [
{
"subsystem": "applications1", // 用我们自己定义的子系统的组件
"components": [
{ "component": "myComponent", "features":[] }
]
},
{
"subsystem": "iothardware",
"components": [
{ "component": "peripheral", "features":[] }
]
},
{
"subsystem": "hiviewdfx",
"components": [
{ "component": "hilog_lite", "features":[] },
{ "component": "hievent_lite", "features":[] },
{ "component": "blackbox", "features":[] },
{ "component": "hidumper_mini", "features":[] }
]
},
{
"subsystem": "systemabilitymgr",
"components": [
{ "component": "samgr_lite", "features":[] }
]
},
{
"subsystem": "security",
"components": [
{ "component": "device_auth", "features":[] },
{ "component": "huks", "features":
[
"disable_huks_binary = false",
"disable_authenticate = false",
"huks_use_lite_storage = true",
"huks_use_hardware_root_key = true",
"huks_config_file = \"hks_config_lite.h\"",
"ohos_security_huks_mbedtls_porting_path = \"//device/soc/hisilicon/hi3861v100/sdk_liteos/third_party/mbedtls\""
]
}
]
},
{
"subsystem": "startup",
"components": [
{ "component": "bootstrap_lite", "features":[] },
{ "component": "syspara_lite", "features":[] },
{ "component": "init_lite", "features":
[
"enable_ohos_startup_init_feature_begetctl_liteos = true",
"enable_ohos_startup_init_lite_use_thirdparty_mbedtls = true"
]
}
]
},
{
"subsystem": "communication",
"components": [
{ "component": "wifi_lite", "features":[] },
{ "component": "dsoftbus", "features":[] },
{ "component": "wifi_aware", "features":[]}
]
},
{
"subsystem": "updater",
"components": [
{ "component": "ota_lite", "features":[] }
]
},
{
"subsystem": "commonlibrary",
"components": [
{ "component": "file", "features":[] }
]
},
{
"subsystem": "xts",
"components": [
{ "component": "xts_acts", "features":
[
"enable_ohos_test_xts_acts_use_thirdparty_lwip = false"
]
},
{ "component": "xts_tools", "features":[] },
{ "component": "device_attest_lite", "features":[] }
]
}
],
"third_party_dir": "//device/soc/hisilicon/hi3861v100/sdk_liteos/third_party",
"product_adapter_dir": "//vendor/hisilicon/hispark_pegasus/hals"
}
2.将hispark_pegasus下的hal/utils复制到我们自己的产品解决方案中


3.创建BUILD.gn文件编写编译脚本
group("product"){
}
▍编译检验
执行hb set命令,观察产品解决方案

完成编译

▍烧录测试
选择我们的产品解决方案product

串口调试,观察控制台输出

▍产品解决方案总结

结束语
希望能够帮助到大家,对OpenHarmony的编译过程有一个全面的感知。
为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05
《鸿蒙开发学习手册》:
如何快速入门:https://qr21.cn/FV7h05
- 基本概念
- 构建第一个ArkTS应用
- ......

开发基础知识:https://qr21.cn/FV7h05
- 应用基础知识
- 配置文件
- 应用数据管理
- 应用安全管理
- 应用隐私保护
- 三方应用调用管控机制
- 资源分类与访问
- 学习ArkTS语言
- ......

基于ArkTS 开发:https://qr21.cn/FV7h05
- Ability开发
- UI开发
- 公共事件与通知
- 窗口管理
- 媒体
- 安全
- 网络与链接
- 电话服务
- 数据管理
- 后台任务(Background Task)管理
- 设备管理
- 设备使用信息统计
- DFX
- 国际化开发
- 折叠屏系列
- ......

鸿蒙开发面试真题(含参考答案):https://qr18.cn/F781PH

鸿蒙开发面试大盘集篇(共计319页):https://qr18.cn/F781PH
1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向
