TExcelTAD
Home › GNU Make & Ninja › Make Functions

Make Functions

Make has a small functional language for computing lists of words: find the sources, turn them into object names, add directories, filter by suffix. A function call looks like $(name arg1,arg2,…). Make treats every value as a space-separated list of words, and most functions work word by word.

Note: Whitespace after a comma can become part of the argument ($(subst a, b,…) replaces with b, a leading space included). Write arguments without spaces: $(patsubst %.c,%.o,$(SRCS)).

The everyday functions

Function Does Example → result
$(wildcard pattern…) existing files matching shell globs $(wildcard src/*.c) → src/a.c src/b.c
$(patsubst pat,repl,text) replace words matching pat (% = stem) $(patsubst src/%.c,build/%.o,src/a.c) → build/a.o
$(VAR:.c=.o) substitution reference, shorthand for a suffix patsubst $(SRCS:.c=.o)
$(filter pat…,text) / $(filter-out …) keep / drop words matching a pattern $(filter %.c,a.c b.h) → a.c
$(sort list) sort and remove duplicates $(sort b a b) → a b
$(dir …), $(notdir …) directory part / file part $(notdir src/a.c) → a.c
$(basename …), $(suffix …) drop / keep the extension $(basename a.c) → a
$(addprefix p,…), $(addsuffix s,…) add to every word $(addprefix -I,inc lib) → -Iinc -Ilib
$(subst from,to,text) plain text replacement $(subst -,_,a-b) → a_b
$(words …), $(word n,…), $(firstword …) count / pick words $(words a b c) → 3
$(foreach v,list,text) expand text once per word, with $(v) set see below
$(call fn,arg1,…) expand a user-defined variable with $(1), $(2) set see below
$(shell command) run a shell command, newlines become spaces $(shell git rev-parse --short HEAD)
$(if c,then,else), $(or …), $(and …) conditionals on empty / non-empty strings $(if $(DEBUG),-g,-O2)

A tour on a real tree

This session creates a small source tree and prints what each function computes. Nothing is compiled; the $(info) lines run while Make reads the file:

bash · try it yourself
mkdir -p proj/src/net proj/src/util && cd proj
touch src/main.c src/net/http.c src/net/tcp.c src/util/log.c src/util/log.h src/util/old_test.c
cat > Makefile <<'MK'
SRCS := $(sort $(wildcard src/*.c src/*/*.c))
$(info SRCS    = $(SRCS))

SRCS := $(filter-out %_test.c,$(SRCS))
$(info no test = $(SRCS))

OBJS := $(patsubst src/%.c,build/%.o,$(SRCS))
$(info OBJS    = $(OBJS))

DEPS := $(OBJS:.o=.d)
$(info DEPS    = $(DEPS))

DIRS := $(sort $(dir $(OBJS)))
$(info DIRS    = $(DIRS))

MODULES := net util
INCS := $(addprefix -Isrc/,$(MODULES))
$(info INCS    = $(INCS))

$(foreach m,$(MODULES),$(info module $(m) has $(words $(wildcard src/$(m)/*.c)) .c files))

COUNT != ls src/net | wc -l
$(info shell   = $(shell echo $(words $(SRCS)) sources) / $(COUNT) files in net)
all: ;
MK
make -j1
SRCS    = src/main.c src/net/http.c src/net/tcp.c src/util/log.c src/util/old_test.c
no test = src/main.c src/net/http.c src/net/tcp.c src/util/log.c
OBJS    = build/main.o build/net/http.o build/net/tcp.o build/util/log.o
DEPS    = build/main.d build/net/http.d build/net/tcp.d build/util/log.d
DIRS    = build/ build/net/ build/util/
INCS    = -Isrc/net -Isrc/util
module net has 2 .c files
module util has 2 .c files
shell   = 4 sources / 2 files in net
make: 'all' is up to date.

Three habits shown here:

  • $(sort $(wildcard …)): the order of wildcard results comes from the file system. Sorting makes the object list, and therefore the link order and the binary, the same on every machine.
  • wildcard doesn't recurse. src/*.c src/*/*.c covers two levels; for deeper trees use $(shell find src -name '*.c') or list directories explicitly.
  • Assigning with := makes each function run once.

Some projects list sources explicitly instead of globbing. A stray .c file (a backup, a half-written experiment) then can't silently join the build, and adding a file is a visible change in review. Both styles are common; pick one per project.

User-defined functions with call

A recursive variable that mentions $(1), $(2)… is a function. $(call name,a,b) expands it with the arguments in place:

bash · try it yourself
mkdir -p fn && cd fn
cat > Makefile <<'MK'
# $(call objs-for,MODULE) -> object files of one module
objs-for = $(patsubst %.c,build/%.o,$(addprefix $(1)/,a.c b.c))

# $(call banner,TEXT,WIDTH) -> TEXT padded with dashes
banner = $(1) $(subst x,-,$(wordlist 1,$(2),x x x x x x x x x x))

$(info $(call objs-for,net))
$(info $(call objs-for,ui))
$(info $(call banner,build,4))
$(info $(if $(filter release,$(MODE)),optimised,debug) build)
$(info $(or $(CC_OVERRIDE),$(CC)) is the compiler)
all: ;
MK
make -j1
echo '--- MODE=release CC_OVERRIDE=clang:'
make -j1 MODE=release CC_OVERRIDE=clang | sed -n '4,5p'
build/net/a.o build/net/b.o
build/ui/a.o build/ui/b.o
build - - - -
debug build
cc is the compiler
make: 'all' is up to date.
--- MODE=release CC_OVERRIDE=clang:
optimised build
clang is the compiler

call plus foreach plus eval (which parses a string as Makefile text) is how large Makefiles generate one block of rules per module. That's powerful and hard to debug, so use it only when plain pattern rules can't express what you need. Recursive vs Non-recursive Make shows a simpler way to organise modules.

Test yourself

What does $(SRCS:.c=.o) do?

  1. Deletes .c files
  2. Replaces the .c suffix of each word with .o
  3. Compiles the sources
  4. Filters out non-C files

Why wrap $(wildcard src/*.c) in $(sort ...)?

  1. wildcard fails without it
  2. To get the same, deterministic order on every machine
  3. To remove .h files
  4. To make it recursive

Inside a function used with $(call f,x,y), how do you refer to y?

  1. $y
  2. $(2)
  3. lt;
  4. $(arg2)