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 後,觀察文件的生成時間:


[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

from:http://www.groad.net/bbs/read.php?tid-3392-fpage-3.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章