Skip to content

Commit 4ac28a0

Browse files
authored
Add proper support for statically linked XCFrameworks, for those who want them (#156)
1 parent a29a5c9 commit 4ac28a0

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

.scripts/build-xcframework

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,51 @@ build_archive() {
101101
else
102102
xcodebuild archive "${build_settings[@]}"
103103
fi
104+
105+
if [ "$BUILD_STATIC" = true ]; then
106+
local framework_path="$ARCHIVE_DIR/$name.xcarchive/Products/Library/Frameworks/$FRAMEWORK_NAME.framework"
107+
bundle_static_dependencies "$framework_path"
108+
fi
109+
}
110+
111+
bundle_static_dependencies() {
112+
local framework_path="$1"
113+
local framework_binary="$framework_path/$FRAMEWORK_NAME"
114+
local parent_dir
115+
parent_dir="$(dirname "$framework_path")"
116+
local dependencies_dirs=(
117+
"$framework_path/Frameworks"
118+
"$framework_path/Versions/Current/Frameworks"
119+
"$parent_dir"
120+
)
121+
122+
for dependencies_dir in "${dependencies_dirs[@]}"; do
123+
if [ -d "$dependencies_dir" ]; then
124+
shopt -s nullglob
125+
for dependency in "$dependencies_dir"/*.framework; do
126+
if [ "$dependency" = "$framework_path" ]; then
127+
continue
128+
fi
129+
local dependency_name
130+
dependency_name="$(basename "$dependency" .framework)"
131+
local dependency_binary="$dependency/$dependency_name"
132+
if [ -f "$dependency_binary" ]; then
133+
if file "$dependency_binary" | grep -q "dynamically linked shared library"; then
134+
echo "⚠️ Skipping dynamic dependency $dependency_name when producing static framework"
135+
else
136+
echo "🔗 Bundling static dependency $dependency_name into $FRAMEWORK_NAME"
137+
/usr/bin/libtool -static -o "$framework_binary.merged" "$framework_binary" "$dependency_binary"
138+
mv "$framework_binary.merged" "$framework_binary"
139+
fi
140+
fi
141+
rm -rf "$dependency"
142+
done
143+
shopt -u nullglob
144+
if [ "$dependencies_dir" != "$parent_dir" ]; then
145+
rm -rf "$dependencies_dir"
146+
fi
147+
fi
148+
done
104149
}
105150

106151
# Function to check if an item exists in an array
@@ -200,4 +245,4 @@ echo "📦 Compressing XCFramework..."
200245
cd "$OUTPUT_DIR"
201246
zip -r "$XCFRAMEWORK_NAME.zip" "$XCFRAMEWORK_NAME"
202247

203-
echo "✨ XCFramework compressed at $OUTPUT_DIR/$XCFRAMEWORK_NAME.zip"
248+
echo "✨ XCFramework compressed at $OUTPUT_DIR/$XCFRAMEWORK_NAME.zip"

0 commit comments

Comments
 (0)