Skip to content

Instantly share code, notes, and snippets.

@jgoodall
Created July 8, 2019 17:00
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 jgoodall/1000509101c24bb60f4c19a187e6479a to your computer and use it in GitHub Desktop.
Save jgoodall/1000509101c24bb60f4c19a187e6479a to your computer and use it in GitHub Desktop.
-include .env
PROJECTNAME := $(shell basename "$(PWD)")
GO ?= go
GOBUILD := $(GO) build
GOARCH := amd64
BIN_DIR := ./bin
GOFILES := $(shell find . -name "*.go")
# These are injected into the build.
BUILD_TIME := $(shell date "+%s")
VERSION := $(shell git describe master --tags --abbrev=0)
COMMIT := $(shell git rev-parse --short HEAD)
# Use linker flags to provide version/build settings.
LDFLAGS=-ldflags "-X=main.Version=$(VERSION) -X=main.Commit=$(COMMIT) -X main.BuildTime=$(BUILD_TIME)"
# Use this for the package directory name.
PKG_DIR := $(PROJECTNAME)-$(VERSION)
## `make`: By default, build binaries for all platforms. To parallelize do `make -j3`.
.PHONY: default
default: all
## `make all`: Build binaries for all platforms. To parallelize do `make -j3`.
.PHONY: all
all: darwin linux windows
.PHONY: darwin linux windows
## `make darwin`: Build binary for darwin.
darwin: vet $(BIN_DIR)/$(PROJECTNAME)_darwin_$(GOARCH)
## `make linux`: Build binary for linux.
linux: vet $(BIN_DIR)/$(PROJECTNAME)_linux_$(GOARCH)
## `make windows`: Build binary for windows.
windows: vet $(BIN_DIR)/$(PROJECTNAME)_windows_$(GOARCH).exe
## `make vet`: Run go vet to see if there are any issues.
.PHONY: vet
vet:
$(info > Checking if there are issues reported by go vet...)
@$(GO) vet -composites=false ./...
## `make test`: Run integrations test to identify test issues.
.PHONY: test
test:
$(info > Running integration test...)
@$(GO) test -v ./test
## `make outdated`: Check dependencies to see if there are any outdated.
.PHONY: outdated
outdated:
$(info > Checking if there is any outdated dependencies...)
@$(GO) list -u -m -f '{{if not .Indirect}}{{if .Update}}{{.}}{{end}}{{end}}' all 2> /dev/null
## `make clean`: Clean build files. Runs `go clean` internally.
.PHONY: clean
clean:
$(info > Cleaning build cache...)
@-rm -rf $(BIN_DIR)
@$(GO) clean
## `make package`: Creates a zip file of binaries, testdata, and Readme.
.PHONY: package
package: all
$(info > Creating package in $(PKG_DIR)...)
mkdir -p $(PKG_DIR)
cp README.md $(PKG_DIR)
cp -r bin $(PKG_DIR)
cp -r test/testdata $(PKG_DIR)
zip -r $(PKG_DIR).zip $(PKG_DIR)
rm -rf $(PKG_DIR)
# Darwin/Linux Build
$(BIN_DIR)/$(PROJECTNAME)_%_$(GOARCH): $(GOFILES)
$(info > Building binary for $*...)
CGO_ENABLED=0 GOOS=$* GOARCH=$(GOARCH) $(GOBUILD) $(LDFLAGS) -o $(BIN_DIR)/$(PROJECTNAME)_$*_$(GOARCH)
# Windows Build
$(BIN_DIR)/$(PROJECTNAME)_%_$(GOARCH).exe: $(GOFILES)
$(info > Building binary for $*...)
CGO_ENABLED=0 GOOS=$* GOARCH=$(GOARCH) $(GOBUILD) $(LDFLAGS) -o $(BIN_DIR)/$(PROJECTNAME)_$*_$(GOARCH).exe
.PHONY: help
help: Makefile
@echo
@echo " Choose a command run in "$(PROJECTNAME)":"
@echo
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
@echo
@jgoodall
Copy link
Author

jgoodall commented Jul 8, 2019

This Makefile can be used with any golang project that uses go modules.

It assumes you have a README.md file and testdata directory - change the package directive if this is different.

It also assumes you have the following in your main.go:

// The following vars will be injected during the build process via
// the Makefile.
var (
        BuildTime string
        Version   string
        Commit    string
)

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