Skip to content

Instantly share code, notes, and snippets.

@benrowe
Forked from prwhite/Makefile
Last active June 16, 2020 01:55
Show Gist options
  • Save benrowe/167f65131cb16942fb5032a9b29937f3 to your computer and use it in GitHub Desktop.
Save benrowe/167f65131cb16942fb5032a9b29937f3 to your computer and use it in GitHub Desktop.
Add a help target to a Makefile that will allow all targets to be self documenting
.PHONY: help
# COLORS
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)
TARGET_MAX_CHAR_NUM=20
## Show help
help:
@echo ''
@echo 'Usage:'
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
@echo ''
@echo 'Targets:'
@awk '/^[a-zA-Z\-\_0-9]+:/ { \
helpMessage = match(lastLine, /^## (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
printf " ${YELLOW}%-$(TARGET_MAX_CHAR_NUM)s${RESET} ${GREEN}%s${RESET}\n", helpCommand, helpMessage; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST)
## example
target-name:
@echo "target-name"
@twitchel
Copy link

This. I like this.

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