|
| 1 | +# All source code excluding any third party code and excluding the testbed. |
| 2 | +# This is the code that we want to run tests for and lint, staticcheck, etc. |
| 3 | +ALL_SRC := $(shell find . -name '*.go' \ |
| 4 | + -not -path './examples/*' \ |
| 5 | + -type f | sort) |
| 6 | + |
| 7 | +# ALL_PKGS is the list of all packages where ALL_SRC files reside. |
| 8 | +ALL_PKGS := $(shell go list $(sort $(dir $(ALL_SRC)))) |
| 9 | + |
| 10 | +# All source code and documents. Used in spell check. |
| 11 | +ALL_DOC := $(shell find . \( -name "*.md" -o -name "*.yaml" \) \ |
| 12 | + -type f | sort) |
| 13 | + |
| 14 | +GOTEST_OPT?= -v -race -timeout 180s |
| 15 | +GO_ACC=go-acc |
| 16 | +GOTEST=go test |
| 17 | +GOOS=$(shell go env GOOS) |
| 18 | +GOARCH=$(shell go env GOARCH) |
| 19 | +ADDLICENSE= addlicense |
| 20 | +MISSPELL=misspell -error |
| 21 | +MISSPELL_CORRECTION=misspell -w |
| 22 | +LINT=golangci-lint |
| 23 | +IMPI=impi |
| 24 | +GOSEC=gosec |
| 25 | +STATIC_CHECK=staticcheck |
| 26 | +# BUILD_TYPE should be one of (dev, release). |
| 27 | +BUILD_TYPE?=release |
| 28 | + |
| 29 | +GIT_SHA=$(shell git rev-parse --short HEAD) |
| 30 | +BUILD_INFO_IMPORT_PATH=github.com/signalfx/splunk-otel-collector/internal/version |
| 31 | +BUILD_X1=-X $(BUILD_INFO_IMPORT_PATH).GitHash=$(GIT_SHA) |
| 32 | +ifdef VERSION |
| 33 | +BUILD_X2=-X $(BUILD_INFO_IMPORT_PATH).Version=$(VERSION) |
| 34 | +endif |
| 35 | +BUILD_X3=-X $(BUILD_INFO_IMPORT_PATH).BuildType=$(BUILD_TYPE) |
| 36 | +BUILD_INFO=-ldflags "${BUILD_X1} ${BUILD_X2} ${BUILD_X3}" |
| 37 | + |
| 38 | +# Function to execute a command. Note the empty line before endef to make sure each command |
| 39 | +# gets executed separately instead of concatenated with previous one. |
| 40 | +# Accepts command to execute as first parameter. |
| 41 | +define exec-command |
| 42 | +$(1) |
| 43 | + |
| 44 | +endef |
| 45 | + |
| 46 | +all-srcs: |
| 47 | + @echo $(ALL_SRC) | tr ' ' '\n' | sort |
| 48 | + |
| 49 | +all-pkgs: |
| 50 | + @echo $(ALL_PKGS) | tr ' ' '\n' | sort |
| 51 | + |
| 52 | +.DEFAULT_GOAL := all |
| 53 | + |
| 54 | +.PHONY: all |
| 55 | +all: checklicense impi lint misspell test otelcol |
| 56 | + |
| 57 | +.PHONY: test |
| 58 | +test: |
| 59 | + $(GOTEST) $(GOTEST_OPT) $(ALL_PKGS) |
| 60 | + |
| 61 | +.PHONY: test-with-cover |
| 62 | +test-with-cover: |
| 63 | + @echo Verifying that all packages have test files to count in coverage |
| 64 | + @internal/buildscripts/check-test-files.sh $(subst go.opentelemetry.io/collector,.,$(ALL_PKGS)) |
| 65 | + @echo pre-compiling tests |
| 66 | + @time go test -i $(ALL_PKGS) |
| 67 | + $(GO_ACC) $(ALL_PKGS) |
| 68 | + go tool cover -html=coverage.txt -o coverage.html |
| 69 | + |
| 70 | +.PHONY: addlicense |
| 71 | +addlicense: |
| 72 | + $(ADDLICENSE) -y "" -c 'The OpenTelemetry Authors' $(ALL_SRC) |
| 73 | + |
| 74 | +.PHONY: checklicense |
| 75 | +checklicense: |
| 76 | + @ADDLICENSEOUT=`$(ADDLICENSE) -check $(ALL_SRC) 2>&1`; \ |
| 77 | + if [ "$$ADDLICENSEOUT" ]; then \ |
| 78 | + echo "$(ADDLICENSE) FAILED => add License errors:\n"; \ |
| 79 | + echo "$$ADDLICENSEOUT\n"; \ |
| 80 | + echo "Use 'make addlicense' to fix this."; \ |
| 81 | + exit 1; \ |
| 82 | + else \ |
| 83 | + echo "Check License finished successfully"; \ |
| 84 | + fi |
| 85 | + |
| 86 | +.PHONY: misspell |
| 87 | +misspell: |
| 88 | + $(MISSPELL) $(ALL_DOC) |
| 89 | + |
| 90 | +.PHONY: misspell-correction |
| 91 | +misspell-correction: |
| 92 | + $(MISSPELL_CORRECTION) $(ALL_DOC) |
| 93 | + |
| 94 | +.PHONY: lint-gosec |
| 95 | +lint-gosec: |
| 96 | + # TODO: Consider to use gosec from golangci-lint |
| 97 | + $(GOSEC) -quiet -exclude=G104 $(ALL_PKGS) |
| 98 | + |
| 99 | +.PHONY: lint-static-check |
| 100 | +lint-static-check: |
| 101 | + @STATIC_CHECK_OUT=`$(STATIC_CHECK) $(ALL_PKGS) 2>&1`; \ |
| 102 | + if [ "$$STATIC_CHECK_OUT" ]; then \ |
| 103 | + echo "$(STATIC_CHECK) FAILED => static check errors:\n"; \ |
| 104 | + echo "$$STATIC_CHECK_OUT\n"; \ |
| 105 | + exit 1; \ |
| 106 | + else \ |
| 107 | + echo "Static check finished successfully"; \ |
| 108 | + fi |
| 109 | + |
| 110 | +.PHONY: lint |
| 111 | +lint: lint-static-check |
| 112 | + $(LINT) run |
| 113 | + |
| 114 | +.PHONY: impi |
| 115 | +impi: |
| 116 | + @$(IMPI) --local github.com/signalfx/splunk-otel-collector --scheme stdThirdPartyLocal --skip internal/data/opentelemetry-proto ./... |
| 117 | + |
| 118 | +.PHONY: install-tools |
| 119 | +install-tools: |
| 120 | + go install github.com/client9/misspell/cmd/misspell |
| 121 | + go install github.com/golangci/golangci-lint/cmd/golangci-lint |
| 122 | + go install github.com/google/addlicense |
| 123 | + go install github.com/ory/go-acc |
| 124 | + go install github.com/pavius/impi/cmd/impi |
| 125 | + go install github.com/securego/gosec/cmd/gosec |
| 126 | + go install honnef.co/go/tools/cmd/staticcheck |
| 127 | + |
| 128 | +.PHONY: otelcol |
| 129 | +otelcol: |
| 130 | + go generate ./... |
| 131 | + GO111MODULE=on CGO_ENABLED=0 go build -o ./bin/otelcol_$(GOOS)_$(GOARCH)$(EXTENSION) $(BUILD_INFO) ./cmd/otelcol |
| 132 | + |
| 133 | +.PHONY: add-tag |
| 134 | +add-tag: |
| 135 | + @[ "${TAG}" ] || ( echo ">> env var TAG is not set"; exit 1 ) |
| 136 | + @echo "Adding tag ${TAG}" |
| 137 | + @git tag -a ${TAG} -s -m "Version ${TAG}" |
| 138 | + |
| 139 | +.PHONY: delete-tag |
| 140 | +delete-tag: |
| 141 | + @[ "${TAG}" ] || ( echo ">> env var TAG is not set"; exit 1 ) |
| 142 | + @echo "Deleting tag ${TAG}" |
| 143 | + @git tag -d ${TAG} |
| 144 | + |
| 145 | +.PHONY: docker-otelcol |
| 146 | +docker-otelcol: |
| 147 | + COMPONENT=otelcol $(MAKE) docker-component |
| 148 | + |
| 149 | +.PHONY: docker-component # Not intended to be used directly |
| 150 | +docker-component: |
| 151 | + GOOS=linux $(MAKE) $(COMPONENT) |
| 152 | + cp ./bin/$(COMPONENT)_linux_amd64 ./cmd/$(COMPONENT)/$(COMPONENT) |
| 153 | + docker build -t $(COMPONENT) ./cmd/$(COMPONENT)/ |
| 154 | + rm ./cmd/$(COMPONENT)/$(COMPONENT) |
| 155 | + |
| 156 | +.PHONY: binaries |
| 157 | +binaries: otelcol |
| 158 | + |
| 159 | +.PHONY: binaries-all-sys |
| 160 | +binaries-all-sys: binaries-darwin_amd64 binaries-linux_amd64 binaries-linux_arm64 binaries-windows_amd64 |
| 161 | + |
| 162 | +.PHONY: binaries-darwin_amd64 |
| 163 | +binaries-darwin_amd64: |
| 164 | + GOOS=darwin GOARCH=amd64 $(MAKE) binaries |
| 165 | + |
| 166 | +.PHONY: binaries-linux_amd64 |
| 167 | +binaries-linux_amd64: |
| 168 | + GOOS=linux GOARCH=amd64 $(MAKE) binaries |
| 169 | + |
| 170 | +.PHONY: binaries-linux_arm64 |
| 171 | +binaries-linux_arm64: |
| 172 | + GOOS=linux GOARCH=arm64 $(MAKE) binaries |
| 173 | + |
| 174 | +.PHONY: binaries-windows_amd64 |
| 175 | +binaries-windows_amd64: |
| 176 | + GOOS=windows GOARCH=amd64 EXTENSION=.exe $(MAKE) binaries |
| 177 | + |
| 178 | +.PHONY: deb-rpm-package |
| 179 | +%-package: ARCH ?= amd64 |
| 180 | +%-package: |
| 181 | + $(MAKE) binaries-linux_$(ARCH) |
| 182 | + docker build -t otelcol-fpm internal/buildscripts/packaging/fpm |
| 183 | + docker run --rm -v $(CURDIR):/repo -e PACKAGE=$* -e VERSION=$(VERSION) -e ARCH=$(ARCH) otelcol-fpm |
0 commit comments