xcframework does not contain internal frameworks
Asked Answered
N

5

7

I am trying to create an .xcframework file for a few third party libraries I'm using. Following the instructions from https://appspector.com/blog/xcframeworks, I

  1. Create platform-specific framework files:
# Archive for device
xcodebuild archive -scheme TestFramework -destination="iOS" -archivePath /tmp/xcf/ios.xcarchive -derivedDataPath /tmp/iphoneos -sdk iphoneos SKIP_INSTALL=NO BUILD_LIBRARIES_FOR_DISTRIBUTION=YES

# Archive for simulator
xcodebuild archive -scheme TestFramework -destination="iOS Simulator" -archivePath /tmp/xcf/iossimulator.xcarchive -derivedDataPath /tmp/iphoneos -sdk iphonesimulator SKIP_INSTALL=NO BUILD_LIBRARIES_FOR_DISTRIBUTION=YES

At this point, I've gotten two frameworks with .swiftmodule files; everything looks good. The frameworks do not come with .swiftinterface files, but I think that is alright as these are objc projects. Then, I

  1. Combine the individual frameworks to form a single xcframework file:
# Build xcframework with two archives
xcodebuild -create-xcframework -framework /tmp/xcf/ios.xcarchive/Products/Library/Frameworks/TestFramework.framework -framework /tmp/xcf/iossimulator.xcarchive/Products/Library/Frameworks/TestFramework.framework -output /tmp/xcf/TestFramework.xcframework

Here, xcodebuild doesn't fail, but it emits a curious error:

No 'swiftinterface' files found within '/Users/user/git/ReactiveObjC/output/xcf/ios.xcarchive/Products/Products/Library/TestFramework.framework/Modules/TestFramework.swiftmodule'.

Crucially, the .xcframework package does not contain the individual frameworks or folders specified in the Info.plist file. Obviously, when I try to link to the xcframework in this state, I get a bunch of "missing symbols" linker errors.

Why is this? What must I do to get xcodebuild to create the xcframework completely?

NB: Manually creating the folders seems to work for now, but this feels wrong and is brittle – Apple can change the way xcodebuild works anytime.

Negress answered 11/11, 2019 at 17:12 Comment(0)
N
25

Turns out that adding BUILD_LIBRARY_FOR_DISTRIBUTION = YES to the end of the xcodebuild command was not enough / not working to produce a framework with the required swiftinterface files. I had to go in the settings for the actual project and manually set BUILD_LIBRARY_FOR_DISTRIBUTION to YES, like this answer suggests. After I got the frameworks with the swiftinterface files, I was then able to create the xcframework file using xcodebuild without any problems.

Negress answered 11/11, 2019 at 19:14 Comment(4)
You used the wrong key, try adding BUILD_LIBRARIES_FOR_DISTRIBUTION=YES (note the plural) to xcodebuildBarbed
FWIW: Checking the documentation bar in Xcode, they list BUILD_LIBRARY_FOR_DISTRIBUTION as the correct key.Tso
Hello, I'm trying to build XCFramework from xcarchives, however I can't find BUILD_LIBRARY_FOR_DISTRIBUTION in project setting, need help :(( thxLayamon
Hi, how can I specify BUILD_LIBRARY_FOR_DISTRIBUTION on a swift package? it seems that from xcodebuild the flag is not passedAtabrine
M
4

Create XCFramework

XCFramework replaces Fat/Universal framework. It is a bundle which contains multiple .framewok for each architecture/platform. We don't need to use lipo[About] for unite and separate frameworks manually

XCFramework supports libraries with headers and Objective-C

  1. Enable Build Libraries for Distribution(BUILD_LIBRARY_FOR_DISTRIBUTION) for your framework. XCFramework works only with enabled [BUILD_LIBRARY_FOR_DISTRIBUTION]. If not when you run step 3 xcodebuild -create-xcframework you get (or don't)

    No 'swiftinterface' files found within '<some_path>/<some_name>.xcarchive/Products/Library/Frameworks/<some_name>.framework/Modules/<some_name>.swiftmodule'
    
  2. Build/archive .framework for different architectures/platforms

     xcodebuild archive \
        -workspace ModuleName.xcworkspace \
        -scheme "schemaName" \
        -sdk "iphoneos" \
        -arch arm64 \
        -archivePath "<some_path>/ArchiveName1.xcarchive" \
        SKIP_INSTALL=NO \
    
     xcodebuild archive \
        -workspace ModuleName.xcworkspace \
        -scheme "schemaName" \
        -sdk "iphonesimulator" \
        -arch x86_64 \
        -archivePath "<some_path>/ArchiveName2.xcarchive" \
        SKIP_INSTALL=NO \
    

    SKIP_INSTALL[About] one of key point to include framework inside archive

  3. Create XCFramework using xcodebuild -create-xcframework

    xcodebuild -create-xcframework \
        -framework "<some_path>/ArchiveName1.xcarchive/Products/Library/Frameworks/ModuleName.framework" \
        -debug-symbols "<some_path>/ArchiveName1.xcarchive/dSYMs/${schemes[$n]}.ModuleName.dSYM" \
        -framework "<some_path>/ArchiveName2.xcarchive/Products/Library/Frameworks/ModuleName.framework"
    

-debug-symbols allows to add .dSYM[About] and .BCSymbolMap[About]

Mayfly answered 20/4, 2021 at 14:50 Comment(0)
M
1

Converting to XCFramework from Cocoapods

For my scenario, I'm trying to build XCFrameworks from the FAT/lipo frameworks generated by cocoapods.

I added this to the end of my podfile:

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
        end
    end
end

This ensured the frameworks were compiled correctly and I was able to run the normal xcodebuild -create-xcframework ... command to generate the XCFramework.

Note: I'm also using the cocoapods-binary plugin to pre-build all the pods frameworks.

Miscall answered 10/9, 2021 at 17:40 Comment(0)
C
1

I faced the same issue. It turned out that Xcode 13's build settings override the SKIP_INSTALL=NO flag in the command line. Once I set this field in project's build setting to NO (and made sure on the way that the Build Library For Distribution is set to YES) I managed to create the xcframework file successfully.

Celio answered 13/6, 2022 at 10:36 Comment(1)
Follow instruction from this for creating xc frameworkAmbagious
A
0

I have added readme file with full details how to create xc framework.

https://github.com/awasthi027/Xcode_lib_framework_issues/blob/master/README.md

Follow instruction from this for creating xc framework

Ambagious answered 9/9, 2022 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.