可能是kbuild中最直接的小目標 -- help

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系統~

祝好,加油~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章