Skip to content

Instantly share code, notes, and snippets.

@bimawa
Created June 19, 2022 08:01
Show Gist options
  • Save bimawa/26a2fa74187ed93fd903fcbafc26727d to your computer and use it in GitHub Desktop.
Save bimawa/26a2fa74187ed93fd903fcbafc26727d to your computer and use it in GitHub Desktop.
Create xcframework for some Package.swift's
#!/bin/bash
set -x
set -e
# Pass scheme name as the first argument to the script
NAME=$1
# Rewrite Package.swift so that it declaras dynamic libraries, since the approach does not work with static libraries
perl -i -p0e 's/type: .static,//g' Package.swift
perl -i -p0e 's/type: .dynamic,//g' Package.swift
perl -i -p0e 's/(library[^,]*,)/$1 type: .dynamic,/g' Package.swift
# Resolving dependenciec in .build/SourcePackages/
xcodebuild -resolvePackageDependencies -scheme $NAME -derivedDataPath ".build"
# GRPC swift-nio not supported ABI yet, we should remove inlinalbe attrubute for make xcframework possible.
# ./Sources needs for grpc-swift foder.
find .build .build/SourcePackages/checkouts/swift-nio .build/SourcePackages/checkouts/swift-log/ .build/SourcePackages/checkouts/swift-protobuf/ ./Sources/\
-type f \
-name '*.swift' \
-exec sed -i '' -e 's/@inlinable//g' -e 's/@usableFromInline//g' {} +
# Build the scheme for all platforms that we plan to support
for PLATFORM in "iOS" "iOS Simulator"; do
case $PLATFORM in
"iOS")
RELEASE_FOLDER="Release-iphoneos"
;;
"iOS Simulator")
RELEASE_FOLDER="Release-iphonesimulator"
;;
esac
ARCHIVE_PATH=$RELEASE_FOLDER
xcodebuild archive -scheme $NAME \
-destination "generic/platform=$PLATFORM" \
-archivePath $ARCHIVE_PATH \
-derivedDataPath ".build" \
SKIP_INSTALL=NO \
BUILD_LIBRARY_FOR_DISTRIBUTION=YES
FRAMEWORK_PATH="$ARCHIVE_PATH.xcarchive/Products/usr/local/lib/$NAME.framework"
MODULES_PATH="$FRAMEWORK_PATH/Modules"
mkdir -p $MODULES_PATH
BUILD_PRODUCTS_PATH=".build/Build/Intermediates.noindex/ArchiveIntermediates/$NAME/BuildProductsPath"
RELEASE_PATH="$BUILD_PRODUCTS_PATH/$RELEASE_FOLDER"
SWIFT_MODULE_PATH="$RELEASE_PATH/$NAME.swiftmodule"
RESOURCES_BUNDLE_PATH="$RELEASE_PATH/${NAME}_${NAME}.bundle"
# Copy Swift modules
if [ -d $SWIFT_MODULE_PATH ]
then
cp -r $SWIFT_MODULE_PATH $MODULES_PATH
else
# In case there are no modules, assume C/ObjC library and create module map
echo "module $NAME { export * }" > $MODULES_PATH/module.modulemap
# TODO: Copy headers
fi
# Copy resources bundle, if exists
if [ -e $RESOURCES_BUNDLE_PATH ]
then
cp -r $RESOURCES_BUNDLE_PATH $FRAMEWORK_PATH
fi
done
xcodebuild -create-xcframework \
-framework Release-iphoneos.xcarchive/Products/usr/local/lib/$NAME.framework \
-framework Release-iphonesimulator.xcarchive/Products/usr/local/lib/$NAME.framework \
-output $NAME.xcframework
# Next commands for zip and upload to github release
#zip -r $NAME.xcframework.zip $NAME.xcframework
#
#rm -rf Release-iphoneos.xcarchive
#rm -rf Release-iphonesimulator.xcarchive
#rm -rf $NAME.xcframework
#gh release upload $RELEASE_TAG $FrameworkBundle
@bimawa
Copy link
Author

bimawa commented Jun 19, 2022

for use call: create-xcframework.sh GRPC
first parameter is a scheme name. For detect which kinds of scheme supported by project call next: xcodebuild -list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment