Created
January 9, 2018 16:59
-
-
Save dhild/f4d479be87446da1097ac5f31e6ebc55 to your computer and use it in GitHub Desktop.
golang Makefile template
This file contains 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
.PHONY: all | |
all: build | |
# Package and name are determined from the directory layout | |
PACKAGE := $(shell go list 2>/dev/null) | |
NAME := $(shell basename ${PACKAGE}) | |
# Version is implied through signed git tags. | |
# If the current branch is not exactly a signed tag, the current branch is used instead. | |
VERSION := $(shell git describe --exact-match 2>/dev/null || git symbolic-ref -q --short HEAD) | |
BUILD_DIR = ${GOPATH}/src/${PACKAGE} | |
CURRENT_DIR = $(shell pwd) | |
BUILD_DIR_LINK = $(shell readlink ${BUILD_DIR}) | |
GO_SOURCES := $(shell go list -f '{{join .GoFiles " "}}') | |
GO_TEST_SOURCES := $(shell go list -f '{{join .TestGoFiles " "}}') | |
GOHOSTOS := $(shell go env GOHOSTOS 2>/dev/null) | |
GOHOSTARCH := $(shell go env GOHOSTARCH 2>/dev/null) | |
BUILD_OPTS := -ldflags '-w -X main.Version=$(VERSION)' -a | |
ifeq ($(BUILD_QUIET),1) | |
Q = @ | |
else | |
Q = | |
endif | |
.PHONY: build | |
build: lint vet test $(NAME) | |
$(NAME): $(GO_SOURCES) vendor rice | |
$(Q)go build $(BUILD_OPTS) -o $(NAME) $(PACKAGE) | |
.PHONY: install | |
install: $(GO_SOURCES) vendor rice | |
$(Q)go install $(BUILD_OPTS) $(PACKAGE) | |
.PHONY: link | |
link: | |
@BUILD_DIR=${BUILD_DIR}; \ | |
BUILD_DIR_LINK=${BUILD_DIR_LINK}; \ | |
CURRENT_DIR=${CURRENT_DIR}; \ | |
if [ "$${BUILD_DIR_LINK}" != "$${CURRENT_DIR}" ]; then \ | |
echo "Fixing symlinks for build"; \ | |
rm -f $${BUILD_DIR}; \ | |
mkdir -p $(shell dirname ${BUILD_DIR}); \ | |
ln -s $${CURRENT_DIR} $${BUILD_DIR}; \ | |
fi | |
GODEP := ${GOPATH}/bin/dep | |
GOLINT := ${GOPATH}/bin/golint | |
GOVET := $(shell go env GOTOOLDIR)/vet | |
GORICE := ${GOPATH}/bin/rice | |
$(GODEP) : ; $(Q)go get github.com/golang/dep/cmd/dep | |
$(GOLINT) : ; $(Q)go get github.com/golang/lint/golint | |
$(GOVET) : ; $(Q)go get golang.org/x/tools/cmd/vet | |
$(GORICE) : ; $(Q)go get github.com/GeertJohan/go.rice/rice | |
# Just make this every time rather than tracking the dependencies.... | |
.PHONY: rice | |
rice: $(GORICE) | |
# $(Q)cd cmd && rice embed-go | |
vendor: $(GODEP) Gopkg.toml Gopkg.lock | |
$(Q)cd $(BUILD_DIR) && $(GODEP) ensure | |
$(Q)touch vendor | |
.PHONY: fmt | |
fmt: | |
$(Q)cd $(BUILD_DIR) && go fmt ./... | |
.PHONY: lint | |
lint: $(GOLINT) | |
$(Q)cd $(BUILD_DIR) && $(GOLINT) $$(cd $(BUILD_DIR) && go list ./...) | |
.PHONY: vet | |
vet: vendor $(GOVET) | |
$(Q)cd $(BUILD_DIR) && \ | |
for src in $(GO_SOURCES) $(GO_TEST_SOURCES); do \ | |
$(GOVET) -all=true $$src; \ | |
done | |
.PHONY: test | |
test: vendor | |
$(Q)cd $(BUILD_DIR) && go test -v -timeout 30s -parallel=4 ./... | |
.PHONY: testacc | |
testacc: vendor | |
$(Q)cd $(BUILD_DIR) && TF_ACC=1 go test -v -timeout 10m ./... | |
.PHONY: clean | |
clean: | |
rm -f $(NAME)* | |
.PHONY: clean-all | |
clean-all: clean | |
rm -fr vendor/ | |
rm -fr ${GOPATH}/bin/$(NAME) | |
.PHONY: release-artifacts | |
release-artifacts: $(NAME)_darwin_amd64 $(NAME)_darwin_amd64.asc $(NAME)_linux_amd64 $(NAME)_linux_amd64.asc | |
$(NAME)_linux_amd64: vendor test $(GO_SOURCES) rice | |
$(Q)GOOS=linux GOARCH=amd64 go build $(BUILD_OPTS) -o $(NAME)_linux_amd64 $(PACKAGE) | |
$(NAME)_darwin_amd64: vendor test $(GO_SOURCES) rice | |
$(Q)GOOS=darwin GOARCH=amd64 go build $(BUILD_OPTS) -o $(NAME)_darwin_amd64 $(PACKAGE) | |
%_amd64.asc: %_amd64 | |
$(Q)gpg --armor --output $@ --detach-sig $< |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment