File: build.sh

package info (click to toggle)
packer 1.6.6%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 33,156 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (157 lines) | stat: -rwxr-xr-x 4,067 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env bash

# This script builds the application from source for multiple platforms.
# Determine the arch/os combos we're building for
ALL_XC_ARCH="386 amd64 arm arm64 ppc64le mips mips64 mipsle mipsle64 s390x"
ALL_XC_OS="linux darwin windows freebsd openbsd solaris"
SKIPPED_OSARCH="!darwin/arm !darwin/arm64 !freebsd/arm !freebsd/arm64"

# Exit immediately if a command fails
set -e

# Validates that a necessary tool is on the PATH
function validateToolPresence
{
    local TOOLNAME=$1
    if ! which ${TOOLNAME} >/dev/null; then
        echo "${TOOLNAME} is not on the path. Exiting..."
        exit 1
    fi
}

# 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
}

# Get the parent directory of where this script is.
# 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/*
}

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 ';'
            ;;
        *)	echo ':'
    esac
}

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
}

validatePreconditions
enterPackerSourceDir
ensureOutputStructure
cleanOutputDirs

PATHSEP=$(getPathSeparator)

# XXX works in MINGW?
# FIXME: What if go is not in the PATH and GOROOT isn't set?
which go &>/dev/null || PATH+=":`convertPathOnCygwin "${GOROOT:?}"`/bin"

OLDIFS="${IFS}"

# make sure GOPATH is consistent - Windows binaries can't handle Cygwin-style paths
IFS="${PATHSEP}"
for d in ${GOPATH:-$(go env GOPATH)}; do
    _GOPATH+="${_GOPATH:+${PATHSEP}}$(convertPathOnCygwin --windows "${d}")"
done
GOPATH="$_GOPATH"

# locate 'gox' and traverse GOPATH if needed
which "${GOX:=gox}" &>/dev/null || {
    for d in ${GOPATH}; do
        GOX="$(convertPathOnCygwin --unix "${d}")/bin/gox"
        [ -x "${GOX}" ] && break || unset GOX
    done
}
IFS="$OLDIFS"

# Build!
echo "==> Building..."

# 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

export CGO_ENABLED=0
set +e
${GOX:?command not found} \
    -os="${XC_OS:-$ALL_XC_OS}" \
    -arch="${XC_ARCH:-$ALL_XC_ARCH}" \
    -osarch="${SKIPPED_OSARCH}" \
    -ldflags "${GOLDFLAGS}" \
    -output "pkg/{{.OS}}_{{.Arch}}/packer" \
    .
set -e

# trim GOPATH to first element
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}"

# Copy our OS/Arch to the bin/ directory
echo "==> Copying binaries for this platform..."
DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)"
for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do
    cp -v ${F} bin/
    cp -v ${F} "${MAIN_GOPATH}/bin/"
done

# Done!
echo
echo "==> Results:"
ls -hl bin/