使用subst
用空格替換點,以便它成爲一個列表。然後使用word
訪問特定的元素:
word-dot = $(word $2,$(subst ., ,$1))
test.%:
export var1=$(call word-dot,$*,1) && export var2=$(call word-dot,$*,2) && export var3=$(call word-dot,$*,3)
,輸出:
$ make test.foo.bar.baz
export var1=foo && export var2=bar && export var3=baz
順便說一句(實際上將佔用大部分我的回答的),如果你事先知道的選項是,你可以去一些強大的元編程。說你要產生一些APPS
test-{app}
目標:
tmpl-for = $(foreach x,$2,$(call $1,$x))
rule-for = $(foreach x,$2,$(eval $(call $1,$x)))
APPS := foo bar baz
tmpl-test = test-$1
define test-vars-rule
$(call tmpl-test,$1): APP := $1
.PHONY: $(call tmpl-test,$1)
endef
$(call rule-for,test-vars-rule,$(APPS))
$(call tmpl-for,tmpl-test,$(APPS)):
@echo Testing app: $(APP)
的前兩行是「庫」功能,將所謂的「模板」(tmpl-for
)或生成列表中的每個元素的規則(rule-for
)你提供作爲第二個參數。我創建了一個tmpl-test
,該應用程序名稱爲test-{app}
。我定義了一個規則模板,它接受應用程序名稱併爲適當的test-{app}
目標(這也是假的方式)設置目標特定的APP
變量。然後我使用rule-for
來創建我所有的設置APP
的規則。最後,我寫下我的目標的實際身體,並使用tmpl-for
獲得所有可能目標的列表。
$ make test-foo
Testing app: foo
$ make test-bar
Testing app: bar
$ make test-baz
Testing app: baz
$ make test-blah
make: *** No rule to make target 'test-blah'. Stop.
這聽起來很複雜,它是,但如果你正確地抽象模板功能,它可以產生靈活和易於維護的構建系統。
聽起來像[XY問題](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem),爲什麼你需要這樣做? – user657267
@ user657267我編輯了這個問題 – Karl
看起來像一個過於複雜的'make app = app1 action = test build = 4.2.1'的方法。 – user657267