forked from marianob85/open_hardware_monitor-telegraf-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
216 lines (181 loc) · 5.93 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
next_version := $(shell cat build_version.txt)
tag := $(shell git describe --exact-match --tags 2>git_describe_error.tmp; rm -f git_describe_error.tmp)
branch := $(shell git rev-parse --abbrev-ref HEAD)
commit := $(shell git rev-parse --short=8 HEAD)
glibc_version := 2.17
plugin_name := open_hardware_monitor-telegraf-plugin
ifdef NIGHTLY
version := $(next_version)
rpm_version := nightly
rpm_iteration := 0
deb_version := nightly
deb_iteration := 0
tar_version := nightly
else ifeq ($(tag),)
version := $(next_version)
rpm_version := $(version)~$(commit)-0
rpm_iteration := 0
deb_version := $(version)~$(commit)-0
deb_iteration := 0
tar_version := $(version)~$(commit)
else ifneq ($(findstring -rc,$(tag)),)
version := $(word 1,$(subst -, ,$(tag)))
version := $(version:v%=%)
rc := $(word 2,$(subst -, ,$(tag)))
rpm_version := $(version)-0.$(rc)
rpm_iteration := 0.$(subst rc,,$(rc))
deb_version := $(version)~$(rc)-1
deb_iteration := 0
tar_version := $(version)~$(rc)
else
version := $(tag:v%=%)
rpm_version := $(version)-1
rpm_iteration := 1
deb_version := $(version)-1
deb_iteration := 1
tar_version := $(version)
endif
MAKEFLAGS += --no-print-directory
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
HOSTGO := env -u GOOS -u GOARCH -u GOARM -- go
LDFLAGS := $(LDFLAGS) -X main.commit=$(commit) -X main.branch=$(branch) -X main.goos=$(GOOS) -X main.goarch=$(GOARCH)
ifneq ($(tag),)
LDFLAGS += -X main.version=$(version)
endif
# Go built-in race detector works only for 64 bits architectures.
ifneq ($(GOARCH), 386)
race_detector := -race
endif
GOFILES ?= $(shell git ls-files '*.go')
GOFMT ?= $(shell gofmt -l -s $(filter-out plugins/parsers/influx/machine.go, $(GOFILES)))
prefix ?= /usr/local
bindir ?= $(prefix)/bin
sysconfdir ?= $(prefix)/etc
localstatedir ?= $(prefix)/var
pkgdir ?= build/dist
.PHONY: all
all:
@$(MAKE) deps
@$(MAKE) $(plugin_name)
.PHONY: help
help:
@echo 'Targets:'
@echo ' all - download dependencies and compile telegraf binary'
@echo ' deps - download dependencies'
@echo ' $(plugin_name) - compile telegraf binary'
@echo ' test - run short unit tests'
@echo ' fmt - format source files'
@echo ' tidy - tidy go modules'
@echo ' check-deps - check docs/LICENSE_OF_DEPENDENCIES.md'
@echo ' clean - delete build artifacts'
@echo ''
@echo 'Package Targets:'
@$(foreach dist,$(dists),echo " $(dist)";)
.PHONY: deps
deps:
go mod download
.PHONY: $(plugin_name)
$(plugin_name):
go build -ldflags "$(LDFLAGS)" ./cmd/open_hardware_monitor-telegraf-plugin
# Used by dockerfile builds
.PHONY: go-install
go-install:
go install -mod=mod -ldflags "-w -s $(LDFLAGS)" ./cmd/open_hardware_monitor-telegraf-plugin
.PHONY: test
test:
go test -short $(race_detector) ./...
.PHONY: fmt
fmt:
@gofmt -s -w $(filter-out plugins/parsers/influx/machine.go, $(GOFILES))
.PHONY: fmtcheck
fmtcheck:
@if [ ! -z "$(GOFMT)" ]; then \
echo "[ERROR] gofmt has found errors in the following files:" ; \
echo "$(GOFMT)" ; \
echo "" ;\
echo "Run make fmt to fix them." ; \
exit 1 ;\
fi
.PHONY: test-windows
test-windows:
go test -short ./...
.PHONY: vet
vet:
@echo 'go vet $$(go list ./... | grep -v ./plugins/parsers/influx)'
@go vet $$(go list ./... | grep -v ./plugins/parsers/influx) ; if [ $$? -ne 0 ]; then \
echo ""; \
echo "go vet has found suspicious constructs. Please remediate any reported errors"; \
echo "to fix them before submitting code for review."; \
exit 1; \
fi
.PHONY: tidy
tidy:
go mod verify
go mod tidy
@if ! git diff --quiet go.mod go.sum; then \
echo "please run go mod tidy and check in changes"; \
exit 1; \
fi
.PHONY: check
check: fmtcheck vet
@$(MAKE) --no-print-directory tidy
.PHONY: test-all
test-all: fmtcheck vet
go test $(race_detector) ./...
.PHONY: check-deps
check-deps:
./scripts/check-deps.sh
.PHONY: clean
clean:
rm -f $(plugin_name)
rm -f $(plugin_name).exe
rm -rf build
.PHONY: plugin-%
plugin-%:
@echo "Starting dev environment for $${$(@)} input plugin..."
@docker-compose -f plugins/inputs/$${$(@)}/dev/docker-compose.yml up
.PHONY: install
install: $(buildbin)
@mkdir -pv $(DESTDIR)$(bindir)
@cp -fv $(buildbin) $(DESTDIR)$(bindir)
@if [ $(GOOS) = "windows" ]; then cp -fv etc/open_hardware_monitor-telegraf-plugin.config $(DESTDIR)/open_hardware_monitor-telegraf-plugin.config; fi
# Telegraf build per platform. This improves package performance by sharing
# the bin between deb/rpm/tar packages over building directly into the package
# directory.
$(buildbin):
@mkdir -pv $(dir $@)
go build -o $(dir $@) -ldflags "$(LDFLAGS)" ./cmd/open_hardware_monitor-telegraf-plugin
zips += $(plugin_name)-$(tar_version)_windows_amd64.zip
zips += $(plugin_name)-$(tar_version)_windows_i386.zip
dists := $(tars) $(zips)
.PHONY: package
package: $(dists)
.PHONY: $(zips)
$(zips):
@$(MAKE) install
@mkdir -p $(pkgdir)
(cd $(dir $(DESTDIR)) && zip -r - ./*) > $(pkgdir)/$@
.PHONY: $(tars)
$(tars):
@$(MAKE) install
@mkdir -p $(pkgdir)
tar --owner 0 --group 0 -czvf $(pkgdir)/$@ -C $(dir $(DESTDIR)) .
%windows_amd64.zip: export GOOS := windows
%windows_amd64.zip: export GOARCH := amd64
%windows_i386.zip: export GOOS := windows
%windows_i386.zip: export GOARCH := 386
%windows_i386.zip %windows_amd64.zip: export prefix =
%windows_i386.zip %windows_amd64.zip: export bindir = $(prefix)
%windows_i386.zip %windows_amd64.zip: export sysconfdir = $(prefix)
%windows_i386.zip %windows_amd64.zip: export localstatedir = $(prefix)
%windows_i386.zip %windows_amd64.zip: export EXEEXT := .exe
%.tar.gz: export pkg := tar
%.tar.gz: export prefix := /usr
%.tar.gz: export sysconfdir := /etc
%.tar.gz: export localstatedir := /var
%.zip: export pkg := zip
%.zip: export prefix := /
%.deb %.rpm %.tar.gz %.zip: export DESTDIR = build/$(GOOS)-$(GOARCH)$(GOARM)$(cgo)-$(pkg)/$(plugin_name)-$(version)
%.deb %.rpm %.tar.gz %.zip: export buildbin = build/$(GOOS)-$(GOARCH)$(GOARM)$(cgo)/$(plugin_name)$(EXEEXT)
%.deb %.rpm %.tar.gz %.zip: export LDFLAGS = -w -s