Created
March 19, 2026 08:45
-
-
Save gnuos/c1a99997b9a9d8ad77071aac0575956e to your computer and use it in GitHub Desktop.
Makefile for golang project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ============================================================================= | |
| # Variables | |
| # ============================================================================= | |
| # Application name | |
| APP := jop | |
| # Package paths | |
| MAIN_DIR := cmd/jop | |
| # Directories | |
| BIN_DIR := bin | |
| DIST_DIR := dist | |
| # Build output | |
| OUTPUT := $(BIN_DIR)/$(APP) | |
| # Platforms for cross-compilation | |
| PLATFORMS := linux/amd64 darwin/amd64 darwin/arm64 windows/amd64 | |
| # Go flags | |
| GO := go | |
| GOFLAGS := -v | |
| LDFLAGS := -w -s -extldflags "-static" | |
| # Version info (from git) | |
| VERSION := $(shell git describe --tags --always 2>/dev/null || echo "dev") | |
| COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown") | |
| DATE := $(shell date -u +%Y-%m-%d 2>/dev/null || date +%Y-%m-%d) | |
| # ============================================================================= | |
| # Colors | |
| # ============================================================================= | |
| RED := \033[31m | |
| GREEN := \033[32m | |
| CYAN := \033[36m | |
| YELLOW := \033[33m | |
| RESET := \033[0m | |
| # ============================================================================= | |
| # Helper Functions | |
| # ============================================================================= | |
| define print_status | |
| @echo -e "$(CYAN)[$(1)] $(2)$(RESET)" | |
| endef | |
| # ============================================================================= | |
| # Default Target | |
| # ============================================================================= | |
| .PHONY: all | |
| all: fmt vet test | |
| # ============================================================================= | |
| # Build Targets | |
| # ============================================================================= | |
| .PHONY: build | |
| build: $(BIN_DIR) | |
| @echo -e "$(CYAN)[EXEC]$(RESET) go build -o $(OUTPUT) -ldflags '$(LDFLAGS)' ./$(MAIN_DIR)" | |
| @go build -o $(OUTPUT) -ldflags '$(LDFLAGS)' ./$(MAIN_DIR) | |
| @echo -e "$(GREEN) Build SUCCESS$(RESET)" | |
| $(BIN_DIR): | |
| @mkdir -p $(BIN_DIR) | |
| .PHONY: build-all | |
| build-all: $(BIN_DIR) | |
| @echo -e "$(CYAN)========================================$(RESET)" | |
| @echo -e "$(CYAN) Cross-Compiling $(GREEN)$(APP)$(RESET)" | |
| @echo -e "$(CYAN)========================================$(RESET)" | |
| $(foreach platform,$(PLATFORMS), \ | |
| $(eval os := $(word 1,$(subst /, ,$(platform)))) \ | |
| $(eval arch := $(word 2,$(subst /, ,$(platform)))) \ | |
| @echo " Building for $(os)/$(arch)..." \ | |
| $(eval output := $(BIN_DIR)/$(APP)-$(platform)) \ | |
| $(if $(filter windows,$(os)),$(eval output := $(output).exe),) \ | |
| GOOS=$(os) GOARCH=$(arch) $(GO) build $(GOFLAGS) -o $(output) -ldflags \ | |
| "-X main.Version=$(VERSION) -X main.Commit=$(COMMIT) -X main.Date=$(DATE)" \ | |
| ./$(MAIN_DIR) && echo "$(GREEN) ✓ $(os)/$(arch)$(RESET)" || exit 1;) | |
| # ============================================================================= | |
| # Test Targets | |
| # ============================================================================= | |
| .PHONY: test | |
| test: | |
| $(call print_status,TEST,go test ./internal/... ./pkg/...) | |
| @$(GO) test ./internal/... ./pkg/... | |
| .PHONY: test-verbose | |
| test-verbose: | |
| $(call print_status,TEST,go test -v -race -cover ./internal/... ./pkg/...) | |
| @$(GO) test -v -race -cover ./internal/... ./pkg/... | |
| # ============================================================================= | |
| # Code Quality Targets | |
| # ============================================================================= | |
| .PHONY: fmt | |
| fmt: | |
| $(call print_status,FMT,go fmt ./...) | |
| @$(GO) fmt ./... | |
| .PHONY: vet | |
| vet: | |
| $(call print_status,VET,go vet ./cmd/... ./internal/... ./pkg/...) | |
| @$(GO) vet ./... | |
| .PHONY: lint | |
| lint: | |
| @which golangci-lint > /dev/null 2>&1 || $(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest | |
| $(call print_status,LINT,golangci-lint run ./...) | |
| @golangci-lint run ./... | |
| # ============================================================================= | |
| # Dependency Targets | |
| # ============================================================================= | |
| .PHONY: deps | |
| deps: | |
| $(call print_status,DEPS,go mod tidy) | |
| @$(GO) mod tidy | |
| $(call print_status,DEPS,go mod download) | |
| @$(GO) mod download | |
| $(call print_status,DEPS,go mod verify) | |
| @$(GO) mod verify | |
| # ============================================================================= | |
| # Clean Targets | |
| # ============================================================================= | |
| .PHONY: clean | |
| clean: | |
| $(call print_status,CLEAN,rm -rf $(BIN_DIR) $(DIST_DIR)) | |
| @rm -rf $(BIN_DIR) $(DIST_DIR) | |
| $(call print_status,CLEAN,go clean -cache) | |
| @$(GO) clean -cache | |
| # ============================================================================= | |
| # Install Targets | |
| # ============================================================================= | |
| .PHONY: install | |
| install: build | |
| $(call print_status,INSTALL,go install -ldflags '-X main.Version=$(VERSION) -X main.Commit=$(COMMIT) -X main.Date=$(DATE)' ./$(MAIN_DIR)) | |
| @$(GO) install -ldflags '-X main.Version=$(VERSION) -X main.Commit=$(COMMIT) -X main.Date=$(DATE)' ./$(MAIN_DIR) | |
| # ============================================================================= | |
| # Release Targets | |
| # ============================================================================= | |
| .PHONY: release | |
| release: clean test build-all | |
| @mkdir -p $(DIST_DIR) | |
| @echo -e "$(GREEN) Release ready in ./dist/$(RESET)" | |
| @for platform in $(PLATFORMS); do \ | |
| os=$$(echo $$platform | cut -d/ -f1); \ | |
| arch=$$(echo $$platform | cut -d/ -f2); \ | |
| src=$(BIN_DIR)/$(APP)-$$platform; \ | |
| [ "$$os" = "windows" ] && src=$$src.exe; \ | |
| dst=$(DIST_DIR)/$(APP)-$$platform; \ | |
| [ "$$os" = "windows" ] && dst=$$dst.exe; \ | |
| cp $$src $$dst; \ | |
| echo " Created: $$dst"; \ | |
| done | |
| # ============================================================================= | |
| # Utility Targets | |
| # ============================================================================= | |
| .PHONY: watch | |
| watch: | |
| @which air > /dev/null 2>&1 || $(GO) install github.com/cosmtrek/air@latest | |
| $(call print_status,WATCH,air) | |
| @air | |
| .PHONY: doc | |
| doc: | |
| $(call print_status,DOC,godoc -http :6060) | |
| @godoc -http :6060 | |
| .PHONY: run | |
| run: build | |
| @$(BIN_DIR)/$(APP) | |
| .PHONY: repl | |
| repl: build | |
| @$(BIN_DIR)/$(APP) | |
| # ============================================================================= | |
| # Help | |
| # ============================================================================= | |
| .PHONY: help | |
| help: | |
| @echo -e "$(CYAN)jop Makefile$(RESET)" | |
| @echo "" | |
| @echo "Build Targets:" | |
| @echo " make build Build $(APP) for current platform" | |
| @echo " make build-all Cross-compile for all platforms" | |
| @echo "" | |
| @echo "Test Targets:" | |
| @echo " make test Run tests" | |
| @echo " make test-verbose Run tests with verbose, race detection and coverage" | |
| @echo "" | |
| @echo "Code Quality:" | |
| @echo " make fmt Format code" | |
| @echo " make vet Run go vet" | |
| @echo " make lint Run golangci-lint" | |
| @echo "" | |
| @echo "Dependency Management:" | |
| @echo " make deps Download and verify dependencies" | |
| @echo "" | |
| @echo "Clean/Install:" | |
| @echo " make clean Remove build artifacts" | |
| @echo " make install Install to \$$GOPATH/bin" | |
| @echo "" | |
| @echo "Release:" | |
| @echo " make release Create release builds" | |
| @echo "" | |
| @echo "Utilities:" | |
| @echo " make run/repl Build and run REPL" | |
| @echo " make watch Watch and rebuild (requires air)" | |
| @echo " make doc Start documentation server" | |
| @echo " make all Run fmt, vet, test" | |
| @echo " make help Show this help" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment