在开发过程当中我们需要自己添加自己开发或者定制话模块,需要以下几个步骤:
第一步:添加源码目录
第二步:在源码添加编译文件Kconfig与Makefile
第三步:源码上层目录的Kconfig包含源码层Kconfig
1.Kconfig是用来配置内核编译选项的文件,其语法格式如下:
(1)最简单例子
config TEST_DRIVER
tristate "test driver"
---help---
if you say 'y' or Y,it will built in kernel
if you say 'n' or 'N',it will not built
if you say 'm' or 'M',it will built as a module
代表TEST_DRIVER配置变量可以被配置为三种状态,y(Y),n(N),m(M),当用户使用make menuconfig的时候会根据用户配置选择那种配置方式编译该TEST模块。
(2)输入提示格式
bool "xxxx"
或者
bool
prompt "xxx"
其中代表是任意字符串,用来在make menuconfig的时候提示用户
bool代表是config的类型是boolean类型,要么是输入y(Y)编译进内核,否则就是n(N)不编译进内核
例子:
config TEST_DRIVER
bool "test driver"
---help---
if you say 'y' or Y,it will built in kernel
if you say 'n' or 'N',it will not built
(2)默认配置
default [if ]
make menuconfig当用户不配置时或者选择默认配置时,默认选择配置编译选项.但是如下例子是不会默认显示为y的
config TEST_DRIVER
bool "test driver"
default y
---help---
if you say 'y' or Y,it will built in kernel
if you say 'n' or 'N',it will not built
最终的make menuconfig默认界面如下图:
(3)依赖配置
depends on <expr>
bool "xxx"
或者
boo "xxx" if <expr>
表示只有depends on 后面的或者if后面的expr为真时才会在menuconfig中显示出来并且可以配置。
(4)选择选项(反向依赖)
select <symbol> [if <expr>]
例如:
select A if B
这是方向依赖,假如B选择,那么A也会自动选择。例如如下例子:
config TEST_DRIVER
bool "test driver"
select TEST_SELECT
---help---
if you say 'y' or Y,it will built in kernel
if you say 'n' or 'N',it will not built
config TEST_SELECT
bool "test select"
---help---
if you say 'y' or Y,it will built in kernel
if you say 'n' or 'N',it will not built
在make menuconfig中胡效果如下图:
如图一开始test driver没有选中胡时候test select也是属于没用选中的,但是当test driver被选选中的时候test select也会自动跟着被选中。如下图:
(5)menu选项
menu "xx"
comment "xx"
config xxx
bool "xxxx"
help
xxxxx
...
endmenu
comment代表menu的条目,显示作用。
menu xxx的不是一个配置选项,只有menu 中config才是一个配置选项。因此不能如下这样写
menu "xx"
bool "xxx" /*menu不是条目不能进行bool选项配配置*/
help
xxxx
config xxx
bool "xxx"
help
xxxx
endmenu
/*例子*/
menu "test dirvers menu"
comment "test dirvers menu list"
depends on y
config TEST_1
bool
prompt "test 1"
help
if you say 'y' or Y ,it will built
if you say 'n' or 'N',it will not built
config TEST_2
bool
prompt "test 2"
help
if you say y or Y,it will built
if you say n or N,it will not built
endmenu
(5)choice配置
choice
prompt "xxx"
default xxx0 /*这时候default就有用了,默认config xxx0*/
config xxx0
bool "xxxx0"
hlep
xxxxx0
config xxx1
```bool "xxxx1"
help
xxxxx1
endchoice
例子:(如下默认选择是FB_1920x1080,menuconfig图形界面也是默认是FB_1920x1080)
choice
prompt "fb resolution"
default FB_1920x1080
config FB_1024x600
bool
prompt "display resolution 1024*600"
---help---
if you say y or Y,display resolution is 1024*600
config FB_1920x1080
bool
prompt "display resolution 1920*1080"
help
if you say y or Y ,display resolution is 1920*1080
endchoice
2.Makefile,这个文件才是真正的编译文件,Kconfig只是配置编译模块是编译进内核还是不编译进内核还是模块形式编译,配置的是编译方式,而且有Kconfig只是为图形界面menuconfig配置用的脚本,如果不用图形界面配置不用写Kcnfig也可以,Makefile则是告诉内核编译那些那些c文件或者h头文件等,或者是那些文件夹等。
(1)编译格式
obj-y = foo.o
表示要由foo.c或者foo.s文件编译得到foo.o并链接进内核,除此之外还有obj-m则表示该foo文件要作为模块编译。obj-n则表示foo文件不会被编译到内核。
obj-CONFIG_TEST_DRIVER = test.o
上面Kconfig中的config TEST_DRIVER 如果在menuconfig界面配置y,test文件编译得到foo.o并链接进内核,如果为m则表示该test文件要作为模块编译。如果为n则表示test文件不会被编译到内核。
(2)除此之外,还可以链接多个模块或者文件
obj-y = foo
foo-y = foo1.o foo2.o foo3.o
表示foo模块由foo1.o foo2.o foo3.o共通编译链接而成
(3)编译文件下的所有文件
obj-y += foodir/
最后如果不用Kconfig的图形界面配置编译选项,一般都会在内核目录下的/arch/arm/configs/xxx_defconfig中配置要编译哪些模块,xxx代表是某个平台board,例如:三星,exynos_defconfig.