柴米油鹽計劃

Kbuild 編譯 Linux 內核系列(四)

Oh Oh Oh!Kbuild 編譯 Linux 內核系列回歸了,來看看我們之前學習了什麼?

Kbuild 編譯 Linux 內核系列(一)
Kbuild 編譯 Linux 內核系列(二)
Kbuild 編譯 Linux 內核系列(三)

溫習過後,再來看看我們今天是什麼菜?


Author: Wei Yang
作者公眾號:楊小偉的世界

help 目標可以說是在 kbuild 中最直接的小目標了,雖然它和我們的代碼基本沒有什麼關係,只是用來生成 kbuild 的簡短使用說明,但是用它來作為走近 kbuild 系統的敲門磚是比較合適的。

如何用?

用法很簡單。

make help

就可以顯示出當前kbuild所支持的小目標們。比如:

  • vmlinux
  • modules
  • clean
  • dir/file.o

具體的大家可以動手操作,體會一下這個過程。

在哪裡?

萬事開頭難,既然是你遇到的頭一個目標,可能你會丈二和尚摸不著頭腦。不知道會是在哪裡。
腫么辦?如果是你,你會想怎麼做呢?請在這裡停留一分鐘,自己思考一下再往下看我提供的做法。


你看,我們平時自己使用 make 的時候是怎麼用的呢? 要寫 makefile 是吧,在 makefile 裡面加上目標和規則是吧。那好了, kbuild 也是基於 make 這個基本結構運作的。那就是找到別人寫的那個 makefile 唄。
先別著急找,我們先來看一下 make 的手冊是怎麼講的。

Once a suitable makefile exists, each time you
change some source files, this simple shell
command:

 make

suffices to perform all necessary recompilations.
The make program uses the makefile description
and the last-modification times of
the files to decide which of the files need
to be updated. For each of those files, it
issues the commands recorded in the makefile.

make executes commands in the makefile to
update one or more target names, where name
is typically a program. If no -f option
is present, make will look for the makefiles
GNUmakefile, makefile, and Makefile, in that
order.

總結一下

  • 運行 make 後,會去尋找 makefile ,根據其中的規則做更新。
  • 可以使用選項 -f 指定要尋找那個 makefile ,如果沒有指定則按照上述順序去尋找。

還稍微需要解釋一下下,這裡的makefile這個詞有兩種不同的意義,頭一次看的估計會暈,說實話我也有點暈。

  • 代詞,代表的是 make 使用的規則文件,並不是具體的哪個文件。
  • 名字,是指 make 運行時,如果沒有傳入 -f 選項,那麼會按照 GNUmakefile、 makefile、 Makefile 這個順序去搜索規則文件。

從上面的手冊中,我們可以看到,運行 make 其實是又其自身的要求的。也就是需要有個規則文件。那我們再來做個實驗。
隨便新建一個目錄,cd進去,運行make,看一下結果。

$ mkdir test
$ cd test/
$ make
make: *** No targets specified and no makefile found.  Stop.

你看是不是啥都幹不了?

整理了一下 make 的基本知識,再回過來看我們執行的命令。

make help

這次我們的 make 命令並沒有帶選項 -f ,所以按照手冊所說,應該是在本地按照順序尋找了規則文件再執行的。 那我們來看一下,內核源碼根目錄下都有誰唄。

ls
OPYING        REPORTING-BUGS include        scripts
CREDITS        arch           init           security
Documentation  block          ipc            sound
Kbuild         certs          kernel         tools
Kconfig        crypto         lib            usr
MAINTAINERS    drivers        mm             virt
Makefile       firmware       net
README         fs             samples

我相信你已經看到了點什麼。 正所謂眾里尋她千百度,驀然回首,那人卻在,燈火闌珊處 。

什麼樣?

已經找到了規則文件 Makefile , 那我們就打開看一下,找找我們的 help 小目標唄。
相信你已經看到了~ 它就長這個樣子。

help:
    @echo  'Cleaning targets:'
    @echo  '  clean       - Remove most generated files but keep the config and'
    @echo  '                    enough build support to build external modules'
    @echo  '  mrproper    - Remove all generated files + config + various backup files'
    @echo  '  distclean   - mrproper + remove editor backup and patch files'
    @echo  ''
    @echo  'Configuration targets:'
    @$(MAKE) -f $(srctree)/scripts/kconfig/Makefile help
    @echo  ''
    @echo  'Other generic targets:'
    @echo  '  all         - Build all targets marked with [*]'
    @echo  '* vmlinux     - Build the bare kernel'
    @echo  '* modules     - Build all modules'
    @echo  '  modules_install - Install all modules to INSTALL_MOD_PATH (default: /)'
    ...

怎麼樣,確實夠直接吧,在根目錄的 Makefile 中就找到了目標。看來我們今天的運氣還不錯~

恭喜你

恭喜,你已經知道了一個 kbuild 的小目標是如何運作起來的了。你看是不是和我們平時見到的最簡單的 makefile 結構差不多呢?

一切事物皆有源頭,哪怕是再複雜的結構都可以將其拆分成簡單的組成部分,而去逐個了解和研究。我們的 kbuild 更是如此。相信你可以通過不斷探索,掌握這看似龐大的 kbuild 系統~

祝好,加油~


本文是 LinuxStory 柴米油鹽計劃的投稿文章,由 LinuxStory 整理髮布。
原作者署名為:Wei Yang 。
轉載請註明出處,否則必究相關責任。
本文鏈接:https://linuxstory.org/kbuild-linux-4/

對這篇文章感覺如何?

太棒了
0
不錯
0
愛死了
0
不太好
0
感覺很糟
0
這裡是柴米油鹽計劃投稿的發布賬號。

    You may also like

    Leave a reply

    您的電子郵箱地址不會被公開。 必填項已用 * 標註

    此站點使用Akismet來減少垃圾評論。了解我們如何處理您的評論數據