Skip to content

Instantly share code, notes, and snippets.

@marcomalva
Created February 18, 2023 03:45
Show Gist options
  • Save marcomalva/55617fbc299513b6375a3bbf7433b049 to your computer and use it in GitHub Desktop.
Save marcomalva/55617fbc299513b6375a3bbf7433b049 to your computer and use it in GitHub Desktop.
[Makefile Help Target]: Self-Describing Makefile
# self describing makefile
# based on idea in https://gist.github.com/jeffsp/3e1b2e10b9181681a604dd9ec6d64ecf
#
# add support for any Makefile name, see https://www.gnu.org/software/make/manual/make.html#Special-Variables
# add colon after target name so bat -l mk gives nicer display on terminal
# for nicer Markdown add asterix around \1 in sed, e.g. for bat or mdcat
#
# Usage
# ==========
#
# Example 1: No post-processing
# make --file self-describing-makefile.mk
#
# Example 2: Using the current grep/sed w/o the asterix:
#
# make --file self-describing-makefile.mk | bat -l mk
#
# Example 3: Change grep+sed to two asterix around the \1 turns into **\1**
#
# make --file self-describing-makefile.mk | mdcat
# make --file self-describing-makefile.mk | bat -l md
#
# Get name of Makefile so the grep picks the right one
name1 := $(lastword $(MAKEFILE_LIST))
# Set help as the default goal
.DEFAULT_GOAL := help
.PHONY: target1 # Target 1 help text
target1: target2 target3
@echo "Target 1, the name of the makefile is: $(name1)"
.PHONY: target2 # Target 2 help text
target2:
@echo "Target 2"
.PHONY: target3
target3:
@echo "No help for this target"
.PHONY: help # Generate list of targets with descriptions
help:
@grep '^.PHONY: .* #' "$(name1)" | sed 's/\.PHONY: \(.*\) # \(.*\)/\1: \2/' | expand -t20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment