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