gcb
gcb

Reputation: 14548

Makefile: PHONY pattern match rule

I have an odd project were a few files are generated at build time. they are among other files but have a special sulffix. (as opposed to a normal project, where all files of some type means they were auto generated)

for example:

src/fileA.js
src/fileB.js.tpl
src/fileC.css
src/fileD.css.tpl
...

then i have a pattern rule:

DATA=$(get string from template.txt)
%: %.tpl
    sed 's/__TEMPLATE__/$(DATA)/g' $< > $@
templates: src/fileB.js src/fileD.css

And all is fine. Until the next build... now src/fileB.js will not get updated because there is one there already, and src/fileB.js.tpl was not changed, though the other file template.txt that i use as a data source to update it might. Which brings me to the clean step.

right now my clean step is rming each file. it is ugly.

.PHONY: clean
clean:
    rm src/fileB.js
    rm src/fileD.css
    ...

You can see how it gets ugly.

In a regular project my clean would be just rm *.o but here i can't do rm *.js as half the files are not auto-generated.

is there any way to make the rule %: %.tpl be a PHONY?

if not, is there any way to feed the file list from template into clean?

Upvotes: 1

Views: 222

Answers (1)

jdarthenay
jdarthenay

Reputation: 3147

What about this?

TEMPLATES=$(wildcard src/*.tpl)
GENERATED=$(TEMPLATES:%.tpl=%)

clean:
    rm -f $(GENERATED)

Well, I would backup before testing this...

Upvotes: 2

Related Questions