Search code examples
iosobjective-ccocoapodspodspeccocoa-touch

How to build cocoa touch framework for all architectures dependent on other frameworks added using cocoapods?


I want to create a private cocoapod of the output framework built for both simulators and devices.

I have created a Cocoa touch framework in which I have integrated CocoaLumberjack using Cocoapods. I want to build the framework for all architectures possible (simulator as well as device).

By default the build setting,

 'Build Active Architectures Only' is set to (Debug - Yes, Release - No).

As soon as I set this setting for debug to No, the build fails with the following linker error:

Undefined symbols for architecture i386:
"_OBJC_CLASS_$_DDASLLogger", referenced from:
  objc-class-ref in MyManager.o
"_OBJC_CLASS_$_DDLog", referenced from:
  objc-class-ref in MyManager.o
"_OBJC_CLASS_$_DDTTYLogger", referenced from:
  objc-class-ref in MyManager.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I get it that CocoaLumberjack is available for only active architectures in debug version.

So I switch back the Build active architectures for debug to Yes and build the framework successfully.

To build the framework for all the architectures I am using a run script added in Build phases that also claims to merge the ios-device and ios-simulator build into one. Here is the script:

    set -e
    set +u
    # Avoid recursively calling this script.
    if [[ $SF_MASTER_SCRIPT_RUNNING ]]
    then
    exit 0
    fi
    set -u
    export SF_MASTER_SCRIPT_RUNNING=1


    # Constants
    SF_TARGET_NAME=${PROJECT_NAME}
    UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal

    # Take build target
    if [[ "$SDK_NAME" =~ ([A-Za-z]+) ]]
    then
    SF_SDK_PLATFORM=${BASH_REMATCH[1]}
    else
    echo "Could not find platform name from SDK_NAME: $SDK_NAME"
    exit 1
    fi

    if [[ "$SF_SDK_PLATFORM" = "iphoneos" ]]
    then
    echo "Please choose iPhone simulator as the build target."
    exit 1
    fi

    IPHONE_DEVICE_BUILD_DIR=${BUILD_DIR}/${CONFIGURATION}-iphoneos

    # Build the other (non-simulator) platform
    xcodebuild -project "${PROJECT_FILE_PATH}" -target "${TARGET_NAME}" -configuration "${CONFIGURATION}" -sdk iphoneos BUILD_DIR="${BUILD_DIR}" OBJROOT="${OBJROOT}" BUILD_ROOT="${BUILD_ROOT}" CONFIGURATION_BUILD_DIR="${IPHONE_DEVICE_BUILD_DIR}/arm64" SYMROOT="${SYMROOT}" ARCHS='arm64' VALID_ARCHS='arm64' $ACTION

    xcodebuild -project "${PROJECT_FILE_PATH}" -target "${TARGET_NAME}" -configuration "${CONFIGURATION}" -sdk iphoneos BUILD_DIR="${BUILD_DIR}" OBJROOT="${OBJROOT}" BUILD_ROOT="${BUILD_ROOT}"  CONFIGURATION_BUILD_DIR="${IPHONE_DEVICE_BUILD_DIR}/armv7" SYMROOT="${SYMROOT}" ARCHS='armv7 armv7s' VALID_ARCHS='armv7 armv7s' $ACTION

    # Copy the framework structure to the universal folder (clean it first)
    rm -rf "${UNIVERSAL_OUTPUTFOLDER}"
    mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"
    cp -R "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework" "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework"

    # Smash them together to combine all architectures
    lipo -create  "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/arm64/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/armv7/${PROJECT_NAME}.framework/${PROJECT_NAME}" -output "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}"

I check the 'Run script only when installing' checkbox present below the run script. Now I build the framework for Generic iOS device. Now i click on the Products group present in the Project hierarchy and select the MyFramework.framework file, right click and select show in finder. It opens up the ~/Library/Developer/Xcode/DerivedData/MyFramework-dlpsipmxkqmemwgqrfeovlzgyhca/Build/Products/Debug-iphoneos/MyFramework.framework in finder.

Now, I create a new tag 'MyFramework-v0.0.1' containing the commit where I added MyFramework.framework file. 

I go to ~/.cocoapods/repos/MyRepoName/ and create a podspec file MyFramework.podspec as follows:

Pod::Spec.new do |s|
s.name         = "MyFramework"
s.version      = "0.0.1"
s.summary      = "A pod for MyFramework"
s.description  = "A pod designed for MyFramework"

s.homepage     = "My_Private_Repo_Path"
s.license      = { :type => "MIT", :file => "FILE_LICENSE" }
s.authors             = { "My_Username" => "my_email_address"
}
s.platform     = :ios, "8.0"
s.ios.deployment_target = "8.0"

s.source       = { :git => "My_Private_Repo_Path", :tag => 'MyFramework-v'+String(s.version) }

s.requires_arc = true
s.vendored_frameworks = "MyFramework.framework"
s.dependency "CocoaLumberjack"
end

Now when I run the following command in the terminal:

pod repo push MyRepoName MyFramework.podspec

I get the following Error:

Validating spec
-> MyFramework (0.0.1)
- ERROR | [iOS] xcodebuild: Returned an unsuccessful exit code. You can use `   --verbose` for more information.
- NOTE  | [iOS] xcodebuild:  ld: warning: ignoring file  MyFramework/MyFramework.framework/MyFramework, missing required architecture i386 in file   MyFramework/MyFramework.framework/MyFramework (2 slices)
- NOTE  | [iOS] xcodebuild:  ld: warning: ignoring file MyFramework/MyFramework.framework/MyFramework, missing required architecture x86_64 in file MyFramework/MyFramework.framework/MyFramework (2 slices)
- NOTE  | [iOS] xcodebuild:  fatal error: lipo: -remove's specified would result in an empty fat file

[!] The `MyFramework.podspec` specification does not validate.

How to build cocoa touch framework for all devices and simulators, that is dependent on another framework (CocoaLumberjack) added using cocoapods? I need to create a private pod of the output framework.


Solution

  • So basically I found out that I just need to follow these simple steps.

    1. Create a cocoa touch framework.
    2. Set bitcode enabled to No.
    3. Select your target and choose edit schemes. Select Run and choose Release from Info tab. 
    4. No other setting required.
    5. Now build the framework for any simulator as simulator runs on x86 architecture.
    6. Click on Products group in Project Navigator and find the .framework file. 
    7. Right click on it and click on Show in finder. Copy and paste it in any folder, I personally prefer the name 'simulator'.
    8. Now build the framework for Generic iOS Device and follow the steps 6 through 
    9. Just rename the folder to 'device' instead of 'simulator'.
    10. Copy the device .framework file and paste in any other directory. I prefer the immediate super directory of both.
    

    So the directory structure now becomes:

     - Desktop
       - device
         - MyFramework.framework
       - simulator
         - MyFramework.framework
       - MyFramework.framework
    

    Now open terminal and cd to the Desktop. Now start typing the following command:

    lipo -create 'device/MyFramework.framework/MyFramework' 'simulator/MyFramework.framework/MyFramework' -output 'MyFramework.framework/MyFramework'
    

    and that's it. Here we merge the simulator and device version of MyFramework binary present inside MyFramework.framework. We get a universal framework that builds for all the architectures including simulator and device.

    Now, creating a pod for this framework doesn't make any difference. It works like a charm. Please also note that there are run scripts available too to achieve the same functionality, but I spent a lot of time in finding the correct script. So I would suggest you use this method.