Skip to content

Commit

Permalink
Merge pull request #110 from infosiftr/bashbrew-arch-to-goenv
Browse files Browse the repository at this point in the history
Update `bashbrew-arch-to-goenv.sh` with more edge cases
  • Loading branch information
yosifkit authored Jan 17, 2025
2 parents 882b15e + baa0648 commit 9eef708
Showing 1 changed file with 52 additions and 20 deletions.
72 changes: 52 additions & 20 deletions scripts/bashbrew-arch-to-goenv.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh
set -eu
#!/usr/bin/env bash
set -Eeuo pipefail

# usage: (from within another script)
# shell="$(./bashbrew-arch-to-goenv.sh arm32v6)"
Expand All @@ -10,33 +10,65 @@ bashbrewArch="$1"; shift # "amd64", "arm32v5", "windows-amd64", etc.

os="${bashbrewArch%%-*}"
[ "$os" != "$bashbrewArch" ] || os='linux'
printf 'export GOOS="%s"\n' "$os"

arch="${bashbrewArch#${os}-}"

declare -A envs=(
# https://pkg.go.dev/cmd/go#hdr-Build_constraints
# https://gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63

[GOOS]="$os"
[GOARCH]="$arch"

# https://go.dev/wiki/MinimumRequirements#architectures
[GO386]=
[GOAMD64]=
[GOARM64]=
[GOARM]=
[GOMIPS64]=
[GOPPC64]=
[GORISCV64]=
)

case "$arch" in
amd64)
envs[GOAMD64]='v1'
;;

arm32v*)
printf 'export GOARCH="%s"\n' 'arm'
printf 'export GOARM="%s"\n' "${arch#arm32v}"
envs[GOARCH]='arm'
envs[GOARM]="${arch#arm32v}" # "6", "7", "8", etc
;;

arm64v*)
printf 'export GOARCH="%s"\n' 'arm64'
# no GOARM(64) for arm64 (yet?):
# https://github.com/golang/go/blob/be0e0b06ac53d3d02ea83b479790404057b6f19b/src/internal/buildcfg/cfg.go#L86
# https://github.com/golang/go/issues/60905
#printf 'export GOARM64="v%s"\n' "${arch#arm64v}"
printf 'unset GOARM\n'
envs[GOARCH]='arm64'
version="${arch#arm64v}"
if [ -z "${version%%[0-9]}" ]; then
# if the version is just a raw number ("8", "9"), we should append ".0"
# https://go-review.googlesource.com/c/go/+/559555/comment/e2049987_1bc3a065/
# (Go has "v8.0" but no bare "v8")
version+='.0'
fi
envs[GOARM64]="v$version" # "v8.0", "v9.0", etc
;;

i386)
printf 'export GOARCH="%s"\n' '386'
printf 'unset GOARM\n'
envs[GOARCH]='386'
;;

# TODO GOAMD64: https://github.com/golang/go/blob/be0e0b06ac53d3d02ea83b479790404057b6f19b/src/internal/buildcfg/cfg.go#L57-L70 (v1 implied)

*)
printf 'export GOARCH="%s"\n' "$arch"
printf 'unset GOARM\n'
;;
# TODO GOMIPS64?
# TODO GOPPC64?
# TODO GORISCV64?
esac

exports=
unsets=
for key in "${!envs[@]}"; do
val="${envs[$key]}"
if [ -n "$val" ]; then
exports+=" $(printf '%s=%q' "$key" "$val")"
else
unsets+=" $key"
fi
done
[ -z "$exports" ] || printf 'export%s\n' "$exports"
[ -z "$unsets" ] || printf 'unset%s\n' "$unsets"

0 comments on commit 9eef708

Please sign in to comment.