github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/build/build.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2018-2020 The OpenEBS Authors. All rights reserved.
     3  #
     4  # Licensed under the Apache License, Version 2.0 (the "License");
     5  # you may not use this file except in compliance with the License.
     6  # You may obtain a copy of the License at
     7  #
     8  #     http://www.apache.org/licenses/LICENSE-2.0
     9  #
    10  # Unless required by applicable law or agreed to in writing, software
    11  # distributed under the License is distributed on an "AS IS" BASIS,
    12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  #
    16  # This script builds the application from source for multiple platforms.
    17  set -e
    18  
    19  # Get the git commit
    20  getGitCommit()
    21  {
    22      if [ -f "$GOPATH"/src/github.com/openebs/node-disk-manager/GITCOMMIT ];
    23      then
    24  	GIT_COMMIT="$(cat "$GOPATH"/src/github.com/openebs/node-disk-manager/GITCOMMIT)"
    25      else
    26  	GIT_COMMIT="$(git rev-parse HEAD)"
    27      fi
    28      echo "${GIT_COMMIT}"
    29  }
    30  
    31  # Delete the old contents
    32  deleteOldContents(){
    33      rm -rf bin/*
    34      mkdir -p bin/
    35  
    36      echo "Successfully deleted old bin contents"
    37  }
    38  
    39  # Move all the compiled things to the $GOPATH/bin
    40  moveCompiled(){
    41      GOPATH=${GOPATH:-$(go env GOPATH)}
    42      case $(uname) in
    43          CYGWIN*)
    44              GOPATH="$(cygpath "$GOPATH")"
    45              ;;
    46      esac
    47      OLDIFS=$IFS
    48      IFS=: MAIN_GOPATH=("$GOPATH")
    49      IFS=$OLDIFS
    50  
    51      # Create the gopath bin if not already available
    52      mkdir -p "${MAIN_GOPATH[*]}"/bin/
    53  
    54      # Copy our OS/Arch to ${MAIN_GOPATH}/bin/ directory
    55      DEV_PLATFORM="./bin/$(go env GOOS)_$(go env GOARCH)"
    56      DEV_PLATFORM_OUTPUT=$(find "${DEV_PLATFORM}" -mindepth 1 -maxdepth 1 -type f)
    57      for F in ${DEV_PLATFORM_OUTPUT}; do
    58          cp "${F}" bin/
    59          cp "${F}" "${MAIN_GOPATH[*]}"/bin/
    60      done
    61  
    62      echo "Moved all the compiled things successfully to :${MAIN_GOPATH[*]}/bin/"
    63  }
    64  
    65  moveCompiledBuildx(){
    66      GOPATH=${GOPATH:-$(go env GOPATH)}
    67      OLDIFS=$IFS
    68      IFS=: MAIN_GOPATH=("$GOPATH")
    69      IFS=$OLDIFS
    70  
    71      # Create the gopath bin if not already available
    72      mkdir -p "${MAIN_GOPATH[*]}"/bin/
    73  
    74      # Copy our OS/Arch to ${MAIN_GOPATH}/bin/ directory
    75      DEV_PLATFORM="./bin"
    76      cp -r "${DEV_PLATFORM}" "${MAIN_GOPATH[*]}"/bin/
    77  
    78      echo "Moved all the compiled things successfully to :${MAIN_GOPATH[*]}/bin/"
    79  }
    80  
    81  # Buildx
    82  buildx(){
    83      output_name="bin/$CTLNAME"
    84      echo "Building for: ${GOOS} ${GOARCH}"
    85      go build \
    86          -ldflags="-X github.com/openebs/node-disk-manager/pkg/version.GitCommit=${GIT_COMMIT} \
    87          -X main.CtlName='${CTLNAME}' \
    88          -X github.com/openebs/node-disk-manager/pkg/version.Version=${VERSION}" \
    89          -o "$output_name" ./cmd/"$BUILDPATH"
    90  
    91      echo "Buildx Successfully built: ${CTLNAME}"
    92  }
    93  
    94  # Build
    95  build(){
    96      for GOOS in "${XC_OSS[@]}"
    97      do
    98          for GOARCH in "${XC_ARCHS[@]}"
    99          do
   100              UNDERSCORE="_"
   101              output_name="bin/${GOOS}${UNDERSCORE}${GOARCH}/$CTLNAME"
   102  
   103              if [ "$GOOS" = "windows" ]; then
   104                  output_name+='.exe'
   105              fi
   106              echo "Building for: ${GOOS} ${GOARCH}"
   107              go build \
   108                  -ldflags="-X github.com/openebs/node-disk-manager/pkg/version.GitCommit=${GIT_COMMIT} \
   109                  -X main.CtlName='${CTLNAME}' \
   110                  -X github.com/openebs/node-disk-manager/pkg/version.Version=${VERSION}" \
   111                  -o "$output_name" ./cmd/"$BUILDPATH"
   112          done
   113      done
   114      echo "Successfully built: ${CTLNAME}"
   115  }
   116  
   117  # Main script starts here .......
   118  export CGO_ENABLED=1
   119  
   120  # Get the parent directory of where this script is.
   121  SOURCE="${BASH_SOURCE[*]}"
   122  
   123  while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
   124  DIR="$( cd -P "$( dirname "$SOURCE" )/../" && pwd )"
   125  
   126  # Change into that directory
   127  cd "$DIR"
   128  
   129  # Get the git commit
   130  GIT_COMMIT=$(getGitCommit)
   131  
   132  # Get the version details. By default set as ci.
   133  VERSION="ci"
   134  
   135  if [ -n "${RELEASE_TAG}" ] ;
   136  then
   137      # When github is tagged with a release, then github action release workflow
   138      # will set the release tag in env RELEASE_TAG
   139      VERSION="${RELEASE_TAG}"
   140  fi;
   141  
   142  
   143  # Determine the arch/os combos we're building for
   144  #XC_ARCH=${XC_ARCH:-"amd64"}
   145  #XC_OS=${XC_OS:-"linux"}
   146  
   147  XC_ARCHS=("${XC_ARCH// / }")
   148  XC_OSS=("${XC_OS// / }")
   149  
   150  echo "==> Removing old bin contents..."
   151  deleteOldContents
   152  
   153  # If its dev mode, only build for ourself
   154  if [[ -n "${NDM_AGENT_DEV}" ]]; then
   155      XC_OS=$(go env GOOS)
   156      XC_ARCH=$(go env GOARCH)
   157  fi
   158  
   159  # Build!
   160  echo "==> Building ${CTLNAME} ..."
   161  if [[ ${BUILDX} ]]; then
   162      buildx
   163      # Move all the compiled things to the $GOPATH/bin
   164      moveCompiledBuildx
   165  else
   166      build
   167      # Move all the compiled things to the $GOPATH/bin
   168      moveCompiled
   169  fi
   170  
   171  ls -hl bin