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