Linux kernel的Makefile和Kconfig

來源:互聯網
上載者:User

作者:Sam(甄峰) sam_code@hotmail.com

Sam需要看看2.6 kernel中USB Mouse的代碼。順便談談Kernel中Makefile和Kconfig檔案的關係以及配合使用。

背景知識:
背景知識一:Kconfig介紹:
在#make menuconfig 時,所顯示的Menu list是由各層Kconfig組成的。
最底層Kconfig存放在 ~/arch/i386/Kconfig. 以此為頭,它會一層層使用source來把需要加入的各個目錄中Keconfig添加近來。
例如:source "drivers/Kconfig"
則將~/drivers/Kconfig添加進Menu list中。

背景知識二:Kconfig寫法語義:
config HID
tristate "Generic HID support"
depends on INPUT
default y
---help---
A human interface device (HID) is a type of computer device that interacts directly with and takes input from humans. The term "HID" most commonly used to refer to the USB-HID specification, but other devices (such as, but not strictly limited to, Bluetooth) are designed using HID specification (this involves certain keyboards, mice, tablets, etc). This option compiles into kernel the generic HID layer code (parser, usages, etc.), which can then be used by transport-specific HID implementation (like USB or Bluetooth).
For docs and specs, see http://www.usb.org/developers/hidpage/
If unsure, say Y

解釋如下:
config HID :表示此條目與CONFIG-HID對應。CONFIG-HID會在Makefile中用到。

tristate "Generic HID support" 引號內的內容是會顯示到Menu list中的。tristate表示這一項是三態的。

depends on INPUT:依賴於INPUT這一項。如果沒有選中INPUT,則Menu list不會顯示這項。

default y :預設被選中。

背景知識三:built-in.o
vmlinux是Linux源碼編譯後未壓縮的核心, vmlinux是由arch/i386/kernel/head.o和arch/i386/kernel/init_task.o以及各個相關子目錄下的built-in.o連結而成的。

背景知識四:Kernel Makefile
Kernel中Makefile的體系以及如何編譯的,其實Sam一直是一知半解的。
其中,kernel目錄中的Makefile被稱為底層Makefile。
當使用類似#make menuconfig配置核心成功後,會產生 .config檔案。
換句話說:make menuconfig 時,Makefile會從~/arch/i386/Kconfig讀取Kconfig.然後根據使用者的選擇。產生.config檔案。
例如:在drivers/hid/Kconfig:
config HID
tristate "Generic HID support"
如果使用者選中Y,則在.config中會反映出來:
CONFIG_HID=y
則在~/drivers/Makefile中可以看到:
obj-$(CONFIG_HID) += hid/
表明:如果CONFIG_HID是Y,則把hid目錄添加到要編譯的目錄中了。
進入到/driver/hid目錄,則看到:
hid-objs := hid-core.o hid-input.o
表明這兩個.o檔案是一定會被編譯出的。
obj-$(CONFIG_HID) += hid.o
表明:如果CONFIG_HID是Y,則hid.o會被編譯出來。並built-in.
如果是 =m. 則hid.o被編譯出來,但最後被做成modules(ko)

背景知識五:KBuild Make:
Linux核心的Makefile與我們平時寫的Makefile有所不同,它由五部分組成:
1.Makefile : 頂層Makefile。
2. .config: kernel設定檔。
3. arch/xxx/Makefile: 具體架構的Makefile。
4. scripts/Makefile.xxx : 通用規則。
5. kbuild Makefile: 整個kernel中大約有數百個這種檔案。

#make menuconfig後,產生 kernel設定檔: .config。
頂層Makefile讀取.config.
頂層Makefile通過解析 .config來決定遞迴訪問哪些目錄中的Kbuild Makefile .
這個過程中,Kbuild Makefile會按.config的設定,逐個添加檔案清單,以供最後的編譯使用。
最簡單的KBuild Makefile如下:
obj-y += foo.o
表明:Kbuild在這目錄裡,有一個名為foo.o的目標檔案。foo.o將從foo.c或foo.S檔案編譯得到。並且它會被包入built-in中去。
所有編譯進核心的目標檔案都存在$(obj-y)列表中。而這些列表依賴核心的配置。Kbuild編譯所有的$(obj-y)檔案。然後,調用"$(LD) -r"將它們合并到一個build-in.o檔案中。稍後,該build-in.o會被其父Makefile聯結進vmlinux中。

如果foo.o要編譯成一模組,那就要用obj-m了。所採用的形式如下:
obj-m += foo.o

例一:
在 ~/driver/hid/hid-core.c中,有以下語句,即核心insmod介面hid_init.
module_init(hid_init);
也就是說,當此模組被buildin或者作為module insmod時,kernel會自動調用hid_init.
然後查看 ~/driver/hid/Makefile,發現
hid-objs := hid-core.o hid-input.o
表明只要hid這個目錄被加入,就會產生hid-core.o.
只好去看上一層目錄中怎樣會進入hid目錄:
obj-$(CONFIG_HID) += hid/
表明只要CONFIG_HID=Y,m. 則hid目錄被加入。
但使用者作了什麼,hid目錄被加入編譯呢?則看~/drivers/hid/Kconfig
config HID
tristate "Generic HID support"
以此得之只要在make menuconfig中選中此項,則hid-core.o被編譯出來。

例2:
Sam想要研究USB Keyboard & Mouse driver. 在 make menuconfig時,需要選中:
config USB_HID
tristate "USB Human Interface Device (full HID) support"
則查看Makefile。發現只要選中CONFIG_USB_HID.則會編譯出usb_hid.o
但~/drivers/hid/usbhid目錄中卻沒有usbhid.c。那usbhid.o如何產生的呢?
drivers/hid/usbhid目錄中,有個.usbhid.o.cmd檔案。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.