如何通过遍历子目录自动生成生成目标的列表? [英] How to generate list of make targets automatically by globbing subdirectories?
问题描述
我想使用一个Makefile在数百个子目录中生成目标.每个子目录都是这样的日期/时间戳:20120119_153957
,它与以下模式????????_??????
相匹配.没有其他子目录与此模式匹配.
I would like to use a single Makefile to generate targets in hundreds of subdirectories. Each subdirectory is a date/time stamp like this: 20120119_153957
, which matches the following pattern ????????_??????
. There are no other subdirectories that match this pattern.
我要生成的一个目标称为????????_??????/graph.pdf
.我有一个名为make_graph
的脚本,它将使图形具有子目录名称.但是我不确定如何编写一个Makefile,它将自动遍历所有子目录并以编程方式生成这些目标.
One target I would like to generate is called ????????_??????/graph.pdf
. I have a script called make_graph
that will make the graph given the subdirectory name. But I'm not sure how to write a Makefile that will automatically glob all of the subdirectores and generate these targets programmatically.
例如,代码SUBDIRS:=????????_??????
似乎正确地遍历了所有子目录.我可以检查以下规则:
For example, the code SUBDIRS:=????????_??????
seems to correctly glob all of the subdirectories. I can check with this rule:
.PHONY: print
print:
echo $(SUBDIRS)
但是这个变量赋值
TARGETS:=$(SUBDIRS:%=%/graph.pdf)
似乎并没有达到我的期望,并分配了很多目标.相反,以下规则仅打印一个目标.
does not seem to do what I expect and assign lots and lots of targets. Instead the following rule just prints one target.
.PHONY: print
print:
echo $(TARGETS)
SUBDIRS
应该具有正确的子目录,但是TARGET
只有一个文件,这非常令人困惑.
It is very confusing that SUBDIRS
should have the correct subdirectories but TARGET
only has one file.
推荐答案
在您的示例中,全局匹配由外壳执行.
In your example glob matching is performed by the shell.
GNU Make具有内置的 wildcard
函数,该函数您可以按如下方式使用:
GNU Make has the built-in wildcard
function, which you can use as follows:
SUBDIRS := $(wildcard ????????_??????)
现在,您可以使用此变量来构建目标列表:
Now you can use this variable to construct a list of targets:
.PHONY : all
all : $(SUBDIRS:%=%/graph.pdf)
%/graph.pdf : # list prerequisites here.
# recipe to make '$@' in directory '$(@D)' from '$^'.