|# Add the following 'help' target to your Makefile|
|# And add help text after each target name starting with '\#\#'|
|help: ## Show this help.|
|@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'|
|# Everything below is an example|
|target00: ## This message will show up when typing 'make help'|
|@echo does nothing|
|target01: ## This message will also show up when typing 'make help'|
|@echo does something|
|# Remember that targets can have multiple entries (if your target specifications are very long, etc.)|
|target02: ## This message will show up too!!!|
|target02: target00 target01|
|@echo does even more|
Aug 14, 2022
To golf a little bit more, the grep expression of the previous post can be shrinked down to
grep -hP '^[\w \-]*?:.*##.*$$' with the perl regex flag and
\w that is an alias for
Nov 21, 2022
just to add another variant to the pile..
- uses only
sedfor pre-processing the makefile
tputto detect if we should use colour
columnfor what it does best
help: @sed \ -e '/^[a-zA-Z0-9_\-]*:.*##/!d' \ -e 's/:.*##\s*/:/' \ -e 's/^\(.\+\):\(.*\)/$(shell tput setaf 6)\1$(shell tput sgr0):\2/' \ $(MAKEFILE_LIST) | column -c2 -t -s :
Dec 1, 2022
What's the most cross-platform solution, without colors and which displays targets even if they don't have comment?
Dec 7, 2022
@arvenil i might be wrong but i think that a makefile works using the same system, across all platforms? Please correct me if i am wrong, i am not very cross platform myself... As for color, i guess the terminal will be responsible for displaying it (or not) depending on the context. Many tools have the ability to force the suppression of color, too.
Dec 7, 2022
@arvenil Depends on what you want to depend on? You have choices. awk, sed, grep, bash, sh, zsh, GNU Make, BSD Make, Linux, MacOS, BSD, WSL, RHEL/Fedora, Debian, Ubuntu, others. For widest cross compatibility, GNU Make and bash are fairly safe. But bash has been losing install base and mind share with MacOS and other smaller Linux distros changing their default shells.
Dec 7, 2022
One other note, since GNU Make is the most popular, it has a fairly powerful and underutilized optional integration of GNU Guile. GNU Guile is language in the Scheme & Lisp family of languages.
If Guile is a bridge too far, then consider bash's BASH_REMATCH facility.
It'll be a bit more code (file I/O, regexes, file parsing, etc) and you'll have to maintain it rather than depending on other well tested tooling like awk, sed, grep.
Dec 8, 2022
@arvenil A bit of golfing and I got this to sort of work with the only deps being GNU
SHELL:=/bin/bash .PHONY: help help_target-funky+names.0k and_with_2_targets_and_spaces_like_bison help_target-funky+names.0k and_with_2_targets_and_spaces_like_bison: ## Funky ones & bison dual target display ok echo "bad - why are you not displaying?" help: ## bash help help: ## moar bash help @RE='^[a-zA-Z0-9 ._+-]*:[a-zA-Z0-9 ._+-]*##' ; while read line ; do [[ "$$line" =~ $$RE ]] && echo "$$line" ; done <$(MAKEFILE_LIST) ; RE=''
make help outputs
$ make help help_target-funky+names.0k and_with_2_targets_and_spaces_like_bison: ## Funky ones & bison dual target display ok help: ## bash help help: ## moar bash help
Here's my fix for that:
-hto hide the file name and tweaked the regex to include numbers and possible whitespace before the target's
:, although it still doesn't cover all of the possible characters in a target identifier.