Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active February 13, 2024 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Integralist/4447885192c7e84e01ca7c9f2e08ef17 to your computer and use it in GitHub Desktop.
Save Integralist/4447885192c7e84e01ca7c9f2e08ef17 to your computer and use it in GitHub Desktop.
[Makefile help output] #make #makefile #help #docs
# IMPORTANT: The help target requires variables to have whitespace stripped.
# But only for variables whose line ends with comment docs (e.g. ## ...)
# To strip whitespace use `$(strip $(VARIABLE_NAME))`
# Alternative was worse, i.e. remove space between value and doc comment.
# Then updating the last line of the help target to account for closed space.
# returns any tag associated with the given HEAD commit.
tag_check: ## A
@if [[ ! $$(git tag --points-at $$(git rev-parse --short HEAD)) ]]; then echo "no git tag found on HEAD commit"; exit 1; fi
tag_delete: ## B
@git tag -d "$(VERSION)" && git push --delete origin "$(VERSION)"
tag_release: ## C
@git tag -s $(VERSION) -m "$(VERSION)" && git push origin $(VERSION)
help:
@printf "Targets\n"
@(grep -h -E '^[0-9a-zA-Z_.-]+:.*?## .*$$' $(MAKEFILE_LIST) || true) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-22s\033[0m %s\n", $$1, $$2}'
@printf "\nDefault target\n"
@printf "\033[36m%s\033[0m" $(.DEFAULT_GOAL)
@printf "\n\nMake Variables\n"
@(grep -h -E '^[0-9a-zA-Z_.-]+\s[:?]?=.*? ## .*$$' $(MAKEFILE_LIST) || true) | sort | awk 'BEGIN {FS = "[:?]?=.*?## "}; {printf "\033[36m%-25s\033[0m %s\n", $$1, $$2}'
# IF YOU NEED TO EXCLUDE TARGETS FROM A PARENT MAKEFILE!
include base.mk
# Regex pattern of targets to omit from the help output (`make h`).
OMIT_TARGETS := (help|lint):
.PHONY: h
h: ## Filtered version of `help` target from base.mk
@printf "Targets\n"
@grep -h -E '^[0-9a-zA-Z_.-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | grep -vE '$(OMIT_TARGETS)' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-22s\033[0m %s\n", $$1, $$2}'
@printf "\nDefault target\n"
@printf "\033[36m%s\033[0m" $(.DEFAULT_GOAL)
@printf "\n\nMake Variables\n"
@grep -h -E '^[0-9a-zA-Z_.-]+\s:=.*?## .*$$' $(MAKEFILE_LIST) || true | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-25s\033[0m %s\n", $$1, $$2}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment