Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ibndias
Forked from rsperl/Makefile #snippet
Created August 5, 2020 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ibndias/1fe8fa455aef232353ee44f7f8a30074 to your computer and use it in GitHub Desktop.
Save ibndias/1fe8fa455aef232353ee44f7f8a30074 to your computer and use it in GitHub Desktop.
self-documenting makefile with colors
SHELL=/bin/bash
# to see all colors, run
# bash -c 'for c in {0..255}; do tput setaf $c; tput setaf $c | cat -v; echo =$c; done'
# the first 15 entries are the 8-bit colors
# define standard colors
BLACK := $(shell tput -Txterm setaf 0)
RED := $(shell tput -Txterm setaf 1)
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
LIGHTPURPLE := $(shell tput -Txterm setaf 4)
PURPLE := $(shell tput -Txterm setaf 5)
BLUE := $(shell tput -Txterm setaf 6)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)
# set target color
TARGET_COLOR := $(BLUE)
POUND = \#
.PHONY: no_targets__ info help build deploy doc
no_targets__:
.DEFAULT_GOAL := help
colors: ## show all the colors
@echo "${BLACK}BLACK${RESET}"
@echo "${RED}RED${RESET}"
@echo "${GREEN}GREEN${RESET}"
@echo "${YELLOW}YELLOW${RESET}"
@echo "${LIGHTPURPLE}LIGHTPURPLE${RESET}"
@echo "${PURPLE}PURPLE${RESET}"
@echo "${BLUE}BLUE${RESET}"
@echo "${WHITE}WHITE${RESET}"
job1: ## help for job 1
@echo "job 1 started"
@$(MAKE) job2
@echo "job 1 finished"
job2: ## help for job 2
@echo "job 2"
job%: ## help for job with wildcard
@echo "job $@"
help:
@echo ""
@echo " ${BLACK}:: ${RED}Self-documenting Makefile${RESET} ${BLACK}::${RESET}"
@echo ""
@echo "Document targets by adding '$(POUND)$(POUND) comment' after the target"
@echo ""
@echo "Example:"
@echo " | job1: $(POUND)$(POUND) help for job 1"
@echo " | @echo \"run stuff for target1\""
@echo ""
@echo "${BLACK}-----------------------------------------------------------------${RESET}"
@grep -E '^[a-zA-Z_0-9%-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "${TARGET_COLOR}%-30s${RESET} %s\n", $$1, $$2}'
# vim:noexpandtab:ts=8:sw=8:ai
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment