Makefile 中会在多处地方看到 FORCE

在内核的 Makefile 中会在多处地方看到 FORCE ,比如:

# vmlinux image - including updated kernel symbols
 vmlinux: $(vmlinux-lds) $(vmlinux-init) $(vmlinux-main) vmlinux.o $(kallsyms.o) FORCE

实际上它是一个伪目标:

PHONY +=FORCE
 FORCE:

# Declare the contents of the .PHONY variable as phony.  We keep that
 # information in a variable so we can use it in if_changed and friends.
 .PHONY: $(PHONY)

从上面看到,FORCE 既没有依赖的规则,其底下也没有可执行的命令。

如果一个规则没有命令或者依赖,而且它的目标不是一个存在的文件名,在执行此规则时,目标总会被认为是最新的。也就是说,这个规则一旦被执行,make 就认为它所表示的目标已经被更新过。当将这样的目标(FORCE)作为一个规则的依赖时(如上的 vmlinux: ),由于依赖总被认为是被更新过的,所以作为依赖所在的规则定义的命令总会被执行。
 比如上面的 vmlinux: 在每次 make 时,它下面的这些命令总会被执行:

ifdef CONFIG_HEADERS_CHECK
        $(Q)$(MAKE)-f $(srctree)/Makefile headers_check
 endif
 ifdef CONFIG_SAMPLES
        $(Q)$(MAKE) $(build)=samples
 endif
 ifdef CONFIG_BUILD_DOCSRC
        $(Q)$(MAKE) $(build)=Documentation
 endif
        $(call vmlinux-modpost)
        $(call if_changed_rule,vmlinux__)
        $(Q)rm -f .old_version

用一个直观的例子可以清楚看到这一点,比如有 1 Makefile 文件:

helloworld:file1.o file2.o
 gcc file1.o file2.o -o helloworld

file1.o:file1.c file2.h
 gcc -c file1.c -o file1.o

file2.o:file2.c file2.h
 gcc -c file2.c -o file2.o

clean: 
 rm -rf *.o helloworld

PHONY  +=FORCE
 FORCE:

.PHONY: $(PHONY)

在执行 make 后,观察文件的生成时间:

helloworld 文件的生成时间是 20:20
如果将上面的 Makefile 文件的 helloworld:file1.o file2.o 这一句后面加个 FORCE,那么再过几分钟后再 make 时,再观察一下 helloworld 的生成时间,可以看到是重新生成的了,当然在 make 执行时命令的输出也能知道该命令被再次执行:

猜你喜欢

转载自www.linuxidc.com/Linux/2016-12/138772.htm