-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.mk
94 lines (79 loc) · 3.04 KB
/
common.mk
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
SHELL := /bin/bash
# Check if we're running in an interactive terminal.
ISATTY := $(shell [ -t 0 ] && echo 1)
# If running interactively, use terminal colors.
ifdef ISATTY
MAGENTA := \e[35;1m
CYAN := \e[36;1m
RED := \e[0;31m
OFF := \e[0m
# Use external echo command since the built-in echo doesn't support '-e'.
else
MAGENTA := ""
CYAN := ""
RED := ""
OFF := ""
endif
# Output messages to stderr instead stdout.
PRINT := printf 1>&2
# Name of git remote pointing to the canonical upstream git repository, i.e.
# git@github.com:oasisprotocol/cli.git.
OASIS_CLI_GIT_ORIGIN_REMOTE ?= origin
# Name of the branch where to tag the next release.
RELEASE_BRANCH ?= master
# Determine project's version from git.
# NOTE: This computes the project's version from the latest version tag
# reachable from current git commit and does not search for version
# tags across the whole $(GIT_ORIGIN_REMOTE) repository.
GIT_VERSION := $(subst v,,$(shell \
git describe --tags --match 'v*' --abbrev=0 2>/dev/null HEAD || \
echo undefined \
))
# Determine project's version.
# If the current git commit is exactly a tag and it equals the Punch version,
# then the project's version is that.
# Else, the project version is the Punch version appended with git commit and
# dirty state info.
GIT_COMMIT_EXACT_TAG := $(shell \
git describe --tags --match 'v*' --exact-match &>/dev/null HEAD && echo YES || echo NO \
)
# Go binary to use for all Go commands.
export OASIS_GO ?= go
# Go command prefix to use in all Go commands.
GO := env -u GOPATH $(OASIS_GO)
VERSION := $(or \
$(and $(call eq,$(GIT_COMMIT_EXACT_TAG),YES), $(GIT_VERSION)), \
$(shell git describe --tags --abbrev=0)-git$(shell git describe --always --match '' --dirty=+dirty 2>/dev/null) \
)
# Project's version as the linker's string value definition.
export GOLDFLAGS_VERSION := -X github.com/oasisprotocol/cli/version.Software=$(VERSION)
# Go's linker flags.
export GOLDFLAGS ?= "$(GOLDFLAGS_VERSION)"
# Helper that ensures the git workspace is clean.
define ENSURE_GIT_CLEAN =
if [[ ! -z `git status --porcelain` ]]; then \
$(PRINT) "$(RED)Error: Git workspace is dirty.$(OFF)\n"; \
exit 1; \
fi
endef
# Helper that checks if the go mod tidy command was run.
# NOTE: go mod tidy doesn't implement a check mode yet.
# For more details, see: https://github.com/golang/go/issues/27005.
define CHECK_GO_MOD_TIDY =
$(GO) mod tidy; \
if [[ ! -z `git status --porcelain go.mod go.sum` ]]; then \
$(PRINT) "$(RED)Error: The following changes detected after running 'go mod tidy':$(OFF)\n"; \
git diff go.mod go.sum; \
exit 1; \
fi
endef
# Helper that checks commits with gitlilnt.
# NOTE: gitlint internally uses git rev-list, where A..B is asymmetric
# difference, which is kind of the opposite of how git diff interprets
# A..B vs A...B.
define CHECK_GITLINT =
BRANCH=$(OASIS_CLI_GIT_ORIGIN_REMOTE)/$(RELEASE_BRANCH); \
COMMIT_SHA=`git rev-parse $$BRANCH` && \
$(PRINT) "$(CYAN)*** Running gitlint for commits from $$BRANCH ($${COMMIT_SHA:0:7})... $(OFF)\n"; \
gitlint --commits $$BRANCH..HEAD
endef