Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

makefile: find a position of word in a variable

Tags:

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

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

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

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).

like image 229
ivan Avatar asked Mar 12 '12 20:03

ivan


1 Answers

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)))
like image 137
Beta Avatar answered Sep 19 '22 14:09

Beta