github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/scripts/build/.variables (about)

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