[ home ]

Use temporary rule-local variables in GNU Make

Disclaimer: this is not well tested by me. For example, I didn't read about defered and parse-time variable-evaluation yet.

Idea from http://stackoverflow.com/questions/1909188/define-make-variable-at-rule-execution-time.

The problem

Possible solution

Use an intermediate rule (or split up a rule in variable-init- and worker-parts):

    .PHONY = use_2_vars
    use_2_vars: FOO = foo
    use_2_vars: BAR = $(shell echo bar)
    use_2_vars: _use_2_vars
        @echo $(FOO)
        @echo $(BAR)
        @echo $(FOO)
        @echo $(BAR)

Things to note:

Testing this:

    $ gmake use_2_vars
    $ gmake print

I.o.w. both variables are set when the corresponding rule is executed, an are not set otherwise.