GNU Make for loop with two variables

Beta's suggestion to use join is on the right track, but the problem is that it's not so easy to use it in a way that constructs a command line containing whitespace, such as the one you originally wanted:

$(foreach (var1, var2), ($(LIST1), $(LIST2)), cp $(var1) $(var2);)

because join joins words together: it was originally intended for constructing filenames. However you can play a trick; here's an example of a way to use join that gives you the output you are looking for:

$(subst ^, ,$(join $(addprefix cp^,$(LIST1)),$(patsubst %,^%;,$(LIST2))))

If you think your lists might contain ^ characters then choose something else. Let me know if you need this unpacked/explained.


LIST1 := a b c
LIST2 := 1 2 3

# outside a rule:
$(foreach var1, a b c, $(foreach var2, 1 2 3, $(info $(var1)_$(var2))))

# inside a rule: first line starts with a TAB, all the rest are spaces
all:
    @for x in $(LIST1);\
    do \
      for y in $(LIST2);\
      do\
        echo $$x $$y; \
      done \
    done

(Please note that a nested loop that does cp doesn't make much sense.)

EDIT:
Well why didn't you say so?

LIST3 := $(join $(LIST1),$(LIST2))

Tags:

Makefile

Gnu