Skip to content

Commit

Permalink
multi-arch build script and version fixes (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimalinux authored Apr 27, 2023
1 parent 498baa3 commit 330a231
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

# Generated binaries
/bin/
/release-bin/

# Test binary, built with `go test -c`
*.test
Expand Down
34 changes: 25 additions & 9 deletions cliutil/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,42 @@ func GetVersion() string {
return "unknown-version"
}

commitHash := "???????"
dirty := ""
commitHash := ""
sourcesModified := false

for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
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.
Expand Down
60 changes: 60 additions & 0 deletions scripts/release-build.sh
Original file line number Diff line number Diff line change
@@ -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}"

0 comments on commit 330a231

Please sign in to comment.