Skip to content

Commit ac90278

Browse files
authored
Merge pull request #63 from crazy-max/goamd64
go amd64 variant support
2 parents c608d8c + 29cd28a commit ac90278

File tree

6 files changed

+125
-20
lines changed

6 files changed

+125
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ RUN mkdir build && cd build && \
309309

310310
## Go / Cgo
311311

312-
Building Go can be achieved with the `xx-go` wrapper that automatically sets up values for `GOOS`, `GOARCH`, `GOARM` etc. It also sets up `pkg-config` and C compiler if building with CGo. Note that by default, CGo is enabled in Go when compiling for native architecture and disabled when cross-compiling. This can easily produce unexpected results; therefore, you should always define either `CGO_ENABLED=1` or `CGO_ENABLED=0` depending on if you expect your compilation to use CGo or not.
312+
Building Go can be achieved with the `xx-go` wrapper that automatically sets up values for `GOOS`, `GOARCH`, `GOARM`, `GOAMD64` etc. It also sets up `pkg-config` and C compiler if building with CGo. Note that by default, CGo is enabled in Go when compiling for native architecture and disabled when cross-compiling. This can easily produce unexpected results; therefore, you should always define either `CGO_ENABLED=1` or `CGO_ENABLED=0` depending on if you expect your compilation to use CGo or not.
313313

