-
Notifications
You must be signed in to change notification settings - Fork 38
/
Makefile
99 lines (84 loc) · 2.3 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
# Metadata about this makefile and position
MKFILE_PATH := $(lastword $(MAKEFILE_LIST))
CURRENT_DIR := $(patsubst %/,%,$(dir $(realpath $(MKFILE_PATH))))
# Ensure GOPATH
GOPATH ?= $(HOME)/go
# List all our actual files, excluding vendor
GOFILES ?= $(shell go list $(TEST) | grep -v /vendor/)
# Tags specific for building
GOTAGS ?=
# Number of procs to use
GOMAXPROCS ?= 4
PROJECT := $(CURRENT_DIR:$(GOPATH)/src/%=%)
OWNER := $(notdir $(patsubst %/,%,$(dir $(PROJECT))))
NAME := $(notdir $(PROJECT))
VERSION := $(shell awk -F\" '/Version/ { print $$2; exit }' "version.go")
# Current system information
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
# List of ldflags
LD_FLAGS ?= -s -w
# List of tests to run
TEST ?= ./...
# build builds the binary into pkg/
build:
@echo "==> Building ${NAME} for ${GOOS}/${GOARCH}"
@env \
-i \
HOME="${HOME}" \
PATH="${PATH}" \
CGO_ENABLED="0" \
GOOS="${GOOS}" \
GOARCH="${GOARCH}" \
GOPATH="${GOPATH}" \
go build -a -o "pkg/${GOOS}_${GOARCH}/${NAME}" -ldflags "${LD_FLAGS}"
.PHONY: build
# dev builds and installs the project locally.
dev:
@echo "==> Installing ${NAME} for ${GOOS}/${GOARCH}"
@env \
-i \
HOME="${HOME}" \
PATH="${PATH}" \
CGO_ENABLED="0" \
GOOS="${GOOS}" \
GOARCH="${GOARCH}" \
GOPATH="${GOPATH}" \
go install -ldflags "${LD_FLAGS}"
.PHONY: dev
# docker builds the docker container.
docker:
@echo "==> Building docker container for ${PROJECT}"
@docker build \
--rm \
--force-rm \
--no-cache \
--compress \
--file="docker/Dockerfile" \
--build-arg="LD_FLAGS=${LD_FLAGS}" \
--tag="${OWNER}/${NAME}" \
--tag="${OWNER}/${NAME}:${VERSION}" \
"${CURRENT_DIR}"
.PHONY: docker
# docker-push pushes the images to the registry
docker-push:
@echo "==> Pushing ${PROJECT} to Docker registry"
@docker push "${OWNER}/${NAME}:latest"
@docker push "${OWNER}/${NAME}:${VERSION}"
# linux builds the linux binary
linux:
@env \
GOOS="linux" \
GOARCH="amd64" \
$(MAKE) -f "${MKFILE_PATH}" build
.PHONY: linux
# test runs the test suite.
test:
@echo "==> Testing ${NAME}"
@go test -v -timeout=30s -parallel=20 -tags="${GOTAGS}" ${GOFILES} ${TESTARGS}
.PHONY: test
# test-race runs the test suite.
test-race:
@echo "==> Testing ${NAME} (race)"
@go test -timeout=60s -race -tags="${GOTAGS}" ${GOFILES} ${TESTARGS}
.PHONY: test-race