Search code examples
makefiledependenciesgnu-make

Can a target's prerequisite without build rule have generated transitive prerequisites?


I have the following Makefile:

.PHONY: all

all: foo qux

foo: bar
    cp bar foo

qux: bar
    cp bar qux

-include bar.d

clean:
    rm -f foo qux

which includes a generated bar.d file:

bar: baz

I'd like to be able to run the rules for foo and qux whenever bar or baz is changed.

Without surprise, it works well when I change bar, but when I change baz, I get the following message:

make: Nothing to be done for 'all'.

If I change the contents of bar.d to the following, I get the expected behaviour:

foo: baz

qux: baz

But it would be easier for me not to have to duplicate the $x: baz rule for every target that depends on bar.

Is there a solution that does not involves changing baz.d?


Solution

  • Since there is no recipe for bar, Make's actions are correct; yes, baz has changed, but there is no way to bring bar up to date, therefore no point in rebuilding the targets that depend on bar.

    You can get the behavior you want by adding an "update" recipe to the rule:

    bar: baz
        @touch $@