github.com/hashicorp/packer@v1.14.3/scripts/build.sh (about) 1 #!/usr/bin/env bash 2 # Copyright (c) HashiCorp, Inc. 3 # SPDX-License-Identifier: BUSL-1.1 4 5 6 # This script builds the application from source for multiple platforms. 7 # Determine the arch/os combos we're building for 8 ALL_XC_ARCH="386 amd64 arm arm64 ppc64le mips mips64 mipsle mipsle64 s390x" 9 ALL_XC_OS="linux darwin windows freebsd openbsd solaris" 10 SKIPPED_OSARCH="!darwin/arm !freebsd/arm !freebsd/arm64 !darwin/386" 11 12 # Exit immediately if a command fails 13 set -e 14 15 # Validates that a necessary tool is on the PATH 16 function validateToolPresence 17 { 18 local TOOLNAME=$1 19 if ! which ${TOOLNAME} >/dev/null; then 20 echo "${TOOLNAME} is not on the path. Exiting..." 21 exit 1 22 fi 23 } 24 25 # Validates that all used tools are present; exits when any is not found 26 function validatePreconditions 27 { 28 echo "==> Checking for necessary tools..." 29 validateToolPresence realpath 30 validateToolPresence dirname 31 validateToolPresence tr 32 validateToolPresence find 33 } 34 35 # Get the parent directory of where this script is. 36 # NOTE: I'm unsure why you don't just use realpath like below 37 function enterPackerSourceDir 38 { 39 echo "==> Entering Packer source dir..." 40 local BUILD_SCRIPT_PATH="${BASH_SOURCE[0]}" 41 SOURCEDIR=$(dirname $(dirname $(realpath "${BUILD_SCRIPT_PATH}"))) 42 cd ${SOURCEDIR} 43 } 44 45 function ensureOutputStructure { 46 echo "==> Ensuring output directories are present..." 47 mkdir -p bin/ 48 mkdir -p pkg/ 49 } 50 51 function cleanOutputDirs { 52 echo "==> Removing old builds..." 53 rm -f bin/* 54 rm -fr pkg/* 55 } 56 57 function lowerCaseOSType { 58 local OS_TYPE=${OSTYPE:=`uname`} 59 echo "${OS_TYPE}" | tr "[:upper:]" "[:lower:]" 60 } 61 62 # Returns the OS appropriate path separator 63 function getPathSeparator { 64 # helpers for Cygwin-hosted builds 65 case "$(lowerCaseOSType)" in 66 mingw*|msys*|cygwin*) 67 # cygwin only translates ';' to ':' on select environment variables 68 echo ';' 69 ;; 70 *) echo ':' 71 esac 72 } 73 74 function convertPathOnCygwin() { 75 local flag 76 local somePath 77 if [ "${1:0:1}" = '-' ]; then 78 flag=$1 79 somePath=$2 80 else 81 somePath=$1 82 fi 83 84 [ -n "${somePath}" ] || return 0 85 case "$(lowerCaseOSType)" in 86 cygwin*) 87 cygpath ${flag} -- "${somePath}" 88 ;; 89 *) echo "${somePath}" 90 esac 91 } 92 93 validatePreconditions 94 enterPackerSourceDir 95 ensureOutputStructure 96 cleanOutputDirs 97 98 PATHSEP=$(getPathSeparator) 99 100 # XXX works in MINGW? 101 # FIXME: What if go is not in the PATH and GOROOT isn't set? 102 which go &>/dev/null || PATH+=":`convertPathOnCygwin "${GOROOT:?}"`/bin" 103 104 OLDIFS="${IFS}" 105 106 # make sure GOPATH is consistent - Windows binaries can't handle Cygwin-style paths 107 IFS="${PATHSEP}" 108 for d in ${GOPATH:-$(go env GOPATH)}; do 109 _GOPATH+="${_GOPATH:+${PATHSEP}}$(convertPathOnCygwin --windows "${d}")" 110 done 111 GOPATH="$_GOPATH" 112 113 # locate 'gox' and traverse GOPATH if needed 114 which "${GOX:=gox}" &>/dev/null || { 115 for d in ${GOPATH}; do 116 GOX="$(convertPathOnCygwin --unix "${d}")/bin/gox" 117 [ -x "${GOX}" ] && break || unset GOX 118 done 119 } 120 IFS="$OLDIFS" 121 122 # Build! 123 echo "==> Building..." 124 125 # If in dev mode, only build for ourself 126 if [ -n "${PACKER_DEV+x}" ]; then 127 XC_OS=$(go env GOOS) 128 XC_ARCH=$(go env GOARCH) 129 fi 130 131 export CGO_ENABLED=0 132 133 ${GOX:?command not found} \ 134 -os="${XC_OS:-$ALL_XC_OS}" \ 135 -arch="${XC_ARCH:-$ALL_XC_ARCH}" \ 136 -osarch="${SKIPPED_OSARCH}" \ 137 -ldflags "${GOLDFLAGS}" \ 138 -output "pkg/{{.OS}}_{{.Arch}}/packer" \ 139 . 140 141 # trim GOPATH to first element 142 IFS="${PATHSEP}" 143 # 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? 144 MAIN_GOPATH=(${GOPATH}) 145 MAIN_GOPATH="$(convertPathOnCygwin --unix "${MAIN_GOPATH[0]}")" 146 IFS="${OLDIFS}" 147 148 # Copy our OS/Arch to the bin/ directory 149 echo "==> Copying binaries for this platform..." 150 DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)" 151 for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do 152 cp -v ${F} bin/ 153 cp -v ${F} "${MAIN_GOPATH}/bin/" 154 done 155 156 # Done! 157 echo 158 echo "==> Results:" 159 ls -hl bin/