diff --git a/README.md b/README.md index 274d1c7..44e32ea 100644 --- a/README.md +++ b/README.md @@ -309,7 +309,7 @@ RUN mkdir build && cd build && \ ## Go / Cgo -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. +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. ```dockerfile FROM --platform=$BUILDPLATFORM golang:alpine diff --git a/src/test-go.bats b/src/test-go.bats index 92d56bd..8805ee7 100755 --- a/src/test-go.bats +++ b/src/test-go.bats @@ -45,6 +45,11 @@ testEnv() { assert_output --partial "GOHOSTARCH=$(TARGETOS= TARGETARCH= xx-info arch)" case "$(xx-info arch)" in + "amd64") + if supportAmd64VariantGo; then + assert_output --partial "GOAMD64=$expAmd64" + fi + ;; "arm") assert_output --partial "GOARM=$expArm" ;; @@ -78,6 +83,39 @@ testEnv() { testEnv } +@test "amd64v2-env" { + if ! supportAmd64VariantGo; then + skip "Amd64 Variant GO not supported" + fi + export TARGETARCH=amd64 + export TARGETVARIANT=v2 + expAmd64=v2 + testEnv + unset TARGETVARIANT +} + +@test "amd64v3-env" { + if ! supportAmd64VariantGo; then + skip "Amd64 Variant GO not supported" + fi + export TARGETARCH=amd64 + export TARGETVARIANT=v3 + expAmd64=v3 + testEnv + unset TARGETVARIANT +} + +@test "amd64v4-env" { + if ! supportAmd64VariantGo; then + skip "Amd64 Variant GO not supported" + fi + export TARGETARCH=amd64 + export TARGETVARIANT=v4 + expAmd64=v4 + testEnv + unset TARGETVARIANT +} + @test "arm64-env" { export TARGETARCH=arm64 testEnv @@ -218,6 +256,16 @@ testHelloGO() { testHelloGO } +@test "amd64v2-hellogo" { + if ! supportAmd64VariantGo; then + skip "Amd64 Variant GO not supported" + fi + export TARGETARCH=amd64 + export TARGETVARIANT=v2 + testHelloGO + unset TARGETVARIANT +} + @test "arm64-hellogo" { export TARGETARCH=arm64 testHelloGO diff --git a/src/test-info-alpine.bats b/src/test-info-alpine.bats index b01b6c7..352c38c 100755 --- a/src/test-info-alpine.bats +++ b/src/test-info-alpine.bats @@ -24,6 +24,24 @@ load 'assert' assert_equal "x86_64" "$(TARGETPLATFORM=linux/amd64 xx-info pkg-arch)" } +@test "amd64v2" { + assert_equal "x86_64-alpine-linux-musl" "$(TARGETPLATFORM=linux/amd64/v2 xx-info triple)" + assert_equal "x86_64" "$(TARGETPLATFORM=linux/amd64/v2 xx-info pkg-arch)" + assert_equal "v2" "$(TARGETPLATFORM=linux/amd64/v2 xx-info variant)" +} + +@test "amd64v3" { + assert_equal "x86_64-alpine-linux-musl" "$(TARGETPLATFORM=linux/amd64/v3 xx-info triple)" + assert_equal "x86_64" "$(TARGETPLATFORM=linux/amd64/v3 xx-info pkg-arch)" + assert_equal "v3" "$(TARGETPLATFORM=linux/amd64/v3 xx-info variant)" +} + +@test "amd64v4" { + assert_equal "x86_64-alpine-linux-musl" "$(TARGETPLATFORM=linux/amd64/v4 xx-info triple)" + assert_equal "x86_64" "$(TARGETPLATFORM=linux/amd64/v4 xx-info pkg-arch)" + assert_equal "v4" "$(TARGETPLATFORM=linux/amd64/v4 xx-info variant)" +} + @test "aarch64" { assert_equal "aarch64-alpine-linux-musl" "$(TARGETPLATFORM=linux/arm64 xx-info triple)" assert_equal "aarch64" "$(TARGETPLATFORM=linux/arm64 xx-info pkg-arch)" diff --git a/src/test_helper.bash b/src/test_helper.bash index 21c18f9..68c620e 100644 --- a/src/test_helper.bash +++ b/src/test_helper.bash @@ -91,6 +91,10 @@ supportRiscVCGo() { versionGTE "$(go version | awk '{print $3}' | sed 's/^go//')" "1.16" } +supportAmd64VariantGo() { + versionGTE "$(go version | awk '{print $3}' | sed 's/^go//')" "1.18" +} + supportRC() { command -v llvm-rc >/dev/null 2>&1 } diff --git a/src/xx-go b/src/xx-go index 7dcb122..e193528 100755 --- a/src/xx-go +++ b/src/xx-go @@ -9,31 +9,50 @@ done export GOOS=${TARGETOS} export GOARCH=${TARGETARCH} -if [ "$TARGETARCH" = "arm" ]; then - if [ -n "$TARGETVARIANT" ]; then - case "$TARGETVARIANT" in - "v5") - export GOARM="5" - ;; - "v6") - export GOARM="6" - ;; - *) - export GOARM="7" - ;; - esac - else - export GOARM="7" - fi -fi +case "$TARGETARCH" in + "amd64") + if [ -z "$GOAMD64" ]; then + case "$TARGETVARIANT" in + "v2") + export GOAMD64="v2" + ;; + "v3") + export GOAMD64="v3" + ;; + "v4") + export GOAMD64="v4" + ;; + esac + fi + ;; + "arm") + if [ -z "$GOARM" ]; then + case "$TARGETVARIANT" in + "v5") + export GOARM="5" + ;; + "v6") + export GOARM="6" + ;; + *) + export GOARM="7" + ;; + esac + fi + ;; +esac if [ -n "$TARGETVARIANT" ]; then case "$TARGETARCH" in "mips64"*) - export GOMIPS64="${TARGETVARIANT}" + if [ -z "$GOMIPS64" ]; then + export GOMIPS64="${TARGETVARIANT}" + fi ;; "mips"*) - export GOMIPS="${TARGETVARIANT}" + if [ -z "$GOMIPS" ]; then + export GOMIPS="${TARGETVARIANT}" + fi ;; esac fi @@ -89,6 +108,9 @@ wrap() { mkdir -p "$(dirname "$f")" echo "GOOS=$GOOS" >"$f" echo "GOARCH=$GOARCH" >>"$f" + if [ -n "$GOAMD64" ]; then + echo "GOAMD64=$GOAMD64" >>"$f" + fi if [ -n "$GOARM" ]; then echo "GOARM=$GOARM" >>"$f" fi diff --git a/src/xx-info b/src/xx-info index 6145186..494d03c 100755 --- a/src/xx-info +++ b/src/xx-info @@ -82,6 +82,19 @@ if [ -n "$TARGETPLATFORM" ]; then TARGETOS="$os" TARGETARCH="$arch" case "$arch" in + "amd64") + case "$(echo $TARGETPLATFORM | cut -d"/" -f3)" in + "v2") + TARGETVARIANT="v2" + ;; + "v3") + TARGETVARIANT="v3" + ;; + "v4") + TARGETVARIANT="v4" + ;; + esac + ;; "arm") case "$(echo $TARGETPLATFORM | cut -d"/" -f3)" in "v5")