在线课堂:https://www.100ask.net/index(课程观看)
论 坛:http://bbs.100ask.net/(学术答疑)
开 发 板:https://100ask.taobao.com/ (淘宝)
https://weidongshan.tmall.com/(天猫)
交流群一:QQ群:869222007(鸿蒙开发/Linux/嵌入式/驱动/资料下载)
交流群二:QQ群:536785813(单片机-嵌入式)
公 众 号:百问科技
版本 | 日期 | 作者 | 说明 |
---|---|---|---|
V1 | 2020 | 韦东山 | 韦东山鸿蒙系统开发教程 |
参考文档:
任一个Linux内核的Documentation\kbuild\kconfig-language.rst
https://www.rt-thread.org/document/site/programming-manual/kconfig/kconfig/
对于各类内核,只要支持menuconfig配置界面,都是使用Kconfig。
在配置界面中,可以选择、设置选项,这些设置会保存在.config文件里。
Makefile会包含.config,根据里面的值决定编译哪些文件、怎么编译文件。
问题:
在配置界面中操作的结果保存在.config文件中,示例如下:
# LOSCFG_COMPILER_HIMIX_32 is not set
LOSCFG_COMPILER_CLANG_LLVM=y
#
# Platform
#
LOSCFG_PLATFORM="stm32mp157"
# LOSCFG_PLATFORM_HI3516DV300 is not set
# LOSCFG_PLATFORM_HI3518EV300 is not set
LOSCFG_PLATFORM_STM32MP157=y
# LOSCFG_PLATFORM_IMX6ULL is not set
LOSCFG_PLATFORM_BSP_GIC_V2=y
LOSCFG_ARCH_ARM=y
LOSCFG_ARCH_ARM_AARCH32=y
LOSCFG_ARCH_ARM_V7A=y
LOSCFG_ARCH_ARM_VER="armv7-a"
LOSCFG_ARCH_FPU_VFP_V4=y
LOSCFG_ARCH_FPU_VFP_D32=y
LOSCFG_ARCH_FPU_VFP_NEON=y
LOSCFG_ARCH_FPU="neon-vfpv4"
LOSCFG_ARCH_CORTEX_A7=y
LOSCFG_ARCH_CPU="cortex-a7"
Makefile会包含.config文件,它会根据里面的变量比如LOSCFG_PLATFORM_STM32MP157
选择单板相关的文件。
在Kconfig文件中,假设配置项的名字是XXX,在.config文件中:
CONFIG_XXX
CONFIG_=ABC
,则对应的变量名为ABC_XXX
export CONFIG_=LOSCFG_
,所以对应的变量名为LOSCFG_XXX
在配置界面,使用方向箭头游走到Enable FAT Cache Sync Thread
后,可以:
输入Y,选择配置项,在.config中对应LOSCFG_FS_FAT_CACHE_SYNC_THREAD=y
输入N,不选择配置项,在.config中对应# LOSCFG_FS_FAT_CACHE_SYNC_THREAD is not set
上图中的配置项怎么实现的?
在Kconfig文件中,它对应下列代码:
解释如下:
config option
,这是Kconfig的基本entry;其他entry是用来管理config的。XXX
选项就会被选中。make menuconfig
界面输入H键时,就会提示帮助信息。在Kconfig中,代码如下:
menu "Lib"
config LIB_LIBC
bool "Enable Libc"
default y
help
Answer Y to enable libc for full code.
config LIB_ZLIB
bool "Enable Zlib"
default y
depends on LIB_LIBC
help
Answer Y to enable LiteOS support compress file library.
endmenu
解释如下:
menu “xxx"表示一个菜单,菜单名是"xxx”
menu和endmenu之间的entry都是"xxx"菜单的选项
在上面的例子中子菜单有2个选项:“Enable Libc”、“Enable Zlib”
由于第二个菜单项依赖于第一个菜单项,所以第二个菜单项缩进一格
在上述界面中,对于LiteOS_Compiler_Type
,有2个选择:arm-linux-ohoseabi、clang-llvm。
在Kconfig文件中怎么描述?如下:
menu "Compiler"
choice
prompt "LiteOS_Compiler_Type"
default COMPILER_CLANG_LLVM
help
Enable arm-himix100 or aarch64-himix100 or compiler.
config COMPILER_HIMIX_32
bool "arm-linux-ohoseabi"
depends on PLATFORM_HI3518EV300 || PLATFORM_HI3516DV300 || PLATFORM_IMX6ULL || PLATFORM_STM32MP157
config COMPILER_CLANG_LLVM
bool "clang-llvm"
depends on PLATFORM_HI3518EV300 || PLATFORM_HI3516DV300 || PLATFORM_IMX6ULL || PLATFORM_STM32MP157
endchoice
endmenu
解释如下:
menuconfig XXX
和config XXX
类似,
唯一不同的是该选项除了能设置y/m/n外,还可以实现菜单效果(能回车进入该项内部)。
对于上述界面,Kconfig文件中代码如下:
menuconfig OF
bool "Device Tree and Open Firmware support"
help
This option enables the device tree infrastructure.
It is automatically selected by platforms that need it or can
be enabled manually for unittests, overlays or
compile-coverage.
if OF
config OF_UNITTEST
bool "Device Tree runtime unit tests"
depends on OF_IRQ
select OF_EARLY_FLATTREE
select OF_RESOLVE
help
This option builds in test cases for the device tree infrastructure
that are executed once at boot time, and the results dumped to the
console.
If unsure, say N here, but this option is safe to enable.
config OF_OVERLAY
bool "Device Tree overlays"
select OF_DYNAMIC
select OF_RESOLVE
help
Overlays are a method to dynamically modify part of the kernel's
device tree with dynamically loaded data.
While this option is selected automatically when needed, you can
enable it manually to improve device tree unit test coverage.
endif # OF
menuconfig常用格式有2种:
menuconfig M
if M
config C1
config C2
endif
或:
menuconfig M
config C1
depends on M
config C2
depends on M
第1项menuconfig M
跟config M
语法是一样的,
不同之处在于menuocnfig M
后面可以跟着好几个依赖于M的config C1
、config C2
等子配置项。
在上面的menuconfig中就有if/endif
的使用,它的语法如下:
"if" <expr>
<if block>
"endif"
示例如下,只有定义的OF项,OF_UNITTEST
和OF_OVERLAY
才会显示出来:
if OF
config OF_UNITTEST
bool "Device Tree runtime unit tests"
depends on OF_IRQ
select OF_EARLY_FLATTREE
select OF_RESOLVE
help
This option builds in test cases for the device tree infrastructure
that are executed once at boot time, and the results dumped to the
console.
If unsure, say N here, but this option is safe to enable.
config OF_OVERLAY
bool "Device Tree overlays"
select OF_DYNAMIC
select OF_RESOLVE
help
Overlays are a method to dynamically modify part of the kernel's
device tree with dynamically loaded data.
While this option is selected automatically when needed, you can
enable it manually to improve device tree unit test coverage.
endif # OF
source 语句用于读取另一个文件中的 Kconfig 文件,如:
source "../../kernel/liteos_a/platform/Kconfig"
comment 语句出现在界面的第一行,用于定义一些提示信息,如:
config ARCH_ARM
bool
source "arch/arm/Kconfig"
comment "Extra Configurations"
config ARCH_FPU_DISABLE
bool "Disable Floating Pointer Unit"
default n
help
This option will bypass floating procedure in system.
界面如下:
## 1.9 comment
comment 语句出现在界面的第一行,用于定义一些提示信息,如:
```shell
config ARCH_ARM
bool
source "arch/arm/Kconfig"
comment "Extra Configurations"
config ARCH_FPU_DISABLE
bool "Disable Floating Pointer Unit"
default n
help
This option will bypass floating procedure in system.