工程分析:Kconfig

1. 一个完整的编译过程

  1. 源码链接
    源码链接

  2. 编译方式

    # for arm
    $ export ARCH=arm
    $ export CROSS_COMPILE=arm-linux-gnueabi-
    $ make  vexpress_ca9x4_defconfig 
    $ make -j4
    

2. Kconfig编译过程分析

  1. make vexpress_ca9x4_defconfig 做了什么?

    # for arm
    $ export ARCH=arm
    $ export CROSS_COMPILE=arm-linux-gnueabi-
    $ make -n vexpress_ca9x4_defconfig 
    

    make.cmd :
    scripts/kconfig/conf --defconfig=arch/…/configs/vexpress_ca9x4_defconfig Kconfig
    输入:arch/…/configs/vexpress_ca9x4_defconfig Kconfig
    输出:.config

  2. make menuconfig 做了什么?

    # for arm
    $ export ARCH=arm
    $ export CROSS_COMPILE=arm-linux-gnueabi-
    $ make -n menuconfig
    

    make.cmd:
    scripts/kconfig/mconf Kconfig
    输入:.config Kconfig
    输出:.config

3. Kconfig实验

3.1 .config 生成方式1(Kconfig)

scripts/kconfig/conf menu_demo/demo_kconfig

特点:demo_kconfig 要求手动输入config项的默认值

#menu_demo/demo_kconfig 是输入文件,用于定义config项的全集合
$cat menu_demo/demo_kconfig

config KCONFIG_DEMO_ITEM1
    bool
    prompt "demonstate item1 for bool learning"

config KCONFIG_DEMO_ITEM2
    string
    prompt "demonstate item2 for string learning"

config KCONFIG_DEMO_ITEM3
    hex
    prompt "demonstate item3 for hex learning"

#scripts/kconfig/conf 用于设置conifg项的默认值,输出文件为.config。 
#and 因为没有指定demo_kconfig中config项的默认值,需要手动输入默认值
$scripts/kconfig/conf menu_demo/demo_kconfig 
*
* Linux Kernel Configuration
*
demonstate item1 for bool learning (KCONFIG_DEMO_ITEM1) [N/y/?] (NEW) N
demonstate item2 for string learning (KCONFIG_DEMO_ITEM2) [] (NEW) hello world!
demonstate item3 for hex learning (KCONFIG_DEMO_ITEM3) [] (NEW) 33
#
# configuration written to .config
#

$cat .config
#
# Automatically generated file; DO NOT EDIT.
# Linux Kernel Configuration
#
# CONFIG_KCONFIG_DEMO_ITEM1 is not set
CONFIG_KCONFIG_DEMO_ITEM2="hello world!"
CONFIG_KCONFIG_DEMO_ITEM3=0x33

3.2 .config 生成方式2(Kconfig + default_config)

scripts/kconfig/conf --defconfig=./default_config menu_demo/demo_kconfig

特点1:demo_kconfig 的默认值在default_config, 因此不再要求手动输入参数
特点2:CONFIG_KCONFIG_DEMO_ITEM_NoUse** 这些参数是demo_kconfig不需要的,因此.config 里根本不体现

$cat default_config 
CONFIG_KCONFIG_DEMO_ITEM1=y
CONFIG_KCONFIG_DEMO_ITEM2="hello world"
CONFIG_KCONFIG_DEMO_ITEM3=0x1

CONFIG_KCONFIG_DEMO_ITEM_NoUse_hex=0xFF
CONFIG_KCONFIG_DEMO_ITEM_NoUse_string="test string"
CONFIG_KCONFIG_DEMO_ITEM_NoUse_bool=y

#通过default_config文件给如默认值,因此不再要求手动给入
$scripts/kconfig/conf --defconfig=./default_config menu_demo/demo_kconfig 
#
# configuration written to .config
#

$cat .config
#
# Automatically generated file; DO NOT EDIT.
# Linux Kernel Configuration
#
CONFIG_KCONFIG_DEMO_ITEM1=y
CONFIG_KCONFIG_DEMO_ITEM2="hello there, listening?"
CONFIG_KCONFIG_DEMO_ITEM3=0x1

