我如何使用模式规则来添加先决条件,就像我可以定义变量一样?

How can I use a pattern rule to add prerequisites like I can to define variables?

我有以下 Makefile:

all: foo/bar/baz

foo/%:
    @echo $(VAR)
    cp $@.in $@

# This works
foo/bar/%: VAR := Hello world

# This doesn't
foo/bar/%: foo/bar/%.in

foo/bar/baz.in:
    touch $@

当我运行它时,输出是

Hello world
cp foo/bar/baz.in foo/bar/baz
cp: cannot stat ‘foo/bar/baz.in’: No such file or directory
Makefile:4: recipe for target 'foo/bar/baz' failed
make: *** [foo/bar/baz] Error 1

换句话说,pattern-specific variable rule 有效,但声明额外先决条件的等效语法无效。我应该怎么做?

真正的用例是在构建之前复制 headers。我写了

obj/subdir/%.o: CPPFLAGS += -Igen/include
obj/subdir/%.o: | gen/include

gen/include:
        # Copy the headers

但是 headers 不会被复制。

你不能这样做。模式规则必须在创建规则时定义所有先决条件模式;以后无法添加。

正在编写没有配方的模式规则deletes the pattern rule