Skip to content

Commit

Permalink
Refactoring makefile.
Browse files Browse the repository at this point in the history
  • Loading branch information
arhamchordia committed Jul 22, 2024
1 parent f443574 commit 66d5733
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 192 deletions.
220 changes: 28 additions & 192 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
#!/usr/bin/make -f

include scripts/makefiles/build.mk
include scripts/makefiles/docker.mk

.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 @@ -105,72 +116,6 @@ ifeq (,$(findstring nostrip,$(QUASAR_BUILD_OPTIONS)))
BUILD_FLAGS += -trimpath
endif

###############################################################################
### Build ###
###############################################################################

all: install lint test

BUILD_TARGETS := build install
#BUILD_TARGETS_DEBUG := build install
build: BUILD_ARGS=-o $(BUILDDIR)/

$(BUILD_TARGETS): go.sum $(BUILDDIR)/
go $@ -mod=readonly $(BUILD_FLAGS) $(BUILD_ARGS) ./cmd/quasarnoded

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

$(BUILDDIR)/:
mkdir -p $(BUILDDIR)/

# 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

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

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

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

###############################################################################
### Proto & Mock Generation ###
###############################################################################
Expand All @@ -196,7 +141,7 @@ proto-doc:

.PHONY: proto-gen proto-doc

mocks: $(MOCKSDIR)/
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 Down Expand Up @@ -247,115 +192,6 @@ test-sim-bench:
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
Expand Down
113 changes: 113 additions & 0 deletions scripts/makefiles/build.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
###############################################################################
### Build ###
###############################################################################

build-help:
@echo "build subcommands"
@echo ""
@echo "Usage:"
@echo " make build-[command]"
@echo ""
@echo "Available Commands:"
@echo " all Build all targets"
@echo " check-version Check Go version"
@echo " dev-build Build development version"
@echo " dev-install Install development build"
@echo " linux Build for Linux"
@echo " reproducible Build reproducible binaries"
@echo " reproducible-amd64 Build reproducible amd64 binary"
@echo " reproducible-arm64 Build reproducible arm64 binary"

build-check-version:
@echo "Go version: $(GO_MAJOR_VERSION).$(GO_MINOR_VERSION)"
@if [ $(GO_MAJOR_VERSION) -gt $(GO_MINIMUM_MAJOR_VERSION) ]; then \
echo "Go version is sufficient"; \
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

build-all: build-check-version go.sum
mkdir -p $(BUILDDIR)/
GOWORK=off go build -mod=readonly $(BUILD_FLAGS) -o $(BUILDDIR)/ ./...

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

# disables optimization, inlining and symbol removal
GC_FLAGS := -gcflags="all=-N -l"
REMOVE_STRING := -w -s
DEBUG_BUILD_FLAGS:= $(subst $(REMOVE_STRING),,$(BUILD_FLAGS))
DEBUG_LDFLAGS = $(subst $(REMOVE_STRING),,$(ldflags))

build-dev-install: go.sum
GOWORK=off go install $(DEBUG_BUILD_FLAGS) $(GC_FLAGS) $(GO_MODULE)/cmd/quasarnoded

build-dev-build:
mkdir -p $(BUILDDIR)/
GOWORK=off go build $(GC_FLAGS) -mod=readonly -ldflags '$(DEBUG_LDFLAGS)' -gcflags "all=-N -l" -trimpath -o $(BUILDDIR) ./...;

all: install lint test

BUILD_TARGETS := build install
#BUILD_TARGETS_DEBUG := build install
build: BUILD_ARGS=-o $(BUILDDIR)/

$(BUILD_TARGETS): go.sum $(BUILDDIR)/
GOWORK=off go $@ -mod=readonly $(BUILD_FLAGS) $(BUILD_ARGS) ./cmd/quasarnoded

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

$(BUILDDIR)/:
mkdir -p $(BUILDDIR)/

###############################################################################
### Build reproducible ###
###############################################################################

build-reproducible: build-reproducible-amd64 build-reproducible-arm64

build-reproducible-amd64: go.sum
mkdir -p $(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:local-amd64 \
--load \
-f Dockerfile .
$(DOCKER) rm -f quasarbinary || true
$(DOCKER) create -ti --name quasarbinary quasar:local-amd64
$(DOCKER) cp quasarbinary:/bin/quasarnoded $(BUILDDIR)/quasarnoded-linux-amd64
$(DOCKER) rm -f quasarbinary

build-reproducible-arm64: go.sum
mkdir -p $(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

go.sum: go.mod
@echo "--> Ensure dependencies have not been modified"
@go mod verify
Loading

0 comments on commit 66d5733

Please sign in to comment.