3.3 .config 修改方式3 (make menuconfig)

scripts/kconfig/mconf menu_demo/demo_kconfig

输入:menu_demo/demo_kconfig && .config
输出:.config

特点1:在一个相对可用的.config 的基础上,用于手动修改.config
特点2:图形化编辑窗口。

3.4 语法标记:if/endif

特点:if < expr > 语句 用于决定config项是否生效

$cat default_config 
CONFIG_KCONFIG_DEMO_ITEM1=y
CONFIG_KCONFIG_DEMO_ITEM2="hello world"
CONFIG_KCONFIG_DEMO_ITEM3=0x10
CONFIG_KCONFIG_DEMO_ITEM4=m
$cat menu_demo/demo_kconfig 
# bool 类型
config KCONFIG_DEMO_ITEM1
    bool
    prompt "demonstate item1 for bool learning"
# string 类型    
config KCONFIG_DEMO_ITEM2
    string
    prompt "demonstate item2 for string learning"
# hex 类型
config KCONFIG_DEMO_ITEM3
    hex
    prompt "demonstate item3 for hex learning"
#tristate 类型
config KCONFIG_DEMO_ITEM4
    tristate
    prompt "demonstate item3 for tristate learning"

#KCONFIG_DEMO_ITEM4 是tristate类型. 
# if KCONFIG_DEMO_ITEM4  为 trure 如果KCONFIG_DEMO_ITEM4  是 {y,m},其他为false
if KCONFIG_DEMO_ITEM4 
  config SUB_OF_ITEM4
  bool "sub item4" 
  default y
endif

# hex 类型,返回false,此段无效
if KCONFIG_DEMO_ITEM3
  config SUB_OF_ITEM3
  bool "sub item3" 
  default y
endif
# string 类型,返回false,此段无效
if KCONFIG_DEMO_ITEM2
  config SUB_OF_ITEM2
  bool  "sub item2"
  default y
endif
#bool 类型,KCONFIG_DEMO_ITEM1=y,表达式为true。否则为false
if KCONFIG_DEMO_ITEM1
  config SUB_OF_ITEM1
  bool  "sub item1" 
  default y
endif
ospin@ospin-VirtualBox:~/code/u-boot-2018.11$ cat .config
#
# Automatically generated file; DO NOT EDIT.
# Linux Kernel Configuration
#
CONFIG_KCONFIG_DEMO_ITEM1=y
CONFIG_KCONFIG_DEMO_ITEM2="hello world"
CONFIG_KCONFIG_DEMO_ITEM3=0x10
CONFIG_KCONFIG_DEMO_ITEM4=y
CONFIG_SUB_OF_ITEM4=y
CONFIG_SUB_OF_ITEM1=y

3.5 配置项:menuconfig

.config 输出项中:

特点1:menuconfig 和 config 作用相同。 但他多一个能力,即,依赖于它的配置项将成为它的子菜单(在图形显示中)

	$git diff  menu_demo/demo_kconfig
	+menuconfig KCONFIG_DEMO_MENUCONFIG1
	+    bool
	+    prompt "menuconfig parent menu: demo_menuconfig1"
	+
	+if KCONFIG_DEMO_MENUCONFIG1
	+
	+config MENUCONFIG_SUBITEM1
	+    bool
	+    prompt "demo_menuconfig subitem1"
	+
	+config MENUCONFIG_SUBITEM2
	+    bool
	+    prompt "demo_menuconfig subitem2"
	+endif
	
	diff --git a/default_config b/default_config
	+CONFIG_KCONFIG_DEMO_MENUCONFIG1=y
	$cat .config
	#
	# Automatically generated file; DO NOT EDIT.
	# Linux Kernel Configuration
	#
	CONFIG_KCONFIG_DEMO_ITEM1=y
	CONFIG_KCONFIG_DEMO_ITEM2="hello world"
	CONFIG_KCONFIG_DEMO_ITEM3=0x1
	CONFIG_KCONFIG_DEMO_MENUCONFIG1=y
	# CONFIG_MENUCONFIG_SUBITEM1 is not set
	# CONFIG_MENUCONFIG_SUBITEM2 is not set

