Pass arguments to the makefile

7

I have a folder with a number of LaTeX files:

arquivo_001.tex
arquivo_002.tex
arquivo_003.tex
arquivo_004.tex

My idea is to write a makefile that allows me to do something like make 004 and it compiles only the arquivo_004.tex file.

I found that using $@ inside the makefile uses the first argument, so I tried a makefile containing

all:
    pdflatex [email protected]

But I get:

make: *** No rule to make target '10'.  Stop.

What makes all sense. So I tried to call make all 10 , but then pdflatex does not find the file arquivo_all.tex , which also makes sense.

Is there any way to get this second argument? What do I want to do in a different way?

    
asked by anonymous 21.06.2014 / 07:14

1 answer

6

You can use a rule that is just a pattern to redirect to some other rule. So it will capture anything that comes from the command line. Note:

%: arquivo_%.pdf
    @# empty line

arquivo_%.pdf: arquivo_%.tex
    @echo Produce $@ from $^

Having this, running make 003 will display: "Produce file_003.pdf from file_003.tex" . You can also call a string, for example: make 003 004 005 .

Note, however, that need to have at least one command line in the rule used to redirect, otherwise it is ignored. I used a suppressed comment there, equivalent to a noop.

Still another option is to use a variable whose default value is to produce everything. This solution is much cleaner in reality. First define a variable whose value is the numbers to produce. So:

PRODUCE = $(patsubst arquivo_%.tex,%,$(wildcard arquivo_*.tex))

If you have the files arquivo_001.tex and arquivo_003.tex in your directory, then PRODUCE=001 003 .

Next, given a PRODUCE variable, calculate the name of the target files, pdfs:

PRODUCE_PDF = $(addprefix arquivo_,$(addsuffix .pdf,$(PRODUCE)))

If PRODUCE=001 003 , then PRODUCE_PDR=arquivo_001.pdf arquivo_003.pdf .

Now it's a simple matter of making the default rule produce the pdfs. So:

all: $(PRODUCE_PDF)

arquivo_%.pdf: arquivo_%.tex
    @echo Produce $@ from $^

If you invoke make with no arguments, it will produce all pdfs. You can specify: make PRODUCE=005 . Or even: make PRODUCE="005 006 002" .

    
21.06.2014 / 14:47