github.com/alowde/packer@v1.3.1/scripts/build.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # This script builds the application from source for multiple platforms.
     4  # Determine the arch/os combos we're building for
     5  ALL_XC_ARCH="386 amd64 arm arm64 ppc64le"
     6  ALL_XC_OS="linux darwin windows freebsd openbsd solaris"
     7  
     8  set -e
     9  
    10  # Get the parent directory of where this script is.
    11  SOURCE="${BASH_SOURCE[0]}"
    12  while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
    13  DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"
    14  
    15  # Change into that directory
    16  cd $DIR
    17  
    18  # Delete the old dir
    19  echo "==> Removing old directory..."
    20  rm -f bin/*
    21  rm -rf pkg/*
    22  mkdir -p bin/
    23  
    24  # helpers for Cygwin-hosted builds
    25  : ${OSTYPE:=`uname`}
    26  
    27  case $OSTYPE in
    28      MINGW*|MSYS*|cygwin|CYGWIN*)
    29          # cygwin only translates ';' to ':' on select environment variables
    30          PATHSEP=';'
    31          ;;
    32      *)	PATHSEP=':'
    33  esac
    34  
    35  function convert_path() {
    36      local flag
    37      [ "${1:0:1}" = '-' ] && { flag="$1"; shift; }
    38  
    39      [ -n "$1" ] || return 0
    40      case ${OSTYPE:-`uname`} in
    41          cygwin|CYGWIN*)
    42              cygpath $flag -- "$1"
    43              ;;
    44          *)  echo "$1"
    45      esac
    46  }
    47  
    48  # XXX works in MINGW?
    49  which go &>/dev/null || PATH+=":`convert_path "${GOROOT:?}"`/bin"
    50  
    51  OLDIFS="$IFS"
    52  
    53  # make sure GOPATH is consistent - Windows binaries can't handle Cygwin-style paths
    54  IFS="$PATHSEP"
    55  for d in ${GOPATH:-$(go env GOPATH)}; do
    56      _GOPATH+="${_GOPATH:+$PATHSEP}$(convert_path --windows "$d")"
    57  done
    58  GOPATH="$_GOPATH"
    59  
    60  # locate 'gox' and traverse GOPATH if needed
    61  which "${GOX:=gox}" &>/dev/null || {
    62      for d in $GOPATH; do
    63          GOX="$(convert_path --unix "$d")/bin/gox"
    64          [ -x "$GOX" ] && break || unset GOX
    65      done
    66  }
    67  IFS="$OLDIFS"
    68  
    69  # Build!
    70  echo "==> Building..."
    71  
    72  # If in dev mode, only build for ourself
    73  if [ -n "${PACKER_DEV+x}" ]; then
    74      XC_OS=$(go env GOOS)
    75      XC_ARCH=$(go env GOARCH)
    76  fi
    77  
    78  set +e
    79  ${GOX:?command not found} \
    80      -os="${XC_OS:-$ALL_XC_OS}" \
    81      -arch="${XC_ARCH:-$ALL_XC_ARCH}" \
    82      -osarch="!darwin/arm !darwin/arm64" \
    83      -ldflags "${GOLDFLAGS}" \
    84      -output "pkg/{{.OS}}_{{.Arch}}/packer" \
    85      .
    86  set -e
    87  
    88  # trim GOPATH to first element
    89  IFS="$PATHSEP"
    90  MAIN_GOPATH=($GOPATH)
    91  MAIN_GOPATH="$(convert_path --unix "$MAIN_GOPATH")"
    92  IFS=$OLDIFS
    93  
    94  # Copy our OS/Arch to the bin/ directory
    95  echo "==> Copying binaries for this platform..."
    96  DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)"
    97  for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f 2>/dev/null); do
    98      cp -v ${F} bin/
    99      cp -v ${F} ${MAIN_GOPATH}/bin/
   100  done
   101  
   102  # Done!
   103  echo
   104  echo "==> Results:"
   105  ls -hl bin/