Build fat static library (device + simulator) using Xcode and SDK 4+

前端 未结 10 1230
独厮守ぢ
独厮守ぢ 2020-11-22 02:21

It appears that we can - theoretically - build a single static library that includes both simulator and iPhone and iPad.

However, Apple has no documentation on this

10条回答
  •  孤城傲影
    2020-11-22 03:20

    There is a command-line utility xcodebuild and you can run shell command within xcode. So, if you don't mind using custom script, this script may help you.

    #Configurations.
    #This script designed for Mac OS X command-line, so does not use Xcode build variables.
    #But you can use it freely if you want.
    
    TARGET=sns
    ACTION="clean build"
    FILE_NAME=libsns.a
    
    DEVICE=iphoneos3.2
    SIMULATOR=iphonesimulator3.2
    
    
    
    
    
    
    #Build for all platforms/configurations.
    
    xcodebuild -configuration Debug -target ${TARGET} -sdk ${DEVICE} ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO
    xcodebuild -configuration Debug -target ${TARGET} -sdk ${SIMULATOR} ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO
    xcodebuild -configuration Release -target ${TARGET} -sdk ${DEVICE} ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO
    xcodebuild -configuration Release -target ${TARGET} -sdk ${SIMULATOR} ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO
    
    
    
    
    
    
    
    #Merge all platform binaries as a fat binary for each configurations.
    
    DEBUG_DEVICE_DIR=${SYMROOT}/Debug-iphoneos
    DEBUG_SIMULATOR_DIR=${SYMROOT}/Debug-iphonesimulator
    DEBUG_UNIVERSAL_DIR=${SYMROOT}/Debug-universal
    
    RELEASE_DEVICE_DIR=${SYMROOT}/Release-iphoneos
    RELEASE_SIMULATOR_DIR=${SYMROOT}/Release-iphonesimulator
    RELEASE_UNIVERSAL_DIR=${SYMROOT}/Release-universal
    
    rm -rf "${DEBUG_UNIVERSAL_DIR}"
    rm -rf "${RELEASE_UNIVERSAL_DIR}"
    mkdir "${DEBUG_UNIVERSAL_DIR}"
    mkdir "${RELEASE_UNIVERSAL_DIR}"
    
    lipo -create -output "${DEBUG_UNIVERSAL_DIR}/${FILE_NAME}" "${DEBUG_DEVICE_DIR}/${FILE_NAME}" "${DEBUG_SIMULATOR_DIR}/${FILE_NAME}"
    lipo -create -output "${RELEASE_UNIVERSAL_DIR}/${FILE_NAME}" "${RELEASE_DEVICE_DIR}/${FILE_NAME}" "${RELEASE_SIMULATOR_DIR}/${FILE_NAME}"
    

    Maybe looks inefficient(I'm not good at shell script), but easy to understand. I configured a new target running only this script. The script is designed for command-line but not tested in :)

    The core concept is xcodebuild and lipo.

    I tried many configurations within Xcode UI, but nothing worked. Because this is a kind of batch processing, so command-line design is more suitable, so Apple removed batch build feature from Xcode gradually. So I don't expect they offer UI based batch build feature in future.

提交回复
热议问题