2015-11-04 09:29:26 -05:00
|
|
|
#!/usr/bin/env bash
|
2017-02-02 02:05:09 -05:00
|
|
|
|
2014-09-02 18:13:55 -04:00
|
|
|
# This script builds the application from source for multiple platforms.
|
2017-02-02 02:05:09 -05:00
|
|
|
# Determine the arch/os combos we're building for
|
2019-04-25 16:29:25 -04:00
|
|
|
ALL_XC_ARCH="386 amd64 arm arm64 ppc64le mips mips64 mipsle mipsle64 s390x"
|
|
|
|
ALL_XC_OS="linux darwin windows freebsd openbsd"
|
2017-02-02 02:05:09 -05:00
|
|
|
|
2019-01-31 09:50:10 -05:00
|
|
|
# Exit immediately if a command fails
|
2014-09-02 18:13:55 -04:00
|
|
|
set -e
|
|
|
|
|
2019-01-31 09:50:10 -05:00
|
|
|
# Validates that a necessary tool is on the PATH
|
|
|
|
function validateToolPresence
|
|
|
|
{
|
|
|
|
local TOOLNAME=$1
|
2019-02-01 17:11:22 -05:00
|
|
|
which ${TOOLNAME} >/dev/null
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "${TOOLNAME} is not on the path. Exiting..."
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-01-31 09:50:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
# Validates that all used tools are present; exits when any is not found
|
|
|
|
function validatePreconditions
|
|
|
|
{
|
|
|
|
echo "==> Checking for necessary tools..."
|
|
|
|
validateToolPresence realpath
|
|
|
|
validateToolPresence dirname
|
|
|
|
validateToolPresence tr
|
|
|
|
validateToolPresence find
|
|
|
|
}
|
|
|
|
|
2014-09-02 18:13:55 -04:00
|
|
|
# Get the parent directory of where this script is.
|
2019-01-31 09:50:10 -05:00
|
|
|
# NOTE: I'm unsure why you don't just use realpath like below
|
|
|
|
function enterPackerSourceDir
|
|
|
|
{
|
|
|
|
echo "==> Entering Packer source dir..."
|
|
|
|
local BUILD_SCRIPT_PATH="${BASH_SOURCE[0]}"
|
|
|
|
SOURCEDIR=$(dirname $(dirname $(realpath "${BUILD_SCRIPT_PATH}")))
|
|
|
|
cd ${SOURCEDIR}
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensureOutputStructure {
|
|
|
|
echo "==> Ensuring output directories are present..."
|
|
|
|
mkdir -p bin/
|
|
|
|
mkdir -p pkg/
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanOutputDirs {
|
|
|
|
echo "==> Removing old builds..."
|
|
|
|
rm -f bin/*
|
|
|
|
rm -fr pkg/*
|
|
|
|
}
|
2017-02-02 02:05:09 -05:00
|
|
|
|
2019-01-31 09:50:10 -05:00
|
|
|
function lowerCaseOSType {
|
|
|
|
local OS_TYPE=${OSTYPE:=`uname`}
|
|
|
|
echo "${OS_TYPE}" | tr "[:upper:]" "[:lower:]"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Returns the OS appropriate path separator
|
|
|
|
function getPathSeparator {
|
|
|
|
# helpers for Cygwin-hosted builds
|
|
|
|
case "$(lowerCaseOSType)" in
|
|
|
|
mingw*|msys*|cygwin*)
|
|
|
|
# cygwin only translates ';' to ':' on select environment variables
|
|
|
|
echo ';'
|
2017-02-02 02:05:09 -05:00
|
|
|
;;
|
2019-01-31 09:50:10 -05:00
|
|
|
*) echo ':'
|
2017-02-02 02:05:09 -05:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2019-01-31 09:50:10 -05:00
|
|
|
function convertPathOnCygwin() {
|
|
|
|
local flag
|
|
|
|
local somePath
|
|
|
|
if [ "${1:0:1}" = '-' ]; then
|
|
|
|
flag=$1
|
|
|
|
somePath=$2
|
|
|
|
else
|
|
|
|
somePath=$1
|
|
|
|
fi
|
|
|
|
|
|
|
|
[ -n "${somePath}" ] || return 0
|
|
|
|
case "$(lowerCaseOSType)" in
|
|
|
|
cygwin*)
|
|
|
|
cygpath ${flag} -- "${somePath}"
|
|
|
|
;;
|
|
|
|
*) echo "${somePath}"
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2019-02-01 14:48:42 -05:00
|
|
|
validatePreconditions
|
2019-01-31 09:50:10 -05:00
|
|
|
enterPackerSourceDir
|
|
|
|
ensureOutputStructure
|
|
|
|
cleanOutputDirs
|
|
|
|
|
|
|
|
PATHSEP=$(getPathSeparator)
|
|
|
|
|
2017-02-02 02:05:09 -05:00
|
|
|
# XXX works in MINGW?
|
2019-01-31 09:50:10 -05:00
|
|
|
# FIXME: What if go is not in the PATH and GOROOT isn't set?
|
|
|
|
which go &>/dev/null || PATH+=":`convertPathOnCygwin "${GOROOT:?}"`/bin"
|
2017-02-02 02:05:09 -05:00
|
|
|
|
2019-01-31 09:50:10 -05:00
|
|
|
OLDIFS="${IFS}"
|
2017-02-02 02:05:09 -05:00
|
|
|
|
|
|
|
# make sure GOPATH is consistent - Windows binaries can't handle Cygwin-style paths
|
2019-01-31 09:50:10 -05:00
|
|
|
IFS="${PATHSEP}"
|
2017-02-02 02:05:09 -05:00
|
|
|
for d in ${GOPATH:-$(go env GOPATH)}; do
|
2019-01-31 09:50:10 -05:00
|
|
|
_GOPATH+="${_GOPATH:+${PATHSEP}}$(convertPathOnCygwin --windows "${d}")"
|
2017-02-02 02:05:09 -05:00
|
|
|
done
|
|
|
|
GOPATH="$_GOPATH"
|
|
|
|
|
|
|
|
# locate 'gox' and traverse GOPATH if needed
|
|
|
|
which "${GOX:=gox}" &>/dev/null || {
|
2019-01-31 09:50:10 -05:00
|
|
|
for d in ${GOPATH}; do
|
|
|
|
GOX="$(convertPathOnCygwin --unix "${d}")/bin/gox"
|
|
|
|
[ -x "${GOX}" ] && break || unset GOX
|
2017-02-02 02:05:09 -05:00
|
|
|
done
|
|
|
|
}
|
|
|
|
IFS="$OLDIFS"
|
|
|
|
|
2014-09-02 18:13:55 -04:00
|
|
|
# Build!
|
|
|
|
echo "==> Building..."
|
2017-02-02 02:05:09 -05:00
|
|
|
|
|
|
|
# If in dev mode, only build for ourself
|
|
|
|
if [ -n "${PACKER_DEV+x}" ]; then
|
|
|
|
XC_OS=$(go env GOOS)
|
|
|
|
XC_ARCH=$(go env GOARCH)
|
|
|
|
fi
|
|
|
|
|
2014-09-11 14:51:44 -04:00
|
|
|
set +e
|
2017-02-02 02:05:09 -05:00
|
|
|
${GOX:?command not found} \
|
|
|
|
-os="${XC_OS:-$ALL_XC_OS}" \
|
|
|
|
-arch="${XC_ARCH:-$ALL_XC_ARCH}" \
|
2017-05-15 16:31:38 -04:00
|
|
|
-osarch="!darwin/arm !darwin/arm64" \
|
2017-05-04 23:30:09 -04:00
|
|
|
-ldflags "${GOLDFLAGS}" \
|
2015-10-21 19:41:58 -04:00
|
|
|
-output "pkg/{{.OS}}_{{.Arch}}/packer" \
|
|
|
|
.
|
2017-02-02 02:05:09 -05:00
|
|
|
set -e
|
2014-09-02 18:13:55 -04:00
|
|
|
|
2017-02-02 02:05:09 -05:00
|
|
|
# trim GOPATH to first element
|
2019-01-31 09:50:10 -05:00
|
|
|
IFS="${PATHSEP}"
|
|
|
|
# FIXME: How do you know that the first path of GOPATH is the main GOPATH? Or is the main GOPATH meant to be the first path in GOPATH?
|
|
|
|
MAIN_GOPATH=(${GOPATH})
|
|
|
|
MAIN_GOPATH="$(convertPathOnCygwin --unix "${MAIN_GOPATH[0]}")"
|
|
|
|
IFS="${OLDIFS}"
|
2014-09-07 18:58:36 -04:00
|
|
|
|
2014-09-02 18:13:55 -04:00
|
|
|
# Copy our OS/Arch to the bin/ directory
|
2014-09-07 18:58:36 -04:00
|
|
|
echo "==> Copying binaries for this platform..."
|
2019-01-31 09:50:10 -05:00
|
|
|
DEV_PLATFORM="./pkg/${XC_OS}_${XC_ARCH}"
|
2018-04-09 19:47:41 -04:00
|
|
|
for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f 2>/dev/null); do
|
2017-02-02 02:05:09 -05:00
|
|
|
cp -v ${F} bin/
|
2019-01-31 09:50:10 -05:00
|
|
|
cp -v ${F} "${MAIN_GOPATH}/bin/"
|
2014-09-02 18:13:55 -04:00
|
|
|
done
|
|
|
|
|
|
|
|
# Done!
|
|
|
|
echo
|
|
|
|
echo "==> Results:"
|
|
|
|
ls -hl bin/
|