bramp
bramp

Reputation: 9741

Conditionally appending to a variable inside a Makefile target

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?

Upvotes: 6

Views: 8177

Answers (1)

Neil
Neil

Reputation: 411

LIST = item1

.PHONY: targetMain targetA targetB

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

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

targetA targetB: targetMain

Upvotes: 15

Related Questions