Skip to content

Commit

Permalink
Merge pull request #63 from crazy-max/goamd64
Browse files Browse the repository at this point in the history
go amd64 variant support
  • Loading branch information
tonistiigi authored Feb 6, 2024
2 parents c608d8c + 29cd28a commit ac90278
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions src/test-go.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
;;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/test-info-alpine.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
4 changes: 4 additions & 0 deletions src/test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
60 changes: 41 additions & 19 deletions src/xx-go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/xx-info
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit ac90278

Please sign in to comment.