Conditionally adding to a variable inside the target Makefile

I have a GNU Makefile that looks something 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 create targetB. Both of them do very similar things, but with a different list of items. The problem is that the variable is not conditionally bound to it, it is always added, which means that my output is always "item1 itemA itemB".

How can I conditionally add to a variable?

+3
source share
1 answer
LIST = item1

.PHONY: targetMain targetA targetB

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

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

targetA targetB: targetMain
+7
source

Source: https://habr.com/ru/post/1730251/


All Articles