Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Cartesian product (combinatorial expansion) of name lists in makefile

Using GNU-make, say that I have two lists in my Makefile, and I want to combine them to get their Cartesian product as another list, so that I can use it as a list of targets.

As an example from a different language that I know better, R has a function expand.grid() which could accomplish this.

I actually figured out a way to do this using a Makefile:

.PHONY: all

prefix := 1 2

base := A B 

add_prefix = $(addsuffix $(base), $(prefix))

Obj = $(foreach base, $(base), $(add_prefix))

all: 
    @echo $(Obj)

But, this is pretty hacky and doesn't use the addsuffix function in an intuitive way. Is there a better way to do it?

like image 339
Andy McKenzie Avatar asked Feb 27 '15 16:02

Andy McKenzie


1 Answers

Why not just do it in two loops?

obj := $(foreach X,$(prefix),$(foreach Y,$(base),$X$Y))
like image 167
MadScientist Avatar answered Nov 09 '22 01:11

MadScientist