柴米油盐计划

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来减少垃圾评论。了解我们如何处理您的评论数据