How to generate list of make targets automatically

2019-02-16 16:19发布

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.

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.

For example, the code SUBDIRS:=????????_?????? seems to correctly glob all of the subdirectories. I can check with this rule:

.PHONY: print
print:
        echo $(SUBDIRS)

However this variable assignment

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)

It is very confusing that SUBDIRS should have the correct subdirectories but TARGET only has one file.

1条回答
Anthone
2楼-- · 2019-02-16 17:00

In your example glob matching is performed by the shell.

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 '$^'.

See also: pattern rules, automatic variables.

查看更多
登录 后发表回答