makefile:在变量中查找单词的位置 [英] makefile: find a position of word in a variable
问题描述
在我的makefile中,我需要基于命令行变量值进行变量分配.例如,我这样做:
In my makefile, I need to make a variable assignment based on a command line variable value. for example, I do:
make var_1=xxx
其中,var_1
可以具有100个可能的值之一.基于var_1
的值,我需要在我的makefile中为var_2
分配一个值.我可以做到:
where var_1
can have one of say 100 possible values. Based on the value of var_1
, I need to assign a value to var_2
in my makefile. I could do:
ifeq ($(var_1), a)
var_2 = A
endif
ifeq ($(var_1), b)
var_2 = B
endif
,依此类推,对于var_1
,var_2
的所有100种可能组合.这里a
,A
,b
,B
代表一些字符串.如何避免100个if
语句?我当时在想定义两个变量:
and so on for all 100 possible combinations of var_1
, var_2
. Here a
,A
,b
,B
represent some strings. How do I do this to avoid 100's of if
statements? I was thinking to define two variables:
var_1_values = a b c d
var_2_values = A B C D
我可以使用$(findstring $(var_1),$(var_1_values))
来查看$(var_1)
是否在$(var_1_values)
中,但是如何在所有$(var_1_values)
中找到$(var_1)
的位置?然后将使用该位置来选择$(var_2_values)
中的相应单词.
I can use $(findstring $(var_1),$(var_1_values))
to see if $(var_1)
is among $(var_1_values)
, but how do I locate the position of $(var_1)
among all $(var_1_values)
? That position is then to be used to pick the corresponding word inside $(var_2_values)
.
推荐答案
有点麻烦,但是如果有一个符号,您知道它不会出现在任何值中(例如"_"),您可以执行以下操作:
It's a little kludgey, but if there's a symbol you know won't be in any of the values (such as "_") you could do this:
var_1_values = a b c d
var_2_values = A B C D
# This will be a_A b_B c_C d_D
LIST1 = $(join $(addsuffix _,$(var_1_values)),$(var_2_values))
var_1 := a
# The filter gives a_A, the subst turns it into A
var_2 = $(subst $(var_1)_,,$(filter $(var_1)_%, $(LIST1)))
这篇关于makefile:在变量中查找单词的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!