Skip to content

Commit

Permalink
build: detect toolchain in direnv
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian committed Nov 3, 2023
1 parent 6ea41d2 commit 4fffe16
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 72 deletions.
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
13 changes: 7 additions & 6 deletions .github/workflows/integration-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ 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
uses: HatsuneMiku3939/direnv-action@v1
- name: Setup direnv
run: |
direnv allow
direnv export gha >> "$GITHUB_ENV"
- name: Fetch kind version from go modules
run: echo "KIND_VERSION=$(go list -mod=readonly -m -f '{{ .Version }}' sigs.k8s.io/kind)" >> $GITHUB_ENV
- name: Set up QEMU
Expand Down
13 changes: 7 additions & 6 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ 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
uses: HatsuneMiku3939/direnv-action@v1
- name: Setup direnv
run: |
direnv allow
direnv export gha >> "$GITHUB_ENV"
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
Expand Down
65 changes: 35 additions & 30 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ 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
uses: HatsuneMiku3939/direnv-action@v1
- name: Setup direnv
run: |
direnv allow
direnv export gha >> "$GITHUB_ENV"
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
Expand All @@ -42,16 +43,17 @@ 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
uses: HatsuneMiku3939/direnv-action@v1
- name: Setup direnv
run: |
direnv allow
direnv export gha >> "$GITHUB_ENV"
- run: make test-full

lint:
Expand All @@ -60,16 +62,17 @@ 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
uses: HatsuneMiku3939/direnv-action@v1
- name: Setup direnv
run: |
direnv allow
direnv export gha >> "$GITHUB_ENV"
- run: make build
- run: make test-vet
- name: lint all
Expand All @@ -85,16 +88,17 @@ 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
uses: HatsuneMiku3939/direnv-action@v1
- name: Setup direnv
run: |
direnv allow
direnv export gha >> "$GITHUB_ENV"
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
Expand All @@ -113,16 +117,17 @@ 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
uses: HatsuneMiku3939/direnv-action@v1
- name: Setup direnv
run: |
direnv allow
direnv export gha >> "$GITHUB_ENV"
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
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

0 comments on commit 4fffe16

Please sign in to comment.