7

I need to create a special makefile rule, which is best explained by an example. Maybe we create files with the rules

%_test.pdf: %.tex
    pdflatex -jobname=%_test %.tex

%_result.pdf: %.tex
    pdflatex -jobname=%_result %.tex

and it is working fine. Just thinking there occur more templates like those above, one might think of one wildcard-rule like

%_WILDCARD.pdf: %.tex
    pdflatex -jobname=%_$(WILDCARD) %.tex

where WILDCARD is determined by make. Is it possible to build such a rule?

Bastian Ebeling
  • 1,138
  • 11
  • 38

3 Answers3

4

Inspired by the answers of @eldar and @andres I think, I got the solution on myself

.SECONDEXPANSION:
%.pdf: $$(firstword $$(subst _, ,%))
    pdflatex -jobname=$* $+

This does exactly, what I needed. Detailed information for this way may be found at GNU make manual.

Bastian Ebeling
  • 1,138
  • 11
  • 38
1

Just merge your targets into a single rule as follows:

%_test.pdf %_result.pdf : %.tex
    pdflatex -jobname=$(basename $@) $<

UPD.

As Bastian said in comments this solution does not work for pattern rules.

Eldar Abusalimov
  • 24,387
  • 4
  • 67
  • 71
  • Thx @eldar-abusalimov ! Sounds interesting... but - this would tell make, that both files get created by one single run, or? Please think of only building _result.pdf... – Bastian Ebeling Jan 17 '12 at 15:10
  • @BastianEbeling No, this tells make that both targets have the same recipe and prerequisite pattern, nothing more. Each target will be built using its own command with it own values of `$@` and `$<`. It's ok to build only a single target. – Eldar Abusalimov Jan 17 '12 at 15:25
  • Okay @eldar-abusalimov , can you explain your proposition, which describes a different behaviour than given in [GNU make manual](http://www.gnu.org/software/make/manual/html_node/Pattern-Examples.html#Pattern-Examples) last paragraph? Further can I replaces those things following the underscore by a wildcard? – Bastian Ebeling Jan 17 '12 at 15:37
  • @BastianEbeling, well, I suggested this because of [Multiple Targets in a Rule](http://www.gnu.org/software/make/manual/make.html#Multiple-Targets) chapter. It seems that it doesn't apply to pattern rules according to the link you provided. – Eldar Abusalimov Jan 17 '12 at 17:11
  • @Beta, I just tried to make both `xxx_test.pdf` and `xxx_result.pdf` from `xxx.tex` and it builds only the first one. For the latter it says that `Nothing to be done for 'xxx_result.pdf'.` – Eldar Abusalimov Jan 17 '12 at 17:17
  • @BastianEbeling: It works. *Try it*. As for replacing `test` and `result` with a wildcard, short answer: no, you can't, long answer: yes, you can, sort of, but it will give you a makefile that is much more complicated and obscure, so it's almost certainly not worth the effort. – Beta Jan 17 '12 at 17:18
  • Sorry, we posted comments at the same time. It works when I try it. – Beta Jan 17 '12 at 17:20
  • @Beta, it seems strange, I tried too, and it does not work. First, I `touch xxx.tex`. Then using `%_test.pdf %_result.pdf : %.tex; touch $@` as Makefile I run `make xxx_test.pdf xxx_result.pdf`. The result of three consecutive runs is here: http://pastebin.com/PhrRbJzU – Eldar Abusalimov Jan 17 '12 at 17:40
1

What you are asking for is not trivial. You could probably get something that meets your needs, but it would take a bit of work.

Just to be clear, (Asuming it worked)

%_WILDCARD.pdf: %.tex
    pdflatex -jobname%_$(WILDCARD) %.tex

Would have a rule that for every .tex file, you would run pdflatex with WILDCARD as the job name. Thus you could type: make doc_test.pdf to get a pdf from the doc.tex file using the job "doc_test".

One way to get a similar behavior is to use:

# First get the names of all the .tex files
TEX_FILE_NAMES := $(wildcard *.tex)

#Find the names of the pdfs that could be made with those .tex files
# Use the wildcard for the job-name instead of the file-name
define PDF_Template
$(1)_%.pdf: $(1).tex
    paflatex -jobname=$$@ $$<
endef

$(foreach TEX_FILE,$(TEX_FILE_NAMES),$(eval $(call PAF_TEMPLATE,$(basename $(TEX_FILE)))))

And run it with

make doc_test.pdf

I'll just go ahead and make the disclaimer that I haven't actually run it, so please excuse any typos.

Andres
  • 5,012
  • 3
  • 24
  • 36