Skip to content

Commit

Permalink
hack: name for target ARM architecture not specified
Browse files Browse the repository at this point in the history
Sandboxed build invocation with xx currently doesn't set
the right name for target ARM architecture through switches
in CGO_CFLAGS and CGO_CXXFLAGS. This was previously fixed in moby#43474

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Jan 12, 2023
1 parent 585e4ce commit 515391f
Showing 1 changed file with 80 additions and 64 deletions.
144 changes: 80 additions & 64 deletions hack/make/.binary
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,88 @@ source "${MAKEDIR}/.go-autogen"
(
export GOGC=${DOCKER_BUILD_GOGC:-1000}

# for non-sandboxed invocation
if ! command -v xx-go > /dev/null 2>&1; then
if [ "$(go env GOOS)/$(go env GOARCH)" != "$(go env GOHOSTOS)/$(go env GOHOSTARCH)" ]; then
# must be cross-compiling!
case "$(go env GOOS)/$(go env GOARCH)" in
windows/amd64)
export CC="${CC:-x86_64-w64-mingw32-gcc}"
export CGO_ENABLED=1
;;
linux/arm)
case "$(go env GOARM)" in
5)
export CC="${CC:-arm-linux-gnueabi-gcc}"
export CGO_ENABLED=1
export CGO_CFLAGS="-march=armv5t"
export CGO_CXXFLAGS="-march=armv5t"
;;
6)
export CC="${CC:-arm-linux-gnueabi-gcc}"
export CGO_ENABLED=1
export CGO_CFLAGS="-march=armv6"
export CGO_CXXFLAGS="-march=armv6"
;;
7)
export CC="${CC:-arm-linux-gnueabihf-gcc}"
export CGO_ENABLED=1
export CGO_CFLAGS="-march=armv7-a"
export CGO_CXXFLAGS="-march=armv7-a"
;;
*)
export CC="${CC:-arm-linux-gnueabihf-gcc}"
export CGO_ENABLED=1
;;
esac
;;
linux/arm64)
export CC="${CC:-aarch64-linux-gnu-gcc}"
export CGO_ENABLED=1
;;
linux/amd64)
export CC="${CC:-x86_64-linux-gnu-gcc}"
export CGO_ENABLED=1
;;
linux/ppc64le)
export CC="${CC:-powerpc64le-linux-gnu-gcc}"
export CGO_ENABLED=1
;;
linux/s390x)
export CC="${CC:-s390x-linux-gnu-gcc}"
export CGO_ENABLED=1
;;
esac
# only necessary for non-sandboxed invocation where TARGETPLATFORM is empty
if [ -z "$TARGETPLATFORM" ]; then
TARGETPLATFORM="$(go env GOOS)/$(go env GOARCH)"
if [ -n "$(go env GOARM)" ]; then
TARGETPLATFORM+="/v$(go env GOARM)"
elif [ -n "$(go env GOAMD64)" ] && [ "$(go env GOAMD64)" != "v1" ]; then
TARGETPLATFORM+="/$(go env GOAMD64)"
fi
fi

iscross=0
sandboxed=0
if command -v xx-info > /dev/null 2>&1; then
sandboxed=1
iscross=$(xx-info is-cross && echo 1 || echo 0)
elif [ "$(go env GOOS)/$(go env GOARCH)" != "$(go env GOHOSTOS)/$(go env GOHOSTARCH)" ]; then
iscross=1
fi

if [[ "$sandboxed" = "0" ]] && [[ "$iscross" = "1" ]]; then
case "$(go env GOOS)/$(go env GOARCH)" in
windows/amd64)
export CC="${CC:-x86_64-w64-mingw32-gcc}"
export CGO_ENABLED=1
;;
linux/arm)
case "$(go env GOARM)" in
5)
export CC="${CC:-arm-linux-gnueabi-gcc}"
export CGO_ENABLED=1
;;
6)
export CC="${CC:-arm-linux-gnueabi-gcc}"
export CGO_ENABLED=1
;;
7)
export CC="${CC:-arm-linux-gnueabihf-gcc}"
export CGO_ENABLED=1
;;
*)
export CC="${CC:-arm-linux-gnueabihf-gcc}"
export CGO_ENABLED=1
;;
esac
;;
linux/arm64)
export CC="${CC:-aarch64-linux-gnu-gcc}"
export CGO_ENABLED=1
;;
linux/amd64)
export CC="${CC:-x86_64-linux-gnu-gcc}"
export CGO_ENABLED=1
;;
linux/ppc64le)
export CC="${CC:-powerpc64le-linux-gnu-gcc}"
export CGO_ENABLED=1
;;
linux/s390x)
export CC="${CC:-s390x-linux-gnu-gcc}"
export CGO_ENABLED=1
;;
esac
fi

if [ "$iscross" = "1" ]; then
# specify name of the target ARM architecture
case "$TARGETPLATFORM" in
linux/arm/v5)
export CGO_CFLAGS="-march=armv5t"
export CGO_CXXFLAGS="-march=armv5t"
;;
linux/arm/v6)
export CGO_CFLAGS="-march=armv6"
export CGO_CXXFLAGS="-march=armv6"
;;
linux/arm/v7)
export CGO_CFLAGS="-march=armv7-a"
export CGO_CXXFLAGS="-march=armv7-a"
;;
esac
fi

# -buildmode=pie is not supported on Windows arm64 and Linux mips*, ppc64be
# https://github.com/golang/go/blob/go1.19.4/src/cmd/internal/sys/supported.go#L125-L132
if ! [ "$DOCKER_STATIC" = "1" ]; then
Expand All @@ -85,18 +112,7 @@ source "${MAKEDIR}/.go-autogen"
fi
fi

# only necessary for non-sandboxed invocation where TARGETPLATFORM is empty
PLATFORM_NAME=$TARGETPLATFORM
if [ -z "$PLATFORM_NAME" ]; then
PLATFORM_NAME="$(go env GOOS)/$(go env GOARCH)"
if [ -n "$(go env GOARM)" ]; then
PLATFORM_NAME+="/$(go env GOARM)"
elif [ -n "$(go env GOAMD64)" ] && [ "$(go env GOAMD64)" != "v1" ]; then
PLATFORM_NAME+="/$(go env GOAMD64)"
fi
fi

echo "Building $([ "$DOCKER_STATIC" = "1" ] && echo "static" || echo "dynamic") $DEST/$BINARY_FULLNAME ($PLATFORM_NAME)..."
echo "Building $([ "$DOCKER_STATIC" = "1" ] && echo "static" || echo "dynamic") $DEST/$BINARY_FULLNAME ($TARGETPLATFORM)..."
go build \
-o "$DEST/$BINARY_FULLNAME" \
"${BUILDFLAGS[@]}" \
Expand Down

0 comments on commit 515391f

Please sign in to comment.