Makefile permutation

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-30 08:45:05

问题


Bash can produce permutations (cartesian product):

$ echo {1,2}{a,b}
1a 1b 2a 2b

I would like to do something similar with a makefile. Here is an example makefile:

all: 1a 1b 2a 2b

I would like something like this if possible:

NOV = 1 2
OSC = a b
all: $(NOV)$(OSC)

However when I use an example like that it just creates "1 2a b" instead of combining them. Is this possible?


回答1:


No need for any complex loops or borrowing from shell. It is much simpler than that.

$(foreach p, $(NOV), $(addprefix $p, $(OSC)))



回答2:


SHELL := /bin/bash

NOV := 1,2
OSC := a,b

NOVW := 1 2
OSCW := a b

EXP := $(shell printf "%s " {$(NOV)}{$(OSC)})
EXPW := $(foreach X,$(NOVW),$(foreach Y,$(OSCW),$(X)$(Y)))

.PHONY: all a b $(EXP) $(EXPW)

all: a b

a: $(EXP)
    @echo target a
    @printf "my first prerequisite is %s\n" "$<"
    @printf "all my prerequisites are %s\n" "$^"

b: $(EXPW)
    @echo target b
    @printf "my first prerequisite is %s\n" "$<"
    @printf "all my prerequisites are %s\n" "$^"

Output:

$ make
target a
my first prerequisite is 1a
all my prerequisites are 1a 1b 2a 2b
target b
my first prerequisite is 1a
all my prerequisites are 1a 1b 2a 2b

Note: the SHELL is needed because brace expansion isn't a POSIX feature. The shell used by GNU Make is /bin/sh by default. When Bash is run as /bin/sh, it doesn't support brace expansion.



来源:https://stackoverflow.com/questions/35028167/makefile-permutation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!