Makefile 中会在多处地方看到 FORCE

转载:http://blog.csdn.net/wzw88486969/article/details/11739737

在内核的 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 后,观察文件的生成时间:

[beyes@SLinux Makefile]$ ll
total 32
-rw-rw-r--. 1 beyes beyes  129 Apr 16 19:00 file1.c
-rw-rw-r--. 1 beyes beyes  924 Apr 16 20:20 file1.o
-rw-rw-r--. 1 beyes beyes  108 Apr 16 19:01 file2.c
-rw-rw-r--. 1 beyes beyes  139 Apr 16 18:49 file2.h
-rw-rw-r--. 1 beyes beyes  880 Apr 16 20:20 file2.o
-rwxrwxr-x. 1 beyes beyes 4786 Apr 16 20:20 helloworld
-rw-rw-r--. 1 beyes beyes  246 Apr 16 20:20 Makefile

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

[beyes@SLinux Makefile]$ ll
total 32
-rw-rw-r--. 1 beyes beyes  129 Apr 16 19:00 file1.c
-rw-rw-r--. 1 beyes beyes  924 Apr 16 20:20 file1.o
-rw-rw-r--. 1 beyes beyes  108 Apr 16 19:01 file2.c
-rw-rw-r--. 1 beyes beyes  139 Apr 16 18:49 file2.h
-rw-rw-r--. 1 beyes beyes  880 Apr 16 20:20 file2.o
-rwxrwxr-x. 1 beyes beyes 4786 Apr 16 20:26 helloworld
-rw-rw-r--. 1 beyes beyes  246 Apr 16 20:20 Makefile
原文地址:https://www.cnblogs.com/pengdonglin137/p/3611165.html