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.

Makefile
# colors for output
cyan := \033[36m
reset := \033[0m
# list all targets
ls: ## 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:

  1. sets up color codes for pretty output
  2. creates an ls target using awk to parse the makefile
  3. prints target names in cyan, followed by descriptions if they exist
  4. sorts everything alphabetically

how to use it

add some targets to your makefile:

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:

Terminal window
available targets:
===================
build compile the project
clean remove build junk
deploy
ls show all targets with descriptions
test run tests

note:

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?