1+ #! /bin/bash
2+
3+ # Documentation:
4+ # This script generates an XCFramework for a certain <TARGET> for all provided <PLATFORMS>.
5+
6+ # Important:
7+ # This script doesn't work on packages, only on .xcproj projects that generate a framework.
8+
9+ # Usage:
10+ # framework.sh <TARGET> [<PLATFORMS> default:iOS macOS tvOS watchOS xrOS]
11+ # e.g. `bash scripts/framework.sh MyTarget iOS macOS`
12+
13+ # Exit immediately if a command exits with a non-zero status
14+ set -e
15+
16+ # Verify that all required arguments are provided
17+ if [ $# -eq 0 ]; then
18+ echo " Error: This script requires exactly one argument"
19+ echo " Usage: $0 <TARGET>"
20+ exit 1
21+ fi
22+
23+ # Define argument variables
24+ TARGET=$1
25+
26+ # Remove TARGET from arguments list
27+ shift
28+
29+ # Define platforms variable
30+ if [ $# -eq 0 ]; then
31+ set -- iOS macOS tvOS watchOS xrOS
32+ fi
33+ PLATFORMS=$@
34+
35+ # Define local variables
36+ BUILD_FOLDER=.build
37+ BUILD_FOLDER_ARCHIVES=.build/framework_archives
38+ BUILD_FILE=$BUILD_FOLDER /$TARGET .xcframework
39+ BUILD_ZIP=$BUILD_FOLDER /$TARGET .zip
40+
41+ # Start script
42+ echo " "
43+ echo " Building $TARGET XCFramework for [$PLATFORMS ]..."
44+ echo " "
45+
46+ # Delete old builds
47+ echo " Cleaning old builds..."
48+ rm -rf $BUILD_ZIP
49+ rm -rf $BUILD_FILE
50+ rm -rf $BUILD_FOLDER_ARCHIVES
51+
52+
53+ # Generate XCArchive files for all platforms
54+ echo " Generating XCArchives..."
55+
56+ # Initialize the xcframework command
57+ XCFRAMEWORK_CMD=" xcodebuild -create-xcframework"
58+
59+ # Build iOS archives and append to the xcframework command
60+ if [[ " ${PLATFORMS[@]} " =~ " iOS " ]]; then
61+ xcodebuild archive -scheme $TARGET -configuration Release -destination " generic/platform=iOS" -archivePath $BUILD_FOLDER_ARCHIVES /$TARGET -iOS SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES
62+ xcodebuild archive -scheme $TARGET -configuration Release -destination " generic/platform=iOS Simulator" -archivePath $BUILD_FOLDER_ARCHIVES /$TARGET -iOS-Sim SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES
63+ XCFRAMEWORK_CMD+=" -framework $BUILD_FOLDER_ARCHIVES /$TARGET -iOS.xcarchive/Products/Library/Frameworks/$TARGET .framework"
64+ XCFRAMEWORK_CMD+=" -framework $BUILD_FOLDER_ARCHIVES /$TARGET -iOS-Sim.xcarchive/Products/Library/Frameworks/$TARGET .framework"
65+ fi
66+
67+ # Build iOS archive and append to the xcframework command
68+ if [[ " ${PLATFORMS[@]} " =~ " macOS " ]]; then
69+ xcodebuild archive -scheme $TARGET -configuration Release -destination " generic/platform=macOS" -archivePath $BUILD_FOLDER_ARCHIVES /$TARGET -macOS SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES
70+ XCFRAMEWORK_CMD+=" -framework $BUILD_FOLDER_ARCHIVES /$TARGET -macOS.xcarchive/Products/Library/Frameworks/$TARGET .framework"
71+ fi
72+
73+ # Build tvOS archives and append to the xcframework command
74+ if [[ " ${PLATFORMS[@]} " =~ " tvOS " ]]; then
75+ xcodebuild archive -scheme $TARGET -configuration Release -destination " generic/platform=tvOS" -archivePath $BUILD_FOLDER_ARCHIVES /$TARGET -tvOS SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES
76+ xcodebuild archive -scheme $TARGET -configuration Release -destination " generic/platform=tvOS Simulator" -archivePath $BUILD_FOLDER_ARCHIVES /$TARGET -tvOS-Sim SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES
77+ XCFRAMEWORK_CMD+=" -framework $BUILD_FOLDER_ARCHIVES /$TARGET -tvOS.xcarchive/Products/Library/Frameworks/$TARGET .framework"
78+ XCFRAMEWORK_CMD+=" -framework $BUILD_FOLDER_ARCHIVES /$TARGET -tvOS-Sim.xcarchive/Products/Library/Frameworks/$TARGET .framework"
79+ fi
80+
81+ # Build watchOS archives and append to the xcframework command
82+ if [[ " ${PLATFORMS[@]} " =~ " watchOS " ]]; then
83+ xcodebuild archive -scheme $TARGET -configuration Release -destination " generic/platform=watchOS" -archivePath $BUILD_FOLDER_ARCHIVES /$TARGET -watchOS SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES
84+ xcodebuild archive -scheme $TARGET -configuration Release -destination " generic/platform=watchOS Simulator" -archivePath $BUILD_FOLDER_ARCHIVES /$TARGET -watchOS-Sim SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES
85+ XCFRAMEWORK_CMD+=" -framework $BUILD_FOLDER_ARCHIVES /$TARGET -watchOS.xcarchive/Products/Library/Frameworks/$TARGET .framework"
86+ XCFRAMEWORK_CMD+=" -framework $BUILD_FOLDER_ARCHIVES /$TARGET -watchOS-Sim.xcarchive/Products/Library/Frameworks/$TARGET .framework"
87+ fi
88+
89+ # Build xrOS archives and append to the xcframework command
90+ if [[ " ${PLATFORMS[@]} " =~ " xrOS " ]]; then
91+ xcodebuild archive -scheme $TARGET -configuration Release -destination " generic/platform=xrOS" -archivePath $BUILD_FOLDER_ARCHIVES /$TARGET -xrOS SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES
92+ xcodebuild archive -scheme $TARGET -configuration Release -destination " generic/platform=xrOS Simulator" -archivePath $BUILD_FOLDER_ARCHIVES /$TARGET -xrOS-Sim SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES
93+ XCFRAMEWORK_CMD+=" -framework $BUILD_FOLDER_ARCHIVES /$TARGET -xrOS.xcarchive/Products/Library/Frameworks/$TARGET .framework"
94+ XCFRAMEWORK_CMD+=" -framework $BUILD_FOLDER_ARCHIVES /$TARGET -xrOS-Sim.xcarchive/Products/Library/Frameworks/$TARGET .framework"
95+ fi
96+
97+ # Genererate XCFramework
98+ echo " Generating XCFramework..."
99+ XCFRAMEWORK_CMD+=" -output $BUILD_FILE "
100+ eval " $XCFRAMEWORK_CMD "
101+
102+ # Genererate iOS XCFramework zip
103+ echo " Generating XCFramework zip..."
104+ zip -r $BUILD_ZIP $BUILD_FILE
105+ echo " "
106+ echo " ***** CHECKSUM *****"
107+ swift package compute-checksum $BUILD_ZIP
108+ echo " ********************"
109+ echo " "
110+
111+ # Complete successfully
112+ echo " "
113+ echo " $TARGET XCFramework created successfully!"
114+ echo " "
0 commit comments