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.
Hello
I really liked @syffer 's solution (looks awesome !), however I had trouble editing it to make the adjustments I wanted (parameter docs, sections not sorted alphabetically) because it's quite complex for me to understand what these
sed
commands are doing 😆 so I went for a much simpler bash script which procedurally generates the docs. The script is not super well written I admit (help doc not generated from the actual help target for example, duplicate regexes with escaping, etc.), however it should be fairly simple to edit & extend !Also, I wanted this 'help' to be available anywhere, from any makefile, so what I did was create two files
and added
/path/to/my/folder
to a$ROOT_PATH
environment variable loaded by my bashrc (note there is also amake install
command that adds that variable to the bashrc if it's not yet added),So that anywhere I want to add
help
target to a makefile, I can just addinclude $ROOT_PATH/tools/Makefile-helper.mk
here's the code:
Makefile-help.mk:
generate-makefile-help:
And here's an example Makefile:
Which generates this:
