《Linux核心修鍊之道》精華分享與討論(5)——Kernel地圖:Kconfig與Makefile

來源:互聯網
上載者:User

推薦博文: Linux核心“問題門”——學習問題、經驗集錦

推薦下載:《Linux核心修鍊之道》精華版之方法論

 

Makefile不是Make Love

從前在學校,混了四年,沒有學到任何東西,每天就是逃課,上網,玩遊戲,睡覺。畢業的時候,人家跟我說Makefile我完全不知,但是一說Make Love我就來勁了,現在想來依然覺得丟人。

毫不誇張地說,Kconfig和Makefile是我們瀏覽核心代碼時最為依仗的兩個檔案。基本上,Linux核心中每一個目錄下邊都會有一個Kconfig檔案和一個Makefile檔案。 對於一個希望能夠在Linux核心的汪洋代碼裡看到一絲曙光的人來說,將它們放在怎麼重要的地位都不過分。

我們去香港,通過海關的時候,總會有免費的地圖和各種指南拿,有了它們在手裡我們才不至於無頭蒼蠅般迷惘的行走在陌生的街道上。即使在內地出去旅遊的時候一般來說也總是會首先找份地圖,當然了,這時就是要去買了,拿是拿不到的,不同的地方有不同的特色, 只不過有的特色是服務,有的特色是索取。

Kconfig和Makefile就是Linux Kernel迷宮裡的地圖。地圖引導我們去認識一個城市,而Kconfig和Makefile則可以讓我們瞭解一個Kernel目錄下面的結構。我們每次瀏覽kernel尋找屬於自己的那一段代碼時,都應該首先看看目錄下的這兩個檔案。

利用Kconfig和Makefile尋找目標代碼

就像利用地圖尋找目的地一樣,我們需要利用Kconfig和Makefile來尋找所要研究的目標代碼。

比如我們打算研究隨身碟驅動的實現,因為隨身碟是一種storage裝置,所以我們應該先進入到drivers/usb/storage/目錄。但是該目錄下的檔案很多,那麼究竟哪些檔案才是我們需要關注的?這時就有必要先去閱讀Kconfig和Makefile檔案。

對於Kconfig檔案,我們可以看到下面的選項。

34 config USB_STORAGE_DATAFAB
35         bool "Datafab Compact Flash Reader support (EXPERIMENTAL)"
36        depends on USB_STORAGE && EXPERIMENTAL
37        help
38          Support for certain Datafab CompactFlash readers.
39          Datafab has a web page at <http://www.datafabusa.com/>.

顯然,這個選項和我們的目的沒有關係。首先它專門針對Datafab公司的產品,其次雖然CompactFlash reader是一種flash裝置,但顯然不是隨身碟。因為drivers/usb/storage目錄下的代碼是針對usb mass storage這一類裝置,而不是針對某一種特定的裝置。隨身碟只是usb mass storage裝置中的一種。再比如:

101 config USB_STORAGE_SDDR55
102         bool "SanDisk SDDR-55 SmartMedia support (EXPERIMENTAL)"
103         depends on USB_STORAGE && EXPERIMENTAL
104         help
105             Say Y here to include additional code to support the Sandisk SDDR-55
106             SmartMedia reader in the USB Mass Storage driver.

很顯然這個選項是有關SanDisk產品的,並且針對的是SM卡,同樣不是隨身碟,所以我們也不需要去關注。

事實上,很容易確定,只有選項CONFIG_USB_STORAGE才是我們真正需要關注的。

9 config USB_STORAGE
10      tristate "USB Mass Storage support"
11       depends on USB && SCSI
12      ---help---
13        Say Y here if you want to connect USB mass storage devices to your
14        computer's USB port. This is the driver you need for USB
15        floppy drives, USB hard disks, USB tape drives, USB CD-ROMs,
16        USB flash devices, and memory sticks, along with
17        similar devices. This driver may also be used for some cameras
18        and card readers.
19
20        This option depends on 'SCSI' support being enabled, but you
21          probably also need 'SCSI device support: SCSI disk support'
22        (BLK_DEV_SD) for most USB storage devices.
23
24        To compile this driver as a module, choose M here: the
25        module will be called usb-storage.

接下來閱讀Makefile檔案。

0 #
1 # Makefile for the USB Mass Storage device drivers.
2 #
3 # 15 Aug 2000, Christoph Hellwig
4 # Rewritten to use lists instead of if-statements.
5 #
6
7 EXTRA_CFLAGS    := -Idrivers/scsi
8
9 obj-$(CONFIG_USB_STORAGE)    += usb-storage.o
10
11 usb-storage-obj-$(CONFIG_USB_STORAGE_DEBUG)    += debug.o
12 usb-storage-obj-$(CONFIG_USB_STORAGE_USBAT)    += shuttle_usbat.o
13 usb-storage-obj-$(CONFIG_USB_STORAGE_SDDR09)    += sddr09.o
14 usb-storage-obj-$(CONFIG_USB_STORAGE_SDDR55)    += sddr55.o
15 usb-storage-obj-$(CONFIG_USB_STORAGE_FREECOM)    += freecom.o
16 usb-storage-obj-$(CONFIG_USB_STORAGE_DPCM)    += dpcm.o
17 usb-storage-obj-$(CONFIG_USB_STORAGE_ISD200)    += isd200.o
18 usb-storage-obj-$(CONFIG_USB_STORAGE_DATAFAB)    += datafab.o
19 usb-storage-obj-$(CONFIG_USB_STORAGE_JUMPSHOT)    += jumpshot.o
20 usb-storage-obj-$(CONFIG_USB_STORAGE_ALAUDA)    += alauda.o
21 usb-storage-obj-$(CONFIG_USB_STORAGE_ONETOUCH)    += onetouch.o
22 usb-storage-obj-$(CONFIG_USB_STORAGE_KARMA)    += karma.o
23
24 usb-storage-objs :=    scsiglue.o protocol.o transport.o usb.o /
25              initializers.o $(usb-storage-obj-y)
26
27 ifneq ($(CONFIG_USB_LIBUSUAL),)
28      obj-$(CONFIG_USB)    += libusual.o
29 endif

前面通過Kconfig檔案的分析,我們確定了只需要去關注CONFIG_USB_STORAGE選項。在Makefile檔案裡尋找CONFIG_USB_STORAGE,從第9行得知,該選項對應的模組為usb-storage。

因為Kconfig檔案裡的其他選項我們都不需要關注,所以Makefile的11~22行可以忽略。第24行意味著我們只需要關注scsiglue.c、protocol.c、transport.c、usb.c、initializers.c以及它們同名的.h標頭檔。

Kconfig和Makefile很好的協助我們定位到了所要關注的目標,就像我們到一個陌生的地方要隨身攜帶地圖,當我們學習Linux核心時,也要謹記尋求Kconfig和Makefile的協助。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.