Skip to content

Commit 9eef708

Browse files
authored
Merge pull request #110 from infosiftr/bashbrew-arch-to-goenv
Update `bashbrew-arch-to-goenv.sh` with more edge cases
2 parents 882b15e + baa0648 commit 9eef708

File tree

1 file changed

+52
-20
lines changed

1 file changed

+52
-20
lines changed

scripts/bashbrew-arch-to-goenv.sh

+52-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#!/bin/sh
2-
set -eu
1+
#!/usr/bin/env bash
2+
set -Eeuo pipefail
33

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

1111
os="${bashbrewArch%%-*}"
1212
[ "$os" != "$bashbrewArch" ] || os='linux'
13-
printf 'export GOOS="%s"\n' "$os"
14-
1513
arch="${bashbrewArch#${os}-}"
14+
15+
declare -A envs=(
16+
# https://pkg.go.dev/cmd/go#hdr-Build_constraints
17+
# https://gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63
18+
19+
[GOOS]="$os"
20+
[GOARCH]="$arch"
21+
22+
# https://go.dev/wiki/MinimumRequirements#architectures
23+
[GO386]=
24+
[GOAMD64]=
25+
[GOARM64]=
26+
[GOARM]=
27+
[GOMIPS64]=
28+
[GOPPC64]=
29+
[GORISCV64]=
30+
)
31+
1632
case "$arch" in
33+
amd64)
34+
envs[GOAMD64]='v1'
35+
;;
36+
1737
arm32v*)
18-
printf 'export GOARCH="%s"\n' 'arm'
19-
printf 'export GOARM="%s"\n' "${arch#arm32v}"
38+
envs[GOARCH]='arm'
39+
envs[GOARM]="${arch#arm32v}" # "6", "7", "8", etc
2040
;;
2141

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

3154
i386)
32-
printf 'export GOARCH="%s"\n' '386'
33-
printf 'unset GOARM\n'
55+
envs[GOARCH]='386'
3456
;;
3557

36-
# TODO GOAMD64: https://github.com/golang/go/blob/be0e0b06ac53d3d02ea83b479790404057b6f19b/src/internal/buildcfg/cfg.go#L57-L70 (v1 implied)
37-
38-
*)
39-
printf 'export GOARCH="%s"\n' "$arch"
40-
printf 'unset GOARM\n'
41-
;;
58+
# TODO GOMIPS64?
59+
# TODO GOPPC64?
60+
# TODO GORISCV64?
4261
esac
62+
63+
exports=
64+
unsets=
65+
for key in "${!envs[@]}"; do
66+
val="${envs[$key]}"
67+
if [ -n "$val" ]; then
68+
exports+=" $(printf '%s=%q' "$key" "$val")"
69+
else
70+
unsets+=" $key"
71+
fi
72+
done
73+
[ -z "$exports" ] || printf 'export%s\n' "$exports"
74+
[ -z "$unsets" ] || printf 'unset%s\n' "$unsets"

0 commit comments

Comments
 (0)