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-01 16:47:17 +00:00
|
|
|
# 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 "${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
|
|
|
|
fi
|
2020-03-03 03:27:49 +00:00
|
|
|
fi
|
|
|
|
|
2021-06-10 23:19:22 +00:00
|
|
|
# -buildmode=pie is not supported on Windows and Linux on mips, riscv64 and ppc64be.
|
2021-06-11 19:34:50 +00:00
|
|
|
# https://github.com/golang/go/blob/77aa209b386a184e7f4b44938f2a05a1b5c5a3cf/src/cmd/internal/sys/supported.go#L89-L99
|
2020-03-03 03:27:49 +00:00
|
|
|
case "$(go env GOOS)/$(go env GOARCH)" in
|
2021-06-10 23:19:22 +00:00
|
|
|
windows/* | linux/mips* | linux/riscv* | linux/ppc64) ;;
|
2021-06-11 19:34:50 +00:00
|
|
|
# TODO remove windows in Go 1.15+: https://github.com/golang/go/commit/95f382139043059a2a0780ba577b53893408f7e4
|
|
|
|
# TODO remove riscv64 in Go 1.16+: https://github.com/golang/go/commit/8eb846fd37eb7bded8a1cf6932be2c59069863e5
|
2020-03-03 03:27:49 +00:00
|
|
|
|
|
|
|
*)
|
|
|
|
BUILDFLAGS+=("-buildmode=pie")
|
2019-04-06 00:20:06 +00:00
|
|
|
;;
|
2016-02-19 22:42:51 +00:00
|
|
|
esac
|
2017-08-02 13:07:36 +00:00
|
|
|
|
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
|
|
|
|
PLATFORM_NAME+="/$(go env GOARM)"
|
|
|
|
elif [ -n "$(go env GOAMD64)" ] && [ "$(go env GOAMD64)" != "v1" ]; then
|
|
|
|
PLATFORM_NAME+="/$(go env GOAMD64)"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Building $DEST/$BINARY_FULLNAME ($PLATFORM_NAME)..."
|
2020-03-03 03:27:49 +00:00
|
|
|
go build \
|
|
|
|
-o "$DEST/$BINARY_FULLNAME" \
|
|
|
|
"${BUILDFLAGS[@]}" \
|
|
|
|
-ldflags "
|
2016-02-19 22:42:51 +00:00
|
|
|
$LDFLAGS
|
|
|
|
$LDFLAGS_STATIC_DOCKER
|
2017-11-15 21:54:56 +00:00
|
|
|
$DOCKER_LDFLAGS
|
2016-02-19 22:42:51 +00:00
|
|
|
" \
|
2020-03-03 03:27:49 +00:00
|
|
|
${GO_PACKAGE}
|
2016-02-19 22:42:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
echo "Created binary: $DEST/$BINARY_FULLNAME"
|