make ls
complex makefiles can be a pain. here’s a quick way to add an ls
target that shows all available targets and what they do.
# colors for outputcyan := \033[36mreset := \033[0m
# list all targetsls: ## show all targets with descriptions @echo "available targets:" @echo "===================" @awk '/^[a-zA-Z0-9_-]+:/ { \ if ($$0 ~ /##/) { \ printf "$(cyan)%-30s$(reset) %s\n", substr($$1, 1, length($$1)-1), substr($$0, index($$0,"##") + 3); \ } else { \ printf "%-30s\n", substr($$1, 1, length($$1)-1); \ } \ }' $(makefile_list) | sort
what this does:
- sets up color codes for pretty output
- creates an
ls
target usingawk
to parse the makefile - prints target names in cyan, followed by descriptions if they exist
- sorts everything alphabetically
how to use it
add some targets to your makefile:
build: ## compile the project @echo "building..."test: ## run tests @echo "testing..."clean: ## remove build junk @echo "cleaning..."deploy: @echo "deploying..."# (add the 'ls' target here)
run make ls
and you’ll see:
available targets:===================build compile the projectclean remove build junkdeployls show all targets with descriptionstest run tests
note:
- targets with
##
comments get descriptions deploy
has no description, so it’s just listed- names are in cyan (you’d see this in your terminal)
- everything’s sorted a-z
this little trick makes it way easier to work with big makefiles. you get a quick view of all your make commands without digging through the file. pretty neat, huh?