Skip to content

Commit

Permalink
build: use direnv to set up gotoolchain version
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian committed Sep 19, 2023
1 parent 6952ec2 commit 8a9b97b
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 77 deletions.
20 changes: 19 additions & 1 deletion .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,35 @@ if [ -z "$GOPATH" ]; then
fi

AKASH_ROOT=$(pwd)

export AKASH_ROOT

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

export SEMVER
export GOTOOLCHAIN
export GOTOOLCHAIN_SEMVER

PATH_add "$AKASH_DEVCACHE_NODE_BIN"
PATH_add "$AKASH_DEVCACHE_BIN"

AKASH_DIRENV_SET=1
AKASH=$AKASH_DEVCACHE_BIN/akash

export AKASH_DIRENV_SET
export AKASH

make cache
12 changes: 6 additions & 6 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: git fetch --prune --unshallow
- run: |
toolchain=$(cat go.mod | grep toolchain | cut -d ' ' -f 2 | sed 's/go*//')
echo "GOTOOLCHAIN=go${toolchain}" >> $GITHUB_ENV
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
echo "GOVERSION=${toolchain}" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
Expand Down Expand Up @@ -50,9 +50,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: git fetch --prune --unshallow
- run: |
toolchain=$(cat go.mod | grep toolchain | cut -d ' ' -f 2 | sed 's/go*//')
echo "GOTOOLCHAIN=go${toolchain}" >> $GITHUB_ENV
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
echo "GOVERSION=${toolchain}" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
Expand Down
70 changes: 37 additions & 33 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,34 @@ jobs:
build-macos:
runs-on: macos-latest
steps:
- run: |
- name: Install dependencies
run: |
brew install bash direnv
sudo chsh -s /usr/local/bin/bash
- name: Hook direnv to bash
run: echo 'eval "$(direnv hook bash)"' >> $HOME/.bashrc
- uses: actions/checkout@v3
- run: git fetch --prune --unshallow
- name: set environment
run: direnv allow
- run: |
toolchain=$(cat go.mod | grep toolchain | cut -d ' ' -f 2 | sed 's/go*//')
echo "GOTOOLCHAIN=go${toolchain}" >> $GITHUB_ENV
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
echo "GOVERSION=${toolchain}" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
go-version: "${{ env.GOVERSION }}"
check-latest: true
- name: Setup direnv
run: |
direnv allow
- run: make bins
build-bins:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: git fetch --prune --unshallow
- run: |
toolchain=$(cat go.mod | grep toolchain | cut -d ' ' -f 2 | sed 's/go*//')
echo "GOTOOLCHAIN=go${toolchain}" >> $GITHUB_ENV
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
echo "GOVERSION=${toolchain}" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
Expand All @@ -56,9 +60,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: git fetch --prune --unshallow
- run: |
toolchain=$(cat go.mod | grep toolchain | cut -d ' ' -f 2 | sed 's/go*//')
echo "GOTOOLCHAIN=go${toolchain}" >> $GITHUB_ENV
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
echo "GOVERSION=${toolchain}" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
Expand All @@ -72,9 +76,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: git fetch --prune --unshallow
- run: |
toolchain=$(cat go.mod | grep toolchain | cut -d ' ' -f 2 | sed 's/go*//')
echo "GOTOOLCHAIN=go${toolchain}" >> $GITHUB_ENV
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
echo "GOVERSION=${toolchain}" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
Expand All @@ -89,9 +93,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: git fetch --prune --unshallow
- run: |
toolchain=$(cat go.mod | grep toolchain | cut -d ' ' -f 2 | sed 's/go*//')
echo "GOTOOLCHAIN=go${toolchain}" >> $GITHUB_ENV
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
echo "GOVERSION=${toolchain}" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
Expand All @@ -111,9 +115,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: git fetch --prune --unshallow
- run: |
toolchain=$(cat go.mod | grep toolchain | cut -d ' ' -f 2 | sed 's/go*//')
echo "GOTOOLCHAIN=go${toolchain}" >> $GITHUB_ENV
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
echo "GOVERSION=${toolchain}" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
Expand All @@ -129,9 +133,9 @@ jobs:
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
- uses: actions/checkout@v3
- run: |
toolchain=$(cat go.mod | grep toolchain | cut -d ' ' -f 2 | sed 's/go*//')
echo "GOTOOLCHAIN=go${toolchain}" >> $GITHUB_ENV
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
echo "GOVERSION=${toolchain}" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
Expand All @@ -152,9 +156,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: git fetch --prune --unshallow
- run: |
toolchain=$(cat go.mod | grep toolchain | cut -d ' ' -f 2 | sed 's/go*//')
echo "GOTOOLCHAIN=go${toolchain}" >> $GITHUB_ENV
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
echo "GOVERSION=${toolchain}" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
Expand All @@ -178,9 +182,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: git fetch --prune --unshallow
- run: |
toolchain=$(cat go.mod | grep toolchain | cut -d ' ' -f 2 | sed 's/go*//')
echo "GOTOOLCHAIN=go${toolchain}" >> $GITHUB_ENV
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
echo "GOVERSION=${toolchain}" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
Expand All @@ -205,9 +209,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: git fetch --prune --unshallow
- run: |
toolchain=$(cat go.mod | grep toolchain | cut -d ' ' -f 2 | sed 's/go*//')
echo "GOTOOLCHAIN=go${toolchain}" >> $GITHUB_ENV
- name: Detect required Go version
run: |
toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//')
echo "GOVERSION=${toolchain}" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/akash-network/node

go 1.20

toolchain go1.21.0
go 1.21

require (
github.com/akash-network/akash-api v0.0.27
Expand Down
42 changes: 9 additions & 33 deletions make/init.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,24 @@ UNAME_ARCH := $(shell uname -m)
BASH_PATH := $(shell which bash)

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

AKASH := $(AKASH_DEVCACHE_BIN)/akash
# 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

BINS := $(AKASH)
SEMVER := $(AKASH_ROOT)/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")
ifeq (, $(GOTOOLCHAIN))
$(error "GOTOOLCHAIN is not set")
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')
BINS := $(AKASH)

GO := GO111MODULE=$(GO111MODULE) go
GOWORK ?= on
Expand All @@ -53,21 +44,6 @@ else
DETECTED_OS := $(shell sh -c 'uname 2>/dev/null || echo Unknown')
endif

ifeq ($(DETECTED_OS), Darwin)
# on MacOS disable deprecation warnings security framework
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
endif

# ==== Build tools versions ====
# Format <TOOL>_VERSION
GOLANGCI_LINT_VERSION ?= v1.51.2
Expand Down
3 changes: 2 additions & 1 deletion meta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"releases": {
"revoked": [
"v0.22.1"
"v0.22.1",
"v0.26.0"
]
},
"upgrades": {
Expand Down
34 changes: 34 additions & 0 deletions script/tools.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

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"
}

case "$1" in
gotoolchain)
get_gotoolchain
;;
esac

0 comments on commit 8a9b97b

Please sign in to comment.