Skip to content

Instantly share code, notes, and snippets.

@narqo
Created August 27, 2017 00:23
Show Gist options
  • Save narqo/7a9add17570c90f70244c56eebdd7aa5 to your computer and use it in GitHub Desktop.
Save narqo/7a9add17570c90f70244c56eebdd7aa5 to your computer and use it in GitHub Desktop.
Makefile for general Go project
NAME := myapp
GO := go
LDFLAGS :=
GOFLAGS :=
TESTFLAGS :=
INSTALL := install
bindir := /usr/local/bin
GITSHA := $(shell git rev-parse --short HEAD)
ifndef VERSION
VERSION := $(GITSHA)
endif
override LDFLAGS += -X main.version=$(VERSION)
BUILD.go = $(GO) build $(GOFLAGS) -i
TEST.go = $(GO) test $(TESTFLAGS)
.PHONY: all
all: build
.PHONY: build
$(NAME) build:
$(BUILD.go) -ldflags "$(LDFLAGS)" -o $(NAME) ./...
.PHONY: test
test:
$(TEST.go) ./...
.PHONY: vet
vet:
$(GO) vet ./...
.PHONY: bench
bench: TESTFLAGS += -bench -run XXX
bench:
$(TEST.go) ./...
.PHONY: install
install: $(NAME)
$(INSTALL) -d $(DESTDIR)$(bindir)
$(INSTALL) -d $(DESTDIR)/etc/rc.d/init.d
$(INSTALL) -d $(DESTDIR)/etc/sysconfig
$(INSTALL) -p -m 755 $(NAME) $(DESTDIR)$(bindir)/$(NAME)
$(INSTALL) -p -m 755 scripts/$(NAME).init $(DESTDIR)/etc/rc.d/init.d/$(NAME)
$(INSTALL) -p -m 755 scripts/$(NAME).conf $(DESTDIR)/etc/sysconfig/$(NAME)
.PHONY: dist
dist: DESTDIR=_dist
dist: install
tar czf $(NAME)-$(VERSION).tar.gz -C $(DESTDIR) .
.PHONY: clean
clean:
$(GO) clean -r -i ./...
-$(RM) -r $(NAME) _dist/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment