github.com/ncodes/nomad@v0.5.7-0.20170403112158-97adf4a74fb3/scripts/build.sh (about)

     1  #!/usr/bin/env bash
     2  #
     3  # This script builds the application from source for multiple platforms.
     4  set -e
     5  
     6  # Get the parent directory of where this script is.
     7  SOURCE="${BASH_SOURCE[0]}"
     8  while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
     9  DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"
    10  
    11  # Change into that directory
    12  cd "$DIR"
    13  
    14  # Get the git commit
    15  GIT_COMMIT="$(git rev-parse HEAD)"
    16  GIT_DIRTY="$(test -n "`git status --porcelain`" && echo "+CHANGES" || true)"
    17  LDFLAG="main.GitCommit=${GIT_COMMIT}${GIT_DIRTY}"
    18  
    19  # Delete the old dir
    20  echo "==> Removing old directory..."
    21  rm -f bin/*
    22  rm -rf pkg/*
    23  mkdir -p bin/
    24  
    25  targets="$(go env GOOS)_$(go env GOARCH)"
    26  if [[ "$TARGETS" == "release" ]]; then
    27      if [[ $(uname) == "Linux" ]]; then
    28          targets="linux_386 linux_amd64 linux_amd64-lxc linux_arm linux_arm64 windows_386 windows_amd64"
    29      elif [[ $(uname) == "Darwin" ]]; then
    30  	targets="darwin_amd64"
    31      else
    32          echo "Unable to build on $(uname). Use Linux or Darwin."
    33          exit 1
    34      fi
    35  elif [[ "$TARGETS" != "" ]]; then
    36      targets="$TARGETS"
    37  fi
    38  
    39  # Don't exit if a single target fails
    40  set +e
    41  
    42  echo "TARGETS=\"$targets\""
    43  for target in $targets; do
    44      case $target in
    45          "linux_386")
    46              echo "==> Building linux 386..."
    47              CGO_ENABLED=1 GOARCH="386"   GOOS="linux" go build -ldflags "-X $LDFLAG" -o "pkg/linux_386/nomad"
    48              ;;
    49          "linux_amd64")
    50              echo "==> Building linux amd64..."
    51              CGO_ENABLED=1 GOARCH="amd64" GOOS="linux" go build -ldflags "-X $LDFLAG" -o "pkg/linux_amd64/nomad"
    52              ;;
    53          "linux_amd64-lxc")
    54              echo "==> Building linux amd64 with lxc..."
    55              CGO_ENABLED=1 GOARCH="amd64" GOOS="linux" go build -ldflags "-X $LDFLAG" -o "pkg/linux_amd64-lxc/nomad" -tags "lxc"
    56              ;;
    57          "linux_arm")
    58              echo "==> Building linux arm..."
    59              CGO_ENABLED=1 CC="arm-linux-gnueabihf-gcc-5" GOOS=linux GOARCH="arm"  go build -ldflags "-X $LDFLAG" -o "pkg/linux_arm/nomad"
    60              ;;
    61          "linux_arm64")
    62              echo "==> Building linux arm64..."
    63              CGO_ENABLED=1 CC="aarch64-linux-gnu-gcc-5"  GOOS=linux GOARCH="arm64" go build -ldflags "-X $LDFLAG" -o "pkg/linux_arm64/nomad"
    64              ;;
    65          "windows_386")
    66              echo "==> Building windows 386..."
    67              CGO_ENABLED=1 GOARCH="386"   GOOS="windows" go build -ldflags "-X $LDFLAG" -o "pkg/windows_386/nomad.exe"
    68              ;;
    69          "windows_amd64")
    70              echo "==> Building windows amd64..."
    71              CGO_ENABLED=1 GOARCH="amd64" GOOS="windows" go build -ldflags "-X $LDFLAG" -o "pkg/windows_amd64/nomad.exe"
    72              ;;
    73          "darwin_amd64")
    74              echo "==> Building darwin amd64..."
    75              CGO_ENABLED=1 GOARCH="amd64" GOOS="darwin"  go build -ldflags "-X $LDFLAG" -o "pkg/darwin_amd64/nomad"
    76              ;;
    77          *)
    78              echo "--> Invalid target: $target"
    79              ;;
    80      esac
    81  done
    82  
    83  set -e
    84  
    85  # Move all the compiled things to $GOPATH/bin
    86  GOPATH=${GOPATH:-$(go env GOPATH)}
    87  case $(uname) in
    88      CYGWIN*)
    89          GOPATH="$(cygpath $GOPATH)"
    90          ;;
    91  esac
    92  OLDIFS=$IFS
    93  IFS=: MAIN_GOPATH=($GOPATH)
    94  IFS=$OLDIFS
    95  
    96  # Copy our OS/Arch to the bin/ directory
    97  DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)"
    98  for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do
    99      cp ${F} bin/
   100      cp ${F} ${MAIN_GOPATH}/bin/
   101  done
   102  
   103  # Zip and copy to the dist dir
   104  echo "==> Packaging..."
   105  for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do
   106      OSARCH=$(basename ${PLATFORM})
   107      echo "--> ${OSARCH}"
   108  
   109      pushd $PLATFORM >/dev/null 2>&1
   110      zip ../${OSARCH}.zip ./*
   111      popd >/dev/null 2>&1
   112  done
   113  
   114  # Done!
   115  echo
   116  echo "==> Results:"
   117  tree pkg/