From 330a231aa4439324b01b068c25fbb7db7e85f0ac Mon Sep 17 00:00:00 2001 From: Dmitry Holodov Date: Thu, 27 Apr 2023 15:39:11 -0500 Subject: [PATCH] multi-arch build script and version fixes (#435) --- .gitignore | 1 + cliutil/utils.go | 34 +++++++++++++++++------ scripts/release-build.sh | 60 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 9 deletions(-) create mode 100755 scripts/release-build.sh diff --git a/.gitignore b/.gitignore index 61edb11bb..31efe634f 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ # Generated binaries /bin/ +/release-bin/ # Test binary, built with `go test -c` *.test diff --git a/cliutil/utils.go b/cliutil/utils.go index 60c385d8f..21ea34223 100644 --- a/cliutil/utils.go +++ b/cliutil/utils.go @@ -99,8 +99,8 @@ func GetVersion() string { return "unknown-version" } - commitHash := "???????" - dirty := "" + commitHash := "" + sourcesModified := false for _, setting := range info.Settings { switch setting.Key { @@ -108,17 +108,33 @@ func GetVersion() string { commitHash = setting.Value case "vcs.modified": if setting.Value == "true" { - dirty = "-dirty" + sourcesModified = true } } } - return fmt.Sprintf("%s %.7s%s-%s", - info.Main.Version, // " (devel)" unless passing a git tagged version to `go install` - commitHash, // 7 bytes is what "git rev-parse --short HEAD" returns - dirty, // add "-dirty" to commit hash if repo was not clean - info.GoVersion, - ) + var version strings.Builder + + // The first part a go.mod style version if using "go install" directly with + // github. If installing from locally checked out source, the string will be + // "(devel)". + version.WriteString(info.Main.Version) + version.WriteByte(' ') + + // The commit hash will be present if installing from locally checked out + // sources, or empty if installing directly from the repo's github URL. + if commitHash != "" { + // 7 bytes is what "git rev-parse --short HEAD" returns + version.WriteString(fmt.Sprintf("%.7s", commitHash)) + if sourcesModified { + version.WriteString("-dirty") + } + } + + version.WriteByte('-') + version.WriteString(info.GoVersion) + + return version.String() } // ReadUnsignedDecimalFlag reads a string flag and parses it into an *apd.Decimal. diff --git a/scripts/release-build.sh b/scripts/release-build.sh new file mode 100755 index 000000000..b04d4f147 --- /dev/null +++ b/scripts/release-build.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash + +# Fail script on any error (do not change) +set -e + +PROJECT_ROOT="$(dirname "$(dirname "$(realpath "$0")")")" +cd "${PROJECT_ROOT}" + +# Make sure no one is sourcing the script, as we export variables +if [[ "${BASH_SOURCE[0]}" != "$0" ]]; then + echo "Execute ${BASH_SOURCE[0]} instead of souring it" + exit 1 +fi + +version="HEAD" # use "latest" for most recent tagged release +install_targets=( + "github.com/athanorlabs/atomic-swap/cmd/swapd@${version}" + "github.com/athanorlabs/atomic-swap/cmd/swapcli@${version}" + "github.com/athanorlabs/atomic-swap/cmd/bootnode@${version}" +) + +# turn on echo +set -x + +dest_dir=release-bin +rm -rf "${dest_dir}" +mkdir "${dest_dir}" + +# Note: We don't bother with static builds (larger binaries) as swapd depends on +# a local monero-wallet-rpc binary and all releases of monero-wallet-rpc depend +# on glibc. +unset CGO_ENABLED + +# Unfortunately, GOBIN can't be set when doing cross platform builds and +# go install doesn't take an -o flag: +# https://github.com/golang/go/issues/57485 +# We are inside a go module project right now and we'll confuse tooling +# if we put the GOPATH inside of the project. We are using "go install", +# so nothing will go wrong even if a go.mod exists at the top of /tmp. +build_dir="$(mktemp -d /tmp/release-build-XXXXXXXXXX)" + +for os in linux darwin; do + for arch in amd64 arm64; do + GOPATH="${build_dir}" GOOS="${os}" GOARCH="${arch}" \ + go install -tags=prod "${install_targets[@]}" + from_dir="${build_dir}/bin/${os}_${arch}" + to_dir="${dest_dir}/${os/darwin/macos}-${arch/amd64/x64}" + if [[ -d "${from_dir}" ]]; then + # non-native binaries + mv "${from_dir}" "${to_dir}" + else + # native binaries + mkdir "${to_dir}" + mv "${build_dir}/bin/"* "${to_dir}" + fi + done +done + +chmod -R u+w "${build_dir}" +rm -rf "${build_dir}"