-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
735 lines (586 loc) · 17.6 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
help:
##
## Make
##
SHELL := /bin/bash
.ONESHELL:
MAKE_MAJOR_VERSION := $(word 1, $(subst ., , $(MAKE_VERSION)))
MAKE_REQUIRED_MAJOR_VERSION := 4
MAKE_BAD_VERSION := $(shell [ $(MAKE_MAJOR_VERSION) -lt $(MAKE_REQUIRED_MAJOR_VERSION) ] && echo true)
ifeq ($(MAKE_BAD_VERSION),true)
$(error Make version is below $(MAKE_REQUIRED_MAJOR_VERSION), please update it.)
endif
##
## uname
##
SHELL_UNAME_S := uname -s
UNAME_S := $(shell $(SHELL_UNAME_S))
ifneq ($(.SHELLSTATUS),0)
$(error $(SHELL_UNAME_S): $(UNAME_S))
endif
SHELL_UNAME_M := uname -m
UNAME_M := $(shell $(SHELL_UNAME_M))
ifneq ($(.SHELLSTATUS),0)
$(error $(SHELL_UNAME_M): $(UNAME_M))
endif
##
## Cache
##
ifeq ($(UNAME_S),Linux)
XDG_CACHE_HOME ?= $(HOME)/.cache
else
ifeq ($(UNAME_S),Darwin)
XDG_CACHE_HOME ?= $(HOME)/Library/Caches
else
$(error Unsupported system: $(UNAME_S))
endif
endif
CACHE_PATH ?= $(XDG_CACHE_HOME)/resonance
##
## Go
##
SHELL_GO_VERSION := cat go.mod | awk '/^go /{print $$2}'
export GOVERSION := go$(shell $(SHELL_GO_VERSION))
ifneq ($(.SHELLSTATUS),0)
$(error $(SHELL_GO_VERSION): $(GOVERSION))
endif
SHELL_GOOS := case $(UNAME_S) in Linux) echo linux;; Darwin) echo darwin;; *) echo Unknown system $(UNAME_S) 1>&2 ; exit 1 ;; esac
export GOOS ?= $(shell $(SHELL_GOOS))
ifneq ($(.SHELLSTATUS),0)
$(error $(SHELL_GOOS): $(GOOS))
endif
SHELL_GOARCH_NATIVE := case $(UNAME_M) in i[23456]86) echo 386;; x86_64) echo amd64;; armv6l|armv7l) echo arm;; aarch64|arm64) echo arm64;; *) echo Unknown machine $(UNAME_M) 1>&2 ; exit 1 ;; esac
GOARCH_NATIVE := $(shell $(SHELL_GOARCH_NATIVE))
ifneq ($(.SHELLSTATUS),0)
$(error $(SHELL_GOARCH_NATIVE): $(GOARCH_NATIVE))
endif
export GOARCH ?= $(GOARCH_NATIVE)
SHELL_GOARCH_DOWNLOAD := case $(GOARCH_NATIVE) in 386) echo 386;; amd64) echo amd64;; arm) echo armv6l;; arm64) echo arm64;; *) echo GOARCH $(GOARCH_NATIVE) 1>&2 ; exit 1 ;; esac
GOARCH_DOWNLOAD ?= $(shell $(SHELL_GOARCH_DOWNLOAD))
ifneq ($(.SHELLSTATUS),0)
$(error $(SHELL_GOARCH_DOWNLOAD): $(GOARCH_DOWNLOAD))
endif
GOROOT_PREFIX := $(CACHE_PATH)/GOROOT
GOROOT := $(GOROOT_PREFIX)/$(GOVERSION).$(GOOS)-$(GOARCH_DOWNLOAD)
GO := $(GOROOT)/bin/go
PATH := $(GOROOT)/bin:$(PATH)
export GOPATH := $(CACHE_PATH)/GOPATH
PATH := $(GOPATH)/bin:$(PATH)
export GOCACHE := $(CACHE_PATH)/GOCACHE
export GOMODCACHE := $(CACHE_PATH)/GOMODCACHE
##
## Go source
##
SHELL_GO_MODULE := cat go.mod | awk '/^module /{print $$2}'
export GO_MODULE := $(shell $(SHELL_GO_MODULE))
ifneq ($(.SHELLSTATUS),0)
$(error $(SHELL_GO_MODULE): $(GO_MODULE))
endif
GO_SOURCE_FILES := $$(find $$PWD -name \*.go ! -path '$(CACHE_PATH)/*')
##
## goimports
##
GOIMPORTS := $(GO) run golang.org/x/tools/cmd/goimports
GOIMPORTS_LOCAL := $(GO_MODULE)
##
## govulncheck
##
GOVULNCHECK := $(GO) run golang.org/x/vuln/cmd/govulncheck
LINT_GOVULNCHECK_DISABLE :=
##
## staticcheck
##
STATICCHECK := $(GO) run honnef.co/go/tools/cmd/staticcheck
export STATICCHECK_CACHE := $(CACHE_PATH)/staticcheck
##
## misspell
##
MISSPELL := $(GO) run github.com/client9/misspell/cmd/misspell
##
## gocyclo
##
GOCYCLO_IGNORE_REGEX := '.*\.pb\.go'
GOCYCLO := $(GO) run github.com/fzipp/gocyclo/cmd/gocyclo
GOCYCLO_OVER := 15
##
## ineffassign
##
INEFFASSIGN := $(GO) run github.com/gordonklaus/ineffassign
##
## go test
##
GO_TEST := $(GO) run github.com/rakyll/gotest
GO_TEST_FLAGS :=
define go_test_build_flags
$(value GO_TEST_BUILD_FLAGS_$(1))
endef
GO_TEST_BUILD_FLAGS :=
# https://go.dev/doc/articles/race_detector#Requirements
ifneq ($(GO_TEST_BUILD_FLAGS_NO_RACE),1)
ifeq ($(GOOS)/$(GOARCH),linux/amd64)
GO_TEST_BUILD_FLAGS_linux_amd64 := -race $(GO_TEST_BUILD_FLAGS)
endif
ifeq ($(GOOS)/$(GOARCH),linux/ppc64le)
GO_TEST_BUILD_FLAGS_linux_ppc64le := -race $(GO_TEST_BUILD_FLAGS)
endif
# https://github.com/golang/go/issues/29948
# ifeq ($(GOOS)/$(GOARCH),linux/arm64)
#_LINUX_ARM64 GO_TEST_BUILD_FLAGS := -race $(GO_TEST_BUILD_FLAGS)
# endif
ifeq ($(GOOS)/$(GOARCH),freebsd/amd64)
GO_TEST_BUILD_FLAGS_freebsd_amd64 := -race $(GO_TEST_BUILD_FLAGS)
endif
ifeq ($(GOOS)/$(GOARCH),netbsd/amd64)
GO_TEST_BUILD_FLAGS_netbsd_amd64 := -race $(GO_TEST_BUILD_FLAGS)
endif
ifeq ($(GOOS)/$(GOARCH),darwin/amd64)
GO_TEST_BUILD_FLAGS_darwin_amd64 := -race $(GO_TEST_BUILD_FLAGS)
endif
ifeq ($(GOOS)/$(GOARCH),darwin/arm64)
GO_TEST_BUILD_FLAGS_darwin_arm64 := -race $(GO_TEST_BUILD_FLAGS)
endif
ifeq ($(GOOS)/$(GOARCH),windows/amd64)
GO_TEST_BUILD_FLAGS_windows_amd64 := -race $(GO_TEST_BUILD_FLAGS)
endif
endif
GO_TEST_PACKAGES_DEFAULT := $(GO_MODULE)/...
GO_TEST_PACKAGES := $(GO_TEST_PACKAGES_DEFAULT)
GO_TEST_BINARY_FLAGS :=
ifneq ($(GO_TEST_NO_COVER),1)
GO_TEST_BINARY_FLAGS := -coverprofile cover.txt -coverpkg $(GO_TEST_PACKAGES) $(GO_TEST_BINARY_FLAGS)
endif
GO_TEST_BINARY_FLAGS := -count=1 $(GO_TEST_BINARY_FLAGS)
GO_TEST_BINARY_FLAGS := -failfast $(GO_TEST_BINARY_FLAGS)
GO_TEST_BINARY_FLAGS_EXTRA :=
GCOV2LCOV := $(GO) run github.com/jandelgado/gcov2lcov
GO_TEST_MIN_COVERAGE := 50
##
## protobuf
##
PROTOC_VERSION := 29.2
ifeq ($(UNAME_S),Linux)
PROTOC_OS := linux
else
ifeq ($(UNAME_S),Darwin)
PROTOC_OS := osx
else
$(error Unsupported system: $(UNAME_S))
endif
endif
SHELL_PROTOC_ARCH := case $(UNAME_M) in i[23456]86) echo x86_32;; x86_64) echo x86_64;; aarch64|arm64) echo aarch_64;; *) echo Unknown machine $(UNAME_M) 1>&2 ; exit 1 ;; esac
PROTOC_ARCH ?= $(shell $(SHELL_PROTOC_ARCH))
ifneq ($(.SHELLSTATUS),0)
$(error $(SHELL_PROTOC_ARCH): $(PROTOC_ARCH))
endif
PROTOC_BIN_PATH := $(CACHE_PATH)/protoc/$(PROTOC_VERSION)/$(PROTOC_OS)-$(PROTOC_ARCH)
PATH := $(PROTOC_BIN_PATH):$(PATH)
PROTOC := $(PROTOC_BIN_PATH)/protoc
PROTOC_PROTO_PATH := ./internal/host/agent_server_grpc/proto
PROTOLINT := $(GO) run github.com/yoheimuta/protolint/cmd/protolint
PROTOLINT_ARGS :=
##
## go build
##
GO_BUILD_FLAGS :=
# osusergo have Lookup and LookupGroup to use pure Go implementation to enable
# management of local users
GO_BUILD_FLAGS_COMMON := -tags osusergo
ifneq ($(GO_BUILD_AGENT_NATIVE_ONLY),1)
GO_BUILD_AGENT_GOARCHS := 386 amd64 arm arm64
else
GO_BUILD_AGENT_GOARCHS := $(GOARCH)
endif
GO_BUILD_MAX_AGENT_SIZE := 8000000
##
## rrb
##
RRB := $(GO) run github.com/fornellas/rrb
RRB_DEBOUNCE ?= 500ms
RRB_IGNORE_PATTERN ?= 'internal/host/agent_server_http_linux_*_gz.go,internal/host/agent_server_grpc_linux_*_gz.go,internal/host/agent_server_grpc/proto/*.pb.go'
RRB_LOG_LEVEL ?= info
RRB_PATTERN ?= '**/*.go,**/*.proto,Makefile'
RRB_MAKE_TARGET ?= ci
RRB_EXTRA_CMD ?= true
##
## Help
##
.PHONY: help
help:
##
## Clean
##
.PHONY: clean
clean:
clean-help:
@echo 'clean: clean all files'
help: clean-help
##
## Go
##
.PHONY: go
go:
set -e
if [ -d $(GOROOT) ] ; then exit ; fi
rm -rf $(GOROOT_PREFIX)/go
mkdir -p $(GOROOT_PREFIX)
curl -sSfL https://go.dev/dl/$(GOVERSION).$(GOOS)-$(GOARCH_DOWNLOAD).tar.gz | \
tar -zx -C $(GOROOT_PREFIX) && \
touch $(GOROOT_PREFIX)/go &&
mv $(GOROOT_PREFIX)/go $(GOROOT)
.PHONY: clean-go
clean-go:
rm -rf $(GOROOT_PREFIX)
rm -rf $(GOCACHE)
find $(GOMODCACHE) -print0 | xargs -0 chmod u+w
rm -rf $(GOMODCACHE)
clean: clean-go
##
## Protobuf
##
.PHONY: install-protoc
install-protoc:
set -e
if [ -x $(PROTOC_BIN_PATH)/protoc ] ; then exit ; fi
mkdir -p $(PROTOC_BIN_PATH)
curl -sSfL https://github.com/protocolbuffers/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-$(PROTOC_OS)-$(PROTOC_ARCH).zip > $(CACHE_PATH)/protoc.zip
unzip -p $(CACHE_PATH)/protoc.zip bin/protoc > $(PROTOC_BIN_PATH)/protoc.tmp
chmod +x $(PROTOC_BIN_PATH)/protoc.tmp
mv $(PROTOC_BIN_PATH)/protoc.tmp $(PROTOC_BIN_PATH)/protoc
.PHONY: clean-install-protoc
clean-install-protoc:
rm -f $(PROTOC_BIN_PATH)/protoc.tmp
rm -f $(PROTOC_BIN_PATH)/protoc
clean: clean-install-protoc
.PHONY: install-protoc-gen-go-grpc
install-protoc-gen-go-grpc: go
$(GO) install google.golang.org/grpc/cmd/protoc-gen-go-grpc
.PHONY: install-protoc-gen-go
install-protoc-gen-go: go
$(GO) install google.golang.org/protobuf/cmd/protoc-gen-go
.PHONY: gen-protofiles
gen-protofiles: install-protoc install-protoc-gen-go install-protoc-gen-go-grpc protolint
$(PROTOC) \
--go_out=. \
--go_opt=paths=source_relative \
--go-grpc_out=. \
--go-grpc_opt=paths=source_relative \
$(PROTOC_PROTO_PATH)/*.proto
.PHONY: clean-gen-protofiles
clean-gen-protofiles:
rm -f $(PROTOC_PROTO_PATH)/*.pb.go
clean: clean-gen-protofiles
##
## Lint
##
# lint
.PHONY: lint-help
lint-help:
@echo 'lint: runs all linters'
@echo ' use LINT_GOVULNCHECK_DISABLE=1 to disable govulncheck (faster)'
@echo ' use PROTOLINT_ARGS to set `protolint lint` arguments (eg: -fix)'
help: lint-help
.PHONY: lint
lint:
# protolint
.PHONY: protolint
protolint: go
$(PROTOLINT) lint $(PROTOLINT_ARGS) .
lint: protolint
# Generate
.PHONY: go-generate
go-generate: go
$(GO) generate ./...
# go mod tidy
.PHONY: go-mod-tidy
go-mod-tidy: go go-generate gen-protofiles
$(GO) mod tidy
lint: go-mod-tidy
# goimports
.PHONY: goimports
goimports: go go-mod-tidy
$(GOIMPORTS) -w -local $(GOIMPORTS_LOCAL) $(GO_SOURCE_FILES)
lint: goimports
# govulncheck
ifneq ($(LINT_GOVULNCHECK_DISABLE),1)
.PHONY: govulncheck
govulncheck: go-generate go go-mod-tidy
$(GOVULNCHECK) $(GO_MODULE)/...
lint: govulncheck
endif
# staticcheck
.PHONY: staticcheck
staticcheck: go go-mod-tidy go-generate goimports
$(STATICCHECK) $(GO_MODULE)/...
lint: staticcheck
.PHONY: clean-staticcheck
clean-staticcheck:
rm -rf $(STATICCHECK_CACHE)
clean: clean-staticcheck
# misspell
.PHONY: misspell
misspell: go go-mod-tidy go-generate
$(MISSPELL) -error $(GO_SOURCE_FILES)
lint: misspell
# gocyclo
.PHONY: gocyclo
gocyclo: go go-generate go-mod-tidy
$(GOCYCLO) -over $(GOCYCLO_OVER) -avg -ignore $(GOCYCLO_IGNORE_REGEX) .
lint: gocyclo
# ineffassign
.PHONY: ineffassign
ineffassign: go go-generate go-mod-tidy
$(INEFFASSIGN) ./...
lint: ineffassign
# go vet
.PHONY: go-vet
go-vet: go go-mod-tidy go-generate
$(GO) vet ./...
lint: go-vet
# go-update
.PHONY: go-update
go-update: go
set -e
set -o pipefail
$(GO) mod edit -go $$(curl -s https://go.dev/VERSION?m=text | head -n 1 | cut -c 3-)
update-deps: go-update
# go get -u
.PHONY: go-get-u-t
go-get-u-t: go go-mod-tidy
$(GO) get -u ./...
update-deps: go-get-u-t
##
## Test
##
# test
.PHONY: test-help
test-help:
@echo 'test: runs all tests:'
@echo ' use GO_TEST_BUILD_FLAGS to set test build flags (see `go test build`)'
@echo ' use GO_TEST_FLAGS to set test flags (see `go help test`)'
@echo ' use GO_TEST_PACKAGES to set packages to test (default: $(GO_TEST_PACKAGES_DEFAULT))'
@echo ' use GO_TEST_BINARY_FLAGS_EXTRA to pass extra flags to the test binary (see `go help testflag`)'
@echo ' use GO_TEST_NO_COVER=1 to disable code coverage (faster)'
@echo ' use GO_TEST_BUILD_FLAGS_NO_RACE=1 to disable -race build flag (faster)'
help: test-help
.PHONY: test
# gotest
.PHONY: gotest
gotest: go go-generate gen-protofiles
$(GO_TEST) \
$(GO_BUILD_FLAGS_COMMON) \
$(call go_test_build_flags,$(GOOS)_$(GOARCH_NATIVE)) \
$(GO_TEST_FLAGS) \
$(GO_TEST_PACKAGES) \
$(GO_TEST_BINARY_FLAGS) \
$(GO_TEST_BINARY_FLAGS_EXTRA)
gotest: build-agent-native
test: gotest
.PHONY: clean-gotest
clean-gotest:
$(GO) env &>/dev/null && $(GO) clean -r -testcache
rm -f cover.txt cover.html
clean: clean-gotest
# cover.html
ifneq ($(GO_TEST_NO_COVER),1)
.PHONY: cover.html
cover.html: go gotest
$(GO) tool cover -html cover.txt -o cover.html
test: cover.html
.PHONY: clean-cover.html
clean-cover.html:
rm -f cover.html
clean: clean-cover.html
# cover.lcov
.PHONY: cover.lcov
cover.lcov: go gotest
$(GCOV2LCOV) -infile cover.txt -outfile cover.lcov
test: cover.lcov
.PHONY: clean-cover.lcov
clean-cover.lcov:
rm -f cover.lcov
clean: clean-cover.lcov
# test-coverage
ifeq ($(GOOS),linux)
.PHONY: test-coverage
test-coverage: go cover.txt
PERCENT=$$($(GO) tool cover -func cover.txt | awk '/^total:/{print $$NF}' | tr -d % | cut -d. -f1) && \
echo "Coverage: $$PERCENT%" && \
if [ $$PERCENT -lt $(GO_TEST_MIN_COVERAGE) ] ; then \
echo "Minimum coverage required: $(GO_TEST_MIN_COVERAGE)%" ; \
exit 1 ; \
fi
test: test-coverage
endif
endif
##
## Build
##
# help
.PHONY: build-help
build-help:
@echo 'build: build everything'
@echo ' use GO_BUILD_FLAGS to add extra build flags (see `go help build`)'
@echo ' use GO_BUILD_AGENT_NATIVE_ONLY=1 to only build agent to native arch (faster)'
help: build-help
# agent http
.PHONY: build-agent-http-%
build-agent-http-%: go-generate
set -e
GOARCH=$* GOOS=linux $(GO) \
build \
-o internal/host/agent_server_http/agent_server_http_linux_$* \
$(GO_BUILD_FLAGS_COMMON) \
$(GO_BUILD_FLAGS) \
./internal/host/agent_server_http/
gzip < internal/host/agent_server_http/agent_server_http_linux_$* > internal/host/agent_server_http/agent_server_http_linux_$*.gz
if ! size=$$(stat -f %z internal/host/agent_server_http/agent_server_http_linux_$*.gz 2>/dev/null) ; then size=$$(stat --printf=%s internal/host/agent_server_http/agent_server_http_linux_$*.gz) ; fi
[ "$$size" -gt $(GO_BUILD_MAX_AGENT_SIZE) ] && { echo "Compressed agent size exceeds $(GO_BUILD_MAX_AGENT_SIZE) bytes" ; exit 1 ; }
cat << EOF > internal/host/agent_server_http_linux_$*_gz.go
package host
import _ "embed"
//go:embed agent_server_http/agent_server_http_linux_$*.gz
var agent_server_http_linux_$* []byte
func init() {
AgentHttpBinGz["linux.$*"] = agent_server_http_linux_$*
}
EOF
build-agent: $(foreach GOARCH,$(GO_BUILD_AGENT_GOARCHS),build-agent-http-$(GOARCH))
build-agent-native: build-agent-http-$(GOARCH_NATIVE)
.PHONY: clean-build-agent-http-%
clean-build-agent-http-%:
rm -f internal/host/agent_server_http/agent_server_http_linux_$*
rm -f internal/host/agent_server_http/agent_server_http_linux_$*.gz
rm -f internal/host/agent_server_http_linux_$*_gz.go
clean-agent: $(foreach GOARCH,$(GO_BUILD_AGENT_GOARCHS),clean-build-agent-http-$(GOARCH))
# agent grpc
.PHONY: build-agent-grpc-%
build-agent-grpc-%: go-generate gen-protofiles
set -e
GOARCH=$* GOOS=linux $(GO) \
build \
-o internal/host/agent_server_grpc/agent_server_grpc_linux_$* \
$(GO_BUILD_FLAGS_COMMON) \
$(GO_BUILD_FLAGS) \
./internal/host/agent_server_grpc/
gzip < internal/host/agent_server_grpc/agent_server_grpc_linux_$* > internal/host/agent_server_grpc/agent_server_grpc_linux_$*.gz
if ! size=$$(stat -f %z internal/host/agent_server_grpc/agent_server_grpc_linux_$*.gz 2>/dev/null) ; then size=$$(stat --printf=%s internal/host/agent_server_grpc/agent_server_grpc_linux_$*.gz) ; fi
[ "$$size" -gt $(GO_BUILD_MAX_AGENT_SIZE) ] && { echo "Compressed agent size exceeds $(GO_BUILD_MAX_AGENT_SIZE) bytes" ; exit 1 ; }
cat << EOF > internal/host/agent_server_grpc_linux_$*_gz.go
package host
import _ "embed"
//go:embed agent_server_grpc/agent_server_grpc_linux_$*.gz
var agent_server_grpc_linux_$* []byte
func init() {
AgentGrpcBinGz["linux.$*"] = agent_server_grpc_linux_$*
}
EOF
build-agent: $(foreach GOARCH,$(GO_BUILD_AGENT_GOARCHS),build-agent-grpc-$(GOARCH))
build-agent-native: build-agent-grpc-$(GOARCH_NATIVE)
.PHONY: clean-build-agent-grpc-%
clean-build-agent-grpc-%:
rm -f internal/host/agent_server_grpc/agent_server_grpc_linux_$*
rm -f internal/host/agent_server_grpc/agent_server_grpc_linux_$*.gz
rm -f internal/host/agent_server_grpc_linux_$*_gz.go
clean-agent: $(foreach GOARCH,$(GO_BUILD_AGENT_GOARCHS),clean-build-agent-grpc-$(GOARCH))
# clean agent
clean: clean-agent
build: clean-agent
go-generate: clean-agent
goimports: clean-agent
go-mod-tidy: clean-agent
go-get-u-t: clean-agent
staticcheck: clean-agent
misspell: clean-agent
gocyclo: clean-agent
go-vet: clean-agent
# build
.PHONY: build
build: go go-generate build-agent gen-protofiles
$(GO) \
build \
-o resonance.$(GOOS).$(GOARCH) \
$(GO_BUILD_FLAGS_COMMON) \
$(GO_BUILD_FLAGS) \
./cmd/
.PHONY: clean-build
clean-build:
$(GO) env &>/dev/null && $(GO) clean -r -cache -modcache
rm -f internal/.version
rm -f internal/.git-toplevel
rm -f resonance.*.*
clean: clean-build
##
## ci
##
.PHONY: ci-help
ci-help:
@echo 'ci: runs the whole build'
@echo 'ci-dev: similar to ci, but uses options that speed up the build, at the expense of minimal signal loss;'
help: ci-help
.PHONY: ci
ci: lint test build
.PHONY: ci-dev
ci-dev:
$(MAKE) $(MFLAGS) MAKELEVEL= ci \
LINT_GOVULNCHECK_DISABLE=1 \
GO_TEST_NO_COVER=1 \
GO_TEST_BUILD_FLAGS_NO_RACE=1 \
GO_BUILD_AGENT_NATIVE_ONLY=1
##
## update
##
.PHONY: update-deps-help
update-deps-help:
@echo 'update-deps: Update all dependencies'
help: update-deps-help
.PHONY: update-deps
update-deps:
##
## rrb
##
ifeq ($(GOOS),linux)
.PHONY: rrb-help
rrb-help:
@echo 'rrb: rerun build automatically on file changes'
@echo ' use RRB_DEBOUNCE to set debounce (default: $(RRB_DEBOUNCE))'
@echo ' use RRB_IGNORE_PATTERN to set ignore pattern (default: $(RRB_IGNORE_PATTERN))'
@echo ' use RRB_LOG_LEVEL to set log level (default: $(RRB_LOG_LEVEL))'
@echo ' use RRB_PATTERN to set the pattern (default: $(RRB_PATTERN))'
@echo ' use RRB_MAKE_TARGET to set the make target (default: $(RRB_MAKE_TARGET))'
@echo ' use RRB_EXTRA_CMD to set a command to run after the build is successful (default: $(RRB_EXTRA_CMD))'
@echo 'rrb-dev: similar to rrb, but with RRB_MAKE_TARGET=ci-dev'
help: rrb-help
.PHONY: rrb
rrb: go
$(RRB) \
--debounce $(RRB_DEBOUNCE) \
--ignore-pattern $(RRB_IGNORE_PATTERN) \
--log-level $(RRB_LOG_LEVEL) \
--pattern $(RRB_PATTERN) \
-- \
sh -c "$(MAKE) $(MFLAGS) $(RRB_MAKE_TARGET) && $(RRB_EXTRA_CMD)"
.PHONY: rrb-dev
rrb-dev:
$(MAKE) $(MFLAGS) MAKELEVEL= \
rrb \
RRB_MAKE_TARGET=ci-dev
endif
##
## shell
##
.PHONY: shell-help
shell-help:
@echo 'shell: starts a development shell'
help: shell-help
.PHONY: shell
shell:
@echo Make targets:
@$(MAKE) help MAKELEVEL=
@PATH=$(GOROOT)/bin:$(PATH) \
GOOS=$(GOOS) \
GOARCH=$(GOARCH) \
GOROOT=$(GOROOT) \
GOCACHE=$(GOCACHE) \
GOMODCACHE=$(GOMODCACHE) \
STATICCHECK_CACHE=$(STATICCHECK_CACHE) \
bash --rcfile .bashrc