github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/cli/scripts/build/binary (about)

     1  #!/usr/bin/env sh
     2  #
     3  # Build a static binary for the host OS/ARCH
     4  #
     5  
     6  set -eu
     7  
     8  : "${CGO_ENABLED=}"
     9  : "${GO_LINKMODE=static}"
    10  : "${GO_BUILDMODE=}"
    11  : "${GO_BUILDTAGS=}"
    12  : "${GO_STRIP=}"
    13  
    14  . ./scripts/build/.variables
    15  
    16  if [ -z "$CGO_ENABLED" ]; then
    17      case "$(go env GOOS)" in
    18          linux)
    19              case "$(go env GOARCH)" in
    20                  amd64|arm64|arm|s390x)
    21                      CGO_ENABLED=1
    22                  ;;
    23                  *)
    24                      CGO_ENABLED=0
    25                  ;;
    26              esac
    27          ;;
    28          darwin|windows)
    29              CGO_ENABLED=1
    30          ;;
    31          *)
    32              CGO_ENABLED=0
    33          ;;
    34      esac
    35  fi
    36  export CGO_ENABLED
    37  if [ "$CGO_ENABLED" = "1" ] && [ "$(go env GOOS)" != "windows" ]; then
    38      case "$(go env GOARCH)" in
    39          mips*|ppc64)
    40              # pie build mode is not supported on mips architectures
    41              ;;
    42          *)
    43              GO_BUILDMODE="-buildmode=pie"
    44              ;;
    45      esac
    46      GO_BUILDTAGS="$GO_BUILDTAGS pkcs11"
    47  fi
    48  
    49  if [ "$CGO_ENABLED" = "1" ] && [ "$GO_LINKMODE" = "static" ] && [ "$(go env GOOS)" = "linux" ]; then
    50      GO_LDFLAGS="$GO_LDFLAGS -extldflags -static"
    51  fi
    52  
    53  if [ -n "$GO_STRIP" ]; then
    54      GO_LDFLAGS="$GO_LDFLAGS -s -w"
    55  fi
    56  
    57  if [ "$(go env GOOS)" = "windows" ]; then
    58      # Generate a Windows file version of the form major,minor,patch,build
    59      VERSION_QUAD=$(printf "%s" "$VERSION" | sed -re 's/^([0-9.]*).*$/\1/' | tr . , | sed -re 's/^[0-9]+$/\0,0/' | sed -re 's/^[0-9]+,[0-9]+$/\0,0/' | sed -re 's/^[0-9]+,[0-9]+,[0-9]+$/\0,0/')
    60  
    61      set --
    62      [ -n "$VERSION" ]      && set -- "$@" -D "DOCKER_VERSION=\"$VERSION\""
    63      [ -n "$VERSION_QUAD" ] && set -- "$@" -D "DOCKER_VERSION_QUAD=$VERSION_QUAD"
    64      [ -n "$GITCOMMIT" ]    && set -- "$@" -D "DOCKER_COMMIT=\"$GITCOMMIT\""
    65  
    66      windres=$($(go env CC) --print-prog-name=windres)
    67  
    68      target="$(dirname "$0")/../../cli/winresources/rsrc_$(go env GOARCH).syso"
    69      mkdir -p "$(dirname "${target}")"
    70      "$windres" -i "$(dirname "$0")/../winresources/docker.rc" -o "$target" "$@"
    71      echo "package winresources" > "$(dirname "${target}")/stub_windows.go"
    72  fi
    73  
    74  echo "Building $GO_LINKMODE $(basename "${TARGET}")"
    75  
    76  export GO111MODULE=auto
    77  
    78  go build -o "${TARGET}" -tags "${GO_BUILDTAGS}" --ldflags "${GO_LDFLAGS}" ${GO_BUILDMODE} "${SOURCE}"
    79  
    80  ln -sf "$(basename "${TARGET}")" "$(dirname "${TARGET}")/docker"