Last active
June 16, 2021 02:49
-
-
Save rbuckland/14adc6f0a608232d47090222c860471a to your computer and use it in GitHub Desktop.
helper make file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# This is the Help Support | |
# Include like | |
# Makefile:----------------- | |
# .... | |
# include help.mk | |
# --------------------------- | |
# | |
# Original is from: https://codereview.stackexchange.com/questions/94307/built-in-help-in-a-makefile | |
# | |
# Howto: | |
# Label the `target: ` with a double hashed (##) comment. @category prefix is optional. | |
# | |
# If you want a variable, available to the help text, you need to set | |
# HELP_VAR_EXPORT = ... | |
# to be the variable name to export. | |
# They generally need to be "safe" variable values, The variables are wrapped in quotes "..." | |
# eg: | |
# HELP_VAR_EXPORT := $(HELP_VAR_EXPORT) SOME_VAR | |
# | |
DBL_QUOTE := " | |
VARS_OLD := $(.VARIABLES) | |
VARS = $(filter-out $(VARS_OLD) VARS_OLD VARS,$(.VARIABLES)) | |
.PHONY: help | |
help: ## @other Show this help. | |
@$(foreach var,$(HELP_VAR_EXPORT), $(var)="$($(var))") \ | |
perl -e '$(HELP_FUNC)' $(MAKEFILE_LIST) | |
# Colours for the Help | |
# | |
GREEN := $(shell tput -Txterm setaf 2) | |
WHITE := $(shell tput -Txterm setaf 7) | |
YELLOW := $(shell tput -Txterm setaf 3) | |
RESET := $(shell tput -Txterm sgr0) | |
# | |
# main function in Perl, which reads the Makefile, extracting the Help text | |
# $$ENV{"$1"}/g; | |
# $$text =~ s/(\$$\((.*?)\))/$$ENV{"$$2"}/g; | |
HELP_FUNC = \ | |
sub env_resolve { \ | |
my ($$text) = @_; \ | |
$$text =~ s/(\$$\((.*?)\))/$$ENV{$$2}/g; \ | |
return $$text; \ | |
} \ | |
while(<>) { \ | |
push @{$$hyy{$$2 // 'options'}}, [$$1, $$3] if \ | |
/^([\$$\(\)a-zA-Z0-9\-\.%\/_]+)\s*:.*\#\#(?:\s?@([a-zA-Z0-9_\-]+))?\s(.*)$$/ \ | |
}; \ | |
print "usage make [target]\n\n"; \ | |
for (sort keys %hyy) { \ | |
print "${WHITE}$$_:${RESET}\n"; \ | |
for (@{$$hyy{$$_}}) { \ | |
$$sep = " " x (32 - length $$_->[0]); \ | |
print " ${YELLOW}$$_->[0]${RESET}$$sep${GREEN}".env_resolve($$_->[1])."${RESET}\n"; \ | |
}; \ | |
print "\n"; \ | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment