-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild_xcframework.sh
More file actions
executable file
·335 lines (284 loc) · 12.2 KB
/
Copy pathbuild_xcframework.sh
File metadata and controls
executable file
·335 lines (284 loc) · 12.2 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env bash
# Builds a GutenbergKitResources XCFramework from the local source target.
#
# Prerequisites:
# - Built web assets in ios/Sources/GutenbergKitResources/Gutenberg/
# - Package.swift must have `resourcesMode` set to `.local`
#
# Output:
# - build/GutenbergKitResources-<git-sha>.xcframework
#
# Adapted from:
# https://github.com/OpenSwiftUIProject/ProtobufKit/blob/937eae542/Scripts/build_xcframework.sh
# https://docs.emergetools.com/docs/analyzing-a-spm-framework-ios
set -euo pipefail
SCHEME="GutenbergKitResources"
# Minimum iOS version, read from Package.swift's declared platform so the
# framework's MinimumOSVersion (and the dylib link target below) can't drift
# from the package. SwiftPM normalizes `.v17` to "17.0" — the exact format
# App Store Connect expects for MinimumOSVersion.
MINIMUM_IOS_VERSION="$(swift package dump-package | jq -er '.platforms[] | select(.platformName == "ios") | .version' 2>/dev/null || true)"
if [[ -z "${MINIMUM_IOS_VERSION}" ]]; then
echo "Error: could not read the iOS deployment target from Package.swift (is 'jq' installed and the Swift toolchain configured? check DEVELOPER_DIR / xcode-select -p)" >&2
exit 1
fi
# Marketing version stamped into the framework's Info.plist, sourced from
# package.json so it always tracks the release. Read with jq (already required
# above) so this script needs no separate Node dependency and both version
# reads stay consistent. Fail closed rather than stamping a placeholder: a
# silently-wrong version is exactly the drift we want to prevent. App Store
# Connect rejects any embedded framework whose Info.plist lacks
# CFBundleShortVersionString.
# `.version // empty` collapses a missing/null/empty `version` to empty output
# so the guard below catches it, rather than stamping a literal "null".
MARKETING_VERSION="$(jq -er '.version // empty' package.json 2>/dev/null || true)"
if [[ -z "${MARKETING_VERSION}" ]]; then
echo "Error: could not read the version field from package.json (is 'jq' installed and is this the repo root?)" >&2
exit 1
fi
BUILD_DIR="$(pwd)/build"
DERIVED_DATA_PATH="${BUILD_DIR}/DerivedData"
GIT_SHA="$(git rev-parse HEAD)"
XCFRAMEWORK_NAME="${SCHEME}-${GIT_SHA}.xcframework"
# Remove prior xcframework artifacts so re-runs are hermetic and downstream
# tools (signing, packaging) never have to disambiguate between stale and
# fresh builds.
mkdir -p "${BUILD_DIR}"
find "${BUILD_DIR}" -maxdepth 1 \
\( -name "*.xcframework" -type d \
-o -name "*.xcframework.zip" -type f \
-o -name "*.xcframework.zip.checksum.txt" -type f \) \
-exec rm -rf {} +
link_dylib() {
local object_file="$1"
local output="$2"
local sdk="$3"
local sdk_path
sdk_path="$(xcrun --sdk "${sdk}" --show-sdk-path)"
local install_name="@rpath/${SCHEME}.framework/${SCHEME}"
local archs
archs=$(xcrun lipo -archs "${object_file}")
local arch_count
arch_count=$(echo "${archs}" | wc -w | tr -d ' ')
local dylibs=()
for arch in ${archs}; do
local thin_o="${BUILD_DIR}/${SCHEME}-${sdk}-${arch}.o"
local thin_dylib="${BUILD_DIR}/${SCHEME}-${sdk}-${arch}.dylib"
if [[ "${arch_count}" -eq 1 ]]; then
thin_o="${object_file}"
else
xcrun lipo "${object_file}" -thin "${arch}" -output "${thin_o}"
fi
local target="${arch}-apple-ios${MINIMUM_IOS_VERSION}"
if [[ "${sdk}" == "iphonesimulator" ]]; then
target="${target}-simulator"
fi
xcrun clang -target "${target}" -dynamiclib \
-install_name "${install_name}" \
-o "${thin_dylib}" \
"${thin_o}" \
-isysroot "${sdk_path}" \
-L "${sdk_path}/usr/lib/swift" \
-L "${sdk_path}/usr/lib"
dylibs+=("${thin_dylib}")
done
if [[ ${#dylibs[@]} -eq 1 ]]; then
cp "${dylibs[0]}" "${output}"
else
xcrun lipo -create "${dylibs[@]}" -output "${output}"
fi
}
build_framework() {
local sdk="$1"
local destination="$2"
local archive_path="${BUILD_DIR}/${SCHEME}-${sdk}.xcarchive"
echo "--- Building ${SCHEME} for ${sdk}"
rm -rf "${archive_path}"
xcodebuild archive \
-scheme "${SCHEME}" \
-archivePath "${archive_path}" \
-derivedDataPath "${DERIVED_DATA_PATH}" \
-sdk "${sdk}" \
-destination "${destination}" \
BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
INSTALL_PATH='Library/Frameworks' \
OTHER_SWIFT_FLAGS=-no-verify-emitted-module-interface \
CODE_SIGN_IDENTITY="-" \
CODE_SIGNING_REQUIRED=NO \
CODE_SIGNING_ALLOWED=NO \
| xcbeautify
# SPM archives don't produce a .framework — assemble it from DerivedData
local intermediates="${DERIVED_DATA_PATH}/Build/Intermediates.noindex/ArchiveIntermediates/${SCHEME}"
local build_products="${intermediates}/BuildProductsPath/Release-${sdk}"
local generated_maps="${intermediates}/IntermediateBuildFilesPath/GeneratedModuleMaps-${sdk}"
local framework_path="${BUILD_DIR}/frameworks/${sdk}/${SCHEME}.framework"
rm -rf "${framework_path}"
mkdir -p "${framework_path}/Modules"
mkdir -p "${framework_path}/Headers"
# Binary: SPM produces a .o object file — link it into a dylib so that
# the resource_bundle_accessor's BundleFinder class stays inside the
# framework at runtime (otherwise it gets statically linked into the
# consuming app binary and Bundle(for:) resolves to the wrong bundle).
link_dylib "${build_products}/${SCHEME}.o" "${framework_path}/${SCHEME}" "${sdk}"
# Swift module
cp -r "${build_products}/${SCHEME}.swiftmodule" "${framework_path}/Modules/${SCHEME}.swiftmodule"
rm -f "${framework_path}/Modules/${SCHEME}.swiftmodule"/*.package.swiftinterface
rm -f "${framework_path}/Modules/${SCHEME}.swiftmodule"/*.private.swiftinterface
# Module map and generated header
cp "${generated_maps}/${SCHEME}.modulemap" "${framework_path}/Modules/module.modulemap"
cp "${generated_maps}/${SCHEME}-Swift.h" "${framework_path}/Headers/${SCHEME}-Swift.h"
# Framework Info.plist. CFBundleShortVersionString, CFBundleVersion, and
# MinimumOSVersion are required by App Store Connect for any framework
# embedded in a submitted app — altool rejects the upload without them.
cat > "${framework_path}/Info.plist" <<PLIST
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>${SCHEME}</string>
<key>CFBundleIdentifier</key>
<string>org.wordpress.GutenbergKitResources</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${SCHEME}</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>${MARKETING_VERSION}</string>
<key>CFBundleVersion</key>
<string>${MARKETING_VERSION}</string>
<key>MinimumOSVersion</key>
<string>${MINIMUM_IOS_VERSION}</string>
</dict>
</plist>
PLIST
}
copy_resource_bundles() {
local sdk="$1"
local framework_path="${BUILD_DIR}/frameworks/${sdk}/${SCHEME}.framework"
local bundle_path="${DERIVED_DATA_PATH}/Build/Intermediates.noindex/ArchiveIntermediates/${SCHEME}/IntermediateBuildFilesPath/UninstalledProducts/${sdk}"
echo "--- Copying resource bundles for ${sdk}"
if [ ! -d "${bundle_path}" ]; then
echo "Error: bundle path not found: ${bundle_path}" >&2
exit 1
fi
find "${bundle_path}" -name "*.bundle" -maxdepth 1 -type d -print0 | while IFS= read -r -d '' bundle; do
bundle_name=$(basename "${bundle}")
echo " ${bundle_name} -> ${framework_path}/"
rm -rf "${framework_path:?}/${bundle_name}"
cp -R "${bundle}" "${framework_path}/"
done
}
# Slice directory names depend on the architectures actually built — e.g.
# `ios-arm64-simulator` when only arm64 is built vs `ios-arm64_x86_64-simulator`
# for a universal slice — so resolve them from the XCFramework rather than
# hardcoding.
resolve_slice_dir() {
local sdk="$1"
local matches=()
local dir name
for dir in "${XCFRAMEWORK_PATH}"/ios-*; do
[ -d "${dir}" ] || continue
name=$(basename "${dir}")
case "${sdk}" in
iphoneos)
case "${name}" in
*-simulator|*-maccatalyst) continue ;;
esac
;;
iphonesimulator)
case "${name}" in
*-simulator) ;;
*) continue ;;
esac
;;
*)
echo "Error: unknown SDK '${sdk}'" >&2
return 1
;;
esac
matches+=("${name}")
done
if [ ${#matches[@]} -ne 1 ]; then
echo "Error: expected exactly one ${sdk} slice in ${XCFRAMEWORK_PATH}, found ${#matches[@]}: ${matches[*]:-<none>}" >&2
return 1
fi
echo "${matches[0]}"
}
copy_dsyms_for_sdk() {
local sdk="$1"
local dsyms_path="${BUILD_DIR}/${SCHEME}-${sdk}.xcarchive/dSYMs"
if [ ! -d "${dsyms_path}" ]; then
return
fi
local slice
slice=$(resolve_slice_dir "${sdk}")
cp -r "${dsyms_path}" "${XCFRAMEWORK_PATH}/${slice}/"
}
# Reads a top-level Info.plist value, printing empty string if the key is
# absent so callers can distinguish "missing" from a real value.
plist_value() {
plutil -extract "$1" raw -o - "$2" 2>/dev/null || true
}
# Guards against the framework's Info.plist drifting out of sync with the
# rest of the release. Runs against the actual shipped slice so a dropped or
# stale key fails GutenbergKit's own CI here, rather than a consumer's App
# Store upload three hops downstream (see altool errors 90057 / 90360).
verify_framework_plist() {
local plist="$1"
plutil -lint "${plist}" >/dev/null
local short_version bundle_version min_os
short_version="$(plist_value CFBundleShortVersionString "${plist}")"
bundle_version="$(plist_value CFBundleVersion "${plist}")"
min_os="$(plist_value MinimumOSVersion "${plist}")"
if [[ "${short_version}" != "${MARKETING_VERSION}" ]]; then
echo "Error: ${plist}: CFBundleShortVersionString='${short_version}' does not match package.json version '${MARKETING_VERSION}'" >&2
exit 1
fi
if [[ "${bundle_version}" != "${MARKETING_VERSION}" ]]; then
echo "Error: ${plist}: CFBundleVersion='${bundle_version}' does not match package.json version '${MARKETING_VERSION}'" >&2
exit 1
fi
if [[ "${min_os}" != "${MINIMUM_IOS_VERSION}" ]]; then
echo "Error: ${plist}: MinimumOSVersion='${min_os}' does not match expected '${MINIMUM_IOS_VERSION}'" >&2
exit 1
fi
echo " OK: ${plist} (v${short_version}, min iOS ${min_os})"
}
# Build for both platforms
build_framework "iphoneos" "generic/platform=iOS"
copy_resource_bundles "iphoneos"
build_framework "iphonesimulator" "generic/platform=iOS Simulator"
copy_resource_bundles "iphonesimulator"
# Create XCFramework
echo "--- Creating XCFramework"
DEVICE_FRAMEWORK="${BUILD_DIR}/frameworks/iphoneos/${SCHEME}.framework"
SIM_FRAMEWORK="${BUILD_DIR}/frameworks/iphonesimulator/${SCHEME}.framework"
XCFRAMEWORK_PATH="${BUILD_DIR}/${XCFRAMEWORK_NAME}"
rm -rf "${XCFRAMEWORK_PATH}"
xcodebuild -create-xcframework \
-framework "${DEVICE_FRAMEWORK}" \
-framework "${SIM_FRAMEWORK}" \
-output "${XCFRAMEWORK_PATH}"
# Copy dSYMs into xcframework slices
copy_dsyms_for_sdk "iphoneos"
copy_dsyms_for_sdk "iphonesimulator"
# Verify every slice's framework Info.plist before the artifact leaves this step
echo "--- Verifying framework Info.plist"
verified_count=0
for slice_dir in "${XCFRAMEWORK_PATH}"/ios-*; do
[ -d "${slice_dir}" ] || continue
verify_framework_plist "${slice_dir}/${SCHEME}.framework/Info.plist"
verified_count=$((verified_count + 1))
done
# Fail closed if the glob matched nothing: without nullglob the loop above
# would run zero iterations and exit 0, letting an empty or malformed
# XCFramework pass the very gate meant to catch it.
if [[ "${verified_count}" -eq 0 ]]; then
echo "Error: no framework slices found to verify in ${XCFRAMEWORK_PATH}" >&2
exit 1
fi
echo ""
echo "XCFramework: ${XCFRAMEWORK_PATH}"