github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/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=0 GOARCH="386"   GOOS="windows" go build -ldflags "-X $LDFLAG" -o "pkg/windows_386/nomad.exe"
    68              # Use the following if CGO is required
    69              #CGO_ENABLED=1 CXX=i686-w64-mingw32-g++ CC=i686-w64-mingw32-gcc GOARCH="386"   GOOS="windows" go build -ldflags "-X $LDFLAG" -o "pkg/windows_386/nomad.exe"
    70              ;;
    71          "windows_amd64")
    72              echo "==> Building windows amd64..."
    73              CGO_ENABLED=0 GOARCH="amd64" GOOS="windows" go build -ldflags "-X $LDFLAG" -o "pkg/windows_amd64/nomad.exe"
    74              # Use the following if CGO is required
    75              #CGO_ENABLED=1 CXX=x86_64-w64-mingw32-g++ CC=x86_64-w64-mingw32-gcc GOARCH="amd64" GOOS="windows" go build -ldflags "-X $LDFLAG" -o "pkg/windows_amd64/nomad.exe"
    76              ;;
    77          "darwin_amd64")
    78              echo "==> Building darwin amd64..."
    79              CGO_ENABLED=1 GOARCH="amd64" GOOS="darwin"  go build -ldflags "-X $LDFLAG" -o "pkg/darwin_amd64/nomad"
    80              ;;
    81          *)
    82              echo "--> Invalid target: $target"
    83              ;;
    84      esac
    85  done
    86  
    87  set -e
    88  
    89  # Move all the compiled things to $GOPATH/bin
    90  GOPATH=${GOPATH:-$(go env GOPATH)}
    91  case $(uname) in
    92      CYGWIN*)
    93          GOPATH="$(cygpath $GOPATH)"
    94          ;;
    95  esac
    96  OLDIFS=$IFS
    97  IFS=: MAIN_GOPATH=($GOPATH)
    98  IFS=$OLDIFS
    99  
   100  # Copy our OS/Arch to the bin/ directory
   101  DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)"
   102  for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do
   103      cp ${F} bin/
   104      cp ${F} ${MAIN_GOPATH}/bin/
   105  done
   106  
   107  # Zip and copy to the dist dir
   108  echo "==> Packaging..."
   109  for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do
   110      OSARCH=$(basename ${PLATFORM})
   111      echo "--> ${OSARCH}"
   112  
   113      pushd $PLATFORM >/dev/null 2>&1
   114      zip ../${OSARCH}.zip ./*
   115      popd >/dev/null 2>&1
   116  done
   117  
   118  # Done!
   119  echo
   120  echo "==> Results:"
   121  tree pkg/