Skip to content

Instantly share code, notes, and snippets.

Last active August 27, 2021 17:03
What would you like to do?
A Cheat Sheet for a bunch of the stuff we did I my Makefile talk
make # runs the first target in the makefile named Makefile
make -f MyMakefile # Runs the first target in the Makefile named MyMakefile
make -f MyMakefile app # Runs the target called app
make -f MyMakefile app --dry-run # Does a dry run of the target called app
make -f MyMakefile app --debug=v --dry-run # Turns on verbose debugging for a command
# Sending parameters at the end of a command
# In this case the parameter is not set with ?= in the Makefile
make <task-name> VAR_NAME=value
# I haven't tried this bugt I don't want to lose thie idea, a self documenting Makefile
# When I try it I'll update this, hopefully.
# Basic Syntax
target: dependency dependency2 ...
# Note that those must BE TABs
# Setting variables
variable_name = "blah"
# Expanding variables
target: $(variable_name)
# Remember in Make everything is just text
# Settting Variables that can be overridden at the command line
VARIABLE_NAME_2 ?= "blah blah"
# Overriding at the command line is then done by
# VARIABLE_NAME_2 = "not blah" make
# Finding files in a path with wildcard
files = $(wildcard path)
# Phony targets (ergo - tasks)
.PHONY: taskname
# Ignoring errors in commands
# Add a dash before each line
-rm filename
-rm filename
# Testing variable presence
test -n $(VARNAME)
# To Add
# := vs = vs ?=
# recursive search (wildcard doesn't do it)
# The automatic variables
Copy link

From Chris Wilson TIL:

Makefile recipe for checking that environment variables are present before attempting to run a rule. You can add additional variables to the check-env rule.

SOURCES := ...

all: check-env $(SOURCES)
	@ echo "do stuff"

check-env: guard-ENV_VAR_1 guard-ENV_VAR_2

# ... other rules ...

guard-%: GUARD
	@ if [ -z '${${*}}' ]; then echo 'Environment variable $* not set.' && exit 1; fi


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment