鴻蒙輕內核Kconfig使用筆記-進階
在《鴻蒙輕內核Kconfig使用筆記》一文介紹了Kconfig的基礎知識,和鴻蒙輕內核的圖形化配置。本文繼續介紹些進階的使用方法。本文中所涉及的源碼,均可以在開源站點https://gitee.com/openharmony/kernel_liteos_m 獲取,涉及開發板時以fnlink v200zr為例, 芯片開發板相關工程路徑如下:
- https://gitee.com/openharmony/vendor_bestechnic
- https://gitee.com/openharmony/device_soc_bestechnic
- https://gitee.com/openharmony/device_board_fnlink
本文在前文的基礎上,再介紹下hb set、Makefile和kconfig的關系,然后介紹下如何使用Kconfig圖形化配置芯片、設備和產品方案。
1、 hb set、Makefile和kconfig的關系
我們知道在make menuconfig 之前,必須使用hb set設置產品解決方案,下面看下具體是如何做到的。
在kernel\liteos_m\Makefile文件中,有如下makefile片段。⑴處使用makefile foreach命令和shell sed命令循環處理hb set輸出的每一行,把“key:value”格式去掉多余的[OHOS INFO]字符,把空格轉換為下劃線,即轉換的格式為“key=value”,然后轉換為makefile的變量形式。hb env的輸出、shell命令的輸出見下文。
- ohos_device_path=/home/zhushy/openharmony/device/board/fnlink/v200zr/liteos_m
⑵處判斷解析hb set獲取的ohos_kernel內核是否等于liteos_m,如果不等于,則說明未使用hb set設置產品解決解決方案,或者設置的不是liteos_m內核。設置liteos_a\linux內核時,不能在kernel\liteos_m目錄下執行make menuconfig。除了ohos_kernel,生成的變量還有ohos_product、ohos_product_path、ohos_device_path、ohos_device_company等等。
⑶處的makefile片段表明,makefile還有make help里面沒有提到的參數用法。可以使用make PRODUCT_PATH=XX_Device_Path_XXX等命令來替代使用hb set設置的產品解決方案對應的設備路徑。⑷處將這些設置導出為環境變量。在kernel\liteos_m\Kconfig文件中會使用這些環境變量。
- ohos_kernel ?= liteos_m
- ⑴ $(foreach line,$(shell hb env | sed 's/\[OHOS INFO\]/ohos/g;s/ /_/g;s/:_/=/g' || true),$(eval $(line)))
- ⑵ ifneq ($(ohos_kernel),liteos_m)
- $(error The selected product ($(ohos_product)) is not a liteos_m kernel type product)
- endif
- ⑶ ifeq ($(PRODUCT_PATH),)
- PRODUCT_PATH:=$(ohos_product_path)
- endif
- ifeq ($(DEVICE_PATH),)
- DEVICE_PATH:=$(ohos_device_path)
- endif
- ifeq ($(BOARD_COMPANY),)
- BOARD_COMPANY:=$(ohos_device_company)
- endif
- ...
- ⑷ export BOARD_COMPANY
- export DEVICE_PATH
- export PRODUCT_PATH
hb env的輸出類似如下:
- [OHOS INFO] root path: /home/zhushy/openharmony
- [OHOS INFO] board: v200zr
- [OHOS INFO] kernel: liteos_m
- [OHOS INFO] product: iotlink_demo
- [OHOS INFO] product path: /home/zhushy/openharmony/vendor/bestechnic/iotlink_demo
- [OHOS INFO] device path: /home/zhushy/openharmony/device/board/fnlink/v200zr/liteos_m
- [OHOS INFO] device company: fnlink
執行shell命令hb env | sed 's/\[OHOS INFO\]/ohos/g;s/ /_/g;s/:_/=/g'的輸出如下:
- ohos_root_path=/home/zhushy/openharmony
- ohos_board=v200zr
- ohos_kernel=liteos_m
- ohos_product=iotlink_demo
- ohos_product_path=/home/zhushy/openharmony/vendor/bestechnic/iotlink_demo
- ohos_device_path=/home/zhushy/openharmony/device/board/fnlink/v200zr/liteos_m
- ohos_device_company=fnlink
2、 芯片、單板、擴展板的Kconfig配置
在執行make menuconfig,進入platform配置路徑后,可以看到如下圖所示的配置界面,支持對擴展板、單板、芯片系列等配置。總體感覺這塊后續應該還需要繼續優化調整。hb set設置產品解決方案時,已經確定了芯片和開發板,這些也只能在Kconfig界面上展示,是無法配置的。擴展板倒是可以繼續選擇。后續等支持的開發板和解決方案豐富起來時,hb set設置和kconfig界面設置需要更好的來協作。比如hb set可以支持一系列開發板和解決方案,具體的選擇哪些開發板和解決方案,可以kconfig界面上來配置,hb set只提供默認值等等。

