Pass arguments to Makefile

Asked

Viewed 579 times

7

I have a folder with a series 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 he compiled only the file arquivo_004.tex.

I found out you use $@ inside 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.

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

Is there any way to take that second argument? What I want to do must be done differently?

  • It seems to me that make considers everything that is not an attribute like Goal. What if you used attributes like make GDK="004", for example, and ${GDK} ?

  • I do not know if it is a real problem or canonical question, however here must have some good reference for those who answer: http://stackoverflow.com/questions/2214575/

  • @Bacco I had seen around that maybe the way was to do something like make file="004" but it seemed more complicated than it could be.

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, run make 003 will display: "Produce arquivo_003.pdf from arquivo_003.tex".
You can also call in chain, for example: make 003 004 005.

Note, however, that it is necessary have at least one command line in the rule used to redirect, otherwise it is ignored. I used a comment there with deleted output, 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 be produced. Thus:

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.

Without next, given a variable PRODUCE, 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 is a mere matter of making the default rule produce the pdfs. So:

all: $(PRODUCE_PDF)

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

If you invoke make without arguments, will produce all pdfs. You can specify so: make PRODUCE=005. Or else: make PRODUCE="005 006 002".

  • Not only solved the problem but also helped explain a lot of other doubts I had reading a example Makefile that I found

Browser other questions tagged

You are not signed in. Login or sign up in order to post.