Makefile 对应内核版本: 2.6.35.13
在编译内核时,我们看下只是简单的执行 make 命令时都会做些什么,而执行时 make all 命令时又和只执行 make 时有什么不同。
在只是执行 make 时,因为没有明确指定目标,所以 $(MAKECMDGOALS) 变量为空,此时 Makefile 几个变量的取值如下:
[C++] 纯文本查看 复制代码 dot-config := 1 // 提示要构建与 .config 文件相关的目标,如 vmlinux bzImage 等。
config-targets=0 // 不需要生成 .config ,所以 config-targets 为 0 。
mixed-targets=0 // make 命令中不指定目标,make all 中指定 1 个目标 all,所以不是构建混合目标,故 mixed-targets 为 0
KBUILD_EXTMOD="" // 不是构建外部模块,所以 KBUILD_EXTMOD 为空
KBUILD_OUTPUT="" // 不使用 O 选项指定构建目标的生成路径,故 KBUILD_OUTPUT 为空
skip-makefile="" // 如果 skip-makefile 变量为 1,就基本什么都不做
在顶层 Makefile 中的 105-106 行可以看到:
[code=Makefile]# That's our default target when none is given on the command line
PHONY := _all
_all:[/mw_shl_code]
上面表示当不指定构建任何目标时, _all 即为默认要构建的目标。
在顶层 Makefile 的 455 行有:
[code=Makefile]include $(srctree)/arch/$(SRCARCH)/Makefile[/mw_shl_code]
以 x86 平台为例,这里将 arch/x86/Makefile 包含进来,在这个文件的第 151 行可以看到:
[code=Makefile]all: bzImage[/mw_shl_code]
在顶层 Makefile 的第 531 行,第 1020 行分别看到:
[code=Makefile]all: vmlinux
all: modules[/mw_shl_code]
又在顶层 Makefile 的第 139-146 行看到:
[code=Makefile]# If building an external module we do not care about the all: rule
# but instead _all depend on modules
PHONY += all
ifeq ($(KBUILD_EXTMOD),)
_all: all
else
_all: modules
endif[/mw_shl_code]
由上可见,缺省目标 _all 依赖于 all,而各个 all 又分别依赖于 vmlinux, bzImage, modules 。所以直接执行 make 会依次生成 vmlinux, bzImage, modules 。由此可见,make 和 make all 是等同的。
如果用 make help 来查看,可以发现:* vmlinux - Build the bare kernel
* modules - Build all modules
* bzImage - Compressed kernel image (arch/x86/boot/bzImage) 上面的 3 个目标前面都有一个星号,表示默认情况下会生成它们。
上面所述的情况可以用下面简单的 Makefile 来表示:
[code=Makefile]_all += all
.PHONY: _all
_all:all
@echo "_all generated"
all: vmlinux
all: bzImage
all: modules
vmlinux:
@touch vmlinux
bzImage:
@touch bzImage
modules:
@touch modules
[/mw_shl_code]
运行输出:$ make
_all generated
$ ls
bzImage Makefile modules vmlinux |