TExcelTAD
Home › GNU Make & Ninja › Conditionals, include & define

Conditionals, include & define

Three directives let one Makefile adapt and grow: conditionals choose lines depending on variables, include reads other makefiles, and define stores multi-line text such as a canned recipe. All three are processed while Make reads the Makefile, before any rule runs.

Conditionals

Directive True when
ifeq (a,b) the two expanded strings are equal
ifneq (a,b) they differ
ifdef VAR VAR has a non-empty value (it isn't expanded further)
ifndef VAR VAR is undefined or empty

Each block ends with endif, optionally with else (and else ifeq … chains). Conditional lines must not start with a TAB, or Make takes them for recipe lines; indent them with spaces if you indent them at all.

bash · try it yourself
mkdir -p cond && cd cond
printf '#include <stdio.h>\nint main(void) {\n#ifdef NDEBUG\n  puts("release build");\n#else\n  puts("debug build");\n#endif\n  return 0;\n}\n' > app.c
cat > Makefile <<'MK'
BUILD ?= debug
UNAME := $(shell uname -s)

ifeq ($(BUILD),release)
  CFLAGS := -O2 -DNDEBUG
else ifeq ($(BUILD),debug)
  CFLAGS := -O0 -g
else
  $(error unknown BUILD '$(BUILD)')
endif

ifeq ($(UNAME),Linux)
  LDLIBS += -lm
endif

ifdef VERBOSE
  Q :=
else
  Q := @
endif

app: app.c
	@echo "CC $@ ($(BUILD))"
	$(Q)$(CC) $(CFLAGS) 
lt; -o $@ $(LDLIBS) MK make -j1 && ./app rm app make -j1 BUILD=release VERBOSE=1 && ./app
CC app (debug)
debug build
CC app (release)
cc -O2 -DNDEBUG app.c -o app -lm
release build

The Q/VERBOSE trick gives quiet output by default and full commands on demand. Many projects (the Linux kernel uses V=1) do the same.

ifeq compares text after expansion, so a value with trailing spaces is not equal to one without. $(strip …) helps when values come from other files.

include

include file… reads other makefiles at that point, as if pasted in. Typical uses:

-include (alias sinclude) does the same but stays silent if a file doesn't exist. And there's a twist: if an included file is missing or out of date and some rule can create it, Make builds it, then restarts and reads everything again:

bash · try it yourself
mkdir -p inc && cd inc
cat > Makefile <<'MK'
include config.mk
$(info VERSION is $(VERSION), CC is $(CC))

config.mk: VERSION.txt
	@echo "generating $@"
	@printf 'VERSION := %s\nCC := gcc\n' "$(cat 
lt;)" > $@ all: ; @echo "building $(VERSION)" .PHONY: all MK echo 1.4.0 > VERSION.txt make -j1 all echo "--- again:" make -j1 all echo "--- after a version bump:" sleep 1 && echo 1.5.0 > VERSION.txt make -j1 all
VERSION is , CC is cc
generating config.mk
VERSION is 1.4.0, CC is gcc
building 1.4.0
--- again:
VERSION is 1.4.0, CC is gcc
building 1.4.0
--- after a version bump:
VERSION is 1.4.0, CC is gcc
generating config.mk
VERSION is 1.5.0, CC is gcc
building 1.5.0

Look at the last run: Make read the old config.mk (first $(info) line), noticed it was older than VERSION.txt, rebuilt it, restarted, and read the new values. The first run shows the same two passes: the first read happened without config.mk (empty VERSION, the default CC), and GNU Make 4.3 didn't complain about the missing file because a rule could build it.

Because of this restart, avoid making included files depend on things that change on every run: the Makefile would restart forever.

define: multi-line variables

define NAME … endef assigns text containing newlines. Its best use is a canned recipe, a sequence of commands used by several rules:

bash · try it yourself
mkdir -p canned && cd canned
printf 'one\n' > a.txt
printf 'two\n' > b.txt
cat > Makefile <<'MK'
define stamp-and-copy
@echo "[$@] from 
lt;" @mkdir -p $(@D) @cp
lt; $@ @echo "# built from
lt;" >> $@ endef all: out/a.txt out/b.txt out/%.txt: %.txt $(stamp-and-copy) MK make -j1 cat out/b.txt
[out/a.txt] from a.txt
[out/b.txt] from b.txt
two
# built from b.txt

Each line of the expanded define becomes its own recipe line (with its own shell), exactly as if you had typed them under the rule.

Test yourself

Why must ifeq/endif lines not begin with a TAB?

  1. They would be ignored
  2. A leading TAB makes Make treat the line as a recipe line
  3. TABs are illegal in Makefiles
  4. They would be run twice

What is the difference between include and -include?

  1. -include reads the file twice
  2. -include silently ignores files that don't exist and can't be made
  3. -include only reads .d files
  4. There is none

An included makefile is out of date and a rule can rebuild it. What does GNU Make do?

  1. Ignores the rule
  2. Rebuilds it, then restarts and re-reads all makefiles
  3. Fails with an error
  4. Uses the old file and rebuilds it at the end