user2052436
user2052436

Reputation: 4765

Makefile special symbols in variable

I need to redirect command output either to log file, or to screen and log file depending on VERBOSE environment variable. Log file name depends on the target.

I am trying this, but it doesn't work:

ifeq "$(VERBOSE)" "yes"
    OUTPUT := 2>&1 | tee [email protected]
else
    OUTPUT := 1>[email protected] 2>&1
endif

target:
        my_command $(OUTPUT)

But I end up with .log file instead of target.log.

I.e., when rule is executed, say for $ make VERBOSE=yes target, make sees the rule as

target:
        my_command 2>&1 | tee .log

instead of

target:
        my_command 2>&1 | tee [email protected]

How can this be fixed?

Upvotes: 0

Views: 81

Answers (1)

MadScientist
MadScientist

Reputation: 100866

You can't use := if you want the variable to have references to other variables that are not set yet: := expands the value of the variable immediately when the variable is assigned. Once the variable is expanded the first time, it won't be re-expanded later. $@ is not set until the variable is used in the rule. Change to use = instead and remove the escape:

ifeq "$(VERBOSE)" "yes"
    OUTPUT = 2>&1 | tee [email protected]
else
    OUTPUT = 1>[email protected] 2>&1
endif

and it will work.

Upvotes: 1

Related Questions