Skip to content

Makefile

Variables

Spaces at the beginning are stripped away

variables

index

  • Simply Expanded
  • := historical GNU extension. Is not POSIX compliant, use ::= instead
EMAIL = "foo@example.com"
AUTHOR ::= foo <$(EMAIL)>
EMAIL = "bar@example.com"
# AUTHOR = foo <foo@example.com>

Recursively Expanded (=)

EMAIL = "foo@example.com"
AUTHOR = foo <$(EMAIL)>
EMAIL = "bar@example.com"
# AUTHOR = foo <bar@example.com>

Conditional Assignment

index

  • read from env
  • set it not defined
  • safe assignment
ENV ?= dev

Shell Assignment

FILES = $(shell ls)
# or
FILES != ls

Automatic Variables

index: magic variables

parent/foo.c: one two
    # Outputs target name: "parent/foo.c"
    echo $@

    # Outputs first prerequisite: `one`
    echo $<

    # Outputs all prerequisites: `one two`
    echo $^

    # Outputs all prerequisites newer than the target
    echo $?

    # Outputs target directory: parent/
    $(@D)

    # Outputs target filepath only: foo.c
    $(@F)

Automatic-Variables

Snippets

Enums

index: oneOf

ALLOWED_ENVS := dev prd
# Default env
ENV ?= dev

ifeq ($(filter $(ENV),$(ALLOWED_ENVS)),)
    $(error Invalid ENV '$(ENV)'. Must be one of: $(ALLOWED_ENVS))
endif

Options

make
  -e, --environment-overrides
  -s, --silent
  -j, --jobs=N   # parallel processing

Resources