我如何在makefile中用点分割字符串 [英] How can i split string with dot in makefile

查看:843
本文介绍了我如何在makefile中用点分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的目标

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

我使用的像test.var1.var2

现在我想再做一个像test.var1.var2.var3一样的关卡,我该如何在makefile中得到它

Now i want to do one more level like test.var1.var2.var3 how can i get that in makefile

我要这样做的原因是因为我正在使用Make文件来部署多个应用程序,并且我想要许多变量.这样用户就可以像

The reason i want to do this is because i am using Make file for deploying multiple apps and i want many variables . so that user ca deploy like

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

推荐答案

使用subst将点替换为空格,使其成为列表.然后使用word访问特定元素:

Use subst to replace dots with spaces so that it becomes a list. Then use word to access a specific element:

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}个目标:

As an aside (that will actually take up most of my answer), if you know in advance what the options are, you could go with some robust metaprogramming. Say you want to generate test-{app} targets for some APPS:

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获取所有可能目标的列表.

The first two lines are "library" functions that will call a "template" (tmpl-for) or generate a rule (rule-for) for each element in the list you provide as the second argument. I create a tmpl-test which takes the app name and gives test-{app}. I define a rule template which takes the app name and sets a target-specific APP variable for the appropriate test-{app} target (which is also made phony by the way). Then I use rule-for to create all my rules for setting APP. Finally I write the actual body of my target, and I get the list of all possible targets using 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.

听起来确实很复杂,但是,如果您适当地抽象化模板功能,则可以生成灵活且易于维护的构建系统.

It sounds complex, and it is, but if you properly abstract the templating functions it can produce flexible and easily maintainable build systems.

这篇关于我如何在makefile中用点分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