views:

161

answers:

1

I have a GNU Makefile that looks a bit like this:

LIST = item1

.PHONY: targetMain targetA targetB preA preB

targetMain:
# DO all the work in here
    echo $(LIST)

targetA: preA targetMain
targetB: preB targetMain

preA:
LIST += itemA

preB:
LIST += itemB

The idea is that I either run make targetA or make targetB. Both of them do a very similar thing, but with a different list of items. The problem is that the variable isn't conditionally appended to, it is always appended to, meaning my output is always "item1 itemA itemB".

How can I conditionally append to a variable?

+3  A: 
LIST = item1

.PHONY: targetMain targetA targetB

targetMain:
# DO all the work in here
    echo $(LIST)

targetA: LIST+=itemA
targetB: LIST+=itemB

targetA targetB: targetMain
Neil
A little explanation: this is called target-specific variables. The way it works is there are really *two* `LIST` variables now: one for targetA and one for targetB. `target: VARIABLE = value` syntax is how target-specific variables are assigned. Which of the two `LIST` variables is expanded depends on which target's command script you are in. Notice that, because of this, target-specific variables can *only* be referenced from inside the command script (they can't be referenced on the prerequesites line, even).
Dan Moulding
Great, that seems to do what I want!
bramp
Thanks, Dan, I should have added some explanation. :-)
Neil