Search code examples
makefilegnu

GNU Make for loop with two variables


I want to write something along these lines:

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

How do I go about doing this in a GNU makefile?


Solution

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