Tavian Barnes
Tavian Barnes

Reputation: 12922

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

I have the following Makefile:

all: foo/bar/baz

foo/%:
    @echo $(VAR)
    cp [email protected] $@

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

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

foo/bar/baz.in:
    touch $@

When I run it, the output is

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

In other words, the pattern-specific variable rule works, but the equivalent syntax to declare an extra prerequisite doesn't. What should I do instead?

The real use case is for copying headers before a build. I wrote

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

gen/include:
        # Copy the headers

but the headers don't get copied.

Upvotes: 3

Views: 1330

Answers (1)

MadScientist
MadScientist

Reputation: 100856

You cannot do this. Pattern rules must define all prerequisite patterns when the rule is created; they cannot be added later.

Writing a pattern rule with no recipe deletes the pattern rule.

Upvotes: 3

Related Questions