2010-01-27 55 views
3

我有一個GNU Makefile文件,看起來有點像這樣:有條件追加到一個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或使targetB。他們兩人都做了非常類似的事情,但有不同的項目列表。問題在於變量沒有被有條件地附加到,它總是被附加到,意味着我的輸出始終是「item1 itemA itemB」。

如何有條件地追加到變量?

回答

7
LIST = item1 

.PHONY: targetMain targetA targetB 

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

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

targetA targetB: targetMain 
+4

有點解釋:這叫做目標特定變量。它的工作方式是現在有兩個* LIST變量:一個用於targetA,另一個用於targetB。 'target:VARIABLE = value'語法是如何分配特定於目標的變量。這兩個'LIST'變量中的哪一個被展開取決於你所在的目標命令腳本。注意,由於這個原因,目標特定的變量只能從命令腳本中被引用(它們不能被引用prerequesites線,甚至)。 – 2010-01-27 18:18:35

+0

太棒了,這似乎是做我想要的! – bramp 2010-01-27 18:19:46

+0

謝謝,丹,我應該補充一些解釋。 :-) – Neil 2010-01-27 18:59:05