Skip to content

Instantly share code, notes, and snippets.

@paytonrules
Last active August 27, 2021 17:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paytonrules/9faaa54e1497f5dad7be12d7ee772c25 to your computer and use it in GitHub Desktop.
Save paytonrules/9faaa54e1497f5dad7be12d7ee772c25 to your computer and use it in GitHub Desktop.
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
# https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
# When I try it I'll update this, hopefully.
# Basic Syntax
target: dependency dependency2 ...
command1
command2
# 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
clean:
# Add a dash before each line
-rm filename
-rm filename
# Testing variable presence
check:
test -n $(VARNAME)
# To Add
# := vs = vs ?=
# recursive search (wildcard doesn't do it)
# The automatic variables
@paytonrules
Copy link
Author

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

.PHONY: GUARD
GUARD:

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