Adam Matan
Adam Matan

Reputation: 136231

Makefile: Run consecutive script only if the target was updated

Consider the following simple Makefile:

clean:
    rm *.log *.aux *.log *.pdf *.toc *.script *.dat

summary.pdf: summary.tex
    /Library/TeX/texbin/pdflatex --shell-escape -interaction=errorstopmode summary.tex

If summary.tex is unchanged, running make summary.pdf returns:

make: `summary.pdf' is up to date.

And error code 0.

However, I would like to display the created PDF only if it was created. The obvious choice would be make summary.pdf && open summary.pdf; however, since make returns 0 (which means success), the PDF viewer will open the existing PDF even if it hasn't been changed.

There are many ugly ways to script this, but I wonder if I'm missing any obvious choice.

How do I invoke a command only if make actually created a target?

Upvotes: 1

Views: 218

Answers (2)

blackghost
blackghost

Reputation: 1825

Assuming you don't want to add the view command to the makefile as @Alex suggested, you could move the decision to open the file outside of make as follows:

summary.pdf: summary.tex
    /Library/TeX/texbin/pdflatex --shell-escape -interaction=errorstopmode summary.tex
    @echo "UPDATED PDF FILE: $@"

And then call your makefile with the following script:

make | grep -q "^UPDATED PDF FILE: " && open file

or

for file in `make | sed -n "s/^UPDATED PDF FILE: \(.*\)$/\1/p"`; do
     open $file; 
done

for something more robust. (sorry, not familiar with OSx, but I'm assuming you can do similar things from there).

As a general rule, you don't want to change the return value of make, as it is well documented that make returning anything but 0 implies an error. This would confuse any scripts which rely on that behavior, and make your code non-portable.

Upvotes: 0

Alex Cohn
Alex Cohn

Reputation: 57163

The obvious way is to add the view command to recipe:

clean:
    rm *.log *.aux *.log *.pdf *.toc *.script *.dat

summary.pdf: summary.tex
    /Library/TeX/texbin/pdflatex --shell-escape -interaction=errorstopmode summary.tex
    open $@

Upvotes: 2

Related Questions