@@ -69,8 +69,60 @@ function main {
6969 buildpack_type=extension
7070 fi
7171
72- buildpack::archive " ${version} " " ${buildpack_type} "
73- buildpackage::create " ${output} " " ${buildpack_type} "
72+ # Read targets from buildpack.toml
73+ local -a targets=()
74+ local buildpack_toml=" ${ROOT_DIR} /buildpack.toml"
75+ if [[ -f " ${buildpack_toml} " ]]; then
76+ util::print::info " Reading targets from ${buildpack_toml} ..."
77+ local targets_json
78+ targets_json=$( cat " ${buildpack_toml} " | yj -tj | jq -r ' .targets[]? | "\(.os)/\(.arch)"' 2> /dev/null || echo " " )
79+
80+ if [[ -n " ${targets_json} " ]]; then
81+ while IFS= read -r target; do
82+ if [[ -n " ${target} " ]]; then
83+ targets+=(" ${target} " )
84+ fi
85+ done <<< " ${targets_json}"
86+ util::print::info " Found ${# targets[@]} target(s) in buildpack.toml: ${targets[*]} "
87+ fi
88+ fi
89+
90+ if [[ ${# targets[@]} -gt 0 ]]; then
91+ # Build and package for each target architecture separately
92+ local arch_outputs=()
93+ for target in " ${targets[@]} " ; do
94+ local arch
95+ arch=$( echo " ${target} " | cut -d' /' -f2)
96+ util::print::title " Building binaries for ${target} (arch: ${arch} )..."
97+
98+ # Clean bin directory before building for this architecture
99+ rm -rf " ${ROOT_DIR} /bin"
100+ mkdir -p " ${ROOT_DIR} /bin"
101+
102+ # Build archive for this architecture (GOARCH will be used by jam pack's pre-package script)
103+ export GOARCH=" ${arch} "
104+ buildpack::archive " ${version} " " ${buildpack_type} " " ${target} "
105+ unset GOARCH
106+
107+ # Package with this target
108+ local arch_output=" ${output% .cnb} -${arch} .cnb"
109+ arch_outputs+=(" ${arch_output} " )
110+ buildpackage::create " ${arch_output} " " ${buildpack_type} " " ${target} "
111+ done
112+
113+ # Create a combined output by copying the first one (for compatibility)
114+ if [[ ${# arch_outputs[@]} -gt 0 ]]; then
115+ cp " ${arch_outputs[0]} " " ${output} "
116+ util::print::title " Created architecture-specific packages:"
117+ for arch_output in " ${arch_outputs[@]} " ; do
118+ echo " - ${arch_output} "
119+ done
120+ echo " - ${output} (copy of ${arch_outputs[0]} )"
121+ fi
122+ else
123+ buildpack::archive " ${version} " " ${buildpack_type} "
124+ buildpackage::create " ${output} " " ${buildpack_type} "
125+ fi
74126}
75127
76128function usage() {
@@ -79,6 +131,8 @@ package.sh --version <version> [OPTIONS]
79131
80132Packages a buildpack or an extension into a buildpackage .cnb file.
81133
134+ Targets are automatically read from buildpack.toml [[targets]] sections.
135+
82136OPTIONS
83137 --help -h prints the command usage
84138 --version <version> -v <version> specifies the version number to use when packaging a buildpack or an extension
@@ -106,6 +160,10 @@ function tools::install() {
106160 --directory " ${BIN_DIR} " \
107161 --token " ${token} "
108162
163+ util::tools::yj::install \
164+ --directory " ${BIN_DIR} " \
165+ --token " ${token} "
166+
109167 if [[ -f " ${ROOT_DIR} /.libbuildpack" ]]; then
110168 util::tools::packager::install \
111169 --directory " ${BIN_DIR} "
@@ -120,8 +178,16 @@ function buildpack::archive() {
120178 local version
121179 version=" ${1} "
122180 buildpack_type=" ${2} "
181+ local target=" ${3:- } "
182+
183+ local archive_name=" buildpack.tgz"
184+ if [[ -n " ${target} " ]]; then
185+ local arch
186+ arch=$( echo " ${target} " | cut -d' /' -f2)
187+ archive_name=" buildpack-${arch} .tgz"
188+ fi
123189
124- util::print::title " Packaging ${buildpack_type} into ${BUILD_DIR} /buildpack.tgz ..."
190+ util::print::title " Packaging ${buildpack_type} into ${BUILD_DIR} /${archive_name} ..."
125191
126192 if [[ -f " ${ROOT_DIR} /.libbuildpack" ]]; then
127193 packager \
@@ -133,36 +199,67 @@ function buildpack::archive() {
133199 jam pack \
134200 " --${buildpack_type} " " ${ROOT_DIR} /${buildpack_type} .toml" \
135201 --version " ${version} " \
136- --output " ${BUILD_DIR} /buildpack.tgz "
202+ --output " ${BUILD_DIR} /${archive_name} "
137203 fi
138204}
139205
140206function buildpackage::create() {
141- local output
207+ local output buildpack_type
142208 output=" ${1} "
143209 buildpack_type=" ${2} "
210+ shift 2
211+ local targets=(" ${@:- } " )
144212
145213 util::print::title " Packaging ${buildpack_type} ... ${output} "
146214
215+ # Determine archive path based on target
216+ local archive_path=" ${BUILD_DIR} /buildpack.tgz"
217+ if [[ ${# targets[@]} -eq 1 ]]; then
218+ local arch
219+ arch=$( echo " ${targets[0]} " | cut -d' /' -f2)
220+ archive_path=" ${BUILD_DIR} /buildpack-${arch} .tgz"
221+ if [[ ! -f " ${archive_path} " ]]; then
222+ archive_path=" ${BUILD_DIR} /buildpack.tgz"
223+ fi
224+ fi
225+
147226 if [ " $buildpack_type " == " extension" ]; then
148227 cwd=$( pwd)
149228 cd ${BUILD_DIR}
150- mkdir cnbdir
229+ mkdir -p cnbdir
151230 cd cnbdir
152- cp ../buildpack.tgz .
153- tar -xvf buildpack.tgz
154- rm buildpack.tgz
155-
156- pack \
157- extension package " ${output} " \
158- --format file
231+ cp " ${archive_path} " .
232+ tar -xvf buildpack* .tgz
233+ rm buildpack* .tgz
234+
235+ pack_args=(
236+ extension package " ${output} "
237+ --format file
238+ )
239+
240+ if [[ ${# targets[@]} -gt 0 ]]; then
241+ for target in " ${targets[@]} " ; do
242+ pack_args+=(--target " ${target} " )
243+ done
244+ fi
245+
246+ pack " ${pack_args[@]} "
159247
160248 cd $cwd
161249 else
162- pack \
163- buildpack package " ${output} " \
164- --path " ${BUILD_DIR} /buildpack.tgz" \
165- --format file
250+ pack_args=(
251+ buildpack package " ${output} "
252+ --path " ${archive_path} "
253+ --format file
254+ )
255+
256+ if [[ ${# targets[@]} -gt 0 ]]; then
257+ for target in " ${targets[@]} " ; do
258+ pack_args+=(--target " ${target} " )
259+ done
260+ fi
261+
262+ pack " ${pack_args[@]} "
166263 fi
167264}
168265
0 commit comments