Last active
April 19, 2018 08:13
-
-
Save abn/27521d5d36094b92ccabfd0e3ba9d279 to your computer and use it in GitHub Desktop.
An example makefile with embedded help messages and dynamic help targets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST))) | |
ROOT_DIR := $(patsubst %/,%,$(dir $(MAKEFILE_PATH))) | |
.PHONY: %/help | |
%/help: HELP_TARGET = $(subst /help,,$@) | |
%/help: | |
@make -C $(ROOT_DIR) help | grep -Pzo "$(HELP_TARGET)(/\w+)*:.*(\n\s+(.*)?)*\n" | |
### help: Displays this help message. | |
.PHONY: help | |
help: | |
@grep -P '^\s*###* ' $(MAKEFILE_LIST) | sed s/'\s*### '// | sed s/'#'/' '/g | |
### hello: Just says hello. | |
#### This target just echos hello. The real intention is to show you the indentation | |
#### in action. Try using the following command; | |
##### $ make hello/help | |
.PHONY: hello | |
hello: | |
@echo hello | |
### hello/world/foo: Saying hello world foo. | |
.PHONY: hello/world/foo | |
hello/world/foo: | |
@echo "hello world foo" | |
### hello/world/bar: Saying hello world bar. | |
#### You should maybe also try: | |
##### $ make hello/world/help | |
.PHONY: hello | |
hello/world/bar: | |
@echo "hello world bar" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was inspired by the work done at https://gist.github.com/prwhite/8168133