1
- #! /bin/sh
2
- set -eu
1
+ #! /usr/ bin/env bash
2
+ set -Eeuo pipefail
3
3
4
4
# usage: (from within another script)
5
5
# shell="$(./bashbrew-arch-to-goenv.sh arm32v6)"
@@ -10,33 +10,65 @@ bashbrewArch="$1"; shift # "amd64", "arm32v5", "windows-amd64", etc.
10
10
11
11
os=" ${bashbrewArch%% -* } "
12
12
[ " $os " != " $bashbrewArch " ] || os=' linux'
13
- printf ' export GOOS="%s"\n' " $os "
14
-
15
13
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
+
16
32
case " $arch " in
33
+ amd64)
34
+ envs[GOAMD64]=' v1'
35
+ ;;
36
+
17
37
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
20
40
;;
21
41
22
42
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
29
52
;;
30
53
31
54
i386)
32
- printf ' export GOARCH="%s"\n' ' 386'
33
- printf ' unset GOARM\n'
55
+ envs[GOARCH]=' 386'
34
56
;;
35
57
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?
42
61
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