Skip to content

Instantly share code, notes, and snippets.

@kiasaki
Created June 3, 2016 07:25
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 kiasaki/9fa37ff71cf9b2229ca3bbd2c76b4929 to your computer and use it in GitHub Desktop.
Save kiasaki/9fa37ff71cf9b2229ca3bbd2c76b4929 to your computer and use it in GitHub Desktop.
Golang project Makefile
# from https://unwiredcouch.com/2016/05/31/go-make.html?utm_source=golangweekly&utm_medium=email
# variable definitions
NAME := coolthings
DESC := a nice toolkit of helpful things
PREFIX ?= usr/local
VERSION := $(shell git describe --tags --always --dirty)
GOVERSION := $(shell go version)
BUILDTIME := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
BUILDDATE := $(shell date -u +"%B %d, %Y")
BUILDER := $(shell echo "`git config user.name` <`git config user.email`>")
PKG_RELEASE ?= 1
PROJECT_URL := "https://github.com/mrtazz/$(NAME)"
LDFLAGS := -X 'main.version=$(VERSION)' \
-X 'main.buildTime=$(BUILDTIME)' \
-X 'main.builder=$(BUILDER)' \
-X 'main.goversion=$(GOVERSION)'
# development tasks
test:
go test $$(go list ./... | grep -v /vendor/ | grep -v /cmd/)
coverage:
@-go test -v -coverprofile=cover.out $$(go list ./... | grep -v /vendor/ | grep -v /cmd/)
@-go tool cover -html=cover.out -o cover.html
benchmark:
@echo "Running tests..."
@go test -bench=. $$(go list ./... | grep -v /vendor/ | grep -v /cmd/)
CMD_SOURCES := $(shell find cmd -name main.go)
TARGETS := $(patsubst cmd/%/main.go,%,$(CMD_SOURCES))
%: cmd/%/main.go
go build -ldflags "$(LDFLAGS)" -o $@ $<
MAN_SOURCES := $(shell find man -name "*.md")
MAN_TARGETS := $(patsubst man/man1/%.md,%,$(MAN_SOURCES))
%.1: man/man1/%.1.md
sed "s/REPLACE_DATE/$(BUILDDATE)/" $< | pandoc -s -t man -o $@
all: $(TARGETS) $(MAN_TARGETS)
.DEFAULT_GOAL:=all
clean:
rm -rf ./usr
rm $(TARGETS)
rm $(MAN_TARGETS)
.PHONY: clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment