Created
January 16, 2017 06:48
-
-
Save ryumei/272bf23a891adcee156ccc7b3272217d to your computer and use it in GitHub Desktop.
sample of Makefile for go
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
NAME := my_go_program | |
VERSION := $(shell git describe --tags) | |
REVISION := $(shell git rev-parse --short HEAD) | |
SRCS := $(shell find . -type f -name '*.go') | |
LDFLAGS := -ldflags="-s -w -X \"main.Version=$(VERSION)\" -X \"main.Revision=$(REVISION)\" -extldflags \"-static\"" | |
all: bin/$(NAME) dist | |
bin/$(NAME): $(SRCS) | |
go build -a -tags netgo -installsuffix netgo $(LDFLAGS) -o bin/$(NAME) | |
.PHONY: install | |
install: bin/$(NAME) | |
go install $(LDFLAGS) | |
.PHONY: clean | |
clean: | |
rm -rf bin/* | |
rm -rf vendor/* | |
.PHONY: test bench | |
test: deps golint | |
go test -cover -v `glide novendor` | |
bench: test | |
go test -bench . -benchmem | |
.PHONY: golint | |
golint: | |
golint . | |
.PHONY: coverage | |
coverage.out: test | |
go test -coverprofile=$@ | |
coverage: coverage.out | |
go tool cover -html=$< | |
.PHONY: cross-build | |
cross-build: test | |
for os in darwin linux windows; do \ | |
for arch in amd64 386; do \ | |
GOOS=$$os GOARCH=$$arch CGO_ENABLED=0 go build -a -tags netgo -installsuffix netgo $(LDFLAGS) -o dist/$$os-$$arch/$(NAME); \ | |
done; \ | |
done; \ | |
for arch in amd64 386; do \ | |
GOOS=windows GOARCH=$$arch CGO_ENABLED=0 go build -a -tags netgo -installsuffix netgo $(LDFLAGS) -o dist/windows-$$arch/$(NAME).exe; \ | |
done | |
DIST_DIRS := find * -type d -exec | |
.PHONY: dist | |
dist: cross-build | |
cd dist && \ | |
$(DIST_DIRS) cp ../LICENSE {} \; && \ | |
$(DIST_DIRS) cp ../README.md {} \; && \ | |
$(DIST_DIRS) cp ../config.tml.sample {} \; && \ | |
$(DIST_DIRS) tar -zcf $(NAME)-$(VERSION)-{}.tar.gz {} \; && \ | |
$(DIST_DIRS) zip -r $(NAME)-$(VERSION)-{}.zip {} \; && \ | |
cd .. | |
.PHONY: glide | |
glide: | |
ifeq ($(shell command -v glide 2>/dev/null),) | |
curl https://glide.sh/get | sh | |
endif | |
.PHONY: deps | |
deps: glide | |
glide install |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment