-
Notifications
You must be signed in to change notification settings - Fork 3
/
Makefile.Common
156 lines (130 loc) · 5.37 KB
/
Makefile.Common
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
151
152
153
154
155
156
# Copied from https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/Makefile.Common
# 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 = /bin/bash
ifeq ($(shell uname -s),Windows)
.SHELLFLAGS = /o pipefile /c
else
.SHELLFLAGS = -o pipefail -c
endif
# SRC_ROOT is the top of the source tree.
SRC_ROOT := $(shell git rev-parse --show-toplevel)
# 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?= -race -timeout 300s -parallel 4 --tags=$(GO_BUILD_TAGS)
GOTEST_INTEGRATION_OPT?= -race -timeout 360s -parallel 4
GOTEST_OPT_WITH_COVERAGE = $(GOTEST_OPT) -coverprofile=cover.out -covermode=atomic
GOTEST_OPT_WITH_INTEGRATION=$(GOTEST_INTEGRATION_OPT) -tags=integration,$(GO_BUILD_TAGS) -run=Integration -coverprofile=integration-coverage.txt -covermode=atomic
GOCMD?= go
GOTEST=$(GOCMD) test
GOOS=$(shell $(GOCMD) env GOOS)
GOARCH=$(shell $(GOCMD) env GOARCH)
SYFT_VERSION= v0.84.0
# In order to help reduce toil related to managing tooling for the open telemetry collector
# this section of the makefile looks at only requiring command definitions to be defined
# as part of $(TOOLS_MOD_DIR)/tools.go, following the existing practice.
# Modifying the tools' `go.mod` file will trigger a rebuild of the tools to help
# ensure that all contributors are using the most recent version to make builds repeatable everywhere.
TOOLS_MOD_DIR := $(SRC_ROOT)/internal/tools
TOOLS_MOD_REGEX := "\s+_\s+\".*\""
TOOLS_PKG_NAMES := $(shell grep -E $(TOOLS_MOD_REGEX) < $(TOOLS_MOD_DIR)/tools.go | tr -d " _\"")
TOOLS_BIN_DIR := $(SRC_ROOT)/.tools
TOOLS_BIN_NAMES := $(addprefix $(TOOLS_BIN_DIR)/, $(notdir $(TOOLS_PKG_NAMES)))
BUILD_DIR := $(SRC_ROOT)/build
DIST_DIR := $(SRC_ROOT)/dist
COVER_DIR := $(SRC_ROOT)/coverage/unit
TEST_DIR := $(SRC_ROOT)/testing/result
.PHONY: install-tools
install-tools: $(TOOLS_BIN_NAMES) install-syft
$(TOOLS_BIN_DIR):
mkdir -p $@
$(BUILD_DIR):
mkdir -p $@
$(DIST_DIR):
mkdir -p $@
$(TOOLS_BIN_NAMES): $(TOOLS_BIN_DIR) $(TOOLS_MOD_DIR)/go.mod
cd $(TOOLS_MOD_DIR) && $(GOCMD) build -o $@ -trimpath $(filter %/$(notdir $@),$(TOOLS_PKG_NAMES))
MDLINKCHECK := $(TOOLS_BIN_DIR)/markdown-link-check
MISSPELL := $(TOOLS_BIN_DIR)/misspell -error
MISSPELL_CORRECTION := $(TOOLS_BIN_DIR)/misspell -w
LINT := $(TOOLS_BIN_DIR)/golangci-lint
GOIMPORTS := $(TOOLS_BIN_DIR)/goimports
PORTO := $(TOOLS_BIN_DIR)/porto
GOTESTSUM := $(TOOLS_BIN_DIR)/gotestsum
BUILDER := $(TOOLS_BIN_DIR)/builder
GOVULNCHECK := $(TOOLS_BIN_DIR)/govulncheck
GORELEASER := $(TOOLS_BIN_DIR)/goreleaser
SYFT := $(TOOLS_BIN_DIR)/syft
# BUILD_TYPE should be one of (dev, release).
BUILD_TYPE?=release
pwd:
@pwd
.DEFAULT_GOAL := common
.PHONY: common
common: lint
.PHONY: test
test:
$(GOTEST) $(GOTEST_OPT) ./...
.PHONY: test-with-cover
test-with-cover:
mkdir -p $(COVER_DIR)
mkdir -p $(TEST_DIR)
$(GOTESTSUM) --junitfile $(TEST_DIR)/$(MOD).xml --jsonfile $(TEST_DIR)/$(MOD).json -- -coverprofile=$(COVER_DIR)/$(MOD).out $(GOTEST_OPT)
.PHONY: do-unit-tests-with-cover
do-unit-tests-with-cover:
@echo "running $(GOCMD) unit test ./... + coverage in `pwd`"
$(GOTEST) $(GOTEST_OPT_WITH_COVERAGE) ./...
$(GOCMD) tool cover -html=coverage.txt -o coverage.html
.PHONY: do-integration-tests-with-cover
do-integration-tests-with-cover:
@echo "running $(GOCMD) integration test ./... + coverage in `pwd`"
$(GOTEST) $(GOTEST_OPT_WITH_INTEGRATION) ./...
@if [ -e integration-coverage.txt ]; then \
$(GOCMD) tool cover -html=integration-coverage.txt -o integration-coverage.html; \
fi
.PHONY: benchmark
benchmark:
$(GOTEST) -bench=. -run=notests --tags=$(GO_BUILD_TAGS) $(ALL_PKGS)
.PHONY: fmt
fmt: $(GOIMPORTS)
gofmt -w -s ./
$(GOIMPORTS) -w -local github.com/open-telemetry/opentelemetry-collector-contrib ./
.PHONY: lint
lint: $(LINT)
$(LINT) run --allow-parallel-runners --build-tags integration --path-prefix $(shell basename "$(CURDIR)")
.PHONY: govulncheck
govulncheck: $(GOVULNCHECK)
$(GOVULNCHECK) ./...
.PHONY: tidy
tidy:
rm -fr go.sum
$(GOCMD) mod tidy -compat=1.21
.PHONY: misspell
misspell: $(TOOLS_BIN_DIR)/misspell
@echo "running $(MISSPELL)"
@$(MISSPELL) $(ALL_SRC_AND_DOC)
.PHONY: misspell-correction
misspell-correction: $(TOOLS_BIN_DIR)/misspell
$(MISSPELL_CORRECTION) $(ALL_SRC_AND_DOC)
.PHONY: moddownload
moddownload:
$(GOCMD) mod download
.PHONY: updatedep
updatedep:
$(PWD)/internal/buildscripts/update-dep
@$(MAKE) tidy
.PHONY: install-syft
install-syft:
curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b $(TOOLS_BIN_DIR) $(SYFT_VERSION)
.PHONY: install-goreleaser
install-goreleaser:
$(GOCMD) install github.com/goreleaser/goreleaser@latest
.PHONY: cleanup
cleanup:
if [ -d $(TOOLS_BIN_DIR) ]; then rm -r $(TOOLS_BIN_DIR); fi
if [ -d $(DIST_DIR) ]; then rm -r $(DIST_DIR); fi
if [ -d $(BUILD_DIR) ]; then rm -r $(BUILD_DIR); fi
if [ -d $(COVER_DIR) ]; then rm -r $(COVER_DIR); fi
if [ -d $(TEST_DIR) ]; then rm -r $(TEST_DIR); fi