Skip to content

Commit

Permalink
go: make go-cross fill main module versions
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdan committed Dec 5, 2022
1 parent 9ef03f6 commit eccc07f
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions .bin/go-cross
@@ -1,31 +1,46 @@
#!/bin/bash

# go-cross [go binary] [version]
# go-cross [go binary]
#
# go-cross builds the main package under the current directory for all the first
# class Go ports. The optional parameters configure which Go command to build
# with, and a version override.
# class Go ports. The optional parameter configures which Go command to build with.
# The build happens via GOPROXY, which allows embedding a useful module version.
# We can avoid that step once https://go.dev/issue/50603 is implemented.
#
# This script is meant to be simple. If you need more, copy and adapt it.
# This script is meant to meet my needs. If you need more, copy and adapt it.

# Consistently turn cgo off, even for our local linux/amd64.
# Consistently turn cgo off, even for our local GOOS/GOARCH.
export CGO_ENABLED=0

# Since we use GOAMD64=v3 locally,
# turn that off for cross-builds for greater compatibility.
export GOAMD64=v1

gocmd=${1:-"go"}
version=${2:-"$(git describe --always --dirty --tags)"}
dir=$(basename "$PWD")
version=${2:-"$(git describe --dirty --tags)"}
outdir=$PWD
name=$(basename "$PWD")
goexe=$($gocmd env GOEXE)

echo "Building version ${version} with $($gocmd version)"
tmpdir=$(mktemp -d --suffix 'go-cross')
finish() { rm -rf tmpdir; }
trap finish EXIT

echo "Building version ${version} with $($gocmd version) from ${tmpdir}"

read modpath pkgpath < <($gocmd list -f '{{.Module.Path}} {{.ImportPath}}' .)
vcshead=$(git rev-parse HEAD)

# Use a temporary module to resolve via GOPROXY. See the docs above.
cd "$tmpdir"
$gocmd mod init test
$gocmd get ${modpath}@${vcshead}

while read goos goarch; do
(
export GOOS=$goos GOARCH=$goarch
out="${dir}_${version}_${goos}_${goarch}$($gocmd env GOEXE)"
$gocmd build -trimpath -ldflags="-w -s -X 'main.version=${version}'" -o="${out}"
out=${name}_${version}_${goos}_${goarch}${goexe}
$gocmd build -trimpath -ldflags="-w -s" -o="${outdir}/${out}" ${pkgpath}
echo $out
) &
done < <(
Expand Down

0 comments on commit eccc07f

Please sign in to comment.