Simple solutions for common Makefile challenges.
all: install
install: run_steps_in_order
run_steps_in_order: first second last
first : ; @echo first
second: ; @echo second
last : ; @echo last
The common misconception about the above Makefile code is that executing make install will run first target, then second target and then last target.
In parallel execution mode, e.g. make -j4 install, targets first, second and last are all run in parallel.
all: install
install: _last
_last: _second
_second: _first
first _first: ; @echo first
second _second: ; @echo second
last _last: ; @echo last
In the example above a separate dependency path is created: install requires _last which requires _second which requires _first.
The recipie for _first ("@echo first") is shared with first target, the recipie _second is shared with second target and so on.
Executing make install or make -j4 install will first run recipie for _first, then recipie for _second, then recipie for _last.
all: install
@install = first second last
include chains.mk
install: @install
first first@install: ; @echo first
second second@install: ; @echo second
last last@install: ; @echo last
Usage:
$ make -j4 install
first
second
last
$ make -j4 second
second
$
all: install
@install = first second last
@selected = last first
include chains.mk
install: @install
selected: @selected
first first@install first@selected: ; @echo first
second second@install : ; @echo second
last last@install last@selected: ; @echo last
Example:
$ make -j4 install
first
second
last
$ make -j4 selected
last
first
$
- Simple syntactic sugar for ordered execution (KISS)
- Keeps Makefiles tidy
- Easy target reordering
- Define
@chain1 = step1 step2 step3 include chains.mk- Define
my_target: @chain1 - Insert additonal target name
step1@chain1for recipie1,step2@chain1for recipie2 and so on.
all: install
install: unpack patch restart
unpack:
tar -zxf archive.tgz
patch:
patch < archive.path
restart:
systemctl restart service1
all: install
@install = unpack patch restart
include chains.mk
install: @install
unpack unpack@install:
tar -zxf archive.tgz
patch patch@install:
patch < archive.path
restart restart@install:
systemctl restart service1