linux内核编译 kbuild,Kbuild 编译 Linux 内核系列(四)

By - 11月, 4th 2017

Oh Oh Oh!

温习过后,再来看看我们今天是什么菜?

Author: Wei Yang

作者公众号:杨小伟的世界

help 目标可以说是在 kbuild 中最直接的小目标了,虽然它和我们的代码基本没有什么关系,只是用来生成 kbuild 的简短使用说明,但是用它来作为走近 kbuild 系统的敲门砖是比较合适的。

如何用?

用法很简单。

make help

1

2

makehelp

就可以显示出当前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

1

2

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

总结一下

运行 make 后,会去寻找 makefile ,根据其中的规则做更新。

可以使用选项 -f 指定要寻找那个 makefile ,如果没有指定则按照上述顺序去寻找。

还稍微需要解释一下下,这里的makefile这个词有两种不同的意义,头一次看的估计会晕,说实话我也有点晕。

代词,代表的是 make 使用的规则文件,并不是具体的哪个文件。

名字,是指 make 运行时,如果没有传入 -f 选项,那么会按照 GNUmakefile、 makefile、

从上面的手册中,我们可以看到,运行 make 其实是又其自身的要求的。也就是需要有个规则文件。那我们再来做个实验。

随便新建一个目录,cd进去,运行make,看一下结果。

$ mkdir test

$ cd test/

$ make

make: *** No targets specified and no makefile found. Stop.

1

2

3

4

5

$mkdirtest

$cdtest/

$make

make:***Notargetsspecifiedandnomakefilefound.Stop.

你看是不是啥都干不了?

整理了一下 make 的基本知识,再回过来看我们执行的命令。

make help

1

2

makehelp

这次我们的 make 命令并没有带选项 -f ,所以按照手册所说,应该是在本地按照顺序寻找了规则文件再执行的。 那我们来看一下,内核源码根目录下都有谁呗。

ls

OPYING REPORTING-BUGS include scripts

CREDITS arch init security

Documentation block ipc sound

1

2

3

4

5

6

7

8

9

10

ls

OPYINGREPORTING-BUGSincludescripts

CREDITSarchinitsecurity

Documentationblockipcsound

certskerneltools

Kconfigcryptolibusr

MAINTAINERSdriversmmvirt

Makefilefirmwarenet

READMEfssamples

我相信你已经看到了点什么。 正所谓众里寻她千百度,蓦然回首,那人却在,灯火阑珊处 。

什么样?

已经找到了规则文件 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: /)'

...

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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/Makefilehelp

@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 https://linuxstory.org/kbuild-linux-4/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值