github.com/dkerwin/nomad@v0.3.3-0.20160525181927-74554135514b/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  
    18  # Determine the arch/os combos we're building for
    19  XC_ARCH=${XC_ARCH:-"386 amd64 arm"}
    20  XC_OS=${XC_OS:-darwin dragonfly freebsd linux netbsd openbsd solaris windows}
    21  
    22  # Delete the old dir
    23  echo "==> Removing old directory..."
    24  rm -f bin/*
    25  rm -rf pkg/*
    26  mkdir -p bin/
    27  
    28  # If its dev mode, only build for ourself
    29  if [[ "${NOMAD_DEV}" ]]; then
    30      XC_OS=$(go env GOOS)
    31      XC_ARCH=$(go env GOARCH)
    32  fi
    33  
    34  # Build!
    35  echo "==> Building..."
    36  gox \
    37      -os="${XC_OS}" \
    38      -os="!dragonfly" \
    39      -os="!freebsd" \
    40      -os="!netbsd" \
    41      -os="!openbsd" \
    42      -os="!solaris" \
    43      -arch="${XC_ARCH}" \
    44      -osarch="!linux/arm !darwin/386" \
    45      -ldflags "-X main.GitCommit='${GIT_COMMIT}${GIT_DIRTY}'" \
    46      -cgo \
    47      -output "pkg/{{.OS}}_{{.Arch}}/nomad" \
    48      .
    49  
    50  # Move all the compiled things to the $GOPATH/bin
    51  GOPATH=${GOPATH:-$(go env GOPATH)}
    52  case $(uname) in
    53      CYGWIN*)
    54          GOPATH="$(cygpath $GOPATH)"
    55          ;;
    56  esac
    57  OLDIFS=$IFS
    58  IFS=: MAIN_GOPATH=($GOPATH)
    59  IFS=$OLDIFS
    60  
    61  # Copy our OS/Arch to the bin/ directory
    62  DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)"
    63  for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do
    64      cp ${F} bin/
    65      cp ${F} ${MAIN_GOPATH}/bin/
    66  done
    67  
    68  if [[ "x${NOMAD_DEV}" == "x" ]]; then
    69      # Zip and copy to the dist dir
    70      echo "==> Packaging..."
    71      for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do
    72          OSARCH=$(basename ${PLATFORM})
    73          echo "--> ${OSARCH}"
    74  
    75          pushd $PLATFORM >/dev/null 2>&1
    76          zip ../${OSARCH}.zip ./*
    77          popd >/dev/null 2>&1
    78      done
    79  fi
    80  
    81  # Done!
    82  echo
    83  echo "==> Results:"
    84  ls -hl bin/