Skip to content

Instantly share code, notes, and snippets.

@jaymecd

jaymecd/Makefile

Last active Mar 18, 2020
Embed
What would you like to do?
Default Makefile with envvar guard
MAKEFLAGS += --warn-undefined-variables
SHELL := bash -o pipefail -c
.DEFAULT_GOAL := help
.PHONY: help all deps build
guardEnvVar = $(if $(value $(1)),,$(error env $(1) not defined))
# Note: help extracts title from ## comment just above the target, builds target/title grid and prints it pretty.
## Show help
help:
@ echo 'Usage: make <target>'
@ echo
@ echo 'Available targets are:'
@ awk '/^[[:alnum:]]+([\.\-_][[:alnum:]]*)*:/ \
{if (match(line, /^## (.*)/)) { \
printf " %s,%s\n", substr($$1, 0, index($$1, ":")-1), substr(line, RSTART + 3, RLENGTH); \
}} { line = $$0 }' $(MAKEFILE_LIST) | sort | column -t -s,
@ echo
## Install and build
all: deps build
# $@: done
## Install dependencies
deps:
# $@
echo "installing dependencies ..."
# $@: done
## Build package / X_VERSION= X_SCOPE=
build:
$(call guardEnvVar,X_VERSION)
$(call guardEnvVar,X_SCOPE)
# $@
echo "building $(X_VERSION) for $(X_SCOPE) scope ..."
# $@: done

Default Makefile with envvar guard

show generated help message:

$ make
Usage: make <target>

Available targets are:
    all    Install and build
    build  Build package / X_VERSION= X_SCOPE=
    deps   Install dependencies
    help   Show help

show error if envvar is not provided:

$ make all
# deps
echo "installing dependencies ..."
installing dependencies ...
# deps: done
Makefile:32: *** env X_VERSION not defined.  Stop

successful invocation:

$ X_VERSION=v1.2.3 X_SCOPE=private make all
# deps
echo "installing dependencies ..."
installing dependencies ...
# deps: done
# build
echo "building v1.2.3 for private scope ..."
building v1.2.3 for private scope ...
# build: done
# all: done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.