-
Notifications
You must be signed in to change notification settings - Fork 16
/
Makefile
254 lines (215 loc) Β· 9.41 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
#!/usr/bin/make -f
VERSION := $(shell echo $(shell git describe --tags) | sed 's/^v//')
COMMIT := $(shell git log -1 --format='%H')
DOCKER := $(shell which docker)
BUILDDIR ?= $(CURDIR)/build
LEDGER_ENABLED ?= true
# ********** Golang configs **********
export GO111MODULE = on
# the Go version to use in reproducible build
GO_VERSION := 1.20
# currently installed Go version
GO_MAJOR_VERSION = $(shell go version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f1)
GO_MINOR_VERSION = $(shell go version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f2)
# minimum supported Go version
GO_MINIMUM_MAJOR_VERSION = $(shell cat go.mod | grep -E 'go [0-9].[0-9]+' | cut -d ' ' -f2 | cut -d'.' -f1)
GO_MINIMUM_MINOR_VERSION = $(shell cat go.mod | grep -E 'go [0-9].[0-9]+' | cut -d ' ' -f2 | cut -d'.' -f2)
GO_VERSION_ERR_MSG = β ERROR: Go version $(GO_MINIMUM_MAJOR_VERSION).$(GO_MINIMUM_MINOR_VERSION)+ is required
# ********** process build tags **********
build_tags = netgo
ifeq ($(LEDGER_ENABLED),true)
ifeq ($(OS),Windows_NT)
GCCEXE = $(shell where gcc.exe 2> NUL)
ifeq ($(GCCEXE),)
$(error gcc.exe not installed for ledger support, please install or set LEDGER_ENABLED=false)
else
build_tags += ledger
endif
else
UNAME_S = $(shell uname -s)
ifeq ($(UNAME_S),OpenBSD)
$(warning OpenBSD detected, disabling ledger support (https://github.com/cosmos/cosmos-sdk/issues/1988))
else
GCC = $(shell command -v gcc 2> /dev/null)
ifeq ($(GCC),)
$(error gcc not installed for ledger support, please install or set LEDGER_ENABLED=false)
else
build_tags += ledger
endif
endif
endif
endif
ifeq (cleveldb,$(findstring cleveldb,$(MARS_BUILD_OPTIONS)))
build_tags += gcc cleveldb
else ifeq (rocksdb,$(findstring rocksdb,$(MARS_BUILD_OPTIONS)))
build_tags += gcc rocksdb
endif
build_tags += $(BUILD_TAGS)
build_tags := $(strip $(build_tags))
whitespace :=
whitespace := $(whitespace) $(whitespace)
comma := ,
build_tags_comma_sep := $(subst $(whitespace),$(comma),$(build_tags))
# ********** process linker flags **********
ldflags = -X github.com/cosmos/cosmos-sdk/version.Name=mars \
-X github.com/cosmos/cosmos-sdk/version.AppName=marsd \
-X github.com/cosmos/cosmos-sdk/version.Version=$(VERSION) \
-X github.com/cosmos/cosmos-sdk/version.Commit=$(COMMIT) \
-X github.com/cosmos/cosmos-sdk/version.BuildTags=$(build_tags_comma_sep)
ifeq (cleveldb,$(findstring cleveldb,$(MARS_BUILD_OPTIONS)))
ldflags += -X github.com/cosmos/cosmos-sdk/types.DBBackend=cleveldb
else ifeq (rocksdb,$(findstring rocksdb,$(MARS_BUILD_OPTIONS)))
ldflags += -X github.com/cosmos/cosmos-sdk/types.DBBackend=rocksdb
endif
ifeq (,$(findstring nostrip,$(MARS_BUILD_OPTIONS)))
ldflags += -w -s
endif
ifeq ($(LINK_STATICALLY),true)
ldflags += -linkmode=external -extldflags "-Wl,-z,muldefs -static"
endif
ldflags += $(LDFLAGS)
ldflags := $(strip $(ldflags))
BUILD_FLAGS := -tags '$(build_tags)' -ldflags '$(ldflags)'
# check for nostrip option
ifeq (,$(findstring nostrip,$(MARS_BUILD_OPTIONS)))
BUILD_FLAGS += -trimpath
endif
all: proto-gen lint test install
################################################################################
### Build ###
################################################################################
install: enforce-go-version
@echo "π€ Installing marsd..."
go install -mod=readonly $(BUILD_FLAGS) ./cmd/marsd
@echo "β
Completed installation!"
build: enforce-go-version
@echo "π€ Building marsd..."
go build $(BUILD_FLAGS) -o $(BUILDDIR)/ ./cmd/marsd
@echo "β
Completed build!"
# For use when publishing releases
#
# Copied from Osmosis:
# https://github.com/osmosis-labs/osmosis/blob/v14.0.1/Makefile#L111
build-reproducible: build-reproducible-amd64 build-reproducible-arm64
build-reproducible-amd64: go.sum $(BUILDDIR)/
$(DOCKER) buildx create --name marsbuilder || true
$(DOCKER) buildx use marsbuilder
$(DOCKER) buildx build \
--build-arg GO_VERSION=$(GO_VERSION) \
--build-arg GIT_VERSION=$(VERSION) \
--build-arg GIT_COMMIT=$(COMMIT) \
--build-arg RUNNER_IMAGE=alpine:3.17 \
--platform linux/amd64 \
-t mars:local-amd64 \
--load \
-f Dockerfile .
$(DOCKER) rm -f marsbinary || true
$(DOCKER) create -ti --name marsbinary mars:local-amd64
$(DOCKER) cp marsbinary:/bin/marsd $(BUILDDIR)/marsd-linux-amd64
$(DOCKER) rm -f marsbinary
build-reproducible-arm64: go.sum $(BUILDDIR)/
$(DOCKER) buildx create --name marsbuilder || true
$(DOCKER) buildx use marsbuilder
$(DOCKER) buildx build \
--build-arg GO_VERSION=$(GO_VERSION) \
--build-arg GIT_VERSION=$(VERSION) \
--build-arg GIT_COMMIT=$(COMMIT) \
--build-arg RUNNER_IMAGE=alpine:3.17 \
--platform linux/arm64 \
-t mars:local-arm64 \
--load \
-f Dockerfile .
$(DOCKER) rm -f marsbinary || true
$(DOCKER) create -ti --name marsbinary mars:local-arm64
$(DOCKER) cp marsbinary:/bin/marsd $(BUILDDIR)/marsd-linux-arm64
$(DOCKER) rm -f marsbinary
# Make sure that Go version is 1.19+
#
# From Osmosis discord:
# https://discord.com/channels/798583171548840026/837144686387920936/1049449765240315925
#
# > Valardragon - 12/05/2022 10:18 PM
# > It was just pointed out from `@jhernandez | stargaze.zone`, that the choice
# of golang version between go 1.18 and go 1.19 is consensus critical.
# With insufficient info, this preliminarily seems due to go 1.19 changing the
# memory model format, and something state-affecting in cosmwasm getting altered.
# https://github.com/persistenceOne/incident-reports/blob/main/06-nov-2022_V4_upgrade_halt.md
enforce-go-version:
@echo "π€ Go version: $(GO_MAJOR_VERSION).$(GO_MINOR_VERSION)"
@if [ $(GO_MAJOR_VERSION) -gt $(GO_MINIMUM_MAJOR_VERSION) ]; then \
exit 0; \
elif [ $(GO_MAJOR_VERSION) -lt $(GO_MINIMUM_MAJOR_VERSION) ]; then \
echo '$(GO_VERSION_ERR_MSG)'; \
exit 1; \
elif [ $(GO_MINOR_VERSION) -lt $(GO_MINIMUM_MINOR_VERSION) ]; then \
echo '$(GO_VERSION_ERR_MSG)'; \
exit 1; \
fi
################################################################################
### Tests ###
################################################################################
test:
@echo "π€ Running tests..."
go test -mod=readonly ./x/...
@echo "β
Completed tests!"
################################################################################
### Protobuf ###
################################################################################
# We use osmolabs' docker image instead of tendermintdev/sdk-proto-gen.
# The only difference is that the Osmosis version uses Go 1.19 while the
# tendermintdev one uses 1.18.
protoVer=v0.8
protoImageName=osmolabs/osmo-proto-gen:$(protoVer)
containerProtoGenGo=mars-proto-gen-go-$(protoVer)
containerProtoGenSwagger=mars-proto-gen-swagger-$(protoVer)
proto-gen: proto-go-gen proto-swagger-gen
proto-go-gen:
@echo "π€ Generating Go code from protobuf..."
@if docker ps -a --format '{{.Names}}' | grep -Eq "^${containerProtoGenGo}$$"; then docker start -a $(containerProtoGenGo); else docker run --name $(containerProtoGenGo) -v $(CURDIR):/workspace --workdir /workspace $(protoImageName) \
sh ./scripts/protocgen.sh; fi
@echo "β
Completed Go code generation!"
proto-swagger-gen:
@echo "π€ Generating Swagger code from protobuf..."
@if docker ps -a --format '{{.Names}}' | grep -Eq "^${containerProtoGenSwagger}$$"; then docker start -a $(containerProtoGenSwagger); else docker run --name $(containerProtoGenSwagger) -v $(CURDIR):/workspace --workdir /workspace $(protoImageName) \
sh ./scripts/protoc-swagger-gen.sh; fi
@echo "β
Completed Swagger code generation!"
################################################################################
### Docker ###
################################################################################
RUNNER_BASE_IMAGE_DISTROLESS := gcr.io/distroless/static-debian11
RUNNER_BASE_IMAGE_ALPINE := alpine:3.17
RUNNER_BASE_IMAGE_NONROOT := gcr.io/distroless/static-debian11:nonroot
docker-build:
@DOCKER_BUILDKIT=1 $(DOCKER) build \
-t mars:local \
-t mars:local-distroless \
--build-arg GO_VERSION=$(GO_MAJOR_VERSION).$(GO_MINOR_VERSION) \
--build-arg RUNNER_IMAGE=$(RUNNER_BASE_IMAGE_DISTROLESS) \
--build-arg GIT_VERSION=$(VERSION) \
--build-arg GIT_COMMIT=$(COMMIT) \
-f Dockerfile .
docker-build-alpine:
@DOCKER_BUILDKIT=1 $(DOCKER) build \
-t mars:local-alpine \
--build-arg GO_VERSION=$(GO_MAJOR_VERSION).$(GO_MINOR_VERSION) \
--build-arg RUNNER_IMAGE=$(RUNNER_BASE_IMAGE_ALPINE) \
--build-arg GIT_VERSION=$(VERSION) \
--build-arg GIT_COMMIT=$(COMMIT) \
-f Dockerfile .
docker-build-nonroot:
@DOCKER_BUILDKIT=1 $(DOCKER) build \
-t mars:local-nonroot \
--build-arg GO_VERSION=$(GO_MAJOR_VERSION).$(GO_MINOR_VERSION) \
--build-arg RUNNER_IMAGE=$(RUNNER_BASE_IMAGE_NONROOT) \
--build-arg GIT_VERSION=$(VERSION) \
--build-arg GIT_COMMIT=$(COMMIT) \
-f Dockerfile .
docker-build-distroless: docker-build
################################################################################
### Linting ###
################################################################################
golangci_lint_cmd=github.com/golangci/golangci-lint/cmd/golangci-lint
lint:
@echo "π€ Running linter..."
go run $(golangci_lint_cmd) run --timeout=10m
@echo "β
Completed linting!"