在Makefile目标中有条件地向变量追加内容

6

我有一个GNU Makefile,看起来有些像这样:

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

我的想法是,我要么运行make targetA,要么运行make targetB。它们都做类似的事情,但是使用不同的项目列表。问题在于变量没有条件地追加,它总是被追加,这意味着我的输出总是“item1 itemA itemB”。

我如何有条件地追加到一个变量中?

1个回答

15
LIST = item1

.PHONY: targetMain targetA targetB

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

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

targetA targetB: targetMain

8
简要说明:这被称为目标特定变量。其工作原理是现在实际上有两个“LIST”变量:一个针对targetA,一个针对targetB。 target: VARIABLE = value 语法是分配目标特定变量的方式。哪个LIST变量扩展取决于您所在的目标命令脚本。请注意,由于此原因,只能从命令脚本内部引用目标特定变量(甚至不能在先决条件行中引用它们)。 - Dan Moulding
太好了,看起来它做到了我想要的! - bramp
1
谢谢,丹,我应该加些解释的。:-) - Neil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接