Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring makefile. #692

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
305 changes: 60 additions & 245 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,33 +1,48 @@
#!/usr/bin/make -f

include scripts/makefiles/build.mk
include scripts/makefiles/docker.mk
include scripts/makefiles/lint.mk
include scripts/makefiles/proto.mk
include scripts/makefiles/test.mk
include tests/e2e/Makefile

.DEFAULT_GOAL := help
help:
@echo "Available top-level commands:"
@echo ""
@echo "Usage:"
@echo " make [command]"
@echo ""
@echo " make build Build quasarnoded binary"
@echo " make build-help Show available build commands"
@echo " make docker Show available docker commands"
@echo " make e2e Show available e2e commands"
@echo " make install Install quasarnoded binary"
@echo " make lint Show available lint commands"
@echo " make test Show available test commands"
@echo ""
@echo "Run 'make [subcommand]' to see the available commands for each subcommand."

VERSION := $(shell echo $(shell git describe --tags) | sed 's/^v//')
COMMIT := $(shell git log -1 --format='%H')
LEDGER_ENABLED ?= true
SDK_PACK := $(shell go list -m github.com/cosmos/cosmos-sdk | sed 's/ /\@/g')
GO_VERSION := $(shell cat go.mod | grep -E 'go [0-9].[0-9]+' | cut -d ' ' -f 2)
DOCKER := $(shell which docker)
GOMOD := $(shell go list -m)
GO_MODULE := $(shell cat go.mod | grep "module " | cut -d ' ' -f 2)
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)

BUILDDIR ?= $(CURDIR)/build
MOCKSDIR = $(CURDIR)/testutil/mock

export GO111MODULE = on

## Helper function to show help with `make` or `make help`

.DEFAULT_GOAL := help

