Make Reference

Kip Landergren

(Updated: )

My cheat sheet for Make covering resources and examples.

Contents

Refresher

Think of Make rules as the files that it should produce: list the prerequisite files (which themselves may be rules) and then the recipe performs the commands.

Helpful Options

-f --file / --makefile specify the name of the makefile
-n --dry-run / --just-print / --recon print the commands that would be executed (but do not execute them)
-p --print-database print the database that results from reading the makefiles, and then execute as normal
-k --keep-going continue to consider prerequisites of the pending targets before it gives up

Rules

TARGET ... : PREREQUISITES ...
        RECIPE
        ...
        ...

Preferences

Resources

Make Terminology

goal
the targets make should strive to update
makefile
a file that contains rules which tell make what to do
phony target
a target that does not generate a file of the same name
prerequisite
a file that is used as input to create the target
recipe
the action—e.g. commands—that make carries out for a rule
rule
the collection of information used to determine when and how to remake certain files
target
the name of a file generated by the rule, except in special cases like phony targets (e.g. the executable or object files)

make-study

README.md

# make-study

Study area for [Make](https://www.gnu.org/software/make/)

examples/basic/Makefile

# default shell is `/bin/sh`
SHELL := /bin/bash

# the first rule will be run if `make` is invoked without arguments

# the target name of `myfile.txt` is chosen because its recipe actually creates
# an output at the path `./myfile.txt`. `make` will use the file's last
# modified timestamp, compared to its prerequisites, to determine if it needs
# to run

# targets `dep1.txt` and `dep2.txt` are specified as prerequisites of
# `myfile.txt`. note that the recipe starts with a TAB character.
myfile.txt: dep1.txt dep2.txt
	cat dep1.txt dep2.txt > myfile.txt

# alternatively could use `printf` instead of `echo` to be more portable
dep1.txt:
	echo -n 'hello' > dep1.txt

dep2.txt:
	echo ' world' > dep2.txt

mydir/foo.txt:
	mkdir mydir
	# each command effectively executes in a new shell—I don't know more
	# details on this—and so chaining path-dependent commands is important
	cd mydir && echo 'foo' > foo.txt

# .PHONY is used because there is no file named `clean` created by this
# target. If one were created by an unrelated process `make` could
# inadvertently not run (because it sees that file `clean` already exists)
.PHONY: clean
clean:
	rm dep1.txt
	rm dep2.txt
	rm myfile.txt
	rm -rf ./mydir

# use of `wildcard`
.PHONY: print
print: $(wildcard *.txt)
	ls -la $?