shouya
shouya

Reputation: 3083

How to create rules from list of targets in Makefile?

My project consists of some configuration files and they are generated from the corresponding templates. e.g. foo.tmpl => foo. Now I wish to write a Makefile that can generate the corresponding file from the template.

I couldn't have something like this:

% : %.tmpl
    generate-from-tmpl $<

Since it apply the target to all possible files, however, I only want to restrict its targets to those with a .tmpl correspondent. Now I have acquired the list of all templated files:

 TEMPLATED_FILES=$(shell find -type f -name "*.tmpl")
 GENERATED_FILES=$(TEMPLATED_FILES:.tmpl=)

I wish to have something that looks like:

 $(foreach GENERATED_FILES) : [email protected]
     generate-from-tmpl $<

How can I achieve that? thanks.

Upvotes: 2

Views: 896

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 81032

%: %.tmpl will attempt to match the target % against any target name but if no matching %.tmpl file exists then make will skip that pattern for that target entirely.

I believe that's what you want exactly. Do you have some reason not to think so?

That being said if you want to be more specific what you want is a Static Pattern Rule:

$(GENERATED_FILES) : % : %.tmpl
        generate-from-tmpl $<

which will only apply to the files in $(GENERATED_FILES).

Also, if your template files are only one directory deep you can use:

TEMPLATED_FILES=$(wildcard *.tmpl)

instead of the shell and find (there are also recursive make wildcard defines available but find is reasonable if you need that).

Upvotes: 3

Related Questions