想了解更多关于开源的统浅谈实内容,请访问:
51CTO 开源基础软件社区
https://ost.51cto.com
经过一段时间的译构南向学习,基于Hi3861智能家居开发套件的建系践内核编程,驱动开发已经基本解决了。统浅谈实这篇来聊聊OpenHarmony的译构编译构建,经过前面的建系践实践,再来看编译构建。统浅谈实会对之前的译构编译流程做一些解释,实践一个基于Hispark_pegasus的建系践自己的解决方案。
在官网中提到了,统浅谈实OpenHarmony编译子系统是以GN和Ninja构建为基座,对构建和配置粒度进行部件化抽象、对内建模块进行功能增强、对业务模块进行功能扩展的系统,该系统提供以下基本功能:
回想我们在OpenHarmony搭建编译环境的时候,进行了编译操作是怎么进行的了吗?首先是hb set 选择了wifiiot_hispark_pegasus,然后进行了全量编译操作hb build -f 。
选择产品或者说选择一个编译的目录,我们可以自己创建自己的产品,哪怕他只有一个hello,world的功能。而其他的产品或者说代码都不会参与编译,这也解释了什么是最小的产品独立编译。编译什么是我们手动选择的,功能可大可小。
编译指定的产品(代码),根据指定的产品开发板,读取开发板config.gni文件的内容,主要是一些编译工具链和编译的配置选项。
我们也可以用-T修饰命令,让他只编译某一个源文件。
hb build -T 路径:目标
这个文件应该说很熟悉了,每一个案例都要去写这个gn文件,gn是Generate ninja的缩写,用于产生ninja文件。在我们之前简单案例的开发中,如“hello,world”,gn文件就是一个编译脚本。
我们对nijia的印象不是很深,因为他是自动执行的,我们作为开发者没有去人工干涉他。
总结来说,hb就是OpenHarmony的命令行工具,用来执行编译命令。gn生成nijia文件,nijia是一个专注于速度的小型编译构建系统。他们三者在整个编译中的流程如下图所示:
整个编译构建的流程图如下:
OpenHarmony整体遵从分层设计,系统功能按照“系统 > 子系统 > 组件”逐级展开,在多设备部署场景下,支持根据实际需求裁剪某些非必要的子系统或部件,非常的灵活,高内聚低耦合。
遵循:{ 领域(子系统集)}/{ 子系统}/{ 组件}的一个规则,从下面的源码中可以看出:
组件定义在build/lite/components/下:
定义就是一个JSON文件,由一个总的components数组包含每一个component对象,对象中包含了组件的所有属性。
至此,我们知道怎么去定义组件,定义在哪里,也就能新建组件了。但是新出现的组件,怎么能后加入到编译中呢,targets参数其实已经说明清楚了,下面通过Wifi组件的案例做具体解释。
我们可以根据targets参数追踪到目录中/foundation/communication/wifi/BUILD.gn文件中的wifi。
$WIFI_ROOT_DIR表示/foundation/communication/wifi,之后继续跟踪,这些dependences,完成相应BUILD.gn脚本的执行,也就让组件被编译系统所识别,完成组件的编译了。
芯片解决方案的路径如下图所示:
芯片解决方案组件会随产品选择的开发板默认编译。
产品解决方案的路径如下图所示:
产品解决方案,在config.json文件中进行配置:
最后,也就能看到我们的hb set从顶层,选择vendor下的产品解决方案,通过方案中的各个子系统集,子系统,组件,进行编译。
首先,在application/sample下创建一个myComponent等如下目录。
完成组件功能的编写
#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" ]}
定义组件:
在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
说明我们的组件编写没什么问题。
创建如下目录,并编写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" }
将hispark_pegasus下的hal/utils复制到我们自己的产品解决方案中。
创建BUILD.gn文件编写编译脚本。
group("product"){ }
执行hb set命令,观察产品解决方案。
完成编译。
选择我们的产品解决方案product。
串口调试,观察控制台输出。
希望能够帮助到大家,对OpenHarmony的编译过程有一个全面的感知。
想了解更多关于开源的内容,请访问:
51CTO 开源基础软件社区
https://ost.51cto.com
责任编辑:jianghua 来源: 51CTO 开源基础软件社区 鸿蒙智能家居开发(责任编辑:休闲)
ST步森(002569.SZ)公布消息:终止收购微动天下100%的股权
ipo审核是什么意思?股票市场上常常被提到的IPO的意思是什么?
OPPO 举行 O 粉节活动:可免费领取手机壳,涉及六种类型优惠
合丰集团(02320.HK)发布公告:年度公司拥有人应占亏损1.72亿港元