Skip to content

Instantly share code, notes, and snippets.

@sinbadbd
Last active June 6, 2024 10:02
Show Gist options
  • Save sinbadbd/285dc3023214993aa2b30be0ac6531c2 to your computer and use it in GitHub Desktop.
Save sinbadbd/285dc3023214993aa2b30be0ac6531c2 to your computer and use it in GitHub Desktop.
XCFramework Build
#!/bin/bash
# Define variables
SCHEME="FWTest"
PROJECT_DIR=$(pwd)
ARCHIVE_PATH="$PROJECT_DIR/build/xcf"
DERIVED_DATA_PATH="$PROJECT_DIR/build/derived_data"
DEVICE_ARCHIVE_PATH="$ARCHIVE_PATH/ios.xcarchive"
SIMULATOR_ARCHIVE_PATH="$ARCHIVE_PATH/iossimulator.xcarchive"
XCFRAMEWORK_OUTPUT_PATH="$PROJECT_DIR/build/FWTest.xcframework"
# Clean previous builds
rm -rf $ARCHIVE_PATH
rm -rf $DERIVED_DATA_PATH
rm -rf $XCFRAMEWORK_OUTPUT_PATH
# Create necessary directories
mkdir -p $ARCHIVE_PATH
# Function to log errors
log_error() {
echo "Error: $1"
exit 1
}
# Function to check if directory exists
check_directory_exists() {
if [ ! -d "$1" ]; then
log_error "Directory $1 does not exist."
fi
}
# Build for iOS Devices
echo "Building for iOS devices..."
xcodebuild archive \
-scheme $SCHEME \
-destination="generic/platform=iOS" \
-archivePath $DEVICE_ARCHIVE_PATH \
-derivedDataPath $DERIVED_DATA_PATH \
-sdk iphoneos \
SKIP_INSTALL=NO \
BUILD_LIBRARY_FOR_DISTRIBUTION=YES || log_error "Failed to build for iOS devices."
# Check if the device framework was created successfully
check_directory_exists "$DEVICE_ARCHIVE_PATH/Products/Library/Frameworks/$SCHEME.framework"
echo "Device framework exists: $DEVICE_ARCHIVE_PATH/Products/Library/Frameworks/$SCHEME.framework"
# Build for iOS Simulator with x86_64 and arm64 architectures
echo "Building for iOS simulator (x86_64 and arm64)..."
xcodebuild archive \
-scheme $SCHEME \
-destination="generic/platform=iOS Simulator" \
-arch x86_64 \
-arch arm64 \
-archivePath $SIMULATOR_ARCHIVE_PATH \
-derivedDataPath $DERIVED_DATA_PATH \
-sdk iphonesimulator \
SKIP_INSTALL=NO \
BUILD_LIBRARY_FOR_DISTRIBUTION=YES || log_error "Failed to build for iOS simulator."
# Check if the simulator framework was created successfully
check_directory_exists "$SIMULATOR_ARCHIVE_PATH/Products/Library/Frameworks/$SCHEME.framework"
echo "Simulator framework exists: $SIMULATOR_ARCHIVE_PATH/Products/Library/Frameworks/$SCHEME.framework"
# Create XCFramework
echo "Creating XCFramework..."
xcodebuild -create-xcframework \
-framework "$DEVICE_ARCHIVE_PATH/Products/Library/Frameworks/$SCHEME.framework" \
-framework "$SIMULATOR_ARCHIVE_PATH/Products/Library/Frameworks/$SCHEME.framework" \
-output $XCFRAMEWORK_OUTPUT_PATH || log_error "Failed to create XCFramework."
echo "XCFramework created at $XCFRAMEWORK_OUTPUT_PATH"
#Verifying the Frameworks
#To ensure the frameworks contain the correct architectures, use the lipo command:
lipo -info $DEVICE_ARCHIVE_PATH/Products/Library/Frameworks/$SCHEME.framework/$SCHEME
lipo -info $SIMULATOR_ARCHIVE_PATH/Products/Library/Frameworks/$SCHEME.framework/$SCHEME
@sinbadbd
Copy link
Author

sinbadbd commented Jun 6, 2024

Running the Script

  1. Save the Script: Save the script as build_xcframework.sh in your project directory.
  2. Make the Script Executable:
    chmod +x build_xcframework.sh

Run the Script:
./build_xcframework.sh

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