github.com/minio/mc@v0.0.0-20240507152021-646712d5e5fb/buildscripts/build.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright (c) 2015-2021 MinIO, Inc.
     4  #
     5  # This file is part of MinIO Object Storage stack
     6  #
     7  # This program is free software: you can redistribute it and/or modify
     8  # it under the terms of the GNU Affero General Public License as published by
     9  # the Free Software Foundation, either version 3 of the License, or
    10  # (at your option) any later version.
    11  #
    12  # This program is distributed in the hope that it will be useful
    13  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  # GNU Affero General Public License for more details.
    16  #
    17  # You should have received a copy of the GNU Affero General Public License
    18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19  #
    20  
    21  _init() {
    22      # Save release LDFLAGS
    23      LDFLAGS=$(go run buildscripts/gen-ldflags.go)
    24  
    25      # Extract release tag
    26      release_tag=$(echo $LDFLAGS | awk {'print $6'} | cut -f2 -d=)
    27  
    28      # Verify release tag.
    29      if [ -z "$release_tag" ]; then
    30          echo "Release tag cannot be empty. Please check return value of \`go run buildscripts/gen-ldflags.go\`"
    31          exit 1;
    32      fi
    33  
    34      # Extract release string.
    35      release_str=$(echo $MC_RELEASE | tr '[:upper:]' '[:lower:]')
    36  
    37      # Verify release string.
    38      if [ -z "$release_str" ]; then
    39          echo "Release string cannot be empty. Please set \`MC_RELEASE\` env variable."
    40          exit 1;
    41      fi
    42  
    43      # List of supported architectures
    44      SUPPORTED_OSARCH='linux/amd64 linux/ppc64le windows/amd64 darwin/amd64'
    45  
    46      ## System binaries
    47      CP=`which cp`
    48      SHASUM=`which shasum`
    49      SHA256SUM="${SHASUM} -a 256"
    50      SED=`which sed`
    51  }
    52  
    53  go_build() {
    54      local osarch=$1
    55      os=$(echo $osarch | cut -f1 -d'/')
    56      arch=$(echo $osarch | cut -f2 -d'/')
    57      package=$(go list -f '{{.ImportPath}}')
    58      echo -n "-->"
    59      printf "%15s:%s\n" "${osarch}" "${package}"
    60  
    61      # Release binary name
    62      release_bin="$release_str/$os-$arch/$(basename $package).$release_tag"
    63      # Release binary downloadable name
    64      release_real_bin="$release_str/$os-$arch/$(basename $package)"
    65  
    66      # Release sha1sum name
    67      release_shasum="$release_str/$os-$arch/$(basename $package).${release_tag}.shasum"
    68      # Release sha1sum default
    69      release_shasum_default="$release_str/$os-$arch/$(basename $package).shasum"
    70  
    71      # Release sha256sum name
    72      release_sha256sum="$release_str/$os-$arch/$(basename $package).${release_tag}.sha256sum"
    73      # Release sha256sum default
    74      release_sha256sum_default="$release_str/$os-$arch/$(basename $package).sha256sum"
    75  
    76      # Go build to build the binary.
    77      CGO_ENABLED=0 GOOS=$os GOARCH=$arch go build -tags kqueue --ldflags "${LDFLAGS}" -o $release_bin
    78  
    79      # Create copy
    80      if [ $os == "windows" ]; then
    81          $CP -p $release_bin ${release_real_bin}.exe
    82      else
    83          $CP -p $release_bin $release_real_bin
    84      fi
    85  
    86      # Calculate sha1sum
    87      shasum_str=$(${SHASUM} ${release_bin})
    88      echo ${shasum_str} | $SED "s/$release_str\/$os-$arch\///g" > $release_shasum
    89      $CP -p $release_shasum $release_shasum_default
    90  
    91      # Calculate sha256sum
    92      sha256sum_str=$(${SHA256SUM} ${release_bin})
    93      echo ${sha256sum_str} | $SED "s/$release_str\/$os-$arch\///g" > $release_sha256sum
    94      $CP -p $release_sha256sum $release_sha256sum_default
    95  }
    96  
    97  main() {
    98      # Build releases.
    99      echo "Executing $release_str builds for OS: ${SUPPORTED_OSARCH}"
   100      echo  "Choose an OS Arch from the below"
   101      for osarch in ${SUPPORTED_OSARCH}; do
   102          echo ${osarch}
   103      done
   104  
   105      read -p "If you want to build for all, Just press Enter: " chosen_osarch
   106      if [ "$chosen_osarch" = "" ] || [ "$chosen_osarch" = "all" ]; then
   107          for each_osarch in ${SUPPORTED_OSARCH}; do
   108              go_build ${each_osarch}
   109          done
   110      else
   111          local found=0
   112          for each_osarch in ${SUPPORTED_OSARCH}; do
   113              if [ "$chosen_osarch" = "$each_osarch" ]; then
   114                  found=1
   115              fi
   116          done
   117          if [ ${found} -eq 1 ]; then
   118              go_build ${chosen_osarch}
   119          else
   120              echo "Unknown architecture \"${chosen_osarch}\""
   121              exit 1
   122          fi
   123      fi
   124  
   125  }
   126  
   127  # Run main.
   128  _init && main