1. 程式人生 > >Linux內核配置編譯及Makefile簡單分析

Linux內核配置編譯及Makefile簡單分析

如果 模塊 mod foo 基本類型 默認 ever 描述 條目

1. 交叉編譯設置:make ARCH=arm CROSS_COMPILE=arm-linux-

註:也可以直接修改頂層Makefile

ARCH        ?= arm
CROSS_COMPILE   ?= arm-linux-

2. 加載默認設置:make mini2440_defconfig

make mini2440_config: 將arch/arm/configs目錄下的mini2440_defconfig文件復制為.config

make menuconfig: 對內核默認配置進行調整

3. 編譯內核

make zImage

make modules

make uImage(uImage是在zImage基礎上加了64Bytes的頭信息)

make bzImage(內核最終鏡像大於512KB)

4. 安裝內核:

make install

make modules_install

5. 內核配置系統由以下3部分組成

① Makefile:

② Kconfig:

③ 配置工具:

註:使用make config、make menuconfig等命令後,內核頂層目錄生成一個“.config‘’配置文件,該文件記錄模塊是否編譯進內核、或者編譯成模塊

6. 運行make menuconfig時,配置工具首先分析與體系結構對應的arch/<arch>/Kconfig文件,該文件除本身包含一些與體系結構相關的配置項和配置菜單外,還通過sourse語句引入下一層的Kconfig文件

7. 在make menuconfig界面中選擇“config ARCH_S3C2410”選項後內核會在配置文件".config"中添加

CONFIG_ARCH_S3C2440=y

同時在include/config目錄中生成由.config得來的配置文件auto.conf,以及在include/linux目錄中以C語言頭文件給出的配置文件autoconf.h

8. 執行make zImage等生成內核鏡像命令時,首先執行頂層Makefile,頂層Makefile又通過include指令導入特定體系架構或子目錄中的Makefile

① 頂層Makefile:

include $(srctree)/arch/$(SRCARCH)/Makefile

② arch/arm/Makefile:

machine-$(CONFIG_ARCH_S3C2410) := s3c2410 s3c2400 s3c2412 s3c2440 s3c2442 s3c2443

③ machine-y用於定義machdirs變量

machdirs := $(patsubst %,arch/arm/mach-%/,$(machine-y))

④ machdirs通過下面語句添加到core-y中(cory-y表示內核需要編譯的核心文件)

cory-y += $(machdirs) $(platdirs)

9. 向內核中添加新的功能的步驟

① 將編寫的源代碼復制到Linux內核源代碼相應目錄

② 在目錄的Kconfig文件中增加新源代碼對應項目的編譯配置選項

③ 在目錄的Makefile文件中增加對新源代碼的編譯條目

10. 向內核中添加RTC驅動:

① 在drivers/rtc中包含S3C24XX處理器的RTC驅動源代碼rtc-s3c.c

② 在drivers/rtc目錄的Kconfig文件中包含RTC_DRV_S3C的配置項目

config RTC_DRV_S3C
    tristate "Samsung S3C series SoC RTC"
    depends on ARCH_S3C2410
    help
      RTC (Realtime Clock) driver for the clock inbuilt into the
      Samsung S3C24XX series of SoCs. This can provide periodic
      interrupt rates from 1Hz to 64Hz for user programs, and
      wakeup from Alarm.

      The driver currently supports the common features on all the
      S3C24XX range, such as the S3C2410, S3C2412, S3C2413, S3C2440
      and S3C2442.

      This driver can also be build as a module. If so, the module
      will be called rtc-s3c.

③ 在drivers/rtc目錄的Makefile文件中添加關於RTC_DRV_S3C的條目

obj-$(CONFIG_RTC_DRV_S3C)    += rtc-s3c.o

11. 內核中的Makefile

① 目標定義:目標定義用來定義哪些內容要作為模塊編譯,哪些要編譯進內核

obj-y += foo.o

更常見的做法是根據auto.conf文件的CONFIG_變量來決定文件的編譯方式,例如

obj-$(CONFIG_RTC_DRV_S3C) += rtc-s3c.o

② 多文件模塊的定義:如果一個模塊由多個文件組成,就應該采用模塊名加-objs後綴或者-y後綴的形式來定義模塊的組成文件。

obj-$(CONFIG_RTC_CLASS)  += rtc-core.o
rtc-core-y         := class.o interface.o

多文件模塊定義也可以按照下面的格式,

obj-$(CONFIG_ISDN)   += isdn.o
isdn-objs            := isdn_net_lib.o isdn_v110.o isdn_common.o

③ 目錄層次叠代:當CONFIG_EXT3_FS的值為y或m時,內核構建系統會將ext3目錄列入向下叠代的目標中

obj-$(CONFIG_EXT3_FS) += ext3/

12. 內核中的Kconfig

① Kconfig的語法比較豐富,按功能可分為配置選項描述語句和菜單結構描述語句

② 配置選項描述:

(1)“config”關鍵字定義新的配置選項,之後的幾行定義該配置選項的屬性

(2) 配置選項的屬性包括類型、數據範圍、輸入提示、依賴關系、幫助信息和默認值

(3) 每個配置選項都必須指定類型,類型包括bool、tristate、string、hex和int。其中,tristate和string是兩種基本類型,其他都是基於這兩種基本類型。

(4) 輸入提示的一般格式:

prompt <prompt> [if <expr>]

(5) 默認值的一般格式:

default <expr> [if <expr>]

(6) 依賴關系的格式:

depend on(requires) <expr>

(7) 選擇(反向依賴)的格式:

select <symbol> [if <expr>]

(8) 數據範圍的格式:

range <symbol> <symbol> [if <expr>]

(9) 幫助信息格式如下:

help(---help---)

(10) 一個簡單的配置選項示例

config MODVERSIONS
bool
prompt "Set version infomation on all module sysbols"
help
    Usually, modules have to be recompiled whenever you switch to a new kernel③

③ 菜單結構

(1)一般菜單結構由menu......endmenu描述

menu "Network device support"
depends on NET
config NETDEVICES
...
endmenu

註:所有處於“menu”和“endmenu”之間的菜單入口都會成為“Network device support”的子菜單,而且所有子菜單入口都會繼承父菜單的依賴關系。

(2)通過分析依賴關系生成菜單結構

config MODULES
    bool "Enable loadable module support"

config MODVERSIONS
    bool "Set version information on all module symbols"
    depend on MODULES

(3)除此之外,Kconfig中還可能使用“choices...endchoices”、“comment”、“if...endif”這樣的語法結構

Linux內核配置編譯及Makefile簡單分析