-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
150 lines (122 loc) · 3.46 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
.DEFAULT_GOAL: default
VERSION ?= $(shell git describe --abbrev=0 --tags 2>/dev/null || echo 0.0.0)
COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo HEAD)
BUILD_DATE ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
GOPATH ?= $(shell pwd)/_workspace
GOBASE := $(firstword $(subst :, ,$(GOPATH)))
GOBIN := $(GOBASE)/bin
GO ?= go
GOFMT ?= gofmt
GOLINT := $(GOBIN)/golangci-lint
CSUM ?= sha256sum
TAR ?= tar
ZIP ?= zip
INSTALL ?= install
INSTALL_PROGRAM = $(INSTALL)
INSTALL_DATA = $(INSTALL) -m 644
prefix ?= /usr/local
datarootdir = $(prefix)/share
datadir = $(datarootdir)
exec_prefix = $(prefix)
bindir = $(exec_prefix)/bin
libexecdir = $(exec_prefix)/libexec
infodir = $(datarootdir)/info
export CGO_ENABLED = 0
export GO111MODULE = on
export GOARCH
export GOOS
export GOBIN
GO_MODULE := $(shell $(GO) list -m)
GO_PACKAGES := $(shell $(GO) list ./... | grep -vE '/(tools|test|vendor)')
GO_SOURCES := $(shell find . -type f -name '*.go' | grep -vE '/(tools|test|vendor)/')
GO_CMDS := $(notdir $(wildcard ./cmd/*))
PROJECT_NAME ?= $(notdir $(GO_MODULE))
DIST_NAME := $(PROJECT_NAME)_$(VERSION)
BINARY_DIST_NAME := $(DIST_NAME)-$(GOOS)-$(GOARCH)
BUILD_DIR ?= out
GO_LDFLAGS := '-extldflags "-static"
GO_LDFLAGS += -X $(GO_MODULE)/pkg/version.version=$(VERSION)
GO_LDFLAGS += -X $(GO_MODULE)/pkg/version.commit=$(COMMIT)
GO_LDFLAGS += -X $(GO_MODULE)/pkg/version.date=$(BUILD_DATE)
GO_LDFLAGS += -w -s # Drop debugging symbols.
GO_LDFLAGS += '
ifeq ($(GOOS),windows)
PROGRAMS := $(addsuffix .exe,$(GO_CMDS))
BINARY_DIST_EXT := zip
else
PROGRAMS := $(GO_CMDS)
BINARY_DIST_EXT := tar.gz
endif
.PHONY: default
default: all
.PHONY: all
all: lint test build
.PHONY: clean
clean:
-$(RM) *.gz *.xz *.tar *.zip *.tgz *.txz *.sha256
-$(RM) -r $(BUILD_DIR)
@$(GO) clean -x
.PHONY: lint
lint: $(GOLINT) $(GO_SOURCES)
$(GOLINT) run ./...
.PHONY: format
format: $(GO_SOURCES)
@$(GOFMT) -s -w $^
.PHONY: test
test: $(GO_SOURCES)
@$(GO) test $(GO_PACKAGES)
.PHONY: build
build: $(addprefix $(BUILD_DIR)/,$(PROGRAMS))
.PHONY: release-assets
release-assets: $(BINARY_DIST_NAME).tgz $(BINARY_DIST_NAME).tgz.sha256
.PHONY: binary-dist
binary-dist: $(BINARY_DIST_NAME).$(BINARY_DIST_EXT)
.PHONY: dist
dist: tar
.PHONY: tar
tar: $(DIST_NAME).tar
.PHONY: install
install: build
$(INSTALL) -d $(DESTDIR)$(bindir)/
$(INSTALL_PROGRAM) $(wildcard $(BUILD_DIR)/*) $(DESTDIR)$(bindir)/
.PHONY: release-tag
release-tag:
git tag -a -m "Release of $(VERSION)" $(VERSION)
.PHONY: github-workflow-commands
github-workflow-commands:
@echo ::set-output name=project_name::$(PROJECT_NAME)
@echo ::set-output name=version::$(VERSION)
@echo ::set-output name=commit::$(COMMIT)
@echo ::set-output name=target_os::$(GOOS)
@echo ::set-output name=target_arch::$(GOARCH)
@echo ::add-path::$(GOBIN)
$(GOBIN)/%:
# go install -v -tags tools ./...
- grep '_ "' tools/tools.go | \
awk '{ print $$2 }' | \
xargs -n1 $(GO) install -v
$(BUILD_DIR)/%: $(GO_SOURCES)
$(GO) build \
-o $@ \
-ldflags $(GO_LDFLAGS) \
$(GO_MODULE)/cmd/$(notdir $(basename $@))
%.tar: $(GO_SOURCES)
$(TAR) -cf $@ \
--exclude-vcs \
--exclude-vcs-ignores \
--exclude .github \
$(shell git ls-files --exclude-standard)
%.zip: build
$(ZIP) -jr $@ $(BUILD_DIR)
%.tar.gz %.tgz: build
$(TAR) -czf $@ \
-C $(BUILD_DIR) \
$(notdir $(wildcard $(BUILD_DIR)/*))
%.tar.xz %.txz: build
$(TAR) -cJf $@ \
-C $(BUILD_DIR) \
$(notdir $(wildcard $(BUILD_DIR)/*))
%.sha256: %
$(CSUM) $< > $@