-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile.common
More file actions
134 lines (111 loc) · 4.38 KB
/
Makefile.common
File metadata and controls
134 lines (111 loc) · 4.38 KB
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
# In order to ensure make instructions fail if there is command that fails a pipe (ie: `go test ... | tee -a ./test_results.txt`)
# the value `-o pipefail` (or `set -o pipefail`) is added to each shell command that make runs
# otherwise in the example command pipe, only the exit code of `tee` is recorded instead of `go test` which can cause
# test to pass in CI when they should not.
SHELL = /usr/bin/env bash
.SHELLFLAGS = -o pipefail -c
GOCMD?= go
GOOS=$(shell $(GOCMD) env GOOS)
ifeq ($(GOOS),windows)
# The "sed" transformation below is needed on Windows, since commands like `go list -f '{{ .Dir }}'`
# return Windows paths and such paths are incompatible with other *nix tools, like `find`,
# used by the Makefile shell.
# The backslash needs to be doubled so its passed correctly to the shell.
NORMALIZE_DIRS = sed -e 's/^/\\//' -e 's/://' -e 's/\\\\/\\//g' | sort
NUM_CORES := ${NUMBER_OF_PROCESSORS}
else
NORMALIZE_DIRS = sort
NUM_CORES := $(shell getconf _NPROCESSORS_ONLN)
endif
# build tags required by any component should be defined as an independent variables and later added to GO_BUILD_TAGS below
GO_BUILD_TAGS=""
GOTEST_OPT?= -v -timeout 180s --tags=$(GO_BUILD_TAGS)
GOTEST=$(GOCMD) test
ADDLICENSES= addlicense
MDLINKCHECK=markdown-link-check
MISSPELL=misspell -error
MISSPELL_CORRECTION=misspell -w
LINT=golangci-lint
SRC_ROOT := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
ALL_PKG_DIRS := $(shell $(GOCMD) list -f '{{ .Dir }}' ./... | $(NORMALIZE_DIRS))
ALL_SRC := $(shell find $(ALL_PKG_DIRS) -name '*.go' \
-not -path '*/example/*' \
-not -path '*/ta/*' \
-type f | sort)
# All source code and documents. Used in spell check.
ALL_SRC_AND_DOC := $(shell find $(ALL_PKG_DIRS) -name "*.md" -o -name "*.go" -o -name "*.yaml" \
-not -path '*/example/*' \
-not -path '*/ta/*' \
-type f | sort)
# Used for building to collect coverage information
COVER_TESTING?=false
# COVER_PKGS is the list of packages to include in the test coverage
EXCLUDE_MODS=-not -path "./example/*" -not -path "./ta/*"
FIND_MOD_ARGS=-type f -name "go.mod"
.PHONY: test
test:
$(GOTEST) $(GOTEST_OPT) $(ALL_PKG_DIRS)
ifeq ($(COVER_TESTING),true)
# These variables and targets are expensive to build, so only build if explicitly requested
COVER_PKGS := $(shell find . $(EXCLUDE_MODS) $(FIND_MOD_ARGS) -execdir $(GOCMD) list ./... \; | tr "\n" "," )
COVER_DIR?=coverage
COVER_DIR_ABS?=$(SRC_ROOT)/$(COVER_DIR)
COVER_OPTS?=-cover -covermode=atomic -coverpkg $(COVER_PKGS)
COVER_TESTING_OPTS?=$(COVER_OPTS) -args -test.gocoverdir="$(COVER_DIR_ABS)"
.PHONY: test-with-codecov
test-with-codecov:
mkdir -p $(COVER_DIR_ABS)
$(GOTEST) $(GOTEST_OPT_WITHOUT_RACE) ./... $(COVER_TESTING_OPTS)
endif
.PHONY: benchmark
benchmark:
$(GOTEST) -bench=./...
.PHONY: addlicense
addlicense:
@ADDLICENSESOUT=`$(ADDLICENSES) -y "" -c 'Splunk, Inc.' -s=only $(ALL_SRC) 2>&1`; \
if [ "$$ADDLICENSESOUT" ]; then \
echo "$(ADDLICENSES) FAILED => add License errors:\n"; \
echo "$$ADDLICENSESOUT\n"; \
exit 1; \
else \
echo "Add License finished successfully"; \
fi
.PHONY: checklicense
checklicense:
@ADDLICENSESOUT=`$(ADDLICENSES) -check $(ALL_SRC) 2>&1`; \
if [ "$$ADDLICENSESOUT" ]; then \
echo "$(ADDLICENSES) FAILED => add License errors:\n"; \
echo "$$ADDLICENSESOUT\n"; \
echo "Use 'make addlicense' to fix this."; \
exit 1; \
else \
echo "Check License finished successfully"; \
fi
.PHONY: checklinks
checklinks:
command -v $(MDLINKCHECK) >/dev/null 2>&1 || { echo >&2 "$(MDLINKCHECK) not installed. Run 'npm install -g markdown-link-check'"; exit 1; }
find . -name \*.md -print0 | xargs -0 -n1 \
$(MDLINKCHECK) -q -c $(SRC_ROOT)/.github/workflows/check_links_config.json || true
.PHONY: fmt
fmt: addlicense misspell-correction
gofumpt -l -w -extra .
gofmt -w -s .
goimports -w -local github.com/splunk/tarunner ./
fieldalignment -fix ./... || true
.PHONY: lint
lint: checklicense misspell
$(LINT) run --allow-parallel-runners -j$(NUM_CORES)
.PHONY: tidy
tidy:
rm -fr go.sum
$(GOCMD) mod tidy
.PHONY: misspell
misspell:
@echo "running $(MISSPELL)"
@$(MISSPELL) $(ALL_SRC_AND_DOC)
.PHONY: misspell-correction
misspell-correction:
$(MISSPELL_CORRECTION) $(ALL_SRC_AND_DOC)
.PHONY: moddownload
moddownload:
$(GOCMD) mod download