Michael R. Head wrote:
I'd like to make a rule that generates a output file for each pair in
the cross product of two lists of input files. I have a simple makefile
that does what I want, but it's ugly.
I'm wondering if anyone has any suggestions on improving it. At a
minimum, I'd like to get rid of the foreachs in the targets.

You can do that because you know how you constructed the cross product targets. In your case: if you replace the underscore ('_') with a space (' '), the cross product target falls apart into its components again:

As=a1 a2 a3
Bs=b4 b5 b6
ABs=$(foreach a,$(As),$(foreach b,$(Bs),$(a)_$(b)))

empty :=
space := $(empty) $(empty)

A=$(word 1,$(subst _,$(space),$@))
B=$(word 2,$(subst _,$(space),$@))
A_SE=$$(word 1,$$(subst _,$(space),$$@))
B_SE=$$(word 2,$$(subst _,$(space),$$@))

all: $(ABs)

$(As) $(Bs):

.SECONDEXPANSION:

$(ABs): $(A_SE) $(B_SE)
        @echo $@: $^
        @echo A=$A, B=$B


But looking at your original problem description, I think I'd use another approach to solve it. One using a define as 'rule template', because it's a bit easier to read and understand in my opinion - but that might be just me. Something like this (untested):

As=a1 a2 a3
Bs=b4 b5 b6

.PHONY: all
all:

$(As) $(Bs) :

define MyRule
all : $(1)_$(2)
$(1)_$(2) : $(1) $(2)
        @echo $$@ : $$^
        @echo A=$1, B=$2
endef

$(foreach a,$(As),$(foreach b,$(Bs),$(eval $(call MyRule,$(a),$(b)))))

Hope this helps,
Danny



_______________________________________________
Help-make mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/help-make

Reply via email to