Program development

Make

Make

The make utility automatically determines
which pieces of a large program need to be
recompiled, and issues commands to recompile
them.

Invoking make

Makefiles

target: [prerequisites]
 - TAB - action
 - TAB - action
 - TAB - ...

Targets

Prerequisites

Actions

Actions

Rules must consist of consecutive lines that start with a TAB character.

Actions

Since these are usually interpreted as shell commands, you can do things such as multi-lines (but use the backslash to make sure that the “single-linedness” of your construction is clear):

for name in dir1 dir2 dir3 \
do ; \
  ${MAKE} $name ; \
done

Actions

ifeq (ARG1,ARG2)
...
endif
ifdef (ARG1)
...
endif

Setting ordinary variables

Pattern rules

One of the nice things that you can do with make is create “pattern rules”.

These are rules that let you abstract a pattern from a set of similar rules, and use that pattern in lieu of explicitly naming all of those rules.

Pattern rules

For instance:

%.o : %.c
    cc -c $< -o $@

#  $@ refers to the target
#  $< refers to the *first*
#   (and only) prerequisite

Automatic variables

$@   # the target of the rule
$<   # the first prerequisite
$^   # all of the prerequisites
$?   # all of the prerequisites that 
     # are newer than the target file
$*   # the ‘‘stem’’ only; essentially, 
     # this is the complement of the static 
     # portion of the target definition

Example Makefile

pandoc_options=--latexmathml --latex-engine=xelatex

targets = 1.html 1.pdf  2.html 2.pdf 3.html 3.pdf \
 4.html 4.pdf 5.html 5.pdf 6.html 6.pdf \
 14-programdev.html 14-programdev.pdf \
 15-makefiles.html 15-makefiles.pdf

all : $(targets) $(pictures)

Example Makefile

%.html : %.txt
    pandoc $(pandoc_options) --ascii -t \
        dzslides+pipe_tables -s $< -o $@
%.tex : %.txt
    pandoc -t beamer --ascii -s $< -o $@
%.pdf : %.tex
    pdflatex $< 
%.png : %.dot
    dot -Tpng $< > $@

Common targets

It is common to such default targets such as: