Skip to content

Instantly share code, notes, and snippets.

@inekipelov
Last active January 13, 2019 12:17
Show Gist options
  • Save inekipelov/17e9d3822a1cb9714aeba1c7d3ff6a49 to your computer and use it in GitHub Desktop.
Save inekipelov/17e9d3822a1cb9714aeba1c7d3ff6a49 to your computer and use it in GitHub Desktop.
tmp-universal-framework
# Script below required run from Aggregate Xcode target
# Settings
FRAMEWORK_NAME=#HERE_YOUR_FRAMEWORK_NAME
SCHEME_NAME="${FRAMEWORK_NAME}"
RELEASE_DIR="${HOME}/Desktop/Frameworks"
# Constants
OBJROOT="${OBJROOT}/DependentBuilds" # After that, xcodebuild manages to build this target without issues with new build system introduced in Xcode 10.
UNIVERSAL_OUTPUTFOLDER="${RELEASE_DIR}/${CONFIGURATION}-universal"
# Make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"
# Step 1. Build Device versions
xcodebuild -scheme "${SCHEME_NAME}" -configuration ${CONFIGURATION} -sdk iphoneos ONLY_ACTIVE_ARCH=NO OBJROOT="${OBJROOT}" CONFIGURATION_BUILD_DIR="${BUILD_DIR}/${CONFIGURATION}-iphoneos" clean build
# Step 2. Copy the framework structure (from iphoneos build) to the universal folder
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${FRAMEWORK_NAME}.framework" "${UNIVERSAL_OUTPUTFOLDER}"
# Step 3. Build Simulator versions
xcodebuild -scheme "${SCHEME_NAME}" -configuration ${CONFIGURATION} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO OBJROOT="${OBJROOT}" CONFIGURATION_BUILD_DIR="${BUILD_DIR}/${CONFIGURATION}-iphonesimulator" clean build
# Step 3.1/2. Copy Swift modules (from iphonesimulator build) to the copied framework directory
# cp -R "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${FRAMEWORK_NAME}.framework/Modules/${FRAMEWORK_NAME}.swiftmodule/." "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_NAME}.framework/Modules/${FRAMEWORK_NAME}.swiftmodule"
# Step 4. Create universal binary file using lipo and place the combined executable in the copied framework directory
xcrun lipo -create "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" \
"${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" \
-output "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}"
# Step 5. Convenience step to copy the framework to the project's directory
cp -R "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_NAME}.framework" "${RELEASE_DIR}"
# Step 6. Read bundle version for the framework from plist file
PLIST=${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_NAME}.framework/Info.plist
BUNDLE_SHORT_VERSION=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${PLIST}")
BUNDLE_VERSION=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PLIST}")
FRAMEWORK_VERSION="${BUNDLE_SHORT_VERSION}(${BUNDLE_VERSION})"
# Step 7. Zip the framework before distribute
cd "${RELEASE_DIR}"
zip -r "${FRAMEWORK_NAME}.${FRAMEWORK_VERSION}.framework.zip" "${FRAMEWORK_NAME}.framework"
lipo -info "${RELEASE_DIR}/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}"
rm -R "${RELEASE_DIR}/${FRAMEWORK_NAME}.framework"
# Step 8. Convenience step to open the project's directory in Finder
open "${RELEASE_DIR}/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment