Android内核驱动开发中的Kconfig文件结构分析(图文)
Android内核驱动开发中的Kconfig文件结构分析(图文)
1 Kconfig和Makefile
毫不夸张地说,Kconfig和Makefile是我们浏览内核代码时最为依仗的两个文件。基本上,Linux 内核中每一个目录下边都会有一个Kconfig文件和一个Makefile文件。Kconfig和Makefile就好似一个城市的地图,地图引导我们去 认识一个城市,而Kconfig和Makefile则可以让我们了解一个内核目录下面的结构。在希望研究内核的某个子系统、某个驱动或其他某个部分时,都 有必要首先仔细阅读一下相关目录下的Kconfig和Makefile文件。
分布到各目录的Kconfig构成了一个分布式的内核配置数据库,每个Kconfig分别描述了所属目录源文档相关的内核配置菜单。在内核配置make menuconfig时,从Kconfig中读出菜单,用户选择后保存到.config的内核配置文档中。 在内核编译时,主Makefile调用这个.config,就知道了用户的选择。
假如想使这个驱动被编译,则要修改Makefile文件,因此,需要添加新的驱动时,需要修改的文件有两个:Kconfig,Makefile.
2 菜单组织结构
一般一个Kconfig文件表示的就是一个菜单,一个菜单由多个菜单项组成,其格式如下:
- menu 菜单名
- 菜单项或菜单链接1
- 菜单项或菜单链接2
- ...
- 菜单项或菜单链接n
- endmenu
例如:
- # drivers/Kconfig
- menu "Device Drivers"
- source "drivers/base/Kconfig"
- source "drivers/connector/Kconfig"
- source "drivers/mtd/Kconfig"
- source "drivers/of/Kconfig"
- source "drivers/parport/Kconfig"
- source "drivers/pnp/Kconfig"
- source "drivers/block/Kconfig"
- source "drivers/hello/Kconfig"
- config test
- bool "提示字符串"
- default y
- ...
- endmenu
2.1 菜单项
语法:
- config <symbol>
- <config options>
例如:
- config UEVENT_HELPER_PATH
- string "path to uevent helper"
- depends on HOTPLUG
- default "/sbin/hotplug"
- help
- Path to uevent helper program forked by the kernel for
- every uevent.
注:每个config菜单项都会产生一个配置选项CONFIG_XXX, XXX即为<symbol>. 如上,则会产生一个配置项:CONFIG_UEVENT_HELPER_PATH,此配置项的值记录在内核根目录下的隐藏文件.config内, 例:~/WORKING_DIRECTORY/kernel/goldfish/.config文件内.
2.2 菜单链接
菜单链接的格式如下:
- source "路径"
- source "drivers/pnp/Kconfig"
2.3 菜单属性
2.3.1 类型
类型可以是:bool、tristate、string、hex和int。
bool类型的只能选中或不选中,选中为y,不选中为n.
tristate类型的菜单项为值可为三种值,多了编译成内核模块的选项。其值可为y,n,m.
string类型表示需要用户输入一串字符串。
hex类型则需要用户输入一个16进制数。
int类型表示用户输入一个整型.
总结:
菜单类型属性就好比一个控件,bool相当于单选框,trstate相当于有三种状态的复选框,string相当于供用户输入字符串的文本编辑框,hex相当于供用户输入16进制数的文本编辑框,而int就相当于供用户输入整型数的文本编辑框。
类型关键字后边可跟随提示字符,也可以不跟随,取决于情况。如:
- string "path to uevent helper"
- bool "Prevent firmware from being built"
2.3.2 默认值
默认值属性default一般在类型属性后边,如:
- config UEVENT_HELPER_PATH
- string "path to uevent helper"
- default "/sbin/hotplug"
2.3.3 依赖
依赖可以是"depends on"或"requires".
语法:
- depends on/requires <expr>
如:
- depends on HOTPLUG
例如:
- config MODULES
- bool "Enable loadable module support"
- config MODVERSIONS
- bool "Set version information on all module symbols"
- depends on MODULES
- comment "module support disabled"
- depends on !MODULES
2.3.4 选择
语法:
- choice
- 选择项
- ..
- endchoice
2.3.5 提示
语法:
- comment "提示信息字符串"
- comment选项
comment只是用来给用户提示信息的,后跟字符串,此字符串也可以在终端中显示。
comment选项只可以是deponds on。
|
评论暂时关闭