tags:

views:

256

answers:

2

In a Makefile, I'm trying to assign the result of a shell command to a variable:

TMP=`mktemp -d /tmp/.XXXXX`

all:
    echo $(TMP)
    echo $(TMP)

but

$ make Makefile all

is echoing 2 different values, eg:

/tmp/.gLpm1T
/tmp/.aR4cDi

What is the syntax for mktemp to be computed on variable assignment?

Thank you.

A: 

Does TMP=$(mktemp -d bla bla bla) work?

Paul Betts
no, neither $(`mktemp -d XXXXX`)...
abernier
+3  A: 

It depends on the flavour of make. With GNU Make, you can use := instead of = as in

TMP:=$(shell mktemp -d /tmp/.XXXXX)

Edit As pointed out by Novelocrat, the = assignment differs from := assignment in that values assigned using = will be evaluated during substitution (and thus, each time, the variable is used), whereas := assigned variables will have their values evaluated only once (during assignment), and hence, the values are fixed after that. See the documentation of GNU Make for a more detailed explanation.

In order for the value to be truelly constant after assignment, though, the it should not contain any parts, which might be special to the shell (which make calls in order to actually run the update rules, etc.) In particular, backticks are best avoided. Instead, use GNU make's built-in shell function and similar to achieve your goals.

Dirk
Variables assigned with `=` are evaluated at each substitution. Variables assigned with `:=` are evaluated at assignment, as you desire.
Novelocrat
Hi Dirk,It does not seem to work: http://pastie.org/619606Is there a syntax error in my Makefile?Thanks
abernier
Edited answer again. I think, the problem is the use of the backticks; for make, the value (incl. the backticks) is essentially "constant" and will get pasted as is at the point of use -- and then, the backticks will be seen by the shell at each point of use. Just a theory, though.
Dirk
Ok, finally found solution, thanks to you and your link:I have to use := for assignment operator, but also $(shell mktemp -d /tmp/.XXXXX)It works now, thank you so much!
abernier
I've accepted your answer, please add the $(shell <cmd>) tip ;)
abernier