-
Notifications
You must be signed in to change notification settings - Fork 5
/
Makefile
189 lines (148 loc) · 5.92 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
DBG_MAKEFILE ?=
ifeq ($(DBG_MAKEFILE),1)
$(warning ***** starting Makefile for goal(s) "$(MAKECMDGOALS)")
$(warning ***** $(shell date))
else
# If we're not debugging the Makefile, don't echo recipes.
MAKEFLAGS += -s
endif
# We don't need make's built-in rules.
MAKEFLAGS += --no-builtin-rules
# Be pedantic about undefined variables.
MAKEFLAGS += --warn-undefined-variables
.SUFFIXES:
# This version-strategy uses git tags to set the version string
VERSION ?= $(shell git describe --tags --always --dirty)
VERSION ?= v0.0.1unknown
HASH := $(shell git rev-parse --short HEAD)
# Protocol Buffer related vars
PROTOC_VERSION := 3.20.1
PROTOC_GEN_GO_GRPC_VERSION := v1.1
PROTOC_GEN_GO_VERSION := v1.5.2
PROTOC_OS := linux
PROTOC_ARCH := x86_64
PROM_CLIENT_VERSION := 1.12.1
OPENMETRICS_VERSION := 1.0.0
# List of supported blockchains by the agent
FLOW := flow
SOLANA := solana
PROTOS = $(FLOW) $(SOLANA)
GOOS := linux
GOARCH := amd64
LINUX_GOARCHAR = amd64 arm64
DARWIN_GOARCHAR = amd64 arm64
OSAR = LINUX DARWIN
# Docker
DOCKER = $(shell which docker)
DOCKERFILE := ./docker/Dockerfile
PROTOBIND = protobind
.PHONY: all
all: build
.PHONY: linux-amd64-env
linux-amd64-env:
$(eval GOOS:=linux)
$(eval GOARCH:=amd64)
.PHONY: linux-arm64-env
linux-arm64-env:
$(eval GOOS:=linux)
$(eval GOARCH:=arm64)
.PHONY: osx-amd64-env
osx-amd64-env:
$(eval GOOS:=darwin)
$(eval GOARCH:=amd64)
.PHONY: osx-arm64-env
osx-arm64-env:
$(eval GOOS:=darwin)
$(eval GOARCH:=arm64)
.PHONY: test-%
test-%:
go test -tags=$* ./... -cover -race -count=1 -coverprofile cover.out
.PHONY: test
test: $(foreach b,$(PROTOS),test-$(b))
.PHONY: cover-%
cover-%: test-%
go tool cover -html cover.out -o coverage.html
.PHONY: $(PROTOBIND)-%
$(PROTOBIND)-%:
@cd $(PROTOBIND) && $(MAKE) install-${*}
.PHONY: generate-%
generate-%: $(PROTOBIND)-%
MA_SRC_PATH=$(dir $(abspath $(lastword $(MAKEFILE_LIST)))) \
CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go generate -tags=${*} ./...
.PHONY: build-%-dbg
build-%-dbg: generate-%
echo "Building Metrikad agent: GOOS: $(GOOS) GOARCH: $(GOARCH) PROTO: ${*}"
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o=metrikad-${*}-$(GOOS)-$(GOARCH) -tags=${*} -ldflags=" \
-X 'agent/internal/pkg/global.Version=${VERSION}' \
-X 'agent/internal/pkg/global.CommitHash=${HASH}' \
-X 'agent/internal/pkg/global.Blockchain=${*}' \
" cmd/agent/main.go
.PHONY: build-%-strip
build-%-strip: generate-%
echo "Building Metrikad agent: GOOS: $(GOOS) GOARCH: $(GOARCH) PROTO: ${*}"
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -buildmode=pie -o=metrikad-${*}-$(GOOS)-$(GOARCH) -tags=${*} -ldflags=" \
-s \
-w \
-extldflags=-Wl,-z,relro,-z,now \
-X 'agent/internal/pkg/global.Version=${VERSION}' \
-X 'agent/internal/pkg/global.CommitHash=${HASH}' \
-X 'agent/internal/pkg/global.Blockchain=${*}' \
" cmd/agent/main.go
.PHONY: checksum-%
checksum-%:
sha256sum metrikad-${*}-$(GOOS)-$(GOARCH) > metrikad-${*}-$(GOOS)-$(GOARCH).sha256
.PHONY: docker-build-%
docker-build-%: generate-%
echo "Building Metrikad Docker agent"
$(DOCKER) build --platform $(GOOS)/$(GOARCH) -f $(DOCKERFILE) \
-t metrikad-${*}:${VERSION} \
--build-arg MA_PROTOCOL=${*} \
--build-arg MA_VERSION=${VERSION} .
.PHONY: protogen
protogen:
$(eval PROTOC_TMP := $(shell mktemp -d))
rm -rf $(PWD)/tmp/include/google $(PWD)/tmp/go/io
mkdir -p tmp/include tmp/go tmp/bin tmp/openmetrics api/v1/proto/openmetrics
cd $(PROTOC_TMP); curl -sSL https://github.com/protocolbuffers/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-$(PROTOC_OS)-$(PROTOC_ARCH).zip -o protoc.zip
cd $(PROTOC_TMP); unzip protoc.zip && mv include/google $(PWD)/tmp/include/
cd $(PROTOC_TMP); git clone https://github.com/prometheus/client_model.git && mv client_model/io/ $(PWD)/tmp/go/
cd $(PROTOC_TMP); git clone https://github.com/OpenObservability/OpenMetrics.git && mv OpenMetrics/proto/openmetrics_data_model.proto $(PWD)/tmp/openmetrics/openmetrics.proto
echo '\noption go_package = "./;model";\n' >> $(PWD)/tmp/openmetrics/openmetrics.proto
mv $(PWD)/tmp/openmetrics/openmetrics.proto api/v1/proto/openmetrics/
mv $(PROTOC_TMP)/bin/protoc $(PWD)/tmp/bin/protoc
GOBIN=$(PWD)/tmp/bin go install github.com/golang/protobuf/protoc-gen-go@$(PROTOC_GEN_GO_VERSION)
GOBIN=$(PWD)/tmp/bin go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@$(PROTOC_GEN_GO_GRPC_VERSION)
PATH=$(PWD)/tmp/bin:$$PATH protoc \
-I tmp/include -I tmp/go -I api/v1/proto -I tmp/openmetrics \
--go_opt=Mapi/v1/openmetrics/openmetrics.proto=agent/api/v1/model \
--go_out=api/v1/model \
--go-grpc_out=api/v1/model \
api/v1/proto/agent.proto api/v1/proto/openmetrics/openmetrics.proto
.PHONY: fmt
fmt:
go fmt ./...
.PHONY: build-all-echo
build-all-echo:
echo "protogen $(foreach o,$(OSAR),$(foreach a,$($(o)_GOARCHAR),$(foreach p,$(PROTOS),$(PROTOBIND)-$(p) $(shell echo $(o) | tr A-Z a-z)-$(a)-env build-$(shell echo $(o) | tr A-Z a-z)-$(a)-$(p) checksum-$(shell echo $(o) | tr A-Z a-z)-$(a)-$(p))))"
.PHONY: build-linux-amd64
build-linux-amd64: linux-amd64-env $(foreach b,$(PROTOS),$(PROTOBIND)-$(b) build-$(b)-strip checksum-$(b))
.PHONY: build-linux-arm64
build-linux-arm64: linux-arm64-env $(foreach b,$(PROTOS),$(PROTOBIND)-$(b) build-$(b)-strip checksum-$(b))
.PHONY: build
build: $(foreach b,$(PROTOS),$(PROTOBIND)-$(b) build-$(b)-strip)
.PHONY: build-dbg
build-dbg: $(foreach b,$(PROTOS),$(PROTOBIND)-$(b) build-$(b)-dbg)
.PHONY: docker-build-linux-amd64
docker-build-linux-amd64: linux-amd64-env $(foreach b,$(PROTOS),$(PROTOBIND)-$(b) docker-build-$(b))
.PHONY: docker-build-linux-arm64
docker-build-linux-arm64: linux-arm64-env $(foreach b,$(PROTOS),$(PROTOBIND)-$(b) docker-build-$(b))
.PHONY: clean-%
clean-%:
@rm -rf metrikad-$*
.PHONY: clean
clean: $(foreach b,$(PROTOS),clean-$(b))
@cd $(PROTOBIND) && $(MAKE) clean
define make-build-blockchain
build-$1-linux-$2: linux-$2-env $(PROTOBIND)-$1 build-$1-strip checksum-$1
endef
$(foreach b,$(PROTOS),$(foreach a,$(LINUX_GOARCHAR),$(eval $(call make-build-blockchain,$(b),$(a)))))