Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update bashbrew-arch-to-goenv.sh with more edge cases #110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading