0

The setup is a Makefile with the following content (inspired by the answer https://stackoverflow.com/a/20714468/185475):

prerule: VALUE = Hello
prerule: 
    @echo "in prerule"
    @echo ${VALUE}

record: prerule
    @echo "in record"
    @echo ${VALUE}

I would like to define a variable in one rule, which is used as a prerequisite for a different rule (or actual a set of rules). With the current setup, the following output is generated:

$ make record 
in prerule
Hello
in record

$

What I would like to have as output is the following:

$ make record 
in prerule
Hello
in record
Hello
$
midtiby
  • 14,550
  • 6
  • 34
  • 43

1 Answers1

1

You could add:

$(eval VALUE := $(VALUE))

to the recipe of prerule but be warned that it will set the VALUE variable globally as soon as the prerule recipe is executed. Not just for the context of the record rule.

Demo:

$ cat Makefile
prerule: VALUE = Hello
prerule: 
    @echo "VALUE = $(VALUE) in $@"
    $(eval VALUE := $(VALUE))

record: prerule
    @echo "VALUE = $(VALUE) in $@"

foobar: 
    @echo "VALUE = $(VALUE) in $@"
$ make foobar
VALUE =  in foobar
$ make foobar record
VALUE =  in foobar
VALUE = Hello in prerule
VALUE = Hello in record
$ make record foobar
VALUE = Hello in prerule
VALUE = Hello in foobar
VALUE = Hello in record
Renaud Pacalet
  • 25,260
  • 3
  • 34
  • 51