Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 735
Nathan Farrington Avatar asked Jan 20 '12 06:01

Nathan Farrington


1 Answers

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.

like image 56
Eldar Abusalimov Avatar answered Oct 15 '22 01:10

Eldar Abusalimov