makefile中的嵌套循环,与"-j n"兼容

N F*_*N F 2 makefile gnu-make

在bobbogo对Stack Overflow问题的回答如何在makefile中编写循环?,它显示了如何在makefile中编写以下伪代码的等价物:

For i in 1, ..., n:
  Add the following rule "job_$i: ; ./a.out $i > output_$i"
Run Code Online (Sandbox Code Playgroud)

正如答案本身所述,bobbogo解决方案的优点在于,如果指定"-j num_threads",则作业将并行运行.其他明显的,更简单的解决方案,没有这个属性.

我的问题:我怎么能做同样的事情,但对于嵌套循环,即:

For i in 1, ..., n:
  For j in 1, ..., m:
    Add the following rule "job_$i_$j: ; ./a.out $i $j > output_$i_$j"
Run Code Online (Sandbox Code Playgroud)

我只期待使用GNU Make.提前致谢!

bob*_*ogo 7

@Michael说得对.唯一棘手的一点是在工作的食谱中导出$ i和$ j.在原始解决方案中,我使用了静态模式规则,其中$*shell命令扩展为%规则中匹配的字符串.不幸的是,当您想要匹配目标名称中的两个字段时,这并不是那么方便.宏帮助了很多.草图:

jobs := $(foreach i,1 2 3 4 5,$(foreach j,1 2 3,job-$i-$j))

.PHONY: all
all: ${jobs} ; echo $@ Success

i = $(firstword $(subst -, ,$*))
j = $(lastword $(subst -, ,$*))

.PHONY: ${jobs}
${jobs}: job-%:
    echo i is $i j is $j
Run Code Online (Sandbox Code Playgroud)