makefile:在变量中查找单词的位置 [英] makefile: find a position of word in a variable

查看:93
本文介绍了makefile:在变量中查找单词的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的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_1var_2的所有100种可能组合.这里aAbB代表一些字符串.如何避免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屋!

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