Kludge
Kludge

Reputation: 2835

Makefile: target-specific variables are always resolved

Consider the following Makefile:

foo: FOOVAR:=$(shell prog_to_execute_only_on_foo)
foo:
    echo "foo"

bar:
    echo "bar"

I would like to be able to run make bar without resolving the shell command for FOOVAR.

Is it possible?

If not, is there a way to work around this? to make sure the shell command will not be executed when running make bar?

Upvotes: 1

Views: 36

Answers (1)

jfMR
jfMR

Reputation: 24788

You can achieve that by defining FOOVAR as a recursively expanded variable instead of a simply expanded variable (i.e., by using = instead of :=):

foo: FOOVAR = $(shell prog_to_execute_only_on_foo)
foo:
    echo "foo"

This way prog_to_execute_only_on_foo will be executed when FOOVAR is expanded.

Note, however, that prog_to_execute_only_on_foo will be executed every time that FOOVAR is expanded, whereas for the simply expanded variable (i.e., :=) it will be only executed once.

Upvotes: 2

Related Questions