代码之家  ›  专栏  ›  技术社区  ›  Adam

使用Xcode和sdk4构建fat静态库(设备+模拟器)+

  •  278
  • Adam  · 技术社区  · 14 年前

    从理论上讲,我们可以构建一个包含模拟器、iPhone和iPad的静态库。

    但是,苹果没有我能找到的关于这个的文档,Xcode的默认模板也没有配置成这样。

    一些历史:

    • 在2008年,我们曾经能够制作一个静态lib,包括sim和device。苹果禁用了它。

    参考文献:

    1. 这是一个很好的想法,这是一个很好的方法,但它不起作用: http://www.drobnik.com/touch/2010/04/universal-static-libraries/

      • 他的脚本中有一些bug,这意味着它只能在他的机器上工作-他应该使用BUILD\u PRODUCTS\u DIR和/或BUILD\u DIR,而不是“猜测”它们)
      • 苹果最新的Xcode阻止你做他所做的事情——它根本不起作用,因为Xcode处理目标的方式(有文件记载)发生了变化
    2. How do i compile a static library (fat) for armv6, armv7 and i386

      • 自从苹果公司的最新变化,模拟器部分不再是相同的arm6/arm7的区别-这是一个不同的问题,见上文)
    9 回复  |  直到 7 年前
        1
  •  274
  •   Adam    7 年前

    选择:

    Easy copy/paste of latest version

    Karl's library 需要更多的精力来设置,但要有更好的长期解决方案(它将库转换为一个框架)。

    Use this, then tweak it to add support for Archive builds


    最近的更改: 1增加了对iOS 10.x的支持(同时保持对旧平台的支持)

    1. 关于如何将此脚本用于嵌入到另一个项目中的项目的信息(尽管我强烈建议不要这样做,但是如果您将项目嵌入到其他项目中(从Xcode3.x到Xcode4.6.x),Apple在Xcode中会有几个show-stopper错误)

    2. 现在支持iPhone5(使用苹果的解决方案来解决lipo中的漏洞)。注意:安装说明已经更改(我可能会在将来通过更改脚本来简化此过程,但现在不想冒险)

    3. 添加了SYMROOT的显式设置(可能还需要设置OBJROOT?),多亏了道格·迪金森


    有关使用/安装说明,请参阅下面的

    ##########################################
    #
    # c.f. https://stackoverflow.com/questions/3520977/build-fat-static-library-device-simulator-using-xcode-and-sdk-4
    #
    # Version 2.82
    #
    # Latest Change:
    # - MORE tweaks to get the iOS 10+ and 9- working
    # - Support iOS 10+
    # - Corrected typo for iOS 1-10+ (thanks @stuikomma)
    # 
    # Purpose:
    #   Automatically create a Universal static library for iPhone + iPad + iPhone Simulator from within XCode
    #
    # Author: Adam Martin - http://twitter.com/redglassesapps
    # Based on: original script from Eonil (main changes: Eonil's script WILL NOT WORK in Xcode GUI - it WILL CRASH YOUR COMPUTER)
    #
    
    set -e
    set -o pipefail
    
    #################[ Tests: helps workaround any future bugs in Xcode ]########
    #
    DEBUG_THIS_SCRIPT="false"
    
    if [ $DEBUG_THIS_SCRIPT = "true" ]
    then
    echo "########### TESTS #############"
    echo "Use the following variables when debugging this script; note that they may change on recursions"
    echo "BUILD_DIR = $BUILD_DIR"
    echo "BUILD_ROOT = $BUILD_ROOT"
    echo "CONFIGURATION_BUILD_DIR = $CONFIGURATION_BUILD_DIR"
    echo "BUILT_PRODUCTS_DIR = $BUILT_PRODUCTS_DIR"
    echo "CONFIGURATION_TEMP_DIR = $CONFIGURATION_TEMP_DIR"
    echo "TARGET_BUILD_DIR = $TARGET_BUILD_DIR"
    fi
    
    #####################[ part 1 ]##################
    # First, work out the BASESDK version number (NB: Apple ought to report this, but they hide it)
    #    (incidental: searching for substrings in sh is a nightmare! Sob)
    
    SDK_VERSION=$(echo ${SDK_NAME} | grep -o '\d\{1,2\}\.\d\{1,2\}$')
    
    # Next, work out if we're in SIM or DEVICE
    
    if [ ${PLATFORM_NAME} = "iphonesimulator" ]
    then
    OTHER_SDK_TO_BUILD=iphoneos${SDK_VERSION}
    else
    OTHER_SDK_TO_BUILD=iphonesimulator${SDK_VERSION}
    fi
    
    echo "XCode has selected SDK: ${PLATFORM_NAME} with version: ${SDK_VERSION} (although back-targetting: ${IPHONEOS_DEPLOYMENT_TARGET})"
    echo "...therefore, OTHER_SDK_TO_BUILD = ${OTHER_SDK_TO_BUILD}"
    #
    #####################[ end of part 1 ]##################
    
    #####################[ part 2 ]##################
    #
    # IF this is the original invocation, invoke WHATEVER other builds are required
    #
    # Xcode is already building ONE target...
    #
    # ...but this is a LIBRARY, so Apple is wrong to set it to build just one.
    # ...we need to build ALL targets
    # ...we MUST NOT re-build the target that is ALREADY being built: Xcode WILL CRASH YOUR COMPUTER if you try this (infinite recursion!)
    #
    #
    # So: build ONLY the missing platforms/configurations.
    
    if [ "true" == ${ALREADYINVOKED:-false} ]
    then
    echo "RECURSION: I am NOT the root invocation, so I'm NOT going to recurse"
    else
    # CRITICAL:
    # Prevent infinite recursion (Xcode sucks)
    export ALREADYINVOKED="true"
    
    echo "RECURSION: I am the root ... recursing all missing build targets NOW..."
    echo "RECURSION: ...about to invoke: xcodebuild -configuration \"${CONFIGURATION}\" -project \"${PROJECT_NAME}.xcodeproj\" -target \"${TARGET_NAME}\" -sdk \"${OTHER_SDK_TO_BUILD}\" ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO" BUILD_DIR=\"${BUILD_DIR}\" BUILD_ROOT=\"${BUILD_ROOT}\" SYMROOT=\"${SYMROOT}\"
    
    xcodebuild -configuration "${CONFIGURATION}" -project "${PROJECT_NAME}.xcodeproj" -target "${TARGET_NAME}" -sdk "${OTHER_SDK_TO_BUILD}" ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" SYMROOT="${SYMROOT}"
    
    ACTION="build"
    
    #Merge all platform binaries as a fat binary for each configurations.
    
    # Calculate where the (multiple) built files are coming from:
    CURRENTCONFIG_DEVICE_DIR=${SYMROOT}/${CONFIGURATION}-iphoneos
    CURRENTCONFIG_SIMULATOR_DIR=${SYMROOT}/${CONFIGURATION}-iphonesimulator
    
    echo "Taking device build from: ${CURRENTCONFIG_DEVICE_DIR}"
    echo "Taking simulator build from: ${CURRENTCONFIG_SIMULATOR_DIR}"
    
    CREATING_UNIVERSAL_DIR=${SYMROOT}/${CONFIGURATION}-universal
    echo "...I will output a universal build to: ${CREATING_UNIVERSAL_DIR}"
    
    # ... remove the products of previous runs of this script
    #      NB: this directory is ONLY created by this script - it should be safe to delete!
    
    rm -rf "${CREATING_UNIVERSAL_DIR}"
    mkdir "${CREATING_UNIVERSAL_DIR}"
    
    #
    echo "lipo: for current configuration (${CONFIGURATION}) creating output file: ${CREATING_UNIVERSAL_DIR}/${EXECUTABLE_NAME}"
    xcrun -sdk iphoneos lipo -create -output "${CREATING_UNIVERSAL_DIR}/${EXECUTABLE_NAME}" "${CURRENTCONFIG_DEVICE_DIR}/${EXECUTABLE_NAME}" "${CURRENTCONFIG_SIMULATOR_DIR}/${EXECUTABLE_NAME}"
    
    #########
    #
    # Added: StackOverflow suggestion to also copy "include" files
    #    (untested, but should work OK)
    #
    echo "Fetching headers from ${PUBLIC_HEADERS_FOLDER_PATH}"
    echo "  (if you embed your library project in another project, you will need to add"
    echo "   a "User Search Headers" build setting of: (NB INCLUDE THE DOUBLE QUOTES BELOW!)"
    echo '        "$(TARGET_BUILD_DIR)/usr/local/include/"'
    if [ -d "${CURRENTCONFIG_DEVICE_DIR}${PUBLIC_HEADERS_FOLDER_PATH}" ]
    then
    mkdir -p "${CREATING_UNIVERSAL_DIR}${PUBLIC_HEADERS_FOLDER_PATH}"
    # * needs to be outside the double quotes?
    cp -r "${CURRENTCONFIG_DEVICE_DIR}${PUBLIC_HEADERS_FOLDER_PATH}"* "${CREATING_UNIVERSAL_DIR}${PUBLIC_HEADERS_FOLDER_PATH}"
    fi
    fi
    

    安装说明

    1. 选择目标
    2. 在“构建设置”选项卡中,将“仅构建活动体系结构”设置为“否”(对于 全部的 项目)
    3. 将脚本(如上)复制/粘贴到框中

    …可选用法:

    1. 可选:…并将它们从“项目”部分拖放到“公共”部分
    2. 可选:注:如果

    如果找不到输出文件,这里有一个解决方法:

    1. 在脚本的最后添加以下代码(由frederikwallner提供):打开“${CREATING\u UNIVERSAL\u DIR}”

    2. 如果您使用的是XCode4的自定义“构建输出”目录,那么XCode会将所有“意外”文件放在错误的位置。

      1. 构建项目
      2. 选择最上面的项目(这是您的“最新版本”)。苹果应该自动选择它,但他们没有想到)


    如何在项目中包含“非源代码”文件(PNG、PLIST、XML等)

    1. 做上面的一切,检查它的工作
    2. 在第一个运行脚本阶段之后创建一个新的运行脚本阶段(复制/粘贴下面的代码)
    3. 在Xcode中创建一个类型为“bundle”的新目标
    4. 在您的主项目中,在“构建阶段”,将新的bundle添加为它“依赖”的内容(顶部,点击加号按钮,滚动到底部,在您的产品中找到“.bundle”文件)
    5. 在新的BUNDLE目标中,在“构建阶段”,添加“复制BUNDLE资源”部分,并将所有PNG文件等拖放到其中

    echo "RunScript2:"
    echo "Autocopying any bundles into the 'universal' output folder created by RunScript1"
    CREATING_UNIVERSAL_DIR=${SYMROOT}/${CONFIGURATION}-universal
    cp -r "${BUILT_PRODUCTS_DIR}/"*.bundle "${CREATING_UNIVERSAL_DIR}"
    
        2
  •  87
  •   Stunner    4 年前

    我花了很多时间尝试构建一个胖静态库,它将在armv7、armv7s和模拟器上工作。最后 found a solution .

    要点是分别构建两个库(一个用于设备,另一个用于模拟器),重命名它们以区别于其他库,然后lipo-将它们创建为一个库。

    lipo -create libPhone.a libSimulator.a -output libUniversal.a
    

    我试过了,很管用!

        3
  •  74
  •   Mohsin Khubaib Ahmed    9 年前

    我已经做了一个决定 XCode 4 project template 这使您可以像创建常规库一样轻松地创建通用框架。

        4
  •  30
  •   eonil    14 年前

    xcodebuild 您可以在xcode中运行shell命令。 所以,如果您不介意使用自定义脚本,这个脚本可能会帮助您。

    #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}"
    

    可能看起来效率不高(我不擅长shell脚本),但很容易理解。 我配置了一个只运行此脚本的新目标。脚本是为命令行设计的,但未在中测试:)

    核心概念是 Xcode生成 lipo

    我在Xcode UI中尝试了许多配置,但都没有成功。因为这是一种批处理,所以命令行设计更适合,所以苹果逐渐从Xcode中删除了批构建功能。所以我不期望他们在将来提供基于UI的批构建特性。

        5
  •  11
  •   Brad Robinson    10 年前

    #!/bin/bash
    xcodebuild -sdk iphoneos
    xcodebuild -sdk iphonesimulator
    lipo -create -output libJsonKit.a build/Release-iphoneos/libJsonKit.a build/Release-iphonesimulator/libJsonKit.a
    
        6
  •  7
  •   ben    8 年前

    IOS 10更新:

    我在用iphoneos10.0构建fatlib时遇到了一个问题,因为脚本中的正则表达式只需要9.x及更低版本,对于ios10.0则返回0.0

    要解决这个问题,只需替换

    SDK_VERSION=$(echo ${SDK_NAME} | grep -o '.\{3\}$')
    

    SDK_VERSION=$(echo ${SDK_NAME} | grep -o '[\\.0-9]\{3,4\}$')
    
        7
  •  4
  •   Michael Tyson    12 年前

    Xcode 4 template ,与Karl的静态框架模板相同。

    我发现构建静态框架(而不是普通的静态库)会导致LLVM的随机崩溃,这是由于一个明显的链接器错误——所以,我猜静态库仍然有用!

        8
  •  3
  •   ShaoJen Chen    4 年前

    XCode 12更新:

    如果你跑了 xcodebuild -arch param,XCode 12将构建默认架构为“arm64 x86\u 64”的模拟器库。

    那就跑吧 xcrun -sdk iphoneos lipo -create -output 会有冲突,因为 arm64

    fork script from Adam git 把它修好。

        9
  •  2
  •   user503821    14 年前

    值得注意的一点。我注意到它不会复制任何你标记为公共的include文件。我已经把我的剧本改编成了你的剧本,效果相当不错。将以下内容粘贴到脚本末尾。

    if [ -d "${CURRENTCONFIG_DEVICE_DIR}/usr/local/include" ]
    then
      mkdir -p "${CURRENTCONFIG_UNIVERSAL_DIR}/usr/local/include"
      cp "${CURRENTCONFIG_DEVICE_DIR}"/usr/local/include/* "${CURRENTCONFIG_UNIVERSAL_DIR}/usr/local/include"
    fi
    
        10
  •  1
  •   Mohsin Khubaib Ahmed    9 年前

    我其实只是 wrote my own script 为此目的。它不使用Xcode。 (它基于Gambit Scheme项目中的一个类似脚本。)