OpenHarmony編譯構(gòu)建系統(tǒng)—淺談與實(shí)踐
想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):
前言
經(jīng)過(guò)一段時(shí)間的南向?qū)W習(xí),基于Hi3861智能家居開(kāi)發(fā)套件的內(nèi)核編程,驅(qū)動(dòng)開(kāi)發(fā)已經(jīng)基本解決了。這篇來(lái)聊聊OpenHarmony的編譯構(gòu)建,經(jīng)過(guò)前面的實(shí)踐,再來(lái)看編譯構(gòu)建。會(huì)對(duì)之前的編譯流程做一些解釋,實(shí)踐一個(gè)基于Hispark_pegasus的自己的解決方案。
編譯構(gòu)建概述
在官網(wǎng)中提到了,OpenHarmony編譯子系統(tǒng)是以GN和Ninja構(gòu)建為基座,對(duì)構(gòu)建和配置粒度進(jìn)行部件化抽象、對(duì)內(nèi)建模塊進(jìn)行功能增強(qiáng)、對(duì)業(yè)務(wù)模塊進(jìn)行功能擴(kuò)展的系統(tǒng),該系統(tǒng)提供以下基本功能:
- 以部件為最小粒度拼裝產(chǎn)品和獨(dú)立編譯。
- 支持輕量、小型、標(biāo)準(zhǔn)三種系統(tǒng)的解決方案級(jí)版本構(gòu)建,以及用于支撐應(yīng)用開(kāi)發(fā)者使用IDE開(kāi)發(fā)的SDK開(kāi)發(fā)套件的構(gòu)建。
- 支持芯片解決方案廠商的靈活定制和獨(dú)立編譯。
hb、GN、Ninja
回想我們?cè)贠penHarmony搭建編譯環(huán)境的時(shí)候,進(jìn)行了編譯操作是怎么進(jìn)行的了嗎?首先是hb set 選擇了wifiiot_hispark_pegasus,然后進(jìn)行了全量編譯操作hb build -f 。
hb set
選擇產(chǎn)品或者說(shuō)選擇一個(gè)編譯的目錄,我們可以自己創(chuàng)建自己的產(chǎn)品,哪怕他只有一個(gè)hello,world的功能。而其他的產(chǎn)品或者說(shuō)代碼都不會(huì)參與編譯,這也解釋了什么是最小的產(chǎn)品獨(dú)立編譯。編譯什么是我們手動(dòng)選擇的,功能可大可小。
hb build
編譯指定的產(chǎn)品(代碼),根據(jù)指定的產(chǎn)品開(kāi)發(fā)板,讀取開(kāi)發(fā)板config.gni文件的內(nèi)容,主要是一些編譯工具鏈和編譯的配置選項(xiàng)。
我們也可以用-T修飾命令,讓他只編譯某一個(gè)源文件。
hb build -T 路徑:目標(biāo)
BUILD.gn
這個(gè)文件應(yīng)該說(shuō)很熟悉了,每一個(gè)案例都要去寫這個(gè)gn文件,gn是Generate ninja的縮寫,用于產(chǎn)生ninja文件。在我們之前簡(jiǎn)單案例的開(kāi)發(fā)中,如“hello,world”,gn文件就是一個(gè)編譯腳本。
我們對(duì)nijia的印象不是很深,因?yàn)樗亲詣?dòng)執(zhí)行的,我們作為開(kāi)發(fā)者沒(méi)有去人工干涉他。
編譯小總結(jié)
總結(jié)來(lái)說(shuō),hb就是OpenHarmony的命令行工具,用來(lái)執(zhí)行編譯命令。gn生成nijia文件,nijia是一個(gè)專注于速度的小型編譯構(gòu)建系統(tǒng)。他們?nèi)咴谡麄€(gè)編譯中的流程如下圖所示:
整個(gè)編譯構(gòu)建的流程圖如下:
OpenHarmony系統(tǒng)
OpenHarmony整體遵從分層設(shè)計(jì),系統(tǒng)功能按照“系統(tǒng) > 子系統(tǒng) > 組件”逐級(jí)展開(kāi),在多設(shè)備部署場(chǎng)景下,支持根據(jù)實(shí)際需求裁剪某些非必要的子系統(tǒng)或部件,非常的靈活,高內(nèi)聚低耦合。
配置規(guī)則
組件配置規(guī)則
遵循:{領(lǐng)域(子系統(tǒng)集)}/{子系統(tǒng)}/{組件}的一個(gè)規(guī)則,從下面的源碼中可以看出:
組件定義
組件定義在build/lite/components/下:
定義就是一個(gè)JSON文件,由一個(gè)總的components數(shù)組包含每一個(gè)component對(duì)象,對(duì)象中包含了組件的所有屬性。
至此,我們知道怎么去定義組件,定義在哪里,也就能新建組件了。但是新出現(xiàn)的組件,怎么能后加入到編譯中呢,targets參數(shù)其實(shí)已經(jīng)說(shuō)明清楚了,下面通過(guò)Wifi組件的案例做具體解釋。
WiFi組件
我們可以根據(jù)targets參數(shù)追蹤到目錄中/foundation/communication/wifi/BUILD.gn文件中的wifi。
$WIFI_ROOT_DIR表示/foundation/communication/wifi,之后繼續(xù)跟蹤,這些dependences,完成相應(yīng)BUILD.gn腳本的執(zhí)行,也就讓組件被編譯系統(tǒng)所識(shí)別,完成組件的編譯了。
組件總結(jié)
芯片解決方案配置規(guī)則
芯片解決方案的路徑如下圖所示:
芯片解決方案組件會(huì)隨產(chǎn)品選擇的開(kāi)發(fā)板默認(rèn)編譯。
產(chǎn)品解決方案配置規(guī)則
產(chǎn)品解決方案的路徑如下圖所示:
產(chǎn)品解決方案,在config.json文件中進(jìn)行配置:
- “product_name”: 產(chǎn)品名稱,指定為"wifiiot_hispark_pegasus"。
- “type”: 產(chǎn)品類型,被標(biāo)記為"mini"。
- “version”: 產(chǎn)品版本號(hào),標(biāo)記為"3.0"。
- “ohos_version”: 操作系統(tǒng)版本,使用的是OpenHarmony 1.0。
- “device_company”: 設(shè)備制造公司,此產(chǎn)品由"hisilicon"制造。
- “device_build_path”: 設(shè)備構(gòu)建路徑,指定為"device/board/hisilicon/hispark_pegasus"。
- “board”: 開(kāi)發(fā)板名稱,被標(biāo)記為"hispark_pegasus"。
- “kernel_type”: 內(nèi)核類型,使用的是"liteos_m"。
- “kernel_is_prebuilt”: 內(nèi)核是否預(yù)構(gòu)建,被標(biāo)記為true。
- “kernel_version”: 內(nèi)核版本號(hào),此處為空。
- “subsystems”: 子系統(tǒng)列表,包含了產(chǎn)品的不同子系統(tǒng)及其組件信息。
- “subsystem”: 子系統(tǒng)名稱,表示不同的功能區(qū)域。
- “components”: 組件列表,表示在該子系統(tǒng)中使用的組件及其特性。
- “component”: 組件名稱,表示不同的功能組件。
- “features”: 特性列表,描述了組件的不同特性。
- “third_party_dir”: 第三方庫(kù)路徑,指定為"http://device/soc/hisilicon/hi3861v100/sdk_liteos/third_party"。
- “product_adapter_dir”: 產(chǎn)品適配層路徑,指定為"http://vendor/hisilicon/hispark_pegasus/hals"。
最后,也就能看到我們的hb set從頂層,選擇vendor下的產(chǎn)品解決方案,通過(guò)方案中的各個(gè)子系統(tǒng)集,子系統(tǒng),組件,進(jìn)行編譯。
新增自己的產(chǎn)品解決方案
組件定義
首先,在application/sample下創(chuàng)建一個(gè)myComponent等如下目錄。
完成組件功能的編寫
component.c
#include <stdio.h>
#include "ohos_init.h"
void entry(void){
printf("test component!"); // 哪怕這個(gè)解決方案是個(gè)hello,world呢
}
SYS_RUN(entry);
BUILD.gn
static_library(test){
sources = [
"component.c"
]
include_dirs = [
"http://commonlibrary/utils_lite/include"
]
}
定義組件:
在build/lite/components/創(chuàng)建application1.json編寫如下代碼:
{
"components": [
{
"component": "myComponent",
"description": "a test component",
"optional": "true",
"dirs": [
"applications/sample/myComponent"
],
"targets": [
"http://applications/sample/myComponent:test"
],
"adapted_kernel": [ "liteos_m" ]
}
]
}
我們可以使用 -T 修飾我們的編譯命令,實(shí)現(xiàn)指定文件編譯。
hb build -f -T //applications/sample/myComponent:test
說(shuō)明我們的組件編寫沒(méi)什么問(wèn)題。
解決方案定義
創(chuàng)建如下目錄,并編寫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", // 用我們自己定義的子系統(tǒng)的組件
"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 = \"http://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": "http://device/soc/hisilicon/hi3861v100/sdk_liteos/third_party",
"product_adapter_dir": "http://vendor/hisilicon/hispark_pegasus/hals"
}
將hispark_pegasus下的hal/utils復(fù)制到我們自己的產(chǎn)品解決方案中。
創(chuàng)建BUILD.gn文件編寫編譯腳本。
group("product"){
}
編譯檢驗(yàn)
執(zhí)行hb set命令,觀察產(chǎn)品解決方案。
完成編譯。
燒錄測(cè)試
選擇我們的產(chǎn)品解決方案product。
串口調(diào)試,觀察控制臺(tái)輸出。
產(chǎn)品解決方案總結(jié)
結(jié)束語(yǔ)
希望能夠幫助到大家,對(duì)OpenHarmony的編譯過(guò)程有一個(gè)全面的感知。