forked from paketo-buildpacks/mri
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·217 lines (183 loc) · 6.47 KB
/
build.sh
File metadata and controls
executable file
·217 lines (183 loc) · 6.47 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env bash
set -eu
set -o pipefail
readonly ROOT_DIR="$(cd "$(dirname "${0}")/.." && pwd)"
readonly PROGDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly BUILDPACKDIR="$(cd "${PROGDIR}/.." && pwd)"
# shellcheck source=SCRIPTDIR/.util/print.sh
source "${ROOT_DIR}/scripts/.util/print.sh"
function main() {
local targets=()
while [[ "${#}" != 0 ]]; do
case "${1}" in
--help|-h)
shift 1
usage
exit 0
;;
--target)
targets+=("${2}")
shift 2
;;
"")
# skip if the argument is empty
shift 1
;;
*)
util::print::error "unknown argument \"${1}\""
esac
done
if [[ ${#targets[@]} -eq 0 ]]; then
# Read targets from buildpack.toml
local buildpack_toml="${BUILDPACKDIR}/buildpack.toml"
if [[ -f "${buildpack_toml}" ]]; then
util::print::info "Reading targets from ${buildpack_toml}..."
# Parse [[targets]] sections from buildpack.toml
# Extract os and arch from each targets section
local in_targets=false
local current_os=""
local current_arch=""
while IFS= read -r line || [[ -n "${line}" ]]; do
# Check if we're entering a targets section
if [[ "${line}" =~ ^\[\[targets\]\] ]]; then
# Save previous target if we have both os and arch
if [[ "${in_targets}" == true && -n "${current_os}" && -n "${current_arch}" ]]; then
targets+=("${current_os}/${current_arch}")
fi
in_targets=true
current_os=""
current_arch=""
elif [[ "${in_targets}" == true ]]; then
# Check if we're leaving the targets section (next section starts)
if [[ "${line}" =~ ^\[\[ ]] && ! [[ "${line}" =~ ^\[\[targets\]\] ]]; then
# Save current target before leaving
if [[ -n "${current_os}" && -n "${current_arch}" ]]; then
targets+=("${current_os}/${current_arch}")
fi
in_targets=false
current_os=""
current_arch=""
# Extract os value
elif [[ "${line}" =~ ^[[:space:]]*os[[:space:]]*=[[:space:]]*\"([^\"]+)\" ]]; then
current_os="${BASH_REMATCH[1]}"
# If we already have arch, add the target immediately
if [[ -n "${current_arch}" ]]; then
targets+=("${current_os}/${current_arch}")
current_os=""
current_arch=""
fi
# Extract arch value
elif [[ "${line}" =~ ^[[:space:]]*arch[[:space:]]*=[[:space:]]*\"([^\"]+)\" ]]; then
current_arch="${BASH_REMATCH[1]}"
# If we already have os, add the target immediately
if [[ -n "${current_os}" ]]; then
targets+=("${current_os}/${current_arch}")
current_os=""
current_arch=""
fi
fi
fi
done < "${buildpack_toml}"
# Handle last target if we ended while still in targets section
if [[ "${in_targets}" == true && -n "${current_os}" && -n "${current_arch}" ]]; then
targets+=("${current_os}/${current_arch}")
fi
if [[ ${#targets[@]} -gt 0 ]]; then
util::print::info "Found ${#targets[@]} target(s) in buildpack.toml: ${targets[*]}"
else
# Fallback to default if no targets found
targets=("linux/amd64")
util::print::info "No targets found in buildpack.toml, using default: linux/amd64"
fi
else
# Fallback to default if buildpack.toml not found
targets=("linux/amd64")
util::print::info "buildpack.toml not found, using default target: linux/amd64"
fi
fi
if [[ ${#targets[@]} -eq 0 ]]; then
targets=("linux/amd64")
util::print::info "Setting default target platform architecture to: linux/amd64"
fi
if [[ ${#targets[@]} -eq 0 ]]; then
targets=("linux/amd64")
util::print::info "Setting default target platform architecture to: linux/amd64"
fi
if [[ ${#targets[@]} -eq 0 ]]; then
targets=("linux/amd64")
util::print::info "Setting default target platform architecture to: linux/amd64"
fi
run::build
cmd::build
## For backwards compatibility with amd64 wokflows
if [[ ${#targets[@]} -eq 1 && "${targets[0]}" == "linux/amd64" ]]; then
cp -r "${BUILDPACKDIR}/linux/amd64/bin/" "${BUILDPACKDIR}/"
fi
}
function usage() {
cat <<-USAGE
build.sh [OPTIONS]
Builds the buildpack executables.
OPTIONS
--target strings Target platforms to build for.
Targets should be in the format '[os][/arch][/variant]'.
- To specify two different architectures: '--target "linux/amd64" --target "linux/arm64"'
--help -h prints the command usage
USAGE
}
function run::build() {
if [[ -f "${BUILDPACKDIR}/run/main.go" ]]; then
pushd "${BUILDPACKDIR}" > /dev/null || return
for target in "${targets[@]}"; do
platform=$(echo "${target}" | cut -d '/' -f1)
arch=$(echo "${target}" | cut -d'/' -f2)
util::print::title "Building run... for platform: ${platform} and arch: ${arch}"
GOOS=$platform \
GOARCH=$arch \
CGO_ENABLED=0 \
go build \
-ldflags="-s -w" \
-o "${platform}/${arch}/bin/run" \
"${BUILDPACKDIR}/run"
echo "Success!"
names=("detect")
if [ -f "${BUILDPACKDIR}/extension.toml" ]; then
names+=("generate")
else
names+=("build")
fi
for name in "${names[@]}"; do
printf "%s" "Linking ${name}... "
ln -fs "run" "${platform}/${arch}/bin/${name}"
echo "Success!"
done
done
popd > /dev/null || return
fi
}
function cmd::build() {
if [[ -d "${BUILDPACKDIR}/cmd" ]]; then
local name
for src in "${BUILDPACKDIR}"/cmd/*; do
name="$(basename "${src}")"
for target in "${targets[@]}"; do
platform=$(echo "${target}" | cut -d '/' -f1)
arch=$(echo "${target}" | cut -d'/' -f2)
if [[ -f "${src}/main.go" ]]; then
util::print::title "Building ${name}... for platform: ${platform} and arch: ${arch}"
GOOS=$platform \
GOARCH=$arch \
CGO_ENABLED=0 \
go build \
-ldflags="-s -w" \
-o "${BUILDPACKDIR}/${platform}/${arch}/bin/${name}" \
"${src}/main.go"
echo "Success!"
else
printf "%s" "Skipping ${name}... "
fi
done
done
fi
}
main "${@:-}"