Skip to content

Instantly share code, notes, and snippets.

@gmolveau
Last active June 8, 2022 13:42
Show Gist options
  • Save gmolveau/a6331c21e8973f4a1ea51185096e37b8 to your computer and use it in GitHub Desktop.
Save gmolveau/a6331c21e8973f4a1ea51185096e37b8 to your computer and use it in GitHub Desktop.
makefile example best practices / opinionated from https://tech.davis-hansson.com/p/make/
SHELL := bash
.ONESHELL:
.SHELLFLAGS := -eu -o pipefail -c
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
ifeq ($(origin .RECIPEPREFIX), undefined)
$(error This Make does not support .RECIPEPREFIX. Please use GNU Make 4.0 or later)
endif
.RECIPEPREFIX = >
.PHONY: help
.DEFAULT: help
help: ## show the usage
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk -F ':.*?## ' 'NF==2 {printf "%-35s %s\n", $$1, $$2}'
.PHONY: build
build: out/image-id ## build it
.PHONY: clean
clean: ## Clean up the output directories; since all the sentinel files go under tmp, this will cause everything to get rebuilt
> rm -rf tmp
> rm -rf out
tmp/.tests-passed.sentinel: $(shell find src -type f) ## Tests - re-ran if any file under src has been changed since tmp/.tests-passed.sentinel was last touched
> mkdir -p $(@D)
> node run test
> touch $@
tmp/.packed.sentinel: tmp/.tests-passed.sentinel ## Webpack - re-built if the tests have been rebuilt (and so, by proxy, whenever the source files have changed)
> mkdir -p $(@D)
> webpack ..
> touch $@
out/image-id: tmp/.packed.sentinel ## Docker image - re-built if the webpack output has been rebuilt
> mkdir -p $(@D)
> image_id="example.com/my-app:$$(pwgen -1)"
> docker build --tag="$${image_id}
> echo "$${image_id}" > out/image-id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment