Wk_of_Angmar
Wk_of_Angmar

Reputation: 572

How do I use a makefile with a wildcard to generate files within all subdirectories?

I have the following directory structure:

superfolder
    makefile
    subfolder1
        file.tex
    subfolder2
        file.tex
    ...
    subfolderN
        file.tex

This is my current superfolder/makefile:

file.pdf : file.tex
    pdftex file.tex

If my current working directory is, for example, superfolder/subfolder1 I can create superfolder/subfolder1/file.pdf by using make --file ../makefile.

Suppose my current working directory was superfolder. How can I edit my makefile such that make all would create file.pdf in every subfolder?
(i.e. create subfolder1/file.pdf, subfolder2/file.pdf, ..., subfolderN/file.pdf)
If it makes things easier, you may assume that every subfolder invariably contains file.tex.

Upvotes: 0

Views: 455

Answers (1)

jmlemetayer
jmlemetayer

Reputation: 4952

Try this:

OUTPUTS := subfolder1/file.pdf
OUTPUTS += subfolder2/file.pdf
OUTPUTS += ...
OUTPUTS += subfolderN/file.pdf

all: $(OUTPUTS)

$(OUTPUTS): %.pdf: %.tex
    (cd $(dir $<) && pdftex $(notdir $<))

.PHONY: all

This way you can manage which files will be built by make. And, as it is using real file targets (not phony targets), make will rebuild only the needed files.

So you should be able to do make (or make all) in the superfolder directory to build all files. If you want to build only one file, do this:

make subfolderN/file

If you are in a subfolder directory you can rebuild the needed file this way:

make --file ../Makefile

Or to rebuild only the file in your directory:

make --file ../Makefile $(basename $(pwd))/file

EDIT: To automate the process, you can replace the first lines by:

OUTPUTS := $(patsubst %.tex,%.pdf,$(wildcard */file.tex))

Upvotes: 3

Related Questions