make targets depend on variables

Viewed 3603

I want (GNU) make to rebuild when variables change. How can I achieve this?

For example,

$ make project
[...]
$ make project
make: `project' is up to date.

...like it should, but then I'd prefer

$ make project IMPORTANTVARIABLE=foobar
make: `project' is up to date.

to rebuild some or all of project.

4 Answers

I might be late with an answer, but here is another way of doing such a dependency with Make conditional syntax (works on GNU Make 4.1, GNU bash, Bash on Ubuntu on Windows version 4.3.48(1)-release (x86_64-pc-linux-gnu)):

1 ifneq ($(shell cat config.sig 2>/dev/null),prefix $(CONFIG))
2 .PHONY: config.sig
3 config.sig:
4   @(echo 'prefix $(CONFIG)' >config.sig &)
5 endif

In the above sample we track the $(CONFIG) variable, writing it's value down to a signature file, by means of the self-titled target which is generated under condition when the signature file's record value is different with that of $(CONFIG) variable. Please, note the prefix on lines 1 and 4: it is needed to distinct the case, when signature file doesn't exist yet.

Of course, consumer targets specify config.sig as a prerequisite.

Related