forked from axone-protocol/axoned
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
269 lines (232 loc) Β· 9.19 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
# βΉ Freely based on: https://gist.github.com/thomaspoignant/5b72d579bd5f311904d973652180c705
# Constants
BINARY_NAME = okp4d
TARGET_FOLDER = target
DIST_FOLDER = $(TARGET_FOLDER)/dist
RELEASE_FOLDER = $(TARGET_FOLDER)/release
DOCKER_IMAGE_GOLANG = golang:1.19-alpine3.16
DOCKER_IMAGE_GOLANG_CI = golangci/golangci-lint:v1.49
DOCKER_IMAGE_BUF = okp4/buf-cosmos:0.3.1
DOCKER_BUILDX_BUILDER = okp4-builder
CMD_ROOT :=./cmd/${BINARY_NAME}
LEDGER_ENABLED ?= true
# Some colors
COLOR_GREEN = $(shell tput -Txterm setaf 2)
COLOR_YELLOW = $(shell tput -Txterm setaf 3)
COLOR_WHITE = $(shell tput -Txterm setaf 7)
COLOR_CYAN = $(shell tput -Txterm setaf 6)
COLOR_RED = $(shell tput -Txterm setaf 1)
COLOR_RESET = $(shell tput -Txterm sgr0)
BUILD_TAGS += netgo
BUILD_TAGS := $(strip $(BUILD_TAGS))
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
# Flags
WHITESPACE := $(subst ,, )
COMMA := ,
BUILD_TAGS_COMMA_SEP := $(subst $(WHITESPACE),$(COMMA),$(BUILD_TAGS))
VERSION := $(shell cat version)
COMMIT := $(shell git log -1 --format='%H')
LD_FLAGS = \
-X github.com/cosmos/cosmos-sdk/version.Name=okp4d \
-X github.com/cosmos/cosmos-sdk/version.ServerName=okp4d \
-X github.com/cosmos/cosmos-sdk/version.ClientName=okp4d \
-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 ($(LINK_STATICALLY),true)
LD_FLAGS += -linkmode=external -extldflags "-Wl,-z,muldefs -static"
endif
LD_FLAGS := $(strip $(LD_FLAGS))
BUILD_FLAGS := -tags "$(BUILD_TAGS)" -ldflags '$(LD_FLAGS)' -trimpath
# Commands
GO_BUiLD := go build $(BUILD_FLAGS)
# Environments
ENVIRONMENTS = \
darwin-amd64 \
darwin-arm64 \
linux-amd64 \
linux-arm64
ENVIRONMENTS_TARGETS = $(addprefix build-go-, $(ENVIRONMENTS))
# Release binaries
RELEASE_BINARIES = \
linux-amd64 \
linux-arm64
RELEASE_TARGETS = $(addprefix release-binary-, $(RELEASE_BINARIES))
.PHONY: all lint lint-go build build-go help
all: help
## Lint:
lint: lint-go lint-proto ## Lint all available linters
lint-go: ## Lint go source code
@echo "${COLOR_CYAN}π Inspecting go source code${COLOR_RESET}"
@docker run --rm \
-v `pwd`:/app:ro \
-w /app \
${DOCKER_IMAGE_GOLANG_CI} \
golangci-lint run -v
lint-proto: ## Lint proto files
@echo "${COLOR_CYAN}ποΈ lint proto${COLOR_RESET}"
@docker run --rm \
-v ${HOME}/.cache:/root/.cache \
-v `pwd`:/proto \
-w /proto \
${DOCKER_IMAGE_BUF} \
lint proto -v
## Format:
format: format-go ## Run all available formatters
format-go: ## Format go files
@echo "${COLOR_CYAN}π Formatting go source code${COLOR_RESET}"
@docker run --rm \
-v `pwd`:/app:rw \
-w /app \
${DOCKER_IMAGE_GOLANG} \
sh -c \
"go install mvdan.cc/gofumpt@v0.4.0; gofumpt -w -l ."
## Build:
build: build-go ## Build all available artefacts (executable, docker image, etc.)
build-go: ## Build node executable for the current environment (default build)
@echo "${COLOR_CYAN} ποΈ Building project ${COLOR_RESET}${CMD_ROOT}${COLOR_CYAN}${COLOR_RESET} into ${COLOR_YELLOW}${DIST_FOLDER}${COLOR_RESET}"
@$(call build-go,"","",${DIST_FOLDER}/${BINARY_NAME})
build-go-all: $(ENVIRONMENTS_TARGETS) ## Build node executables for all available environments
$(ENVIRONMENTS_TARGETS):
@GOOS=$(word 3, $(subst -, ,$@)); \
GOARCH=$(word 4, $(subst -, ,$@)); \
if [ $$GOARCH = "amd64" ]; then \
TARGET_ARCH="x86_64"; \
elif [ $$GOARCH = "arm64" ]; then \
TARGET_ARCH="aarch64"; \
fi; \
HOST_OS=`uname -s | tr A-Z a-z`; \
HOST_ARCH=`uname -m`; \
if [ $$GOOS != $$HOST_OS ] || [ $$TARGET_ARCH != $$HOST_ARCH ]; then \
echo "${COLOR_RED} β Cross compilation impossible${COLOR_RESET}" >&2; \
exit 1; \
fi; \
FOLDER=${DIST_FOLDER}/$$GOOS/$$GOARCH; \
FILENAME=$$FOLDER/${BINARY_NAME}; \
echo "${COLOR_CYAN} ποΈ Building project ${COLOR_RESET}${CMD_ROOT}${COLOR_CYAN} for environment ${COLOR_YELLOW}$$GOOS ($$GOARCH)${COLOR_RESET} into ${COLOR_YELLOW}$$FOLDER${COLOR_RESET}" && \
$(call build-go,$$GOOS,$$GOARCH,$$FILENAME)
## Install:
install: ## Install node executable
@echo "${COLOR_CYAN} π Installing project ${BINARY_NAME}${COLOR_RESET}"
@go install ${BUILD_FLAGS} ${CMD_ROOT}
## Test:
test: test-go ## Pass all the tests
test-go: build ## Pass the test for the go source code
@echo "${COLOR_CYAN} π§ͺ Passing go tests${COLOR_RESET}"
@go test -v -covermode=count -coverprofile ./target/coverage.out ./...
## Clean:
clean: ## Remove all the files from the target folder
@echo "${COLOR_CYAN} π Cleaning folder $(TARGET_FOLDER)${COLOR_RESET}"
@rm -rf $(TARGET_FOLDER)/
## Proto:
proto-format: ## Format Protobuf files
@echo "${COLOR_CYAN} π Formatting Protobuf files${COLOR_RESET}"
@docker run --rm \
-v ${HOME}/.cache:/root/.cache \
-v `pwd`:/proto \
-w /proto \
${DOCKER_IMAGE_BUF} \
format -w -v
proto-build: ## Build all Protobuf files
@echo "${COLOR_CYAN} π¨οΈBuild Protobuf files${COLOR_RESET}"
@docker run --rm \
-v ${HOME}/.cache:/root/.cache \
-v `pwd`:/proto \
-w /proto \
${DOCKER_IMAGE_BUF} \
build proto -v
proto-gen: proto-build ## Generate all the code from the Protobuf files
@echo "${COLOR_CYAN} π Generating code from Protobuf files${COLOR_RESET}"
@docker run --rm \
-v ${HOME}/.cache:/root/.cache \
-v `pwd`:/proto \
-w /proto \
${DOCKER_IMAGE_BUF} \
generate proto --template buf.gen.proto.yaml -v
@cp -r github.com/okp4/okp4d/x/* x/
@rm -rf github.com
proto-gen-doc: proto-gen ## Generate the documention from the Protobuf files
@for MODULE in $(shell find proto -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq | xargs dirname) ; do \
echo "${COLOR_CYAN} π Generate documentation for $${MODULE} module${COLOR_RESET}" ; \
docker run --rm \
-v ${HOME}/.cache:/root/.cache \
-v `pwd`:/proto \
-w /proto \
${DOCKER_IMAGE_BUF} \
generate --path $${MODULE} --template buf.gen.doc.yaml -v ; \
mv docs/proto/docs.md docs/$${MODULE}.md ; \
done
## Release:
release-assets: release-binary-all release-checksums ## Generate release assets
release-binary-all: $(RELEASE_TARGETS)
$(RELEASE_TARGETS): ensure-buildx-builder
@GOOS=$(word 3, $(subst -, ,$@)); \
GOARCH=$(word 4, $(subst -, ,$@)); \
BINARY_NAME="okp4d-${VERSION}-$$GOOS-$$GOARCH"; \
echo "${COLOR_CYAN} π Building ${COLOR_GREEN}$$GOOS $$GOARCH ${COLOR_CYAN}release binary${COLOR_RESET} into ${COLOR_YELLOW}${RELEASE_FOLDER}${COLOR_RESET}"; \
docker buildx use ${DOCKER_BUILDX_BUILDER}; \
docker buildx build \
--platform $$GOOS/$$GOARCH \
-t $$BINARY_NAME \
--load \
.; \
mkdir -p ${RELEASE_FOLDER}; \
docker rm -f tmp-okp4d || true; \
docker create -ti --name tmp-okp4d $$BINARY_NAME; \
docker cp tmp-okp4d:/usr/bin/okp4d ${RELEASE_FOLDER}/$$BINARY_NAME; \
docker rm -f tmp-okp4d; \
tar -zcvf ${RELEASE_FOLDER}/$$BINARY_NAME.tar.gz ${RELEASE_FOLDER}/$$BINARY_NAME;
release-checksums:
@echo "${COLOR_CYAN} πΎ Generating release binary checksums${COLOR_RESET} into ${COLOR_YELLOW}${RELEASE_FOLDER}${COLOR_RESET}"
@rm ${RELEASE_FOLDER}/sha256sum.txt; \
for asset in `ls ${RELEASE_FOLDER}`; do \
shasum -a 256 ${RELEASE_FOLDER}/$$asset >> ${RELEASE_FOLDER}/sha256sum.txt; \
done;
ensure-buildx-builder:
@echo "${COLOR_CYAN} π· Ensuring docker buildx builder${COLOR_RESET}"
@docker buildx ls | sed '1 d' | cut -f 1 -d ' ' | grep -q ${DOCKER_BUILDX_BUILDER} || \
docker buildx create --name ${DOCKER_BUILDX_BUILDER}
## Help:
help: ## Show this help.
@echo ''
@echo 'Usage:'
@echo ' ${COLOR_YELLOW}make${COLOR_RESET} ${COLOR_GREEN}<target>${COLOR_RESET}'
@echo ''
@echo 'Targets:'
@awk 'BEGIN {FS = ":.*?## "} { \
if (/^[a-zA-Z_-]+:.*?##.*$$/) {printf " ${COLOR_YELLOW}%-20s${COLOR_GREEN}%s${COLOR_RESET}\n", $$1, $$2} \
else if (/^## .*$$/) {printf " ${COLOR_CYAN}%s${COLOR_RESET}\n", substr($$1,4)} \
}' $(MAKEFILE_LIST)
@echo ''
@echo 'This Makefile depends on ${COLOR_CYAN}docker${COLOR_RESET}. To install it, please follow the instructions:'
@echo '- for ${COLOR_YELLOW}macOS${COLOR_RESET}: https://docs.docker.com/docker-for-mac/install/'
@echo '- for ${COLOR_YELLOW}Windows${COLOR_RESET}: https://docs.docker.com/docker-for-windows/install/'
@echo '- for ${COLOR_YELLOW}Linux${COLOR_RESET}: https://docs.docker.com/engine/install/'
# Build go executable
# $1: operating system (GOOS)
# $2: architecture (GOARCH)
# $3: filename of the executable generated
define build-go
GOOS=$1 GOARCH=$2 $(GO_BUiLD) -o $3 ${CMD_ROOT}
endef