Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Use a help target in your Makefile
AWS Community Builders  profile imageJoris Conijn
Joris Conijn forAWS Community Builders

Posted on • Originally published atbinx.io

     

Use a help target in your Makefile

In one of my previousblog. 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 ahelp target. Lets imagine that we have a project that allow you tobuild,start andstop a docker container. These names are pretty straightforward. But without looking in the Makefile you never know for sure.

By calling thehelp target you could list all available targets:

makehelp
Enter fullscreen modeExit fullscreen mode

All targets are listed with a help text

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 Makefileformy blog post)awk'BEGIN {FS = ":.*##"; printf "\nUsage:\n  make \033[36m<target>\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", 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    dockerrmmy-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:
Enter fullscreen modeExit fullscreen mode

You need to add thehelp target. And for each target you need to supply a help text. And each target needs a prefix with##.

By adding ahelp 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 bylalesh aldarwish

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Build On!

Would you like to become an AWS Community Builder? Learn more about the program and apply to join when applications are open next.

More fromAWS Community Builders

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp