2021-03-03 02:03:02 -05:00
|
|
|
#!/usr/bin/env sh
|
2017-05-11 18:52:17 -04:00
|
|
|
set -eu
|
|
|
|
|
2021-11-17 11:18:15 -05:00
|
|
|
: "${CGO_ENABLED=}"
|
|
|
|
: "${GO_LINKMODE=static}"
|
|
|
|
: "${GO_BUILDMODE=}"
|
|
|
|
: "${GO_BUILDTAGS=}"
|
|
|
|
: "${GO_STRIP=}"
|
|
|
|
|
2021-03-03 02:03:02 -05:00
|
|
|
TARGET=${TARGET:-"build"}
|
|
|
|
|
2017-12-01 08:47:20 -05:00
|
|
|
PLATFORM=${PLATFORM:-}
|
2021-03-05 17:34:32 -05:00
|
|
|
VERSION=${VERSION:-$(git describe --match 'v[0-9]*' --dirty='.m' --always --tags | sed 's/^v//' 2>/dev/null || echo "unknown-version" )}
|
2017-05-11 18:52:17 -04:00
|
|
|
GITCOMMIT=${GITCOMMIT:-$(git rev-parse --short HEAD 2> /dev/null || true)}
|
2022-04-06 05:06:44 -04:00
|
|
|
|
|
|
|
if [ "$(uname)" = "Darwin" ]; then
|
|
|
|
# Using BSD date (macOS), which doesn't suppoort the --date option
|
|
|
|
# date -jf "<input format>" "<input value>" +"<output format>" (https://unix.stackexchange.com/a/86510)
|
2022-04-06 05:16:48 -04:00
|
|
|
BUILDTIME=${BUILDTIME:-$(TZ=UTC date -jf "%s" "${SOURCE_DATE_EPOCH:-$(date +%s)}" +"%Y-%m-%dT%H:%M:%SZ")}
|
2022-04-06 05:06:44 -04:00
|
|
|
else
|
|
|
|
# Using GNU date (Linux)
|
2022-04-06 05:16:48 -04:00
|
|
|
BUILDTIME=${BUILDTIME:-$(TZ=UTC date -u --date="@${SOURCE_DATE_EPOCH:-$(date +%s)}" +"%Y-%m-%dT%H:%M:%SZ")}
|
2022-04-06 05:06:44 -04:00
|
|
|
fi
|
2017-05-11 18:52:17 -04:00
|
|
|
|
2024-01-04 07:55:28 -05:00
|
|
|
case "$VERSION" in
|
|
|
|
refs/tags/v*) VERSION=${VERSION#refs/tags/v} ;;
|
|
|
|
refs/tags/*) VERSION=${VERSION#refs/tags/} ;;
|
|
|
|
refs/heads/*) VERSION=$(echo "${VERSION#refs/heads/}" | sed -r 's#/+#-#g') ;;
|
|
|
|
refs/pull/*) VERSION=pr-$(echo "$VERSION" | grep -o '[0-9]\+') ;;
|
|
|
|
esac
|
|
|
|
|
2021-03-03 02:03:02 -05:00
|
|
|
GOOS="$(go env GOOS)"
|
|
|
|
GOARCH="$(go env GOARCH)"
|
2019-05-02 12:05:47 -04:00
|
|
|
if [ "${GOARCH}" = "arm" ]; then
|
2021-11-17 11:18:15 -05:00
|
|
|
GOARM="$(go env GOARM)"
|
2019-05-02 12:05:47 -04:00
|
|
|
fi
|
|
|
|
|
2021-03-03 02:03:02 -05:00
|
|
|
TARGET="$TARGET/docker-${GOOS}-${GOARCH}"
|
2019-05-02 12:05:47 -04:00
|
|
|
if [ "${GOARCH}" = "arm" ] && [ -n "${GOARM}" ]; then
|
2021-03-03 02:03:02 -05:00
|
|
|
TARGET="${TARGET}-v${GOARM}"
|
2019-05-02 12:05:47 -04:00
|
|
|
fi
|
|
|
|
if [ "${GOOS}" = "windows" ]; then
|
2021-11-17 11:18:15 -05:00
|
|
|
TARGET="${TARGET}.exe"
|
2019-05-02 12:05:47 -04:00
|
|
|
fi
|
|
|
|
export TARGET
|
|
|
|
|
2021-11-17 11:18:15 -05:00
|
|
|
if [ -z "$CGO_ENABLED" ]; then
|
|
|
|
case "$(go env GOOS)" in
|
|
|
|
linux)
|
|
|
|
case "$(go env GOARCH)" in
|
|
|
|
amd64|arm64|arm|s390x)
|
|
|
|
CGO_ENABLED=1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
CGO_ENABLED=0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
darwin|windows)
|
|
|
|
CGO_ENABLED=1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
CGO_ENABLED=0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
export CGO_ENABLED
|
|
|
|
|
|
|
|
if [ "$CGO_ENABLED" = "1" ] && [ "$(go env GOOS)" != "windows" ]; then
|
|
|
|
case "$(go env GOARCH)" in
|
|
|
|
mips*|ppc64)
|
|
|
|
# pie build mode is not supported on mips architectures
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
GO_BUILDMODE="-buildmode=pie"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
export GO_BUILDMODE
|
|
|
|
|
2022-07-23 03:38:50 -04:00
|
|
|
GO_LDFLAGS="${GO_LDFLAGS:-}"
|
use GO_LDFLAGS instead of LDFLAGS to prevent inheriting unrelated options
When building on Fedora 36, the build failed. I suspect this is because the
rpm tools also set LDFLAGS, but with options that cannot be used;
GO_LINKMODE=dynamic
+ ./scripts/build/binary
/go/src/github.com/docker/cli ~/rpmbuild/BUILD/src
Building dynamic docker-linux-arm64
+ go build -o build/docker-linux-arm64 -tags ' pkcs11' -ldflags '-Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -Wl,--build-id=sha1 -Wl,-dT,/root/rpmbuild/BUILD/src/.package_note-docker-ce-cli-0.0.0.20220330082637.68cad50-0.fc36.aarch64.ld -w -X "github.com/docker/cli/cli/version.GitCommit=68cad50" -X "github.com/docker/cli/cli/version.BuildTime=2022-03-30T20:05:36Z" -X "github.com/docker/cli/cli/version.Version=0.0.0-20220330082637-68cad50" -X "github.com/docker/cli/cli/version.PlatformName=Docker Engine - Community"' -buildmode=pie github.com/docker/cli/cmd/docker
# github.com/docker/cli/cmd/docker
flag provided but not defined: -Wl,-z,relro
usage: link [options] main.o
This patch changes the variable we use to `GO_LDFLAGS`, taking a similar approach
as containerd, and various other projects using this name: https://grep.app/search?q=GO_LDFLAGS
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-31 04:30:56 -04:00
|
|
|
GO_LDFLAGS="$GO_LDFLAGS -X \"github.com/docker/cli/cli/version.GitCommit=${GITCOMMIT}\""
|
|
|
|
GO_LDFLAGS="$GO_LDFLAGS -X \"github.com/docker/cli/cli/version.BuildTime=${BUILDTIME}\""
|
|
|
|
GO_LDFLAGS="$GO_LDFLAGS -X \"github.com/docker/cli/cli/version.Version=${VERSION}\""
|
2021-11-17 11:18:15 -05:00
|
|
|
if test -n "${PLATFORM}"; then
|
use GO_LDFLAGS instead of LDFLAGS to prevent inheriting unrelated options
When building on Fedora 36, the build failed. I suspect this is because the
rpm tools also set LDFLAGS, but with options that cannot be used;
GO_LINKMODE=dynamic
+ ./scripts/build/binary
/go/src/github.com/docker/cli ~/rpmbuild/BUILD/src
Building dynamic docker-linux-arm64
+ go build -o build/docker-linux-arm64 -tags ' pkcs11' -ldflags '-Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -Wl,--build-id=sha1 -Wl,-dT,/root/rpmbuild/BUILD/src/.package_note-docker-ce-cli-0.0.0.20220330082637.68cad50-0.fc36.aarch64.ld -w -X "github.com/docker/cli/cli/version.GitCommit=68cad50" -X "github.com/docker/cli/cli/version.BuildTime=2022-03-30T20:05:36Z" -X "github.com/docker/cli/cli/version.Version=0.0.0-20220330082637-68cad50" -X "github.com/docker/cli/cli/version.PlatformName=Docker Engine - Community"' -buildmode=pie github.com/docker/cli/cmd/docker
# github.com/docker/cli/cmd/docker
flag provided but not defined: -Wl,-z,relro
usage: link [options] main.o
This patch changes the variable we use to `GO_LDFLAGS`, taking a similar approach
as containerd, and various other projects using this name: https://grep.app/search?q=GO_LDFLAGS
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-31 04:30:56 -04:00
|
|
|
GO_LDFLAGS="$GO_LDFLAGS -X \"github.com/docker/cli/cli/version.PlatformName=${PLATFORM}\""
|
2021-11-17 11:18:15 -05:00
|
|
|
fi
|
|
|
|
if [ "$CGO_ENABLED" = "1" ] && [ "$GO_LINKMODE" = "static" ] && [ "$(go env GOOS)" = "linux" ]; then
|
use GO_LDFLAGS instead of LDFLAGS to prevent inheriting unrelated options
When building on Fedora 36, the build failed. I suspect this is because the
rpm tools also set LDFLAGS, but with options that cannot be used;
GO_LINKMODE=dynamic
+ ./scripts/build/binary
/go/src/github.com/docker/cli ~/rpmbuild/BUILD/src
Building dynamic docker-linux-arm64
+ go build -o build/docker-linux-arm64 -tags ' pkcs11' -ldflags '-Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -Wl,--build-id=sha1 -Wl,-dT,/root/rpmbuild/BUILD/src/.package_note-docker-ce-cli-0.0.0.20220330082637.68cad50-0.fc36.aarch64.ld -w -X "github.com/docker/cli/cli/version.GitCommit=68cad50" -X "github.com/docker/cli/cli/version.BuildTime=2022-03-30T20:05:36Z" -X "github.com/docker/cli/cli/version.Version=0.0.0-20220330082637-68cad50" -X "github.com/docker/cli/cli/version.PlatformName=Docker Engine - Community"' -buildmode=pie github.com/docker/cli/cmd/docker
# github.com/docker/cli/cmd/docker
flag provided but not defined: -Wl,-z,relro
usage: link [options] main.o
This patch changes the variable we use to `GO_LDFLAGS`, taking a similar approach
as containerd, and various other projects using this name: https://grep.app/search?q=GO_LDFLAGS
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-31 04:30:56 -04:00
|
|
|
GO_LDFLAGS="$GO_LDFLAGS -extldflags -static"
|
2021-11-17 11:18:15 -05:00
|
|
|
fi
|
scripts/build: make sure osusergo is set for static with CGO enabled
Our previous CI probably did not cross-compile for s390x and ppc64le,
and therefore did not get these errors; the scripts/build/.variables
file sets CGO_ENABLED=1 for amd64|arm64|arm|s390x architectures if
it's not set;
https://github.com/docker/cli/blob/87b8e57868adba9e189e9fb74e5bd5d189a024c8/scripts/build/.variables#L34-L46
When compiling statically with CGO enabled, we must have osusergo
enabled as well, so set it accordingly, to prevent;
#53 [linux/amd64 build 2/2] RUN --mount=type=bind,target=.,ro --mount=type=cache,target=/root/.cache --mount=from=dockercore/golang-cross:xx-sdk-extras,target=/xx-sdk,src=/xx-sdk --mount=type=tmpfs,target=cli/winresources xx-go --wrap && TARGET=/out ./scripts/build/binary && xx-verify $([ "static" = "static" ] && echo "--static") /out/docker
#53 953.6 # github.com/docker/cli/cmd/docker
#53 953.6 /usr/bin/s390x-linux-gnu-ld: /tmp/go-link-2402600021/000022.o: in function `New':
#53 953.6 /go/src/github.com/docker/cli/vendor/github.com/miekg/pkcs11/pkcs11.go:75: warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
#53 953.6 /usr/bin/s390x-linux-gnu-ld: /tmp/go-link-2402600021/000018.o: in function `mygetgrouplist':
#53 953.6 /usr/local/go/src/os/user/getgrouplist_unix.go:18: warning: Using 'getgrouplist' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
#53 953.6 /usr/bin/s390x-linux-gnu-ld: /tmp/go-link-2402600021/000017.o: in function `mygetgrgid_r':
#53 953.6 /usr/local/go/src/os/user/cgo_lookup_unix.go:40: warning: Using 'getgrgid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
#53 953.6 /usr/bin/s390x-linux-gnu-ld: /tmp/go-link-2402600021/000017.o: in function `mygetgrnam_r':
#53 953.6 /usr/local/go/src/os/user/cgo_lookup_unix.go:45: warning: Using 'getgrnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
#53 953.6 /usr/bin/s390x-linux-gnu-ld: /tmp/go-link-2402600021/000017.o: in function `mygetpwnam_r':
#53 953.6 /usr/local/go/src/os/user/cgo_lookup_unix.go:35: warning: Using 'getpwnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
#53 953.6 /usr/bin/s390x-linux-gnu-ld: /tmp/go-link-2402600021/000017.o: in function `mygetpwuid_r':
#53 953.6 /usr/local/go/src/os/user/cgo_lookup_unix.go:30: warning: Using 'getpwuid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
#53 953.6 /usr/bin/s390x-linux-gnu-ld: /tmp/go-link-2402600021/000004.o: in function `_cgo_3c1cec0c9a4e_C2func_getaddrinfo':
#53 953.6 /tmp/go-build/cgo-gcc-prolog:58: warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-25 08:52:39 -04:00
|
|
|
if [ "$CGO_ENABLED" = "1" ] && [ "$GO_LINKMODE" = "static" ]; then
|
|
|
|
# compiling statically with CGO enabled requires osusergo to be set.
|
|
|
|
GO_BUILDTAGS="$GO_BUILDTAGS osusergo"
|
|
|
|
fi
|
2021-11-17 11:18:15 -05:00
|
|
|
if [ -n "$GO_STRIP" ]; then
|
2022-07-28 16:49:02 -04:00
|
|
|
# if stripping enabled and building with llvm < 12 against darwin/amd64
|
|
|
|
# platform, it will fail with:
|
|
|
|
# # github.com/docker/cli/cmd/docker
|
|
|
|
# /usr/local/go/pkg/tool/linux_amd64/link: /usr/local/go/pkg/tool/linux_amd64/link: running strip failed: exit status 1
|
|
|
|
# llvm-strip: error: unsupported load command (cmd=0x5)
|
|
|
|
# more info: https://github.com/docker/cli/pull/3717
|
2022-07-23 03:38:50 -04:00
|
|
|
GO_LDFLAGS="$GO_LDFLAGS -s -w"
|
2021-11-17 11:18:15 -05:00
|
|
|
fi
|
use GO_LDFLAGS instead of LDFLAGS to prevent inheriting unrelated options
When building on Fedora 36, the build failed. I suspect this is because the
rpm tools also set LDFLAGS, but with options that cannot be used;
GO_LINKMODE=dynamic
+ ./scripts/build/binary
/go/src/github.com/docker/cli ~/rpmbuild/BUILD/src
Building dynamic docker-linux-arm64
+ go build -o build/docker-linux-arm64 -tags ' pkcs11' -ldflags '-Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -Wl,--build-id=sha1 -Wl,-dT,/root/rpmbuild/BUILD/src/.package_note-docker-ce-cli-0.0.0.20220330082637.68cad50-0.fc36.aarch64.ld -w -X "github.com/docker/cli/cli/version.GitCommit=68cad50" -X "github.com/docker/cli/cli/version.BuildTime=2022-03-30T20:05:36Z" -X "github.com/docker/cli/cli/version.Version=0.0.0-20220330082637-68cad50" -X "github.com/docker/cli/cli/version.PlatformName=Docker Engine - Community"' -buildmode=pie github.com/docker/cli/cmd/docker
# github.com/docker/cli/cmd/docker
flag provided but not defined: -Wl,-z,relro
usage: link [options] main.o
This patch changes the variable we use to `GO_LDFLAGS`, taking a similar approach
as containerd, and various other projects using this name: https://grep.app/search?q=GO_LDFLAGS
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-31 04:30:56 -04:00
|
|
|
export GO_LDFLAGS="$GO_LDFLAGS" # https://github.com/koalaman/shellcheck/issues/2064
|
2021-11-17 11:18:15 -05:00
|
|
|
|
2017-05-11 18:52:17 -04:00
|
|
|
export SOURCE="github.com/docker/cli/cmd/docker"
|