Makefile patternrule with additional wildcards in the target file name

I need to create a special makefile rule that is best explained with an example. Perhaps we are creating rules files

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

and it works fine. Just thinking that there are more templates like the ones above, you can think of one wildcard rule, for example

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

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

+6
source share
3 answers

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

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

This is exactly what I need. Detailed information on this path can be found in the GNU make manual .

+4
source

Just combine your goals into one rule as follows:

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

UPD

As Bastian said in the comments, this solution does not work for template rules.

+1
source

What you ask for is not trivial. You can probably get something that suits your needs, but it takes a bit of work.

Just to be clear, (Assuming this worked)

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

It would be a rule that for each .tex file you will run pdflatex with WILDCARD as the job name. So you can type: make doc_test.pdf to get the pdf file from the doc.tex file using the doc_test job.

One way to get this 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 just go ahead and make a disclaimer that I didn’t actually run it, so please excuse any typos.

+1
source

Source: https://habr.com/ru/post/906207/


All Articles