Makefile Functions & Text Processing

GNU make text, path, conditional, shell, and metaprogramming functions with practical examples.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Text and list functions
subst replace text
OUT := $(subst src/,build/,$(SRC))

# Replace every occurrence of one string with another.

patsubst pattern replacement
OBJ := $(patsubst %.c,%.o,$(SRC))

# Replace words matching a `%` pattern.

strip whitespace
NAME := $(strip $(NAME))

# Trim extra leading, trailing, and repeated internal whitespace.

findstring substring search
HAS_TEST := $(findstring test,$(MAKECMDGOALS))

# Return the search string if found.

word and wordlist
FIRST := $(word 1,$(FILES))
REST := $(wordlist 2,$(words $(FILES)),$(FILES))

# Extract words by position.

firstword and lastword
CURRENT_MK := $(lastword $(MAKEFILE_LIST))

# Grab the first or last word of a list.

join lists
PAIRS := $(join a b c,.o .d .map)

# Concatenate corresponding words from two lists.

addprefix and addsuffix
BINS := $(addprefix dist/,$(TARGETS))
OBJ := $(addsuffix .o,$(NAMES))

# Add text to every word in a list.

dir, notdir, basename, suffix
DIRS := $(dir $(FILES))
BASE := $(basename $(FILES))

# Split and recombine filenames.

abspath and realpath
ROOT := $(realpath .)
CACHE := $(abspath .cache)

# Normalize paths.

## Conditional and boolean-like functions
if function
MODE_MSG := $(if $(CI),ci-mode,local-mode)

# Select one of two expansions based on non-empty condition text.

or function
PORT := $(or $(APP_PORT),3000)

# Return the first non-empty argument.

and function
READY := $(and $(DB_URL),$(API_KEY),yes)

# Return the last argument if all are non-empty.

intcmp function
RESULT := $(intcmp $(JOBS),8,lt,eq,gt)

# Compare integers in GNU make 4.4+.

## Shell, call, eval, and generated rules
shell function
GIT_SHA := $(shell git rev-parse --short HEAD)

# Capture shell command output at parse time.

call macro with arguments
define banner
$(info === $(1) ===)
endef

$(call banner,Deploying API)

# Invoke a user-defined macro like a function.

eval generated rules
define make-test-target
.PHONY: test-$(1)
test-$(1):
  pytest -q tests/$(1)
endef

$(foreach p,unit integration e2e,$(eval $(call make-test-target,$(p))))

# Generate new rules dynamically.

value raw variable text
RAW_RULE := $(value SOME_TEMPLATE)

# Read a variable without expanding it.

file function
$(file >build/version.txt,$(VERSION))

# Write content to a file from make.

warning and error functions
$(warning building with DEBUG=$(DEBUG))
$(if $(DB_URL),,$(error DB_URL is required))

# Emit diagnostics or abort parsing.

info tracing
$(info Sources: $(SRC))

# Print information during parsing.

## Useful function recipes
Self-documenting help target
.PHONY: help
help:
  @grep -E '^[a-zA-Z0-9_.-]+:.*?## ' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "%-24s %s
", $$1, $$2}'

# Extract target comments from the current makefile.

Use MAKECMDGOALS
$(info goals=$(MAKECMDGOALS))

# Inspect requested goals.

Current makefile name
CURRENT_MK := $(lastword $(MAKEFILE_LIST))

# Get the most recently parsed makefile path.

Define whitespace helpers
empty :=
space := $(empty) $(empty)
comma := ,

# Create reusable space or comma variables.

Recommended next

No recommendations yet.