I have make target like this
test.%
export var1=$(basename $*) && export var2=$(subst .,,$(suffix $*))
and i use like test.var1.var2
Now i want to do one more level like test.var1.var2.var3
how can i get that in makefile
edit:
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}
The file name of the target of the rule. If the target is an archive member, then ' $@ ' is the name of the archive file. In a pattern rule that has multiple targets (see Introduction to Pattern Rules), ' $@ ' is the name of whichever target caused the rule's recipe to be run.
String split example s = "Python string example. We split it using the dot character." parts = s. split(".")
In bash, a string can also be divided without using $IFS variable. The 'readarray' command with -d option is used to split the string data. The -d option is applied to define the separator character in the command like $IFS. Moreover, the bash loop is used to print the string in split form.
The standard solution to split a string in Kotlin is with the native split() function, which takes one or more delimiters as an argument and splits the string around occurrences of the specified delimiters. The split() function returns a list of strings.
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)
Which outputs:
$ make test.foo.bar.baz
export var1=foo && export var2=bar && export var3=baz
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With