Skip to content

Instantly share code, notes, and snippets.

@abn
Last active April 19, 2018 08:13
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 abn/27521d5d36094b92ccabfd0e3ba9d279 to your computer and use it in GitHub Desktop.
Save abn/27521d5d36094b92ccabfd0e3ba9d279 to your computer and use it in GitHub Desktop.
An example makefile with embedded help messages and dynamic help targets
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"
@abn
Copy link
Author

abn commented Apr 19, 2018

This was inspired by the work done at https://gist.github.com/prwhite/8168133

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