How to generate list of make targets automatically by globbing subdirectories?

混江龙づ霸主 提交于 2019-12-19 05:23: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:


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.



来源:https://stackoverflow.com/questions/8937500/how-to-generate-list-of-make-targets-automatically-by-globbing-subdirectories

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