A “simple” make
rule that allows pretty-printing short documentation for the
rules inside a Makefile:
Easy: simply copy everything starting at .DEFAULT_GOAL := show-help
to the end
of your own Makefile (or include show-help-minified.make
, and copy that file
into your project). Then document any rules by adding a single line starting
with ##
immediately before the rule. E.g.:
## Run unit tests
test:
./run-tests
Displaying the documentation is done by simply executing make
. This overrides
any previously set default command — you may not wish to do so; in that case,
simply remove the line that sets the .DEFAULT_GOAL
. You can then display the
help via make show-help
. This makes it less discoverable, of course.
This has been extremely useful to me. Thanks a lot, everyone! @hilnius I noticed that your solution is currently lacking support for targets defined/documented in included makefiles or if the makefile does not use the default name (i.e. make invoked with -f or with -C). A quick and dirty solution I came up with is to modify your bash script as follows:
It would then have to be invoked (after all includes) with:
I.e. if you save this snippet in a
Makefile-help.mk
as suggested by @hilnius, make sure to include it at the very and of your makefile (or at least after all other makefile includes) in order to catch documentation in all the relevant makefiles. This approach, however, does not work for conditional file inclusion.