Kconfig语法

内容来自《【正点原子】I.MX6U嵌入式Linux驱动开发指南V1.5.2.pdf》


1、 mainmenu

顾名思义 mainmenu 就是主菜单,也就是输入“make menuconfig”以后打开的默认界面,在顶层 Kconfig 中有如下代码:

mainmenu "U-Boot $UBOOTVERSION Configuration"

上述代码就是定义了一个名为“U-Boot $UBOOTVERSION Configuration”的主菜单,其中UBOOTVERSION=2016.03,因此主菜单名为“U-Boot 2016.03 Configuration”,如图所示:
在这里插入图片描述

2、调用其他目录下的 Kconfig 文件

和 makefile 一样, Kconfig 也可以调用其他子目录中的 Kconfig 文件,调用方法如下:

source "xxx/Kconfig" //xxx 为具体的目录名,相对路径

在顶层 Kconfig 中有如下代码:

12 source "arch/Kconfig"
......
225
226 source "common/Kconfig"
227
228 source "cmd/Kconfig"
229
230 source "dts/Kconfig"
231
232 source "net/Kconfig"
233
234 source "drivers/Kconfig"
235
236 source "fs/Kconfig"
237
238 source "lib/Kconfig"
239
240 source "test/Kconfig"

从示例代码可以看出,顶层 Kconfig 文件调用了很多其他子目录下的 Kcofig 文件,这些子目录下的 Kconfig 文件在主菜单中生成各自的菜单项。

3、 menu/endmenu 条目

menu 用于生成菜单, endmenu 就是菜单结束标志,这两个一般是成对出现的。在顶层Kconfig 中有如下代码:

14 menu "General setup"
15
16 config LOCALVERSION
17 string "Local version - append to U-Boot release"
18 help
19 Append an extra string to the end of your U-Boot version.
20 This will show up on your boot log, for example.
21 The string you set here will be appended after the contents of
22 any files with a filename matching localversion* in your
23 object and source tree, in that order. Your total string can
24 be a maximum of 64 characters.
25
......
100 endmenu # General setup
101
102 menu "Boot images"
103
104 config SUPPORT_SPL
105 bool
106
......
224 endmenu # Boot images

示例代码有两个 menu/endmenu 代码块,这两个代码块就是两个子菜单,第 14行的“menu “General setup””表示子菜单“General setup”。第 102 行的“menu “Boot images””表示子菜单“Boot images”。体现在主菜单界面中就如图 34.2.2.2 所示:
在这里插入图片描述
在“ General setup”菜单上面还有 “ Architecture select (ARM architecture)”和“ ARM architecture”这两个子菜单,但是在顶层 Kconfig 中并没有看到这两个子菜单对应的menu/endmenu 代码块,那这两个子菜单是怎么来的呢?这两个子菜单就是 arch/Kconfig 文件生成的。包括主界面中的“Boot timing”、“Console recording”等等这些子菜单,都是分别由顶层
Kconfig 所调用的 common/Kconfig、 cmd/Kconfig 等这些子 Kconfig 文件来创建的。

3、 config 条目

顶层 Kconfig 中的“General setup”子菜单内容如下:

14 menu "General setup"
15
16 config LOCALVERSION
17 string "Local version - append to U-Boot release"
18 help
19 Append an extra string to the end of your U-Boot version.
20 This will show up on your boot log, for example.
21 The string you set here will be appended after the contents of
22 any files with a filename matching localversion* in your
23 object and source tree, in that order. Your total string can
24 be a maximum of 64 characters.
25
26 config LOCALVERSION_AUTO
27 bool "Automatically append version information to the version
string"
28 default y
29 help
......
45
46 config CC_OPTIMIZE_FOR_SIZE
47 bool "Optimize for size"
48 default y
49 help
......
54
55 config SYS_MALLOC_F
56 bool "Enable malloc() pool before relocation"
57 default y if DM
58 help
......
63
64 config SYS_MALLOC_F_LEN
65 hex "Size of malloc() pool before relocation"
66 depends on SYS_MALLOC_F
67 default 0x400
68 help
......
73
74 menuconfig EXPERT
75 bool "Configure standard U-Boot features (expert users)"
76 default y
77 help
......
82
83 if EXPERT
84 config SYS_MALLOC_CLEAR_ON_INIT
85 bool "Init with zeros the memory reserved for malloc (slow)"
86 default y
87 help
......
99 endif
100 endmenu # General setup

