Complicated Template Rule in Makefile

I have the following makefile that I use to create files from some templates, the generated files have two possible extensions:

%.tex: %.tex*_tpl
    ./generate $@_tpl -o $@

%.xml: %.xml*_tpl
    ./generate $@_tpl -o $@

Dependency list will correspond to parameters such as a.tex_tpl, a.tex-subpart1_tpl, a.tex-subpart2_tpl.

While this works, is there a way to avoid a recurrence? For example, matching *.{tex,xml}in a name of a rule and using the full matching name in the list of dependencies? Something that would look like this:

%.{tex,xml}: $@_tpl
    ./generate $< -o $@

(Although I know that %.{tex,xml}it is not a valid rule name, and you cannot use $@the dependency list)

Or any other (cleaner?) Way.

+4
source share
1 answer

, , :

#
# I've assumed that files of the form:
#
#  a.xml_tpl
#  b.tex_tpl
#
# determine what targets you want to build
#
TARGETS:=$(patsubst %_tpl,%,$(wildcard *.xml_tpl *.tex_tpl))

.PHONY: all
all: $(TARGETS)

.SECONDEXPANSION:
$(TARGETS): %: $$(wildcard %*_tpl)
    ./generate $^ -o $@

.SECONDEXPANSION, $$(wildcard %*_tpl) . , $ ; .

:

a.tex-subpart1_tpl
a.tex_tpl
a.xml-subpart1_tpl
a.xml-subpart2_tpl
a.xml_tpl

make -n, :

./generate a.xml_tpl a.xml-subpart1_tpl a.xml-subpart2_tpl -o a.xml
./generate a.tex_tpl a.tex-subpart1_tpl -o a.tex

?

$(wildcard %*_tpl) , $$ . make $$(wildcard..) , , , .

, $(wildcard %*_tpl) , ( " " ). % , wildcard - ls %*_tpl .

make - , . , .SECONDEXPANSION, . - , . $$(wildcard: $(wildcard . make $(wildcard %*_tpl), % , wildcard , %.

$(TARGETS) ?

:

%: $$(wildcard %*_tpl)
    ./generate $^ -o $@

$(TARGETS). , " match-anything" . , make , , , , , Makefile . , , Makefile .

$(TARGETS) , match-anything. $(TARGETS) , , .

+4

All Articles