From 4fffe16f26b63fa5eaadd5afd9c6a51e1078b341 Mon Sep 17 00:00:00 2001 From: Artur Troian Date: Fri, 3 Nov 2023 08:54:34 -0400 Subject: [PATCH] build: detect toolchain in direnv Signed-off-by: Artur Troian --- .envrc | 20 ++++++++ .github/workflows/integration-tests.yaml | 13 ++--- .github/workflows/release.yaml | 13 ++--- .github/workflows/tests.yaml | 65 +++++++++++++----------- go.mod | 2 - make/init.mk | 41 +++++---------- script/tools.sh | 34 +++++++++++++ 7 files changed, 116 insertions(+), 72 deletions(-) create mode 100755 script/tools.sh diff --git a/.envrc b/.envrc index f5094632..8134cbe9 100644 --- a/.envrc +++ b/.envrc @@ -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 @@ -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 diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml index a57e4e7d..c583c35f 100644 --- a/.github/workflows/integration-tests.yaml +++ b/.github/workflows/integration-tests.yaml @@ -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 diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index d13b42e5..300e847a 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -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 diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 2164e3bc..4273f247 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -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 @@ -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: @@ -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 @@ -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 @@ -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: diff --git a/go.mod b/go.mod index 52ffe1fa..4c941341 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/make/init.mk b/make/init.mk index efd74011..3e44900c 100644 --- a/make/init.mk +++ b/make/init.mk @@ -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) @@ -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 @@ -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 diff --git a/script/tools.sh b/script/tools.sh new file mode 100755 index 00000000..599e1a0e --- /dev/null +++ b/script/tools.sh @@ -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