linux内核__force,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

以下实例:

FORCE:

$(BUILDROOT)/make.flags: FORCE

@echo ……

……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值