OpenHarmony編譯構建系統—淺談與實踐
前言
經過一段時間的南向學習,基于Hi3861智能家居開發套件的內核編程,驅動開發已經基本解決了。這篇來聊聊OpenHarmony的編譯構建,經過前面的實踐,再來看編譯構建。會對之前的編譯流程做一些解釋,實踐一個基于Hispark_pegasus的自己的解決方案。
編譯構建概述
在官網中提到了,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”: 子系統列表,包含了產品的不同子系統及其組件信息。
- “subsystem”: 子系統名稱,表示不同的功能區域。
- “components”: 組件列表,表示在該子系統中使用的組件及其特性。
- “component”: 組件名稱,表示不同的功能組件。
- “features”: 特性列表,描述了組件的不同特性。
- “third_party_dir”: 第三方庫路徑,指定為"http://device/soc/hisilicon/hi3861v100/sdk_liteos/third_party"。
- “product_adapter_dir”: 產品適配層路徑,指定為"http://vendor/hisilicon/hispark_pegasus/hals"。
最后,也就能看到我們的hb set從頂層,選擇vendor下的產品解決方案,通過方案中的各個子系統集,子系統,組件,進行編譯。
新增自己的產品解決方案
組件定義
首先,在application/sample下創建一個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/創建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 修飾我們的編譯命令,實現指定文件編譯。
hb build -f -T //applications/sample/myComponent:test
說明我們的組件編寫沒什么問題。
解決方案定義
創建如下目錄,并編寫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 = \"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復制到我們自己的產品解決方案中。
創建BUILD.gn文件編寫編譯腳本。
group("product"){
}
編譯檢驗
執行hb set命令,觀察產品解決方案。
完成編譯。
燒錄測試
選擇我們的產品解決方案product。
串口調試,觀察控制臺輸出。
產品解決方案總結
結束語
希望能夠幫助到大家,對OpenHarmony的編譯過程有一個全面的感知。