github.com/emate/packer@v0.8.1-0.20150625195101-fe0fde195dc6/scripts/build.sh (about) 1 #!/bin/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 # If its dev mode, only build for ourself 19 if [ "${TF_DEV}x" != "x" ]; then 20 XC_OS=${XC_OS:-$(go env GOOS)} 21 XC_ARCH=${XC_ARCH:-$(go env GOARCH)} 22 fi 23 24 # Determine the arch/os combos we're building for 25 XC_ARCH=${XC_ARCH:-"386 amd64 arm"} 26 XC_OS=${XC_OS:-linux darwin windows freebsd openbsd} 27 28 # Install dependencies 29 echo "==> Getting dependencies..." 30 go get ./... 31 32 # Delete the old dir 33 echo "==> Removing old directory..." 34 rm -f bin/* 35 rm -rf pkg/* 36 mkdir -p bin/ 37 38 # Build! 39 echo "==> Building..." 40 set +e 41 gox \ 42 -os="${XC_OS}" \ 43 -arch="${XC_ARCH}" \ 44 -ldflags "-X main.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \ 45 -output "pkg/{{.OS}}_{{.Arch}}/packer-{{.Dir}}" \ 46 ./... 47 set -e 48 49 # Make sure "packer-packer" is renamed properly 50 for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do 51 set +e 52 mv ${PLATFORM}/packer-packer.exe ${PLATFORM}/packer.exe 2>/dev/null 53 mv ${PLATFORM}/packer-packer ${PLATFORM}/packer 2>/dev/null 54 set -e 55 done 56 57 # Move all the compiled things to the $GOPATH/bin 58 GOPATH=${GOPATH:-$(go env GOPATH)} 59 case $(uname) in 60 CYGWIN*) 61 GOPATH="$(cygpath $GOPATH)" 62 ;; 63 esac 64 OLDIFS=$IFS 65 IFS=: MAIN_GOPATH=($GOPATH) 66 IFS=$OLDIFS 67 68 # Copy our OS/Arch to the bin/ directory 69 echo "==> Copying binaries for this platform..." 70 DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)" 71 for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do 72 cp ${F} bin/ 73 cp ${F} ${MAIN_GOPATH}/bin/ 74 done 75 76 # Done! 77 echo 78 echo "==> Results:" 79 ls -hl bin/