一、Kconfig 的整体认知框架
- 配置界面:通过 make menuconfig 等命令启动可视化菜单,用户可直观勾选 / 取消功能选项。
- 配置结果:配置最终保存为 .config 文件,所有配置项以 CONFIG_XXX 格式呈现,是编译的核心依据。
- 单个配置项:用 config 关键字定义最小配置单元,包含类型、依赖、默认值等属性。
二、配置结果的保存规则
1、配置项前缀:所有 Kconfig 中定义的 config XXX,最终都会生成 CONFIG_XXX 格式的配置项,写入 .config 文件。
2、文件属性:.config 是自动生成文件,禁止手动编辑,否则下次执行 make defconfig 或 make menuconfig 时会被覆盖。
- CONFIG_XXX=y:表示启用该功能(编译对应代码)。
- # CONFIG_XXX is not set:表示禁用该功能(不编译对应代码)。
三、单个配置项(config)的完整语法
通过测试代码 config 100ASK,你掌握了配置项的核心属性:
config 100ASK
bool "test for 100ask" # 1. 类型与显示名称
depends on (IA64_SGI_SN2 || IA64_GENERIC) # 2. 依赖条件
default y # 3. 默认值
help # 4. 帮助信息
If you have an SGI Altix and you want to enable system
controller communication from user space (you want this!),
say Y. Otherwise, say N.
1、类型(Type):
- bool:布尔型,取值为 y(启用)或 n(禁用),是最常用的类型。
- 其他类型:string(字符串,如路径)、int(数值,如波特率)、choice(互斥选择)。
2、依赖(depends on):
- 定义配置项的生效条件,只有依赖满足时,该选项才会显示并可配置。
- 示例中 depends on (IA64_SGI_SN2 || IA64_GENERIC) 表示:只有当 IA64_SGI_SN2 或 IA64_GENERIC 被启用时,100ASK 才会出现在菜单中。
3、默认值(default):
- 为配置项提供初始值,减少用户手动配置。示例中 default y 表示默认启用 100ASK。
4、帮助(help)
- 提供配置项的功能说明和选择建议,用户在 menuconfig 中按 ? 可查看,提升配置的可读性。
四、菜单(menu/endmenu)的层级组织
语法规则:
- menu "菜单名称":定义一个新菜单,名称会显示在 menuconfig 中。
- 菜单内部可嵌套其他 menu 或 config 项,形成多级子菜单。
- 需用 endmenu 结束菜单定义。
menu "test menu"
config TEST_A
bool "TEST A"
default y
endmenu
五、实现单选:choice/endchoice
1、作用
choice 用于创建互斥的配置组,用户只能在一组选项中选择一个。这在需要 “二选一 / 多选一” 的场景中非常关键,例如:
- 选择调试 UART 的驱动类型(如截图中的 Altera UART、Xilinx Uartlite 等)
- 选择系统的启动模式(NAND Flash / SD Card /eMMC)
- 选择编译器版本(GCC / Clang)
2、语法结构
choice
prompt "提示文本" # 在 menuconfig 中显示的选择提示
default 子配置项 # (可选)默认选中的子项
help # (可选)帮助信息
对该选择组的说明…
# 子配置项(必须是 bool 类型,且互斥)
config 子项1
bool "子项1的显示名称"
help 子项1的说明…
config 子项2
bool "子项2的显示名称"
help 子项2的说明…
endchoice
3、示例
choice
prompt "Select which UART will provide the debug UART"
depends on DEBUG_UART # 只有 DEBUG_UART 开启时,才显示这个选择组
default DEBUG_UART_S5P # 默认选中 Samsung S5P UART
config DEBUG_UART_ALTERA_UART
bool "Altera UART"
help
Select this to enable a debug UART using the altera_uart driver.
config DEBUG_UART_XILINX_UARTLITE
bool "Xilinx Uartlite"
help
Select this for Xilinx Uartlite debug UART.
config DEBUG_UART_S5P
bool "Samsung S5P"
help
Select this for Samsung S5P UART.
endchoice
六、menuconfig
1、作用
menuconfig 是一个自带开关的菜单。它既是一个独立的 bool 配置项(控制整个菜单是否启用),又能作为容器包含子配置项。这比普通的 menu 更灵活,因为:
- 普通 menu 只是视觉分组,没有开关;
- menuconfig 可以通过自身的 y/n 控制子项是否显示。
2、语法结构
menuconfig 名称
bool "菜单显示名称"
help 菜单说明…
# 子配置项(通常依赖于该 menuconfig 本身)
if 名称 # 只有当该 menuconfig 为 y 时,子项才显示
config 子项1
bool "子项1"
depends on 名称
config 子项2
bool "子项2"
depends on 名称
endif
3、示例
menuconfig SERIAL_DRIVERS
bool "Enable Driver Model for serial drivers"
help
Enable the driver model for serial/UART drivers.
If unsure, say Y.
if SERIAL_DRIVERS # 只有 SERIAL_DRIVERS=y 时,下面的子项才可见
config DEBUG_UART
bool "Enable early debug UART for debugging"
help
Enable a debug UART that runs before the real driver model.
config UART_INPUT_CLOCK
int "UART input clock (Hz)"
depends on DEBUG_UART
default 115200
endif # 结束条件块
七、条件判断:if/endif
1、作用
if/endif 用于实现条件包含,只有满足 if 后的条件时,块内的配置项才会被解析和显示。这让 Kconfig 能根据架构、平台或其他配置动态调整配置树。
2、语法结构
if 条件表达式
# 条件满足时才生效的配置项
config …
menu …
choice …
endif
3、示例
if ARCH_IMX6ULL # 只有当架构是 i.MX6ULL 时,以下配置才生效
config MX6ULL_UART
bool "MX6ULL UART support"
default y
help
Enable UART support for i.MX6ULL EVK board.
config MX6ULL_ETH
bool "MX6ULL Ethernet support"
default y
endif
if ARCH_JETSON_ORIN_NX # 只有 Jetson Orin NX 时生效
config JETSON_CSI
bool "Jetson CSI camera support"
default y
help
Enable CSI camera for vehicle detection.
endif
关键效果:
八、文件包含:source
1、作用
source 用于包含其他 Kconfig 文件,将庞大的配置系统拆分成多个模块化的文件。这是 U-Boot 和 Linux 内核管理数千个配置项的核心手段。
2、语法结构
source "路径/文件名"
路径是相对路径,相对于当前 Kconfig 文件所在的目录。
3、示例(U-Boot 顶层 Kconfig)
# 顶层 Kconfig 入口
source "arch/arm/Kconfig" # 引入 ARM 架构相关配置
source "board/freescale/mx6ullevk/Kconfig" # 引入 mx6ull 开发板专属配置
source "drivers/serial/Kconfig" # 引入串口驱动配置
source "drivers/video/Kconfig" # 引入视频/摄像头驱动配置
九、注释提示:comment
1、作用
comment 用于在 menuconfig 界面中显示纯文本注释,不生成任何 CONFIG_* 项。它的作用是:
- 给用户提供操作提示(如截图中的 “Press <?> for additional information”)
- 作为菜单的分隔线,让界面更易读(如 — Serial Drivers —)
2、 语法结构
comment "提示文本"
3、示例
comment "— Debug UART Configuration —"
comment "Select a UART driver only if DEBUG_UART is enabled."
choice
prompt "Select which UART will provide the debug UART"
depends on DEBUG_UART
…
endchoice