我們來看下對應的makefile片段,深入了解下Kconfig配置的規則。⑴處可以在開發板設備下提供下配置選項,如device\board\fnlink\v200zr\liteos_m目錄下維護Kconfig文件提供可定制的配置項。⑵處提供設備的公司名稱用來定位構建路徑等,這個配置項config SOC_COMPANY只提供string類型、prompt提示、help幫助信息等屬性。后續在SOC部分的配置里,如device\soc\bestechnic\Kconfig.liteos_m.soc,繼續提供這個配置項的默認值default信息。Kconfig里,運行對同一個config配置項多處出現。
⑶處設置擴展板shields、⑷到⑸用于配置開發板信息,⑹到⑺用于配置芯片族和芯片信息。下文分別詳細分析。
- # Device Kconfig import
- ⑴ osource "$(DEVICE_PATH)/Kconfig"
- ⑵ config SOC_COMPANY
- string "SoC company name to locate soc build path"
- help
- This option specifies the SoC company name, used to locate the build path for soc. This option is set by the
- SoC's Kconfig file, and should be exactly the same with SoC company path, and the user should generally avoid
- modifying it via the menu configuration.
- ⑶ orsource "../../device/board/*/Kconfig.liteos_m.shields"
- ⑷ orsource "../../device/board/$(BOARD_COMPANY)/Kconfig.liteos_m.defconfig.boards"
- choice
- prompt "Board Selection"
- orsource "../../device/board/$(BOARD_COMPANY)/Kconfig.liteos_m.boards"
- ⑸ endchoice
- ⑹ orsource "../../device/soc/*/Kconfig.liteos_m.defconfig"
- choice
- prompt "SoC Series Selection"
- orsource "../../device/soc/*/Kconfig.liteos_m.series"
- endchoice
- ⑺ orsource "../../device/soc/*/Kconfig.liteos_m.soc"
2.1 擴展板配置
上面的小節中"../../device/board/*/Kconfig.liteos_m.shields"用于配置擴展板信息,使用*通配符匹配所有的擴展板,可以將所有擴展板配置信息都加載進來。設計者認為不同單板廠商的擴展板可以兼容使用吧。還比較有意思的是,Kconfig文件采用liteos_m.shields作為后綴,一方面指明內核類型,又指明是擴展板的配置。fnlink的擴展板設置路徑為device\board\fnlink\Kconfig.liteos_m.shields,其內容如下。可以看到又進一步包含shields目錄下面Kconfig.liteos_m.shields。
- orsource "shields/Kconfig.liteos_m.shields"
文件device\board\fnlink\shields\Kconfig.liteos_m.shields的內容如下:⑴處為各個開發板的默認配置項取值,界面上不會顯示。⑵處用于展示,并讓開發者界面上選擇需要的開發板。選擇開發板時,對應的一些依賴配置項會被打開,可以自行參考文件device\board\fnlink\shields\v200zr-evb-t1\Kconfig.liteos_m.shield。
- ⑴ orsource "*/Kconfig.liteos_m.defconfig.shield"
- choice
- prompt "shield Selection"
- ⑵ orsource "*/Kconfig.liteos_m.shield"
- endchoice
下面附上fnlink擴展板目錄shields下相關的文件信息:
- shields
- ├── BUILD.gn
- ├── Kconfig.liteos_m.shields
- ├── v200zr-evb-t0
- │ ├── BUILD.gn
- │ ├── Kconfig.liteos_m.defconfig.shield
- │ ├── Kconfig.liteos_m.shield
- └── v200zr-evb-t1
- ├── BUILD.gn
- ├── Kconfig.liteos_m.defconfig.shield
- ├── Kconfig.liteos_m.shield
2.2 開發板配置
文件"../../device/board/$(BOARD_COMPANY)/Kconfig.liteos_m.defconfig.boards"提供指定公司的開發板的默認配置項信息,如文件device\board\fnlink\Kconfig.liteos_m.defconfig.boards內容如下,又進一步引入公司各個開發板的默認配置項信息,可以具體查看文件device\board\fnlink\v200zr\Kconfig.liteos_m.defconfig.board了解下公司開發板默認配置項信息。默認配置項信息不會在配置界面上進行展示。
- orsource "*/Kconfig.liteos_m.defconfig.board"
文件"../../device/board/$(BOARD_COMPANY)/Kconfig.liteos_m.boards"提供指定公司開發板的配置項信息,如文件device\board\fnlink\Kconfig.liteos_m.boards的配置項如下,又進一步引入公司各個開發板的默認配置項信息,可以具體查看文件device\board\fnlink\v200zr\Kconfig.liteos_m.board了解下公司開發板配置項信息。這些配置項用于在界面上供開發者選擇所需的開發板。因為開發板依賴SoC配置項,SoC在hb set時已經確認,這里的配置在界面上只起到展示作用,開發者并不能進行選擇配置,這塊預計后續會繼續優化。
- orsource "*/Kconfig.liteos_m.board"
device\board\fnlink\v200zr\Kconfig.liteos_m.board內容如下:
- config BOARD_V200ZR
- bool "select board V200Z-R"
- depends on SOC_BES2600W
2.3 芯片配置
文件"../../device/soc/*/Kconfig.liteos_m.defconfig"提供芯片系列的默認配置項信息,如文件device\soc\bestechnic\Kconfig.liteos_m.defconfig內容如下,又進一步把各個芯片型號的默認配置信息引入進來,如device\soc\bestechnic\bes2600\Kconfig.liteos_m.defconfig.series。
- rsource "*/Kconfig.liteos_m.defconfig.series"
- config HALS_COMMUCATION_WIFI_LITE
- bool "WIFI LITE"
- default y
在"SoC Series Selection"Soc系列選擇項中,使用的"../../device/soc/*/Kconfig.liteos_m.series"會把SoC各個系列的配置項引入進來,如device\soc\bestechnic\Kconfig.liteos_m.series,文件內容如下,會進一步把文件device\soc\bestechnic\bes2600\Kconfig.liteos_m.series引入進來。細心的同學可能已經注意到,文件Kconfig.liteos_m.series在目錄bestechnic和目錄bestechnic\bes2600下都有,屬于同名文件。所以,Kconfig中的路徑通配符*只通配了一級目錄。
- rsource "*/Kconfig.liteos_m.series"
SoC和SoC Serial的配置項類似,可以自行查看。
小結
本文在前文的基礎上,先介紹下hb set、Makefile和kconfig的關系,然后介紹下如何使用Kconfig圖形化配置芯片、設備和產品方案。因為時間關系,倉促寫作,或能力限制,若有失誤之處,請各位讀者多多指正。