sshc
sshc

Reputation: 23

makefile variable automatically assignment with index

I have a question about variable assignments in makefile. given 2 lists of the same size as below : (there are no any characters or string match between 2 lists)

index := 1 2 3 4 5
objects := one_obj two_obj three_obj four_obj five_obj

and I need some index to be active, for example, 1 and 3

act_idx := 1 3

all :
    @echo act_obj = $(act_obj)

how should I assign $(act_obj) to get the output as "act_obj = one_obj three_obj" after I type "make all" in shell ?

Upvotes: 0

Views: 273

Answers (2)

Vroomfondel
Vroomfondel

Reputation: 2898

The GNUmake library at https://github.com/markpiffer/gmtt was originally written with this exact use case in mind (it grew into a everything-but-the-kitchen-sink, but oh well).

The formulation for the selection process which you are basically needing here is done with the relational table idiom. You produce a table as GNUmake string list and execute a select statement which mimicks a simple SQL select.

include gmtt/gmtt.mk

# define a table with 2 columns (no empty cells allowed!)
define object_tbl :=
2
key1 one_obj
key2 two_obj
key3 three_obj
key4 four_obj
key5 five_obj
endef

act_keys := key1 key3

# SELECT column 2 FROM object_tbl WHERE column 1 is found in act_keys
objects := $(call select,2,$(object_tbl),$$(filter $$1,$(act_keys)))

$(info $(objects))

Output:

one_obj three_obj
make: *** No targets.  Stop.

More complex clauses (using arithmetic e.g.) are also possible.

Upvotes: 0

MadScientist
MadScientist

Reputation: 100946

Something like this should do the trick:

act_obj := $(foreach I,$(act_idx),$(filter $I_%,$(objects)))

Based on the adjusted question you can do something like this:

act_obj := $(foreach I,$(act_idx),$(word $I,$(objects)))

You can probably work this out yourself from the set of functions described in the GNU make manual.

Upvotes: 1

Related Questions