曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 8425|回复: 0
打印 上一主题 下一主题

FORCE

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34397
跳转到指定楼层
楼主
发表于 2011-4-16 20:30:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在内核的 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
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-6-18 03:23 , Processed in 0.081384 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表