knarf
knarf

Reputation: 732

How do you archive Swift Package Dependencies to include in your XCFramework for binary distribution as a Swift Package?

Goal is to host a git repo for a Swift Package to distribute proprietary code as a binary in a XCFramework. The source code has dependencies on other Swift Packages. Listing the dependencies in the .package config is not enough. Simply asking consumers to import dependencies is not an option, as they may be making use of the same packages at different levels. The main problem I am trying to solve is how to resolve these dependencies when my Swift Package is imported into a project.

  1. Each dependency package should be archived and wrapped into the XCFramework as well, right?
  2. How would I do that?

The packages I am using are also available in a CocoaPod as well.

  1. Would it be easier to import them as pods for the purpose of archiving?

I have gone thru the following resources and SO posts, but can not find answer to this specific purpose.

Upvotes: 9

Views: 4725

Answers (2)

knarf
knarf

Reputation: 732

Found a way to do it on the Swift forums. Someone suggested using a "dummy" target to wrap the binary framework in. You can then list dependencies in the dummy target's definition in the manifest.

https://forums.swift.org/t/issue-with-third-party-dependencies-inside-a-xcframework-through-spm/41977/2

I have the binary hosted in a zip and it is available by both SPM and Cocoa Pods. Tackling both package managers with one framework binary.

Upvotes: 1

Egor  Gaydamak
Egor Gaydamak

Reputation: 21

I faced into same issue couple of months ago and haven't found a proper solution so I just started to use Cocoapods for distribution and this script for archiving my framework target which have some Cocoapods as a dependencies:

xcodebuild archive -workspace "../workspace_name.xcworkspace" \
  -scheme %scheme_name% \
  -sdk iphonesimulator \
  -archivePath "./SdkArchives/ios_simulators.xcarchive" \
  BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
  SKIP_INSTALL=NO
  
xcodebuild archive -workspace "../workspace_name.xcworkspace" \
  -scheme %scheme_name% \
  -sdk iphoneos \
  -archivePath "./SdkArchives/ios_devices.xcarchive" \
  BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
  SKIP_INSTALL=NO
  
rm -rf ../DevPods/SDK/framework_name.xcframework
  
xcodebuild -create-xcframework \
-framework ./SdkArchives/ios_devices.xcarchive/Products/Library/Frameworks/framework_name.framework \
-framework ./SdkArchives/ios_simulators.xcarchive/Products/Library/Frameworks/framework_name.framework \
-output ../DevPods/SDK/framework_name.xcframework

rm -rf SdkArchives

Than you can simply package this xcframework as a pod using this lines in your podspeck:

s.vendored_frameworks = 'framework_name.xcframework'
s.dependency 'framework_name_dependency'

Also you could check out this article, I haven't tried it but looks promising. https://kean.blog/post/xcframeworks-caveats

Upvotes: 2

Related Questions