2016-08-12 45 views
0

我需要写为以下情况下的图案规则:Makefile。多维列表?

  • 有2个文件夹:AB
  • 运行命令python gen.py --a=A/file1.foo --b=file2.bar --c=file3.bar生成B/file1.foo
  • file1file2file3是不同的字符串

有没有办法将这些文件名分组在一些多维的ar射线,使所有文件都写一次(我将使用Python语法):

files = [["a1.foo", "a2.bar", "a3.bar"], 
     #...200 other groups... 
     ["b1.foo", "b2.bar", "b3.bar"]] 

,然后规则是这样的:

$(files): B/{reference 1 elem}: A/{1 elem} {2 elem} {3 elem} 
    python gen.py --a=A/{1 elem} --b={2 elem} --c={3 elem} 

任何想法如何存档呢?

回答

1

您可以使用标准的make语法为:

all : 

targets := 
define add_target 
B/${1}: A/${1} ${2} ${3} 
targets += B/${1} 
endef 

# Build dependencies. 
$(eval $(call add_target,a1.foo,a2.bar,a3.bar)) 
# ... 
$(eval $(call add_target,b1.foo,b2.bar,b3.bar)) 

# One generic rule for all ${targets}  
${targets} : % : 
    @echo Making [email protected] from $^ 

all : ${targets} 

.PHONY: all 

注意,这些$(eval $(call add_target,...)是空白敏感的,不存在插入空格。

如果您想make自动创建的输出目录下执行:

${targets} : % : | B 

B : 
    mkdir [email protected] 
+0

哇!这正是我想要的。非常感谢! –

+0

B/$ {1}:A/$ {1} $ {2} $ {3} |我怎样才能追加所有的参数(如果有多于3个的话)?我在哪里可以阅读更多关于它的信息?我还没有发现任何相关的事情。 –

+0

@ViacheslavKroilov https://www.gnu.org/software/make/manual/make.html#index-call –

0

有时有点重复是没有那么糟糕真的

targets := B/a1.foo B/b1.foo 

.PHONY: all 

all: $(targets) 

$(targets): B/%: A/% 
    python gen.py --a=$< --b=$(word 2,$^) --c=$(word 3,$^) 

B/a1.foo: a2.bar a3.bar 
B/b1.foo: b2.bar b3.bar