OUT := $(subst src/,build/,$(SRC))Useful for path rewrites and simple string replacement.
GNU make text, path, conditional, shell, and metaprogramming functions with practical examples.
Manipulate words, whitespace, patterns, and filenames.
OUT := $(subst src/,build/,$(SRC))Useful for path rewrites and simple string replacement.
OBJ := $(patsubst %.c,%.o,$(SRC))A core building block in many makefiles.
Trim extra leading, trailing, and repeated internal whitespace.
NAME := $(strip $(NAME))Useful before equality tests or diagnostics.
HAS_TEST := $(findstring test,$(MAKECMDGOALS))A simple way to check for word presence.
FIRST := $(word 1,$(FILES))
REST := $(wordlist 2,$(words $(FILES)),$(FILES))Handy when manipulating word lists.
CURRENT_MK := $(lastword $(MAKEFILE_LIST))A common pattern when introspecting included makefiles.
PAIRS := $(join a b c,.o .d .map)Less common, but useful for generated file lists.
BINS := $(addprefix dist/,$(TARGETS))
OBJ := $(addsuffix .o,$(NAMES))Very common in build list generation.
DIRS := $(dir $(FILES))
BASE := $(basename $(FILES))Useful for path manipulation without shelling out.
ROOT := $(realpath .)
CACHE := $(abspath .cache)`realpath` resolves symlinks when possible; `abspath` just normalizes.
Inline branching without parser-level `ifeq`.
MODE_MSG := $(if $(CI),ci-mode,local-mode)Good when you need expression-like branching inside variables.
PORT := $(or $(APP_PORT),3000)Useful for defaults and override chains.
READY := $(and $(DB_URL),$(API_KEY),yes)Handy for gating optional behavior.
RESULT := $(intcmp $(JOBS),8,lt,eq,gt)Useful when you need numeric branching purely inside make.
The metaprogramming toolbox for large makefiles.
GIT_SHA := $(shell git rev-parse --short HEAD)Use sparingly; every call runs during parsing.
Invoke a user-defined macro like a function.
define banner
$(info === $(1) ===)
endef
$(call banner,Deploying API)`$(1)`, `$(2)`, and so on are positional arguments inside `define` blocks and recursive vars.
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))))`eval` lets make parse generated text as makefile syntax.
RAW_RULE := $(value SOME_TEMPLATE)Useful when working with `eval` and templates.
$(file >build/version.txt,$(VERSION))GNU make can create or append files during parsing or expansion.
$(warning building with DEBUG=$(DEBUG))
$(if $(DB_URL),,$(error DB_URL is required))Great for fail-fast validation of required inputs.
$(info Sources: $(SRC))Helpful when debugging generated variables and includes.
Practical patterns built from core functions.
Extract target comments from the current makefile.
.PHONY: help
help:
@grep -E '^[a-zA-Z0-9_.-]+:.*?## ' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "%-24s %s
", $$1, $$2}'Popular pattern for `make help`.
$(info goals=$(MAKECMDGOALS))`MAKECMDGOALS` contains explicit command-line goals.
Get the most recently parsed makefile path.
CURRENT_MK := $(lastword $(MAKEFILE_LIST))A practical use of `MAKEFILE_LIST`.
empty :=
space := $(empty) $(empty)
comma := ,Useful in advanced text generation scenarios.