Skip to content

Instantly share code, notes, and snippets.

@scotchneat
Last active July 10, 2023 14:26
Show Gist options
  • Save scotchneat/92030199f295e24fae1e9efa6431a773 to your computer and use it in GitHub Desktop.
Save scotchneat/92030199f295e24fae1e9efa6431a773 to your computer and use it in GitHub Desktop.
Makefile

Useful Makefile targets

Help

This target assumes all targets have comments describing its purpose. It greps the file for comments that start with ## and prints the command with its respective description (comment).

.PHONY: help
help: ## Display this help output.
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'


Here's an approach using awk which prints targets in groups:

.PHONY: help
help: ## Display this help.
    @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n  make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Build

.PHONY: build
build: manifests generate fmt vet ## Build manager binary.
	go build -o bin/manager cmd/main.go

The output of this will look like:

Usage:
  make <target>

General
  help             Display this help.

Build
  build            Build manager binary.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment