Makefile 不检查依赖时间戳

Makefile don't check dependency timestamps

我正在写这样的 makefile

foo: bar
    touch foo

但我希望 foo 仅在 缺失时构建 ,无论 bar 是否比 foo 更新。 make 有可能吗?

but I want foo to be built only if it's missing, regardless of whether bar is newer than foo

在这种情况下使用 order-only dependency:

foo: | bar
    touch foo

如果不存在则创建目标:

foo:
    touch foo

如果过时则制作bar,如果不存在则制作foo

foo: | bar
    touch foo

请注意,在 example in the GNU Make documentation$(OBJDIR) 不依赖于任何东西,只有在它不存在时才会创建。

PS: 关于仅订单先决条件如何工作的一些附加信息。

$ ls
Makefile
$ cat Makefile 
foo1: bar
    touch foo1

foo2: | bar
    touch foo2

foo3:
    touch foo3
$ make foo1 foo2 foo3
make: *** No rule to make target `bar', needed by `foo1'.  Stop.
$ touch bar
$ make foo1 foo2 foo3
touch foo1
touch foo2
touch foo3
$ make foo1 foo2 foo3
make: `foo1' is up to date.
make: `foo2' is up to date.
make: `foo3' is up to date.
$ touch bar
$ make foo1 foo2 foo3
touch foo1
make: `foo2' is up to date.
make: `foo3' is up to date.
$ make foo1 foo2 foo3
make: `foo1' is up to date.
make: `foo2' is up to date.
make: `foo3' is up to date.