...
1#!/usr/bin/env sh
2set -eu
3
4: "${CGO_ENABLED=}"
5: "${GO_LINKMODE=static}"
6: "${GO_BUILDMODE=}"
7: "${GO_BUILDTAGS=}"
8: "${GO_STRIP=}"
9
10TARGET=${TARGET:-"build"}
11
12PLATFORM=${PLATFORM:-}
13VERSION=${VERSION:-$(git describe --match 'v[0-9]*' --dirty='.m' --always --tags | sed 's/^v//' 2>/dev/null || echo "unknown-version" )}
14GITCOMMIT=${GITCOMMIT:-$(git rev-parse --short HEAD 2> /dev/null || true)}
15
16if [ "$(uname)" = "Darwin" ]; then
17 # Using BSD date (macOS), which doesn't suppoort the --date option
18 # date -jf "<input format>" "<input value>" +"<output format>" (https://unix.stackexchange.com/a/86510)
19 BUILDTIME=${BUILDTIME:-$(TZ=UTC date -jf "%s" "${SOURCE_DATE_EPOCH:-$(date +%s)}" +"%Y-%m-%dT%H:%M:%SZ")}
20else
21 # Using GNU date (Linux)
22 BUILDTIME=${BUILDTIME:-$(TZ=UTC date -u --date="@${SOURCE_DATE_EPOCH:-$(date +%s)}" +"%Y-%m-%dT%H:%M:%SZ")}
23fi
24
25case "$VERSION" in
26 refs/tags/v*) VERSION=${VERSION#refs/tags/v} ;;
27 refs/tags/*) VERSION=${VERSION#refs/tags/} ;;
28 refs/heads/*) VERSION=$(echo "${VERSION#refs/heads/}" | sed -r 's#/+#-#g') ;;
29 refs/pull/*) VERSION=pr-$(echo "$VERSION" | grep -o '[0-9]\+') ;;
30esac
31
32GOOS="$(go env GOOS)"
33GOARCH="$(go env GOARCH)"
34if [ "${GOARCH}" = "arm" ]; then
35 GOARM="$(go env GOARM)"
36fi
37
38TARGET="$TARGET/docker-${GOOS}-${GOARCH}"
39if [ "${GOARCH}" = "arm" ] && [ -n "${GOARM}" ]; then
40 TARGET="${TARGET}-v${GOARM}"
41fi
42if [ "${GOOS}" = "windows" ]; then
43 TARGET="${TARGET}.exe"
44fi
45export TARGET
46
47if [ -z "$CGO_ENABLED" ]; then
48 case "$(go env GOOS)" in
49 linux)
50 case "$(go env GOARCH)" in
51 amd64|arm64|arm|s390x)
52 CGO_ENABLED=1
53 ;;
54 *)
55 CGO_ENABLED=0
56 ;;
57 esac
58 ;;
59 darwin|windows)
60 CGO_ENABLED=1
61 ;;
62 *)
63 CGO_ENABLED=0
64 ;;
65 esac
66fi
67export CGO_ENABLED
68
69if [ "$CGO_ENABLED" = "1" ] && [ "$(go env GOOS)" != "windows" ]; then
70 case "$(go env GOARCH)" in
71 mips*|ppc64)
72 # pie build mode is not supported on mips architectures
73 ;;
74 *)
75 GO_BUILDMODE="-buildmode=pie"
76 ;;
77 esac
78fi
79export GO_BUILDMODE
80
81GO_LDFLAGS="${GO_LDFLAGS:-}"
82GO_LDFLAGS="$GO_LDFLAGS -X \"github.com/docker/cli/cli/version.GitCommit=${GITCOMMIT}\""
83GO_LDFLAGS="$GO_LDFLAGS -X \"github.com/docker/cli/cli/version.BuildTime=${BUILDTIME}\""
84GO_LDFLAGS="$GO_LDFLAGS -X \"github.com/docker/cli/cli/version.Version=${VERSION}\""
85if test -n "${PLATFORM}"; then
86 GO_LDFLAGS="$GO_LDFLAGS -X \"github.com/docker/cli/cli/version.PlatformName=${PLATFORM}\""
87fi
88if [ "$CGO_ENABLED" = "1" ] && [ "$GO_LINKMODE" = "static" ] && [ "$(go env GOOS)" = "linux" ]; then
89 GO_LDFLAGS="$GO_LDFLAGS -extldflags -static"
90fi
91if [ "$CGO_ENABLED" = "1" ] && [ "$GO_LINKMODE" = "static" ]; then
92 # compiling statically with CGO enabled requires osusergo to be set.
93 GO_BUILDTAGS="$GO_BUILDTAGS osusergo"
94fi
95if [ -n "$GO_STRIP" ]; then
96 # if stripping enabled and building with llvm < 12 against darwin/amd64
97 # platform, it will fail with:
98 # # github.com/docker/cli/cmd/docker
99 # /usr/local/go/pkg/tool/linux_amd64/link: /usr/local/go/pkg/tool/linux_amd64/link: running strip failed: exit status 1
100 # llvm-strip: error: unsupported load command (cmd=0x5)
101 # more info: https://github.com/docker/cli/pull/3717
102 GO_LDFLAGS="$GO_LDFLAGS -s -w"
103fi
104export GO_LDFLAGS="$GO_LDFLAGS" # https://github.com/koalaman/shellcheck/issues/2064
105
106export SOURCE="github.com/docker/cli/cmd/docker"
View as plain text