An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
Use a help target in your Makefile
In one of my previous blog. I wrote how you could make your life easier when you start using Makefile. But when you start using Makefile in many projects. The targets that you use may vary from project to project.
In this blog post I will address a trick how you could write a help
target. Lets imagine that we have a project that allow you to build
, start
and stop
a docker container. These names are pretty straightforward. But without looking in the Makefile you never know for sure.
By calling the help
target you could list all available targets:
make help
When you have a look at the Makefile used for this blog post. It looks like this:
.DEFAULT_GOAL:=help.PHONY: helphelp: ## Display this help $(info Example Makefile for my blog post) awk 'BEGIN {FS = ":.*##"; printf "
Usage:
make \033[36m<target>\033[0m
"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s
", $$1, $$2 } /^##@/ { printf "
\033[1m%s\033[0m
", substr($$0, 5) } ' $(MAKEFILE_LIST).PHONY: buildbuild: ## Build the container image docker build -t my-container:latest ..PHONY: stopstop: ## Stop the container docker stop my-named-container > /dev/null 2>&1 || True docker rm my-named-container > /dev/null 2>&1 || True.PHONY: startstart: stop ## Start the container docker run --name my-named-container my-container:latest > /dev/null.PHONY: shellshell: ## Start a shell session on the container docker run -it my-named-container:latest bash.PHONY: logslogs: ## Tail the logs of the running container docker logs my-named-container -f$(VERBOSE).SILENT:
You need to add the help
target. And for each target you need to supply a help text. And each target needs a prefix with ##
.
By adding a help
target to your Makefile you make it easier to use for others. But also for yourself because you don't need to remember each target in the Makefile.
Photo by lalesh aldarwish
Original Link: https://dev.to/aws-builders/use-a-help-target-in-your-makefile-46a3
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To