Makefile snippets

Quick Makefile reference

Special macros

  • $@ The name of the target
  • $? List of dependents more recent than the target
  • $^ All the dependencies of the target
  • $+ Like $^, but keeping duplicates and in order of apparence
  • $< Only the first dependency; safer than $? when you have only one dependency that needs to be on the command
  • $* The matched part in wildcard targets. Eg, if the target label is %.c: and you call it with myfile.c, the value for $* will be myfile.

Building a bunch of files

This is an example standard Makefile I use to build UI files from Qt Designer into Python modules.

# Build the UIs

ui_files = \ \ \ \
rc_files =

.PHONY : all
.PHONY : clean clean_ui clean_rc clean_pyc

all : ui rc

ui: $(ui_files)
rc: $(rc_files)

$(ui_files): : uis/%.ui
    @## Create UI file
    pyuic4 -x $< -o $@
    @## Fix wrong import
    @# sed "s/^import resources_rc$$/from gui import resources_rc/" -i $@

$(rc_files): : %.qrc
    pyrcc4 -compress 9 $< -o $@

clean: clean_pyc clean_ui clean_rc

    rm -f $(ui_files)

    rm -f $(rc_files)

    rm -f *.pyc

Automatic targets list

In this example case, we have an indefinite bunch of .aa files, to be built in .bb files. To avoid writing the whole list of output .bb files, we can use the following method:

# "Build" .aa files into .bb files (just prefixes each line with '>>> ')

LIBOBJECTS := $(patsubst %.aa,,$(wildcard src/*.aa))

all: $(LIBOBJECTS) %.aa
        @# Build the .aa file into .bb file
        cat $< | sed 's/^/>>> /' > $@

        rm $(LIBOBJECTS)

Exporting PNGs from a single SVG file

## Makefile to build the single images out of the big SVG

INKSCAPE=inkscape -z
OUTPUT_IMAGES=pan.png zoom.png

.PHONY: all controls clean-elements

all: controls

clean: clean-controls

controls: $(OUTPUT_IMAGES)


pan.png: $(SOURCE_IMAGE)
    $(INKSCAPE) --file=$< --export-png=$@ --export-area=0:350:50:400

zoom.png: $(SOURCE_IMAGE)
    $(INKSCAPE) --file=$< --export-png=$@ --export-area=50:350:75:400
comments powered by Disqus