可以看出,在 menu/endmenu 代码块中有大量的“config xxxx”的代码块,也就是 config 条目。 config 条目就是“General setup”菜单的具体配置项,如图 34.2.2.3 所示:
在这里插入图片描述
“config LOCALVERSION”对应着第一个配置项,“config LOCALVERSION_AUTO”对应着 第 二 个 配 置 项 , 以 此 类 推 。 我 们 以 “ config LOCALVERSION ” 和 “ config LOCALVERSION_AUTO”这两个为例来分析一下 config 配置项的语法:

16 config LOCALVERSION
17 string "Local version - append to U-Boot release"
18 help
19 Append an extra string to the end of your U-Boot version.
......
24 be a maximum of 64 characters.
25
26 config LOCALVERSION_AUTO
27 bool "Automatically append version information to the version
string"
28 default y
29 help
30 This will try to automatically determine if the current tree is a
31 release tree by looking for git tags that belong to the current
......
43
44 which is done within the script "scripts/setlocalversion".)

  第 16 和 26 行,这两行都以 config 关键字开头,后面跟着 LOCALVERSION 和LOCALVERSION_AUTO,这两个就是配置项名字。假如我们使能了 LOCALVERSION_AUTO这个功能,那么就会下.config 文件中生成CONFIG_LOCALVERSION_AUTO,这个在上一小节讲解如何使能 dns 命令的时候讲过了。由此可知, .config 文件中的“CONFIG_xxx” (xxx 就是具体的配置项名字)就是 Kconfig 文件中 config 关键字后面的配置项名字加上“CONFIG_”前缀。
  config 关键字下面的这几行是配置项属性, 17~24 行是 LOCALVERSION 的属性, 27~44 行是 LOCALVERSION_AUTO 的属性。属性里面描述了配置项的类型、输入提示、依赖关系、帮助信息和默认值等。
  第 17 行的 string 是变量类型,也就是“CONFIG_ LOCALVERSION”的变量类型。可以为:bool、 tristate、 string、 hex 和 int,一共 5 种。最常用的是 bool、 tristate 和 string 这三种, bool 类型有两种值: y 和 n,当为 y 的时候表示使能这个配置项,当为 n 的时候就禁止这个配置项。tristate 类型有三种值: y、 m 和 n,其中 y 和 n 的涵义与 bool 类型一样, m 表示将这个配置项编译为模块。 string 为字符串类型,所以 LOCALVERSION 是个字符串变量,用来存储本地字符串,选中以后即可输入用户定义的本地版本号,如图 34.2.2.4 所示:
在这里插入图片描述
  string 后面的“Local version - append to U-Boot release”就是这个配置项在图形界面上的显示出来的标题。
  第 18 行, help 表示帮助信息,告诉我们配置项的含义,当我们按下“h”或“?”弹出来的帮助界面就是 help 的内容。
  第 27 行,说明“CONFIG_LOCALVERSION_AUTO”是个 bool 类型,可以通过按下 Y 或N 键来使能或者禁止 CONFIG_LOCALVERSION_AUTO。
  第 28 行,“default y”表示 CONFIG_LOCALVERSION_AUTO 的默认值就是 y,所以这一行默认会被选中。

4、 depends on 和 select

打开 arch/Kconfig 文件,在里面有这如下代码:

7 config SYS_GENERIC_BOARD
8 bool
9 depends on HAVE_GENERIC_BOARD
10
11 choice
12 prompt "Architecture select"
13 default SANDBOX
14
15 config ARC
16 bool "ARC architecture"
17 select HAVE_PRIVATE_LIBGCC
18 select HAVE_GENERIC_BOARD
19 select SYS_GENERIC_BOARD
20 select SUPPORT_OF_CONTROL

  第 9 行,“depends on”说明“SYS_GENERIC_BOARD”项依赖于“HAVE_GENERIC_BOARD”,也就是说“HAVE_GENERIC_BOARD”被选中以后“ SYS_GENERIC_BOARD”才能被选中。
  第 17~20 行,“select”表示方向依赖,当选中“ARC”以后,“HAVE_PRIVATE_LIBGCC”、“HAVE_GENERIC_BOARD”、“SYS_GENERIC_BOARD”和“SUPPORT_OF_CONTROL”这四个也会被选中。

4、 choice/endchoice

在 arch/Kconfig 文件中有如下代码:

11 choice
12 prompt "Architecture select"
13 default SANDBOX
14
15 config ARC
16 bool "ARC architecture"
......
21
22 config ARM
23 bool "ARM architecture"
......
29
30 config AVR32
31 bool "AVR32 architecture"
......
35
36 config BLACKFIN
37 bool "Blackfin architecture"
......
40
41 config M68K
42 bool "M68000 architecture"
......
117
118 endchoice

choice/endchoice 代码段定义了一组可选择项,将多个类似的配置项组合在一起,供用户单选或者多选。示例代码 34.2.2.7 就是选择处理器架构,可以从 ARC、 ARM、 AVR32 等这些架构中选择,这里是单选。在 uboot 图形配置界面上选择“Architecture select”,进入以后如图 34.2.2.5所示:
在这里插入图片描述
可以在图 34.2.2.5 中通过移动光标来选择所使用的 CPU 架构。第 12 行的 prompt 给出这个choice/endchoice 段的提示信息为“Architecture select”。

5、 menuconfig

menuconfig 和 menu 很类似,但是 menuconfig 是个带选项的菜单,其一般用法为:

1 menuconfig MODULES
2 bool "菜单"
3 if MODULES
4 ...
5 endif # MODULES

第 1 行,定义了一个可选的菜单 MODULES,只有选中了 MODULES 第 3~5 行 if 到 endif之间的内容才会显示。在顶层 Kconfig 中有如下代码:

14 menu "General setup"
......
74 menuconfig EXPERT
75 bool "Configure standard U-Boot features (expert users)"
76 default y
77 help
78 This option allows certain base U-Boot options and settings
79 to be disabled or tweaked. This is for specialized
80 environments which can tolerate a "non-standard" U-Boot.
81 Only use this if you really know what you are doing.
82
83 if EXPERT
84 config SYS_MALLOC_CLEAR_ON_INIT
85 bool "Init with zeros the memory reserved for malloc (slow)"
86 default y
87 help
88 This setting is enabled by default. The reserved malloc
89 memory is initialized with zeros, so first malloc calls
......
98 should be replaced by calloc - if expects zeroed memory.
99 endif
100 endmenu # General setup

第 74~99 行使用 menuconfig 实现了一个菜单,路径如下:

General setup
    -> Configure standard U-Boot features (expert users) --->

在这里插入图片描述
从图 34.2.2.6 可以看到,前面有“[ ]”说明这个菜单是可选的,当选中这个菜单以后就可以进入到子选项中,也就是示例代码 34.2.2.9 中的第 83~99 行所描述的菜单,如图 34.2.2.7 所示:
在这里插入图片描述
如果不选择“Configure standard U-Boot features (expert users)”,那么示例代码 34.2.2.9 中的第 83~99 行所描述的菜单就不会显示出来,进去以后是空白的。

6、 comment

comment 用 于 注 释 , 也 就 是 在 图 形 化 界 面 中 显 示 一 行 注 释 , 打 开 文 件drivers/mtd/nand/Kconfig,有如下所示代码:

74 config NAND_ARASAN
75 bool "Configure Arasan Nand"
76 help
......
80
81 comment "Generic NAND options"

第 81 行使用 comment 标注了一行注释,注释内容为:“Generic NAND options”,这行注释在配置项 NAND_ARASAN 的下面。在图形化配置界面中按照如下路径打开:

-> Device Drivers
   -> NAND Device Support

结果如图 34.2.2.8 所示:
在这里插入图片描述
从图 34.2.2.8 可以看出,在配置项“Configure Arasan Nand”下面有一行注释,注释内容为

“*** Generic NAND options ***”。

7、 source

source 用于读取另一个 Kconfig,比如:

source "arch/Kconfig"
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

【ql君】qlexcel

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值