Skip to content

Instantly share code, notes, and snippets.

@tigi44
Forked from quangDecember/build-xcframework.sh
Created February 28, 2022 06:52
Show Gist options
  • Save tigi44/a0110b6fd3bb984adb36c9caedd0f223 to your computer and use it in GitHub Desktop.
Save tigi44/a0110b6fd3bb984adb36c9caedd0f223 to your computer and use it in GitHub Desktop.

Revisions

  1. @quangDecember quangDecember revised this gist Nov 4, 2019. No changes.
  2. @quangDecember quangDecember revised this gist Oct 15, 2019. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions build-xcframework.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    # Paste the following code to your Build Phase > New Run Script Phase
    env > env.txt
    instruments -s devices > devices.txt
    #! /bin/sh -e
    @@ -25,12 +24,14 @@ function archivePathDevice {
    # 3rd == archivePath
    function archive {
    echo "▸ Starts archiving the scheme: ${1} for destination: ${2};\n▸ Archive path: ${3}.xcarchive"
    xcodebuild archive \
    xcodebuild clean archive \
    -project "${PROJECT_NAME}.xcodeproj" \
    -scheme ${1} \
    -configuration ${CONFIGURATION} \
    -destination "${2}" \
    -archivePath "${3}" \
    SKIP_INSTALL=NO \
    OBJROOT="${OBJROOT}/DependentBuilds" \
    BUILD_LIBRARY_FOR_DISTRIBUTION=YES | xcpretty
    }

  3. @quangDecember quangDecember created this gist Sep 24, 2019.
    91 changes: 91 additions & 0 deletions build-xcframework.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    # Paste the following code to your Build Phase > New Run Script Phase
    env > env.txt
    instruments -s devices > devices.txt
    #! /bin/sh -e
    # This script demonstrates archive and create action on frameworks and libraries
    # Based on script by @author Boris Bielik

    # Release dir path
    OUTPUT_DIR_PATH="${PROJECT_DIR}/XCFramework"

    function archivePathSimulator {
    local DIR=${OUTPUT_DIR_PATH}/archives/"${1}-SIMULATOR"
    echo "${DIR}"
    }

    function archivePathDevice {
    local DIR=${OUTPUT_DIR_PATH}/archives/"${1}-DEVICE"
    echo "${DIR}"
    }

    # Archive takes 3 params
    #
    # 1st == SCHEME
    # 2nd == destination
    # 3rd == archivePath
    function archive {
    echo "▸ Starts archiving the scheme: ${1} for destination: ${2};\n▸ Archive path: ${3}.xcarchive"
    xcodebuild archive \
    -project "${PROJECT_NAME}.xcodeproj" \
    -scheme ${1} \
    -destination "${2}" \
    -archivePath "${3}" \
    SKIP_INSTALL=NO \
    BUILD_LIBRARY_FOR_DISTRIBUTION=YES | xcpretty
    }

    # Builds archive for iOS simulator & device
    function buildArchive {
    SCHEME=${1}

    archive $SCHEME "generic/platform=iOS Simulator" $(archivePathSimulator $SCHEME)
    archive $SCHEME "generic/platform=iOS" $(archivePathDevice $SCHEME)
    }

    # Creates xc framework
    function createXCFramework {
    FRAMEWORK_ARCHIVE_PATH_POSTFIX=".xcarchive/Products/Library/Frameworks"
    FRAMEWORK_SIMULATOR_DIR="$(archivePathSimulator $1)${FRAMEWORK_ARCHIVE_PATH_POSTFIX}"
    FRAMEWORK_DEVICE_DIR="$(archivePathDevice $1)${FRAMEWORK_ARCHIVE_PATH_POSTFIX}"

    xcodebuild -create-xcframework \
    -framework ${FRAMEWORK_SIMULATOR_DIR}/${1}.framework \
    -framework ${FRAMEWORK_DEVICE_DIR}/${1}.framework \
    -output ${OUTPUT_DIR_PATH}/xcframeworks/${1}.xcframework
    }

    ### Static Libraries cant be turned into frameworks
    function createXCFrameworkForStaticLibrary {

    LIBRARY_ARCHIVE_PATH_POSTFIX=".xcarchive/Products/usr/local/lib"
    LIBRARY_SIMULATOR_DIR="$(archivePathSimulator $1)${LIBRARY_ARCHIVE_PATH_POSTFIX}"
    LIBRARY_DEVICE_DIR="$(archivePathDevice $1)${LIBRARY_ARCHIVE_PATH_POSTFIX}"

    xcodebuild -create-xcframework \
    -library ${LIBRARY_SIMULATOR_DIR}/libStaticLibrary.a \
    -library ${LIBRARY_DEVICE_DIR}/libStaticLibrary.a \
    -output ${OUTPUT_DIR_PATH}/xcframeworks/${1}.xcframework
    }

    echo "#####################"
    echo "▸ Cleaning the dir: ${OUTPUT_DIR_PATH}"
    rm -rf $OUTPUT_DIR_PATH

    #### Static Library ####
    #LIBRARY="${PROJECT_NAME}"

    #echo "▸ Archive $LIBRARY"
    #buildArchive ${LIBRARY}

    #echo "▸ Create $FRAMEWORK.xcframework"
    #createXCFrameworkForStaticLibrary ${LIBRARY}

    #### Dynamic Framework ####

    DYNAMIC_FRAMEWORK="${PROJECT_NAME}"

    echo "▸ Archive $DYNAMIC_FRAMEWORK"
    buildArchive ${DYNAMIC_FRAMEWORK}

    echo "▸ Create $DYNAMIC_FRAMEWORK.xcframework"
    createXCFramework ${DYNAMIC_FRAMEWORK}