HELP_FUN = \
%help; while(<>){push@{$$help{$$2//'targets'}},[$$1,$$3] \
if/^([\w-_]+)\s*:.*\#\#(?:@(\w+))?\s(.*)$$/}; \
print"$$_:\n", map" $$_->[0]".(" "x(40-length($$_->[0])))."$$_->[1]\n",\
@{$$help{$$_}},"\n" for keys %help; \

help: ##@misc Show this help
@echo "Usage: make [target] ...\n"
@perl -e '$(HELP_FUN)' $(MAKEFILE_LIST)



# process build tags

build_tags = netgo
Expand Down Expand Up @@ -106,97 +121,48 @@ ifeq (,$(findstring nostrip,$(QUASAR_BUILD_OPTIONS)))
endif

###############################################################################
### Build ###
### Build & Install ###
###############################################################################

all: install lint test

BUILD_TARGETS := build install
#BUILD_TARGETS_DEBUG := build install
build: BUILD_ARGS=-o $(BUILDDIR)/
update-deps:
@go mod tidy;

$(BUILD_TARGETS): go.sum $(BUILDDIR)/
go $@ -mod=readonly $(BUILD_FLAGS) $(BUILD_ARGS) ./cmd/quasarnoded
build: build-check-version go.sum
# back up before build
@cp go.mod go.mod.backup
@cp go.sum go.sum.backup
@go mod tidy

$(BUILD_TARGETS_DEBUG): go.sum $(BUILDDIR)/
go $@ -mod=readonly $(BUILD_FLAGS_DEBUG) -gcflags='all=-N -l' $(BUILD_ARGS) ./cmd/quasarnoded

$(BUILDDIR)/:
mkdir -p $(BUILDDIR)/
GOWORK=off go build -mod=readonly $(BUILD_FLAGS) -o $(BUILDDIR)/ $(GO_MODULE)/cmd/quasarnoded

# Cross-building for arm64 from amd64 (or viceversa) takes
# a lot of time due to QEMU virtualization but it's the only way (afaik)
# to get a statically linked binary with CosmWasm

build-reproducible: build-reproducible-amd64 build-reproducible-arm64
# clean up before install
@mv go.mod.backup go.mod
@mv go.sum.backup go.sum
@rm -f go.mod.bak
@go mod tidy

build-reproducible-amd64: $(BUILDDIR)/
$(DOCKER) buildx create --name quasarbuilder || true
$(DOCKER) buildx use quasarbuilder
$(DOCKER) buildx build \
--build-arg GO_VERSION=$(GO_VERSION) \
--build-arg RUNNER_IMAGE=$(RUNNER_BASE_IMAGE_DISTROLESS) \
--build-arg GIT_VERSION=$(VERSION) \
--build-arg GIT_COMMIT=$(COMMIT) \
--platform linux/amd64 \
-t quasar-amd64 \
--load \
-f Dockerfile .
$(DOCKER) rm -f quasarbinary || true
$(DOCKER) create -ti --name quasarbinary quasar-amd64
$(DOCKER) cp quasarbinary:/bin/quasarnoded $(BUILDDIR)/quasarnoded-linux-amd64
$(DOCKER) rm -f quasarbinary
install: build-check-version go.sum
# back up before build
@cp go.mod go.mod.backup
@cp go.sum go.sum.backup
@go mod tidy

build-reproducible-arm64: $(BUILDDIR)/
$(DOCKER) buildx create --name quasarbuilder || true
$(DOCKER) buildx use quasarbuilder
$(DOCKER) buildx build \
--build-arg GO_VERSION=$(GO_VERSION) \
--build-arg RUNNER_IMAGE=$(RUNNER_BASE_IMAGE_DISTROLESS) \
--build-arg GIT_VERSION=$(VERSION) \
--build-arg GIT_COMMIT=$(COMMIT) \
--platform linux/arm64 \
-t quasar-arm64 \
--load \
-f Dockerfile .
$(DOCKER) rm -f quasarbinary || true
$(DOCKER) create -ti --name quasarbinary quasar-arm64
$(DOCKER) cp quasarbinary:/bin/quasarnoded $(BUILDDIR)/quasarnoded-linux-arm64
$(DOCKER) rm -f quasarbinary
GOWORK=off go install -mod=readonly $(BUILD_FLAGS) $(GO_MODULE)/cmd/quasarnoded

build-linux: go.sum
LEDGER_ENABLED=false GOOS=linux GOARCH=amd64 $(MAKE) build

go.sum: go.mod
@echo "--> Ensure dependencies have not been modified"
@go mod verify
# clean up before install
@mv go.mod.backup go.mod
@mv go.sum.backup go.sum
@rm -f go.mod.bak
@go mod tidy

###############################################################################
### Proto & Mock Generation ###
### Go Mock ###
###############################################################################

proto-all: proto-format proto-gen
BUF_VERSION=1.26.1
BUILDER_VERSION=0.13.5
proto-gen:
@echo "Generating Protobuf files"
@sh ./scripts/protocgen.sh

proto-gen-1:
@echo "🤖 Generating code from protobuf..."
@echo "PWD is $(PWD)"

@docker run --rm --volume "$(PWD)":/workspace --workdir /workspace \
ghcr.io/cosmos/proto-builder:$(BUILDER_VERSION) sh ./scripts/protocgen.sh
@echo "✅ Completed code generation!"

proto-doc:
@echo "Generating Protoc docs"
@sh ./scripts/generate-docs.sh

.PHONY: proto-gen proto-doc

mocks: $(MOCKSDIR)/
# todo : need ideas on external libraries
# example : mockgen -source=/path/to/go/pkg/mod/github.com/cosmos/ibc-go/[email protected]/modules/core/05-port/types/module.go -destination=/path/to/quasar/mock/ics4_wrapper_mocks.go -package=mock -mock_names=MockICS4Wrapper
mocks: $(MOCKSDIR)/
mockgen -package=mock -destination=$(MOCKSDIR)/ibc_channel_mocks.go $(GOMOD)/x/qoracle/types ChannelKeeper
# mockgen -package=mock -destination=$(MOCKSDIR)/ica_mocks.go $(GOMOD)/x/intergamm/types ICAControllerKeeper
# mockgen -package=mock -destination=$(MOCKSDIR)/ibc_mocks.go $(GOMOD)/x/intergamm/types IBCTransferKeeper
Expand All @@ -208,157 +174,6 @@ mocks: $(MOCKSDIR)/
$(MOCKSDIR)/:
mkdir -p $(MOCKSDIR)/

###############################################################################
### Tests & Simulation ###
###############################################################################

PACKAGES_UNIT=$(shell go list ./x/epochs/... ./x/qoracle/... ./x/tokenfactory/... | grep -E -v "simapp|e2e" | grep -E -v "x/qoracle/client/cli")
PACKAGES_E2E=$(shell go list ./... | grep '/tests/e2e')
PACKAGES_SIM=$(shell go list ./... | grep '/tests/simulator')
TEST_PACKAGES=./...

include tests/e2e/Makefile

test: test-unit test-build

test-all: check test-race test-cover

test-unit:
@VERSION=$(VERSION) go test -mod=readonly -tags='ledger test_ledger_mock norace' $(PACKAGES_UNIT)

test-race:
@VERSION=$(VERSION) go test -mod=readonly -race -tags='ledger test_ledger_mock' $(PACKAGES_UNIT)

test-cover:
@VERSION=$(VERSION) go test -mod=readonly -timeout 30m -coverprofile=coverage.txt -tags='norace' -covermode=atomic $(PACKAGES_UNIT)

test-sim-suite:
@VERSION=$(VERSION) go test -mod=readonly $(PACKAGES_SIM)

test-sim-app:
@VERSION=$(VERSION) go test -mod=readonly -run ^TestFullAppSimulation -v $(PACKAGES_SIM)

test-sim-determinism:
@VERSION=$(VERSION) go test -mod=readonly -run ^TestAppStateDeterminism -v $(PACKAGES_SIM)

test-sim-bench:
@VERSION=$(VERSION) go test -benchmem -run ^BenchmarkFullAppSimulation -bench ^BenchmarkFullAppSimulation -cpuprofile cpu.out $(PACKAGES_SIM)

benchmark:
@go test -mod=readonly -bench=. $(PACKAGES_UNIT)

###############################################################################
### 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 quasar:local \
-t quasar:local-distroless \
--build-arg GO_VERSION=$(GO_VERSION) \
--build-arg RUNNER_IMAGE=$(RUNNER_BASE_IMAGE_DISTROLESS) \
--build-arg GIT_VERSION=$(VERSION) \
--build-arg GIT_COMMIT=$(COMMIT) \
-f Dockerfile .

docker-build-distroless: docker-build

docker-build-alpine:
@DOCKER_BUILDKIT=1 docker build \
-t quasar:local-alpine \
--build-arg GO_VERSION=$(GO_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 quasar:local-nonroot \
--build-arg GO_VERSION=$(GO_VERSION) \
--build-arg RUNNER_IMAGE=$(RUNNER_BASE_IMAGE_NONROOT) \
--build-arg GIT_VERSION=$(VERSION) \
--build-arg GIT_COMMIT=$(COMMIT) \
-f Dockerfile .


# This is not available to avoid missbehavior since it seems to be a bug in docker compose -p localenv:
# https://github.com/docker/compose/issues/10068
# docker-compose-up-attached: ##@docker Run (and build if needed) env in docker compose. Attach if running in background.
# @echo "Launching local env with docker-compose"
# DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 docker compose -p localenv -f tests/docker/docker-compose.yml up

docker-compose-up: ##@docker Run local env, build only if no images available
@echo "Launching local env, building images if not available"
DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 docker compose -p localenv -f tests/docker/docker-compose.yml up -d

docker-compose-up-recreate: ##@docker DESTROY env containers and respawn them
@echo "Recreate local env (will destroy application state)"
DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 docker compose -p localenv -f tests/docker/docker-compose.yml up -d --force-recreate

docker-compose-build: ##@docker Build new image if there are code changes, won't recreate containers.
@echo "Rebuilding image for local env"
DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 docker compose -p localenv -f tests/docker/docker-compose.yml build

docker-compose-rebuild: docker-compose-build docker-compose-up-recreate ##@docker Recreate containers building new code if needed
@echo "Rebuilding images and restarting containers"

docker-compose-stop: ##@docker Stop containers without deleting them
@echo "Stop docker containers without removing them"
DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 docker compose -p localenv -f tests/docker/docker-compose.yml stop

docker-compose-down: ##@docker Stop AND DELETE delete the containers
@echo "Stopping docker containers and REMOVING THEM"
DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 docker compose -p localenv -f tests/docker/docker-compose.yml down

docker-attach-quasar: ##@docker Connect to a terminal prompt in QUASAR node container
@echo "Connecting to quasar docker container"
docker exec -it localenv-quasar-1 /bin/bash

docker-attach-osmosis: ##@docker Connect to a terminal prompt in OSMOSIS node container
@echo "Connecting to osmosis docker container"
docker exec -it localenv-osmosis-1 /bin/ash

docker-attach-relayer: ##@docker Connect to a terminal prompt in RLY node container
@echo "Connecting to relayer docker container"
docker exec -it localenv-relayer-1 /bin/bash

docker-test-e2e: docker-compose-up
@echo "Running e2e tests"
cd ./tests/shell/ && ./create_and_execute_contract.sh

###############################################################################
### Docker E2E InterchainTest ###
###############################################################################

docker-e2e-build:
$(eval CHAINS=$(filter-out $@,$(MAKECMDGOALS)))
@for chain in $(CHAINS); do \
echo "Building $$chain"; \
DOCKER_BUILDKIT=1 docker build \
-t $$chain:local \
-t $$chain:local-distroless \
--build-arg GO_VERSION=$(GO_VERSION) \
--build-arg RUNNER_IMAGE=$(RUNNER_BASE_IMAGE_DISTROLESS) \
--build-arg GIT_VERSION=$(VERSION) \
--build-arg GIT_COMMIT=$(COMMIT) \
-f ./tests/e2e/dockerfiles/$$chain.Dockerfile . ;\
done

%:
@:

###############################################################################
### Linting ###
###############################################################################

lint:
@echo "--> Running linter"
@go run github.com/golangci/golangci-lint/cmd/golangci-lint run --timeout=10m

.PHONY: all build-linux install format lint build \
test test-all test-build test-cover test-unit test-race benchmark
Loading
Loading