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