Skip to content

Commit

Permalink
build: detect gotoolchain in envrc (#82)
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian authored Oct 26, 2023
1 parent fed1855 commit c37f56a
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 47 deletions.
20 changes: 20 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@ export AKASH_ROOT=$(pwd)
dotenv
dotenv_if_exists dev.env

TOOLS=${AKASH_ROOT}/script/tools.sh
SEMVER=${AKASH_ROOT}/script/semver.sh

GOTOOLCHAIN=$(${TOOLS} gotoolchain)
GOTOOLCHAIN_SEMVER=$(echo "${GOTOOLCHAIN}" | sed 's/go*/v/' | tr -d '\n')

if [[ "$OSTYPE" == "darwin"* ]]; then
# on MacOS disable deprecation warnings security framework
CGO_CFLAGS=-Wno-deprecated-declarations

export CGO_CFLAGS
fi

AKASH_DIRENV_SET=1

export SEMVER
export GOTOOLCHAIN
export GOTOOLCHAIN_SEMVER
export AKASH_DIRENV_SET

PATH_add "$AKASH_DEVCACHE_NODE_BIN"
PATH_add "$AKASH_DEVCACHE_BIN"

Expand Down
55 changes: 8 additions & 47 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,21 @@ UNAME_ARCH := $(shell uname -m)
PROTO_LEGACY ?= true

ifeq (, $(shell which direnv))
$(warning "No direnv in $(PATH), consider installing. https://direnv.net")
$(error "No direnv in $(PATH), consider installing. https://direnv.net")
endif

ifneq (1, $(AKASH_DIRENV_SET))
$(error "no envrc detected. might need to run \"direnv allow\"")
endif

# AKASH_ROOT may not be set if environment does not support/use direnv
# in this case define it manually as well as all required env variables
ifndef AKASH_ROOT
AKASH_ROOT := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))/../)
include $(AKASH_ROOT)/.env

# setup .cache bins first in paths to have precedence over already installed same tools for system wide use
PATH := $(AKASH_DEVCACHE_BIN):$(AKASH_DEVCACHE_NODE_BIN):$(PATH)
$(error "AKASH_ROOT is not set. might need to run \"direnv allow\"")
endif

SEMVER := $(ROOT_DIR)/script/semver.sh

__local_go := $(shell GOTOOLCHAIN=local go version | cut -d ' ' -f 3 | sed 's/go*//' | tr -d '\n')
__is_local_go_satisfies := $(shell $(SEMVER) compare "v$(__local_go)" "v1.20.7"; echo $?)

ifeq (-1, $(__is_local_go_satisfies))
$(error "unsupported local go$(__local_go) version . min required go1.21.0")
endif

GO_VERSION := $(shell go mod edit -json | jq -r .Go | tr -d '\n')
GOTOOLCHAIN := $(shell go mod edit -json | jq -r .Toolchain | tr -d '\n')
GOTOOLCHAIN_SEMVER := v$(shell echo "$(GOTOOLCHAIN)" | sed 's/go*//' | tr -d '\n')

ifeq ($(OS),Windows_NT)
DETECTED_OS := Windows
else
DETECTED_OS := $(shell sh -c 'uname 2>/dev/null || echo Unknown')
endif

# on MacOS disable deprecation warnings security framework
ifeq ($(DETECTED_OS), Darwin)
export CGO_CFLAGS=-Wno-deprecated-declarations

# on MacOS Sonoma Beta there is a bit of discrepancy between Go and new prime linker
clang_version := $(shell echo | clang -dM -E - | grep __clang_major__ | cut -d ' ' -f 3 | tr -d '\n')
go_has_ld_fix := $(shell $(SEMVER) compare "$(GOTOOLCHAIN_SEMVER)" "v1.22.0" | tr -d '\n')

ifeq (15,$(clang_version))
ifeq (-1,$(go_has_ld_fix))
export CGO_LDFLAGS=-Wl,-ld_classic -Wno-deprecated-declarations
endif
endif
ifeq (, $(GOTOOLCHAIN))
$(error "GOTOOLCHAIN is not set")
endif

GO := GO111MODULE=$(GO111MODULE) go
Expand Down Expand Up @@ -90,15 +60,6 @@ SWAGGER_COMBINE := $(AKASH_DEVCACHE_NODE_BIN)/swagger-combine
DOCKER_RUN := docker run --rm -v $(shell pwd):/workspace -w /workspace
DOCKER_BUF := $(DOCKER_RUN) bufbuild/buf:$(BUF_VERSION)

# AKASH_ROOT may not be set if environment does not support/use direnv
# in this case define it manually as well as all required env variables
ifndef AKASH_ROOT
AKASH_ROOT := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))/../)
include $(AKASH_ROOT)/.env
# setup .cache bins first in paths to have precedence over already installed same tools for system wide use
PATH := $(AKASH_DEVCACHE_BIN):$(AKASH_DEVCACHE_NODE_BIN):$(PATH)
endif

include $(AKASH_ROOT)/make/setup-cache.mk
include $(AKASH_ROOT)/make/mod.mk
include $(AKASH_ROOT)/make/test.mk
Expand Down
114 changes: 114 additions & 0 deletions script/tools.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/usr/bin/env bash

set -o pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
SEMVER=$SCRIPT_DIR/semver.sh

gomod="$SCRIPT_DIR/../go.mod"

function get_gotoolchain() {
local gotoolchain
local goversion

gotoolchain=$(grep -E '^toolchain go[0-9]{1,}.[0-9]{1,}.[0-9]{1,}$' < "$gomod" | cut -d ' ' -f 2 | tr -d '\n')

if [[ ${gotoolchain} == "" ]]; then
# determine go toolchain from go version in go.mod
if which go > /dev/null 2>&1 ; then
goversion=$(GOTOOLCHAIN=local go version | cut -d ' ' -f 3 | sed 's/go*//' | tr -d '\n')
fi

if [[ $goversion != "" ]] && [[ $($SEMVER compare "v$goversion" v1.21.0) -ge 0 ]]; then
gotoolchain=go${goversion}
else
gotoolchain=go$(grep -E '^go [0-9]{1,}.[0-9]{1,}$' < "$gomod" | cut -d ' ' -f 2 | tr -d '\n').0
fi
fi

echo -n "$gotoolchain"
}

replace_paths() {
local file="${1}"
local cversion="${2}"
local nversion="${3}"
local sedcmd=sed

if [[ "$OSTYPE" == "darwin"* ]]; then
sedcmd=gsed
fi

$sedcmd -ri "s/github.com\/akash-network\/node\/(v${cversion})?/github.com\/akash-network\/node\/v${nversion}\//g" "${file}"
}

function replace_import_path() {
local next_major_version=$1
local import_path_to_replace
import_path_to_replace=$(go list -m)

local version_to_replace
version_to_replace=$(echo "$import_path_to_replace" | sed -n 's/.*v\([0-9]*\).*/\1/p')

echo "$version_to_replace"
echo Current import paths are "$version_to_replace", replacing with "$next_major_version"

# list all folders containing Go modules.
# local modules
# modules=$(go list -tags e2e ./... | sed "s/g.*v${version_to_replace}\///")

while IFS= read -r line; do
modules_to_upgrade_manually+=("$line")
done < <(find . -name go.mod -exec grep -l "github.com/akash-network/node" {} \; | grep -v "^./go.mod$" | sed 's|/go.mod||' | sed 's|^./||')

echo "Replacing import paths in all files"

declare -a files

while IFS= read -r line; do
files+=("$line")
done < <(find . -type f -not \(-path "./install.sh" -or -path "./upgrades/software/*" -or -path "./upgrades/heightpatches/*" -or -path "./.cache/*" -or -path "./dist/*" -or -path "./.git*" -or -name "*.md" -or -path "./.idea/*" \))

# echo "Updating all files"

for file in "${files[@]}"; do
if test -f "$file"; then
# skip files that need manual upgrading
for excluded_file in "${modules_to_upgrade_manually[@]}"; do
if [[ "$file" == *"$excluded_file"* ]]; then
continue 2
fi
done
replace_paths "$file" "$version_to_replace" "$next_major_version"
fi
done

# exit 0

# echo "Updating go.mod and vendoring"
# go.mod
# replace_paths "go.mod"
# go mod tidy >/dev/null
# go mod vendor >/dev/null

# ensure that generated files are updated.
# N.B.: This must be run after go mod vendor.
# echo "running make proto-gen"
# make proto-gen >/dev/null
#
# echo "Run go mod vendor after proto-gen to avoid vendoring issues"
# go mod vendor >/dev/null
#
# echo "running make run-querygen"
# make run-querygen >/dev/null
}

case "$1" in
gotoolchain)
get_gotoolchain
;;
replace-import-path)
shift
replace_import_path "$@"
;;
esac

0 comments on commit c37f56a

Please sign in to comment.