Kconfig,Makefile 和 .config

来源:互联网 发布:网络举报工作 编辑:程序博客网 时间:2024/05/17 08:38

原博文的地址

http://blog.csdn.net/nxh_love/article/details/11846861

写的言简意赅,读一遍基本可以明白各个文件的作用。


 最新在做Sensor驱动移植的时候,发现了Android driver 中有Kconfig,Makefile文件。在查看编译后的文件时,又发现还存在.config文件。自己对这几个文件不明白,用度娘来整理下网友对这几个文件的理解。

        分布在各目录下的Kconfig构成了一个分布式的内核配置数据库,每个Kconfig分别描述了所属目录源文件相关的内核配置菜单。在内核配置make menuconfig(或xconfig等)时,从Kconfig中读出配置菜单,用户配置完后保存到.config(在顶层目录下生成)中。在内核编译时,主Makefile调用这个.config,就知道了用户对内核的配置情况。Kconfig就是对应着内核的配置菜单。假如要想添加新的驱动到内核的源码中,可以通过修改Kconfig来增加对我们驱动的配置菜单,这样就有途径选择我们的驱动,假如想使这个驱动被编译,还要修改该驱动所在目录下的Makefile。

Kconfig

    先来看下一个相对完整的Kconfig文件:

[html] view plain copy
  1. menuconfig MISC_DEVICES  
  2.         bool "Misc devices"  
  3.         ---help---  
  4.           Say Y here to get to see options for device drivers from various  
  5.           different categories. This option alone does not add any kernel code.  
  6.   
  7.           If you say N, all options in this submenu will be skipped and disabled.  
  8.   
  9. if MISC_DEVICES  
  10.   
  11. config ST_L3GD20_GYR  
  12.         tristate "L3GD20_GYR gyroscope sensor support"  
  13.         depends on I2C=y  
  14.         help  
  15.           If you say yes here you get support for ST's  
  16.           gyroscope sensors L3GD20_GYR.  
  17.   
  18. choice  
  19.         prompt "Preemption Model"  
  20.         depends on SENSORS_AFA750  
  21.         default CALI_NONE  
  22.   
  23. config CALI_NONE  
  24.         bool "None"  
  25.         help  
  26.           Say yes here to disable calibration function for AFA750  
  27.   
  28. config CALI_POSITIVE  
  29.         bool "positive calibration"  
  30.         help  
  31.           Say yes here when the afa750 and LCD are laid towared the same direction on your board  
  32.   
  33. endchoice  
  34.   
  35. config SENSORS_LSM303D  
  36.         tristate "LSM303 sensor driver"  
  37.         depends on I2C=y  
  38.         help  
  39.           Say yes here to support the sensor  
  40.   
  41. endif  
1.语法:
           config   symbol
                         options
           symbol是一个新的标记的菜单项,options是在这个新的菜单项下的属性和选项。

2.菜单结构:

          配置文件描述了菜单选项,每行都是以一关键字开头(除了帮助信息)。下面的关键字结束一菜单选项:
         - config
        - menuconfig
        - choice/endchoice
        - comment
        - menu/endmenu
        - if/endif
        - source
2.options类型定义:
        每个config菜单项都要有类型定义:bool布尔类型、 tristate三态(内建、模块、移除)、 string字符串、 hex十六进制、 integer整型。

        例如:

[html] view plain copy
  1. config CALI_NONE  
  2.         bool "None"  

         bool类型的只能选中或不选中,tristate类型的菜单项多了编译成内核模块的选项,如果选择编译成内核模块,则会在.config中生成一个CONFIG_CALI_NONEE=m的配置,如果选择内建,就是直接编译成内核影响,就会在.config中生成一个CONFIG_CALI_NONE=y的配置.

3.依赖型定义depends on或requires
           指此菜单的出现与否依赖于另一个定义

[html] view plain copy
  1. config SENSORS_LSM303D  
  2.             tristate "LSM303 sensor driver"  
  3.              depends on I2C=y  
          这个例子表明SENSORS_LSM303D这个菜单项只I2C有效。

4.select与depends on是相反的逻辑关系。
           A depends on B
           那么只有在B选中才能选A
           A select B
          那么只要选中A就会选中B
5.帮助性定义
           只是增加帮助用关键字help或者---help---,"---help---" 和 "help" 在实现的作用上没有区别,"---help---" 有助于将文件中的配置逻辑与给开发人员的提示分开。

6.prompt --输入提示

Makefile

1.顶层的Makefile文档读取 .config文档的内容,并总体上负责build内核和模块。
2.Arch Makefile则提供补充体系结构相关的信息。 
3.scripts目录下的Makefile文档包含了任何用来根据kbuild Makefile 构建内核所需的定义和规则。
            其中.config的内容是在make menuconfig的时候,通过Kconfig文档配置的结果,在/Documentation/kbuild目录下有详细的介绍有关kernel makefile的知识。

举个例子:

假设想把G-sensor LSM303D驱动code加载到工程中,配置内核时该怎么办呢?
1:将您写的lsm303d.c 文档添加到/driver/misc/ 目录下。
2:修改/driver/misc/ 目录下的kconfig文档:

[html] view plain copy
  1. config SENSORS_LSM303D  
  2.         tristate "LSM303 sensor driver"  
  3.         depends on I2C=y  
  4.         help  
  5.           Say yes here to support the sensor  
3:修改该目录下makefile文档。
添加code:
[html] view plain copy
  1. obj-$(CONFIG_SENSORS_LSM303D)   += lsm303d.o  
从上述分析知道CONFIG_SENSORS_LSM303D 是从.config 中读出的。
4.配置kernel下configs/XXXX_defconfig文件
添加code:
[html] view plain copy
  1. CONFIG_SENSORS_LSM303D=y  
当您编译内核时,将会读取.config文档,当发现CONFIG_SENSORS_LSM303D=y,系统在调用/driver/misc下的makefile 时,将会把 lsm303d.o 加入到内核中。即可达到您的目的。


主要参考文章:http://blog.sina.com.cn/s/blog_4a377e150100c896.html

0 0
原创粉丝点击