314314
```dockerfile
315315
FROM --platform=$BUILDPLATFORM golang:alpine

src/test-go.bats

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ testEnv() {
4545
assert_output --partial "GOHOSTARCH=$(TARGETOS= TARGETARCH= xx-info arch)"
4646

4747
case "$(xx-info arch)" in
48+
"amd64")
49+
if supportAmd64VariantGo; then
50+
assert_output --partial "GOAMD64=$expAmd64"
51+
fi
52+
;;
4853
"arm")
4954
assert_output --partial "GOARM=$expArm"
5055
;;
@@ -78,6 +83,39 @@ testEnv() {
7883
testEnv
7984
}
8085

86+
@test "amd64v2-env" {
87+
if ! supportAmd64VariantGo; then
88+
skip "Amd64 Variant GO not supported"
89+
fi
90+
export TARGETARCH=amd64
91+
export TARGETVARIANT=v2
92+
expAmd64=v2
93+
testEnv
94+
unset TARGETVARIANT
95+
}
96+
97+
@test "amd64v3-env" {
98+
if ! supportAmd64VariantGo; then
99+
skip "Amd64 Variant GO not supported"
100+
fi
101+
export TARGETARCH=amd64
102+
export TARGETVARIANT=v3
103+
expAmd64=v3
104+
testEnv
105+
unset TARGETVARIANT
106+
}
107+
108+
@test "amd64v4-env" {
109+
if ! supportAmd64VariantGo; then
110+
skip "Amd64 Variant GO not supported"
111+
fi
112+
export TARGETARCH=amd64
113+
export TARGETVARIANT=v4
114+
expAmd64=v4
115+
testEnv
116+
unset TARGETVARIANT
117+
}
118+
81119
@test "arm64-env" {
82120
export TARGETARCH=arm64
83121
testEnv
@@ -218,6 +256,16 @@ testHelloGO() {
218256
testHelloGO
219257
}
220258

259+
@test "amd64v2-hellogo" {
260+
if ! supportAmd64VariantGo; then
261+
skip "Amd64 Variant GO not supported"
262+
fi
263+
export TARGETARCH=amd64
264+
export TARGETVARIANT=v2
265+
testHelloGO
266+
unset TARGETVARIANT
267+
}
268+
221269
@test "arm64-hellogo" {
222270
export TARGETARCH=arm64
223271
testHelloGO

src/test-info-alpine.bats

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ load 'assert'
2424
assert_equal "x86_64" "$(TARGETPLATFORM=linux/amd64 xx-info pkg-arch)"
2525
}
2626

27+
@test "amd64v2" {
28+
assert_equal "x86_64-alpine-linux-musl" "$(TARGETPLATFORM=linux/amd64/v2 xx-info triple)"
29+
assert_equal "x86_64" "$(TARGETPLATFORM=linux/amd64/v2 xx-info pkg-arch)"
30+
assert_equal "v2" "$(TARGETPLATFORM=linux/amd64/v2 xx-info variant)"
31+
}
32+
33+
@test "amd64v3" {
34+
assert_equal "x86_64-alpine-linux-musl" "$(TARGETPLATFORM=linux/amd64/v3 xx-info triple)"
35+
assert_equal "x86_64" "$(TARGETPLATFORM=linux/amd64/v3 xx-info pkg-arch)"
36+
assert_equal "v3" "$(TARGETPLATFORM=linux/amd64/v3 xx-info variant)"
37+
}
38+
39+
@test "amd64v4" {
40+
assert_equal "x86_64-alpine-linux-musl" "$(TARGETPLATFORM=linux/amd64/v4 xx-info triple)"
41+
assert_equal "x86_64" "$(TARGETPLATFORM=linux/amd64/v4 xx-info pkg-arch)"
42+
assert_equal "v4" "$(TARGETPLATFORM=linux/amd64/v4 xx-info variant)"
43+
}
44+
2745
@test "aarch64" {
2846
assert_equal "aarch64-alpine-linux-musl" "$(TARGETPLATFORM=linux/arm64 xx-info triple)"
2947
assert_equal "aarch64" "$(TARGETPLATFORM=linux/arm64 xx-info pkg-arch)"

src/test_helper.bash

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ supportRiscVCGo() {
9191
versionGTE "$(go version | awk '{print $3}' | sed 's/^go//')" "1.16"
9292
}
9393

94+
supportAmd64VariantGo() {
95+
versionGTE "$(go version | awk '{print $3}' | sed 's/^go//')" "1.18"
96+
}
97+
9498
supportRC() {
9599
command -v llvm-rc >/dev/null 2>&1
96100
}

src/xx-go

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,50 @@ done
99
export GOOS=${TARGETOS}
1010
export GOARCH=${TARGETARCH}
1111

12-
if [ "$TARGETARCH" = "arm" ]; then
13-
if [ -n "$TARGETVARIANT" ]; then
14-
case "$TARGETVARIANT" in
15-
"v5")
16-
export GOARM="5"
17-
;;
18-
"v6")
19-
export GOARM="6"
20-
;;
21-
*)
22-
export GOARM="7"
23-
;;
24-
esac
25-
else
26-
export GOARM="7"
27-
fi
28-
fi
12+
case "$TARGETARCH" in
13+
"amd64")
14+
if [ -z "$GOAMD64" ]; then
15+
case "$TARGETVARIANT" in
16+
"v2")
17+
export GOAMD64="v2"
18+
;;
19+
"v3")
20+
export GOAMD64="v3"
21+
;;
22+
"v4")
23+
export GOAMD64="v4"
24+
;;
25+
esac
26+
fi
27+
;;
28+
"arm")
29+
if [ -z "$GOARM" ]; then
30+
case "$TARGETVARIANT" in
31+
"v5")
32+
export GOARM="5"
33+
;;
34+
"v6")
35+
export GOARM="6"
36+
;;
37+
*)
38+
export GOARM="7"
39+
;;
40+
esac
41+
fi
42+
;;
43+
esac
2944

3045
if [ -n "$TARGETVARIANT" ]; then
3146
case "$TARGETARCH" in
3247
"mips64"*)
33-
export GOMIPS64="${TARGETVARIANT}"
48+
if [ -z "$GOMIPS64" ]; then
49+
export GOMIPS64="${TARGETVARIANT}"
50+
fi
3451
;;
3552
"mips"*)
36-
export GOMIPS="${TARGETVARIANT}"
53+
if [ -z "$GOMIPS" ]; then
54+
export GOMIPS="${TARGETVARIANT}"
55+
fi
3756
;;
3857
esac
3958
fi
@@ -89,6 +108,9 @@ wrap() {
89108
mkdir -p "$(dirname "$f")"
90109
echo "GOOS=$GOOS" >"$f"
91110
echo "GOARCH=$GOARCH" >>"$f"
111+
if [ -n "$GOAMD64" ]; then
112+
echo "GOAMD64=$GOAMD64" >>"$f"
113+
fi
92114
if [ -n "$GOARM" ]; then
93115
echo "GOARM=$GOARM" >>"$f"
94116
fi

src/xx-info

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ if [ -n "$TARGETPLATFORM" ]; then
8282
TARGETOS="$os"
8383
TARGETARCH="$arch"
8484
case "$arch" in
85+
"amd64")
86+
case "$(echo $TARGETPLATFORM | cut -d"/" -f3)" in
87+
"v2")
88+
TARGETVARIANT="v2"
89+
;;
90+
"v3")
91+
TARGETVARIANT="v3"
92+
;;
93+
"v4")
94+
TARGETVARIANT="v4"
95+
;;
96+
esac
97+
;;
8598
"arm")
8699
case "$(echo $TARGETPLATFORM | cut -d"/" -f3)" in
87100
"v5")

0 commit comments

Comments
 (0)