如何在makefile中拆分带点的字符串

如何在makefile中拆分带点的字符串,makefile,gnu-make,Makefile,Gnu Make,我已经把目标定成这样了 test.% export var1=$(basename $*) && export var2=$(subst .,,$(suffix $*)) 我使用像test.var1.var2 现在我想再做一个级别,比如test.var1.var2.var3如何在makefile中获得它 编辑: 我之所以要这样做,是因为我正在使用Make file部署多个应用程序,并且我需要很多变量。这样用户就可以像 make install.{app1}.{test}

我已经把目标定成这样了

test.%
    export var1=$(basename $*) && export var2=$(subst .,,$(suffix $*))
我使用像
test.var1.var2

现在我想再做一个级别,比如
test.var1.var2.var3
如何在makefile中获得它

编辑:

我之所以要这样做,是因为我正在使用Make file部署多个应用程序,并且我需要很多变量。这样用户就可以像

make install.{app1}.{test}.{build_number}

使用
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
顺便说一句(这实际上占据了我大部分的答案),如果您事先知道选项是什么,您可以使用一些健壮的元编程。假设您要为某些
应用程序生成
测试-{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-{app}
。我定义了一个规则模板,它采用应用程序名称,并为相应的
test-{app}
target(顺便说一句,它也是假的)设置一个特定于目标的
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.

这听起来很复杂,确实如此,但如果您正确抽象模板功能,它可以生成灵活且易于维护的构建系统。

听起来像,你到底为什么需要这样做?@user657267我已经编辑了这个问题,它看起来是一种非常复杂的方法,可以使用@user657267 OP来完成
make-app=app1 action=test-build=4.2.1
。@user657267 OP的用法很简单,但是在更复杂的情况下,它可以处理动态目标(例如
test-{app1,app2,…}
)让生活变得更轻松谢谢伙计这就是我一直在寻找的