-
Notifications
You must be signed in to change notification settings - Fork 17
/
Makefile
167 lines (138 loc) · 8.52 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
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
157
158
159
160
161
162
163
164
165
166
167
################################################################################
# This file is AUTOGENERATED with <https://github.com/sapcc/go-makefile-maker> #
# Edit Makefile.maker.yaml instead. #
################################################################################
# Copyright 2024 SAP SE
# SPDX-License-Identifier: Apache-2.0
MAKEFLAGS=--warn-undefined-variables
# /bin/sh is dash on Debian which does not support all features of ash/bash
# to fix that we use /bin/bash only on Debian to not break Alpine
ifneq (,$(wildcard /etc/os-release)) # check file existence
ifneq ($(shell grep -c debian /etc/os-release),0)
SHELL := /bin/bash
endif
endif
default: build-all
install-golangci-lint: FORCE
@if ! hash golangci-lint 2>/dev/null; then printf "\e[1;36m>> Installing golangci-lint (this may take a while)...\e[0m\n"; go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; fi
install-go-licence-detector: FORCE
@if ! hash go-licence-detector 2>/dev/null; then printf "\e[1;36m>> Installing go-licence-detector...\e[0m\n"; go install go.elastic.co/go-licence-detector@latest; fi
install-addlicense: FORCE
@if ! hash addlicense 2>/dev/null; then printf "\e[1;36m>> Installing addlicense...\e[0m\n"; go install github.com/google/addlicense@latest; fi
prepare-static-check: FORCE install-golangci-lint install-go-licence-detector install-addlicense
GO_BUILDFLAGS = -mod vendor
GO_LDFLAGS =
GO_TESTENV =
GO_BUILDENV =
# These definitions are overridable, e.g. to provide fixed version/commit values when
# no .git directory is present or to provide a fixed build date for reproducibility.
BININFO_VERSION ?= $(shell git describe --tags --always --abbrev=7)
BININFO_COMMIT_HASH ?= $(shell git rev-parse --verify HEAD)
BININFO_BUILD_DATE ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
build-all: build/ntp_exporter
build/ntp_exporter: FORCE
@env $(GO_BUILDENV) go build $(GO_BUILDFLAGS) -ldflags '-s -w -X github.com/sapcc/go-api-declarations/bininfo.binName=ntp_exporter -X github.com/sapcc/go-api-declarations/bininfo.version=$(BININFO_VERSION) -X github.com/sapcc/go-api-declarations/bininfo.commit=$(BININFO_COMMIT_HASH) -X github.com/sapcc/go-api-declarations/bininfo.buildDate=$(BININFO_BUILD_DATE) $(GO_LDFLAGS)' -o build/ntp_exporter .
DESTDIR =
ifeq ($(shell uname -s),Darwin)
PREFIX = /usr/local
else
PREFIX = /usr
endif
install: FORCE build/ntp_exporter
install -d -m 0755 "$(DESTDIR)$(PREFIX)/bin"
install -m 0755 build/ntp_exporter "$(DESTDIR)$(PREFIX)/bin/ntp_exporter"
# which packages to test with test runner
GO_TESTPKGS := $(shell go list -f '{{if or .TestGoFiles .XTestGoFiles}}{{.ImportPath}}{{end}}' ./...)
ifeq ($(GO_TESTPKGS),)
GO_TESTPKGS := ./...
endif
# which packages to measure coverage for
GO_COVERPKGS := $(shell go list ./...)
# to get around weird Makefile syntax restrictions, we need variables containing nothing, a space and comma
null :=
space := $(null) $(null)
comma := ,
check: FORCE static-check build/cover.html build-all
@printf "\e[1;32m>> All checks successful.\e[0m\n"
run-golangci-lint: FORCE install-golangci-lint
@printf "\e[1;36m>> golangci-lint\e[0m\n"
@golangci-lint run
build/cover.out: FORCE | build
@printf "\e[1;36m>> Running tests\e[0m\n"
@env $(GO_TESTENV) go test -shuffle=on -p 1 -coverprofile=$@ $(GO_BUILDFLAGS) -ldflags '-s -w -X github.com/sapcc/go-api-declarations/bininfo.binName=ntp_exporter -X github.com/sapcc/go-api-declarations/bininfo.version=$(BININFO_VERSION) -X github.com/sapcc/go-api-declarations/bininfo.commit=$(BININFO_COMMIT_HASH) -X github.com/sapcc/go-api-declarations/bininfo.buildDate=$(BININFO_BUILD_DATE) $(GO_LDFLAGS)' -covermode=count -coverpkg=$(subst $(space),$(comma),$(GO_COVERPKGS)) $(GO_TESTPKGS)
build/cover.html: build/cover.out
@printf "\e[1;36m>> go tool cover > build/cover.html\e[0m\n"
@go tool cover -html $< -o $@
static-check: FORCE run-golangci-lint check-dependency-licenses check-license-headers
build:
@mkdir $@
vendor: FORCE
go mod tidy
go mod vendor
go mod verify
vendor-compat: FORCE
go mod tidy -compat=$(shell awk '$$1 == "go" { print $$2 }' < go.mod)
go mod vendor
go mod verify
force-license-headers: FORCE install-addlicense
@printf "\e[1;36m>> addlicense\e[0m\n"
echo -n $(patsubst $(shell awk '$$1 == "module" {print $$2}' go.mod)%,.%/*.go,$(shell go list ./...)) | xargs -d" " -I{} bash -c 'year="$$(rg -P "Copyright (....) SAP SE" -Nor "\$$1" {})"; awk -i inplace '"'"'{if (display) {print} else {!/^\/\*/ && !/^\*/ && !/^\$$/}}; /^package /{print;display=1}'"'"' {}; addlicense -c "SAP SE" -s=only -y "$$year" -- {}'
license-headers: FORCE install-addlicense
@printf "\e[1;36m>> addlicense\e[0m\n"
@addlicense -c "SAP SE" -s=only -- $(patsubst $(shell awk '$$1 == "module" {print $$2}' go.mod)%,.%/*.go,$(shell go list ./...))
check-license-headers: FORCE install-addlicense
@printf "\e[1;36m>> addlicense --check\e[0m\n"
@addlicense --check -- $(patsubst $(shell awk '$$1 == "module" {print $$2}' go.mod)%,.%/*.go,$(shell go list ./...))
check-dependency-licenses: FORCE install-go-licence-detector
@printf "\e[1;36m>> go-licence-detector\e[0m\n"
@go list -m -mod=readonly -json all | go-licence-detector -includeIndirect -rules .license-scan-rules.json -overrides .license-scan-overrides.jsonl
clean: FORCE
git clean -dxf build
vars: FORCE
@printf "BININFO_BUILD_DATE=$(BININFO_BUILD_DATE)\n"
@printf "BININFO_COMMIT_HASH=$(BININFO_COMMIT_HASH)\n"
@printf "BININFO_VERSION=$(BININFO_VERSION)\n"
@printf "DESTDIR=$(DESTDIR)\n"
@printf "GO_BUILDENV=$(GO_BUILDENV)\n"
@printf "GO_BUILDFLAGS=$(GO_BUILDFLAGS)\n"
@printf "GO_COVERPKGS=$(GO_COVERPKGS)\n"
@printf "GO_LDFLAGS=$(GO_LDFLAGS)\n"
@printf "GO_TESTENV=$(GO_TESTENV)\n"
@printf "GO_TESTPKGS=$(GO_TESTPKGS)\n"
@printf "PREFIX=$(PREFIX)\n"
help: FORCE
@printf "\n"
@printf "\e[1mUsage:\e[0m\n"
@printf " make \e[36m<target>\e[0m\n"
@printf "\n"
@printf "\e[1mGeneral\e[0m\n"
@printf " \e[36mvars\e[0m Display values of relevant Makefile variables.\n"
@printf " \e[36mhelp\e[0m Display this help.\n"
@printf "\n"
@printf "\e[1mPrepare\e[0m\n"
@printf " \e[36minstall-golangci-lint\e[0m Install golangci-lint required by run-golangci-lint/static-check\n"
@printf " \e[36minstall-go-licence-detector\e[0m Install-go-licence-detector required by check-dependency-licenses/static-check\n"
@printf " \e[36minstall-addlicense\e[0m Install addlicense required by check-license-headers/license-headers/static-check\n"
@printf " \e[36mprepare-static-check\e[0m Install any tools required by static-check. This is used in CI before dropping privileges, you should probably install all the tools using your package manager\n"
@printf "\n"
@printf "\e[1mBuild\e[0m\n"
@printf " \e[36mbuild-all\e[0m Build all binaries.\n"
@printf " \e[36mbuild/ntp_exporter\e[0m Build ntp_exporter.\n"
@printf " \e[36minstall\e[0m Install all binaries. This option understands the conventional 'DESTDIR' and 'PREFIX' environment variables for choosing install locations.\n"
@printf "\n"
@printf "\e[1mTest\e[0m\n"
@printf " \e[36mcheck\e[0m Run the test suite (unit tests and golangci-lint).\n"
@printf " \e[36mrun-golangci-lint\e[0m Install and run golangci-lint. Installing is used in CI, but you should probably install golangci-lint using your package manager.\n"
@printf " \e[36mbuild/cover.out\e[0m Run tests and generate coverage report.\n"
@printf " \e[36mbuild/cover.html\e[0m Generate an HTML file with source code annotations from the coverage report.\n"
@printf " \e[36mstatic-check\e[0m Run static code checks\n"
@printf "\n"
@printf "\e[1mDevelopment\e[0m\n"
@printf " \e[36mvendor\e[0m Run go mod tidy, go mod verify, and go mod vendor.\n"
@printf " \e[36mvendor-compat\e[0m Same as 'make vendor' but go mod tidy will use '-compat' flag with the Go version from go.mod file as value.\n"
@printf " \e[36mforce-license-headers\e[0m Remove and re-add all license headers to all non-vendored source code files.\n"
@printf " \e[36mlicense-headers\e[0m Add license headers to all non-vendored source code files.\n"
@printf " \e[36mcheck-license-headers\e[0m Check license headers in all non-vendored .go files.\n"
@printf " \e[36mcheck-dependency-licenses\e[0m Check all dependency licenses using go-licence-detector.\n"
@printf " \e[36mclean\e[0m Run git clean.\n"
.PHONY: FORCE