Convert old FAT .framework to .xcframework

Sometimes when working with old fat frameworks they can cause warnings in Xcode. By using lipo and xcodebuild they can be converted to the new format and Xcode will not complain anymore!

See Usage at the bottom of the script

Note:

In below script bash is required because I use automatic word splitting for REMOVE_FOR_IOS and REMOVE_FOR_SIM expansion. Using word splitting is probably no the best option however simplicity wins here. Specially in a script that is supposed to be executed only a couple of times in the time of our lifes.

#! /bin/bash
# Bash (not zsh) is required for word splitting for REMOVE_FOR_IOS and REMOVE_FOR_SIM inside make_part function

make_part() {
    FRAMEWORK_NAME=${1%.framework}
    TEMP_DIR=$2
    ARCHS_TO_REMOVE=$3

    rm -rf "$TEMP_DIR"
    mkdir $TEMP_DIR
    cp -R "${FRAMEWORK_NAME}.framework" "$TEMP_DIR"
    pushd "${TEMP_DIR}/${FRAMEWORK_NAME}.framework" > /dev/null || return
    #xcrun lipo -info "${FRAMEWORK_NAME}"
    mv "${FRAMEWORK_NAME}" "${FRAMEWORK_NAME}_original" 
    # xcrun lipo "${FRAMEWORK_NAME}_original" -remove i386 -remove x86_64 -output "${FRAMEWORK_NAME}"
    xcrun lipo "${FRAMEWORK_NAME}_original" $ARCHS_TO_REMOVE -output "${FRAMEWORK_NAME}"
    xcrun lipo -info "${FRAMEWORK_NAME}"
    rm "${FRAMEWORK_NAME}_original"
    popd > /dev/null
}

make_xcframework () {
    FRAMEWORK_NAME=${1%.framework}
    REMOVE_FOR_IOS=$2
    REMOVE_FOR_SIM=$3
    DIR_IOS=temp_ios
    DIR_SIM=temp_sim
    
    echo "Processing framework: ${FRAMEWORK_NAME}.framework"

    echo "Creating ios part"
    make_part "$FRAMEWORK_NAME" "$DIR_IOS" "$REMOVE_FOR_IOS"

    echo "Creating simulator part"
    make_part "$FRAMEWORK_NAME" "$DIR_SIM" "$REMOVE_FOR_SIM"

    echo "Creating xcframework"
    rm -rf "${FRAMEWORK_NAME}.xcframework"
    xcodebuild -create-xcframework \
        -framework "${DIR_IOS}/${FRAMEWORK_NAME}.framework" \
        -framework "${DIR_SIM}/${FRAMEWORK_NAME}.framework" \
        -output "${FRAMEWORK_NAME}.xcframework"
    
    rm -rf "$DIR_IOS" "$DIR_SIM"
    #tree "${FRAMEWORK_NAME}.xcframework"
    echo 'OK!'
}

# First parameter: name of the framework to convert
# Second parameter: architectures to remove for ios (device) in the form: "-remove ABC -remove XYZ"
# Third parameter: architectures to remove for ios simulator  in the form: "-remove ABC -remove XYZ"

Examples: 
make_xcframework MyOldFramework.framework "-remove i386 -remove x86_64" "-remove armv7 -remove arm64"
make_xcframework OtherFramework.framework "-remove i386 -remove x86_64" "-remove armv7 -remove arm64"
make_xcframework AnotherFramework.framework "-remove i386 -remove x86_64" "-remove armv7s -remove armv7 -remove arm64"

0 comments :

This work is licensed under BSD Zero Clause License | nacho4d ®