2017-02-13 19:01:54 +00:00
|
|
|
#!/usr/bin/env bash
|
2016-02-19 22:42:51 +00:00
|
|
|
set -e
|
|
|
|
|
2017-07-05 19:38:23 +00:00
|
|
|
# a helper to provide ".exe" when it's appropriate
|
|
|
|
binary_extension() {
|
2019-01-10 01:23:38 +00:00
|
|
|
if [ "$(go env GOOS)" = 'windows' ]; then
|
2017-07-05 19:38:23 +00:00
|
|
|
echo -n '.exe'
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-02-19 22:42:51 +00:00
|
|
|
BINARY_EXTENSION="$(binary_extension)"
|
|
|
|
BINARY_FULLNAME="$BINARY_NAME$BINARY_EXTENSION"
|
|
|
|
|
|
|
|
source "${MAKEDIR}/.go-autogen"
|
|
|
|
|
|
|
|
(
|
2020-03-03 03:27:49 +00:00
|
|
|
export GOGC=${DOCKER_BUILD_GOGC:-1000}
|
2016-02-19 22:42:51 +00:00
|
|
|
|
2023-01-20 14:13:11 +00:00
|
|
|
if [ "$(go env GOOS)/$(go env GOARCH)" != "$(go env GOHOSTOS)/$(go env GOHOSTARCH)" ]; then
|
|
|
|
# must be cross-compiling!
|
|
|
|
if [ "$(go env GOOS)/$(go env GOARCH)" = "linux/arm" ]; then
|
|
|
|
# specify name of the target ARM architecture
|
|
|
|
case "$(go env GOARM)" in
|
|
|
|
5)
|
|
|
|
export CGO_CFLAGS="-march=armv5t"
|
|
|
|
export CGO_CXXFLAGS="-march=armv5t"
|
|
|
|
;;
|
|
|
|
6)
|
|
|
|
export CGO_CFLAGS="-march=armv6"
|
|
|
|
export CGO_CXXFLAGS="-march=armv6"
|
|
|
|
;;
|
|
|
|
7)
|
|
|
|
export CGO_CFLAGS="-march=armv7-a"
|
|
|
|
export CGO_CXXFLAGS="-march=armv7-a"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
2020-03-03 03:27:49 +00:00
|
|
|
fi
|
|
|
|
|
2022-12-27 03:20:34 +00:00
|
|
|
# -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
|
2022-12-27 01:45:43 +00:00
|
|
|
if ! [ "$DOCKER_STATIC" = "1" ]; then
|
2023-01-05 13:04:32 +00:00
|
|
|
# -buildmode=pie not supported when -race is enabled
|
|
|
|
if [[ " $BUILDFLAGS " != *" -race "* ]]; then
|
|
|
|
case "$(go env GOOS)/$(go env GOARCH)" in
|
|
|
|
windows/arm64 | linux/mips* | linux/ppc64) ;;
|
|
|
|
*)
|
|
|
|
BUILDFLAGS+=("-buildmode=pie")
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
2022-12-27 01:45:43 +00:00
|
|
|
fi
|
2017-08-02 13:07:36 +00:00
|
|
|
|
hack/make/.binary: don't use "netgo" when building Windows binaries
Starting with go1.19, the Go runtime on Windows now supports the `netgo` build-
flag to use a native Go DNS resolver. Prior to that version, the build-flag
only had an effect on non-Windows platforms. When using the `netgo` build-flag,
the Windows's host resolver is not used, and as a result, custom entries in
`etc/hosts` are ignored, which is a change in behavior from binaries compiled
with older versions of the Go runtime.
From the go1.19 release notes: https://go.dev/doc/go1.19#net
> Resolver.PreferGo is now implemented on Windows and Plan 9. It previously
> only worked on Unix platforms. Combined with Dialer.Resolver and Resolver.Dial,
> it's now possible to write portable programs and be in control of all DNS name
> lookups when dialing.
>
> The net package now has initial support for the netgo build tag on Windows.
> When used, the package uses the Go DNS client (as used by Resolver.PreferGo)
> instead of asking Windows for DNS results. The upstream DNS server it discovers
> from Windows may not yet be correct with complex system network configurations,
> however.
Our Windows binaries are compiled with the "static" (`make/binary-daemon`)
script, which has the `netgo` option set by default. This patch unsets the
`netgo` option when cross-compiling for Windows.
Co-authored-by: Bjorn Neergaard <bjorn.neergaard@docker.com>
Signed-off-by: Bjorn Neergaard <bjorn.neergaard@docker.com>
2023-05-24 14:51:05 +00:00
|
|
|
# XXX: Disable netgo on Windows and use Window's system resolver instead.
|
|
|
|
#
|
|
|
|
# go1.19 and newer added support for netgo on Windows (https://go.dev/doc/go1.19#net),
|
|
|
|
# which won't ask Windows for DNS results, and hence may be ignoring
|
|
|
|
# custom "C:\Windows\System32\drivers\etc\hosts".
|
|
|
|
# See https://github.com/moby/moby/issues/45251#issuecomment-1561001817
|
|
|
|
# https://github.com/moby/moby/issues/45251, and
|
|
|
|
# https://go-review.googlesource.com/c/go/+/467335
|
|
|
|
if [ "$(go env GOOS)" = "windows" ]; then
|
|
|
|
BUILDFLAGS=("${BUILDFLAGS[@]/netgo/}")
|
|
|
|
fi
|
|
|
|
|
2023-01-01 16:47:17 +00:00
|
|
|
# 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
|
2023-01-12 19:24:49 +00:00
|
|
|
PLATFORM_NAME+="/v$(go env GOARM)"
|
2023-01-01 16:47:17 +00:00
|
|
|
elif [ -n "$(go env GOAMD64)" ] && [ "$(go env GOAMD64)" != "v1" ]; then
|
|
|
|
PLATFORM_NAME+="/$(go env GOAMD64)"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-09-06 08:10:55 +00:00
|
|
|
if [ -n "${DOCKER_DEBUG}" ]; then
|
|
|
|
GCFLAGS="all=-N -l"
|
|
|
|
fi
|
|
|
|
|
2022-12-27 01:45:43 +00:00
|
|
|
echo "Building $([ "$DOCKER_STATIC" = "1" ] && echo "static" || echo "dynamic") $DEST/$BINARY_FULLNAME ($PLATFORM_NAME)..."
|
2023-01-18 21:03:34 +00:00
|
|
|
if [ -n "$DOCKER_DEBUG" ]; then
|
|
|
|
set -x
|
|
|
|
fi
|
2023-09-06 08:10:55 +00:00
|
|
|
./hack/with-go-mod.sh go build -mod=vendor -modfile=vendor.mod -o "$DEST/$BINARY_FULLNAME" "${BUILDFLAGS[@]}" -ldflags "$LDFLAGS $LDFLAGS_STATIC $DOCKER_LDFLAGS" -gcflags="${GCFLAGS}" "$GO_PACKAGE"
|
2016-02-19 22:42:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
echo "Created binary: $DEST/$BINARY_FULLNAME"
|