提示:我们来看下图形

$scripts/kconfig/mconf  menu_demo/demo_kconfig

在这里插入图片描述
在这里插入图片描述

3.6 语法标记:choice/end choice

特点: choice 只能为bool类型或者tristate类型。choice下只能有一个config entry被选中为y。

  1. choice 为bool类型,默认选中了config KCONFIG_CHOICE_ITEM1为y

    $cat menu_demo/demo_kconfig 
    #choice config could be of type: bool or tristate
    
    choice KCONFIG_DEMO_CHOICE1
    bool
    prompt "choice demo1"
    
    config KCONFIG_CHOICE_ITEM1
    bool
    prompt "choice1"
    
    config KCONFIG_CHOICE_ITEM2
    bool
    prompt "choice2"
    
    config KCONFIG_CHOICE_ITEM3
    tristate
    prompt "choice3"
    endchoice
    
    $cat .config
    #
    # Automatically generated file; DO NOT EDIT.
    # Linux Kernel Configuration
    #
    CONFIG_CHOICE_ITEM1=y
    # CONFIG_CHOICE_ITEM2 is not set
    # CONFIG_CHOICE_ITEM3 is not set
    
  2. choice 为bool类型,默认配置全部打开
    特点:.config 文件最终只能有一个打开, 选中的是CONFIG_CHOICE_ITEM3为y

    $cat default_config 
    
    CONFIG_CHOICE_ITEM1=y
    CONFIG_CHOICE_ITEM2=y
    CONFIG_CHOICE_ITEM3=y
    
    $scripts/kconfig/conf --defconfig=./default_config menu_demo/demo_kconfig 
    ./default_config:3:warning: override: CHOICE_ITEM2 changes choice state
    ./default_config:4:warning: override: CHOICE_ITEM3 changes choice state
    #
    # configuration written to .config
    #
    
    $cat .config
    #
    # Automatically generated file; DO NOT EDIT.
    # Linux Kernel Configuration
    #
    # CONFIG_CHOICE_ITEM1 is not set
    # CONFIG_CHOICE_ITEM2 is not set
    CONFIG_CHOICE_ITEM3=y
    

3.7 语法标记:menu

$cat menu_demo/demo_kconfig 
menu "menu1"

config MENU_SUBITEM1-1
bool
prompt "entry1.1"

config MENU_SUBITEM1-2
bool
prompt "entry1.2"

config MENU_SUBITEM1-3
bool
prompt "entry1.3"

endmenu

#######################################
menu "menu2"

config MENU_SUBITEM2-1
bool
prompt "entry2.1"

config MENU_SUBITEM2-2
bool
prompt "entry2.2"

config MENU_SUBITEM2-3
bool
prompt "entry2.3"

endmenu
$cat .config
#
# Automatically generated file; DO NOT EDIT.
# Linux Kernel Configuration
#

#
# menu1
#
# CONFIG_MENU_SUBITEM1-1 is not set
# CONFIG_MENU_SUBITEM1-2 is not set
# CONFIG_MENU_SUBITEM1-3 is not set

#
# menu2
#
# CONFIG_MENU_SUBITEM2-1 is not set
# CONFIG_MENU_SUBITEM2-2 is not set
# CONFIG_MENU_SUBITEM2-3 is not set

特点:menu只是一个语法标记,用于在图形界面显示的时候显示一个菜单。

$scripts/kconfig/mconf menu_demo/demo_kconfig

在这里插入图片描述
在这里插入图片描述

3.8 语法标记:mainmenu

标题

3.9 文件嵌套:source

  • 输入:arch/…/configs/vexpress_ca9x4_defconfig + Kconfig
    输出:.config

    输入:/Kconfig 把各种CPU/SOC/Board 可能使用到的配置项目,打包在一起。
    vexpress_ca9x4_defconfig 初始化一些配置项,这些配置项用于选中Kconfig中的配置
    输出:代码编译使用到的配置项
    请添加图片描述

3. Kconfig 参考文档

https://www.kernel.org/doc/html/latest/kbuild/index.html
https://www.kernel.org/doc/html/latest/kbuild/kconfig-language.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值