Skip to content

Instantly share code, notes, and snippets.

@jessequinn
Forked from eleniums/Makefile
Created November 23, 2022 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessequinn/5226115fd49548905d9c02082feeef74 to your computer and use it in GitHub Desktop.
Save jessequinn/5226115fd49548905d9c02082feeef74 to your computer and use it in GitHub Desktop.
Sample Go Makefile to build binaries for Windows, Mac, and Linux.
EXECUTABLE=executable-name
WINDOWS=$(EXECUTABLE)_windows_amd64.exe
LINUX=$(EXECUTABLE)_linux_amd64
DARWIN=$(EXECUTABLE)_darwin_amd64
VERSION=$(shell git describe --tags --always --long --dirty)
.PHONY: all test clean
all: test build ## Build and run tests
test: ## Run unit tests
./scripts/test_unit.sh
build: windows linux darwin ## Build binaries
@echo version: $(VERSION)
windows: $(WINDOWS) ## Build for Windows
linux: $(LINUX) ## Build for Linux
darwin: $(DARWIN) ## Build for Darwin (macOS)
$(WINDOWS):
env GOOS=windows GOARCH=amd64 go build -i -v -o $(WINDOWS) -ldflags="-s -w -X main.version=$(VERSION)" ./cmd/service/main.go
$(LINUX):
env GOOS=linux GOARCH=amd64 go build -i -v -o $(LINUX) -ldflags="-s -w -X main.version=$(VERSION)" ./cmd/service/main.go
$(DARWIN):
env GOOS=darwin GOARCH=amd64 go build -i -v -o $(DARWIN) -ldflags="-s -w -X main.version=$(VERSION)" ./cmd/service/main.go
clean: ## Remove previous build
rm -f $(WINDOWS) $(LINUX) $(DARWIN)
help: ## Display available commands
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment