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

build: detect toolchain in direnv #160

Merged
merged 1 commit into from
Nov 3, 2023
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
20 changes: 20 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,20 @@ if ! has readlink ; then
echo -e "\033[31mreadlink is not installed"; exit 1
fi

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

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

AKASH_DIRENV_SET=1

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

export CGO_CFLAGS

if ! has brew; then
echo -e "\033[31mhomebrew is not installed. visit https://brew.sh"
exit 1
Expand Down Expand Up @@ -88,6 +101,13 @@ PATH_add "$path"
PROVIDER_SERVICES=$AP_DEVCACHE_BIN/provider-services
AKASH=$AP_DEVCACHE_BIN/akash

export SEMVER
export GOTOOLCHAIN
export GOTOOLCHAIN_SEMVER

PATH_add "$path"

export AKASH_DIRENV_SET
export PROVIDER_SERVICES
export AKASH

Expand Down
9 changes: 4 additions & 5 deletions .github/workflows/integration-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- 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
echo "GOROOT=/opt/hostedtoolcache/go/${toolchain}/x64" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
go-version: "${{ env.GOVERSION }}"
- name: set environment
- name: Setup direnv
uses: HatsuneMiku3939/direnv-action@v1
- name: Fetch kind version from go modules
run: echo "KIND_VERSION=$(go list -mod=readonly -m -f '{{ .Version }}' sigs.k8s.io/kind)" >> $GITHUB_ENV
Expand Down
9 changes: 4 additions & 5 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- 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
echo "GOROOT=/opt/hostedtoolcache/go/${toolchain}/x64" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
go-version: "${{ env.GOVERSION }}"
- name: set environment
- name: Setup direnv
uses: HatsuneMiku3939/direnv-action@v1
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
Expand Down
54 changes: 29 additions & 25 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- 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
echo "GOROOT=/opt/hostedtoolcache/go/${toolchain}/x64" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
go-version: "${{ env.GOVERSION }}"
- name: set environment
- name: Setup direnv
uses: HatsuneMiku3939/direnv-action@v1
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
Expand All @@ -42,15 +41,14 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- 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
echo "GOROOT=/opt/hostedtoolcache/go/${toolchain}/x64" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
go-version: "${{ env.GOVERSION }}"
- name: set environment
- name: Setup direnv
uses: HatsuneMiku3939/direnv-action@v1
- run: make test-full

Expand All @@ -60,15 +58,14 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- 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
echo "GOROOT=/opt/hostedtoolcache/go/${toolchain}/x64" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
go-version: "${{ env.GOVERSION }}"
- name: set environment
- name: Setup direnv
uses: HatsuneMiku3939/direnv-action@v1
- run: make build
- run: make test-vet
Expand All @@ -85,15 +82,14 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- 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
echo "GOROOT=/opt/hostedtoolcache/go/${toolchain}/x64" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
go-version: "${{ env.GOVERSION }}"
- name: set environment
- name: Setup direnv
uses: HatsuneMiku3939/direnv-action@v1
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
Expand All @@ -113,15 +109,14 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- 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
echo "GOROOT=/opt/hostedtoolcache/go/${toolchain}/x64" >> $GITHUB_ENV
- uses: actions/setup-go@v4
with:
go-version: "${{ env.GOVERSION }}"
- name: set environment
- name: Setup direnv
uses: HatsuneMiku3939/direnv-action@v1
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
Expand All @@ -142,6 +137,15 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 1
- 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 }}"
- name: Setup direnv
uses: HatsuneMiku3939/direnv-action@v1
- run: make shellcheck

integration-tests:
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module github.com/akash-network/provider

go 1.21

toolchain go1.21.0

require (
github.com/akash-network/akash-api v0.0.29
github.com/akash-network/node v0.24.2
Expand Down
41 changes: 13 additions & 28 deletions make/init.mk
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@ ifneq (true, $(shell [ $(BASH_MAJOR_VERSION) -ge 4 ] && echo true))
$(error "bash version $(shell $(BASH_PATH) -c 'echo $$BASH_VERSION') is outdated. min required 4.0")
endif

# AP_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 AP_ROOT
AP_ROOT := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))/../)
ifeq (, $(shell which direnv))
$(error "No direnv in $(PATH), consider installing. https://direnv.net")
endif

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

PROVIDER_SERVICES ?= $(AP_DEVCACHE_BIN)/provider-services
AKASH ?= $(AP_DEVCACHE_BIN)/akash
# 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 AP_ROOT
$(error "AP_ROOT is not set. might need to run \"direnv allow\"")
endif

# setup .cache bins first in paths to have precedence over already installed same tools for system wide use
PATH := $(AP_DEVCACHE_BIN):$(AP_DEVCACHE_NODE_BIN):$(PATH)
ifeq (, $(GOTOOLCHAIN))
$(error "GOTOOLCHAIN is not set")
endif

UNAME_OS := $(shell uname -s)
Expand All @@ -62,10 +66,6 @@ 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')

GOWORK ?= on
GO_MOD ?= readonly
export GO := GO111MODULE=$(GO111MODULE) go
Expand All @@ -79,21 +79,6 @@ 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
endif

GO_MOD_NAME := $(shell go list -m 2>/dev/null)

AKASHD_MODULE := github.com/akash-network/node
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