How on earth can I create a .framework from a Swift package?

According to the responses on this post, Package.swift is no longer intended to be "convertible" to an Xcode project. This eliminates the ability to generate Xcode-specifc products such as .frameworks, since SPM doesn't support those proprietary formats out of the box.

The next thing tried is adding my package to a vanilla Xcode project and creating a Framework target and making the library from my package a dependency of it. But that doesn't seem to re-export the stuff in my package, as other Swift code can't see any of the types from the package when I import the framework.

I tried adding a single Swift file and @_exported import MyLibrary-ing it, but that gives me this error:

Underlying Objective-C module 'MyLibrary' not found

I'm at a loss. The only option now seems to be to build it by hand like a neanderthal. Does anyone know how to either resolve the above error, or how to do this all more gracefully?

2 Likes

See How to build Swift Package as XCFramework - #53 by monocularvision

I appreciate you linking that, but I still want to know from someone at Apple why I have to rely on a random shell script I found online to use Apple's own package manager to build my code in one of Apple's own binary formats

4 Likes

Although an official answer would be nice, maybe it has to do something with the fact that an xcframework is a proprietary format that is intended to only work on Apple devices. And the Swift open source ecosystem (including the swift package manager) has probably not much incentive to start adding support for proprietary things like this because its focus is to be a general-purpose language.

In that way it makes sense (to me) that Apple related things are bundled in Xcode. And actually I'm happy that all the Swift tools don't have too much dependencies on Xcode and its tools (that are often very old and have many bugs).

1 Like

SPM doesn't need to support it, Xcode could do it. Xcode can open Package.swift files, so it should provide a mechanism to create projects from Package.swifts the way Tuist can.

There's a ton of ideas to experiment with in this space. Could also do something like, putting a .xcconfig alongside Package.swift that references targets in the package and specifies how to build them (like as a .framework or .xcframework, or a static library) to make them show up in Xcode when you open Package.swift

Someone's probably gonna say this should just be a radar if I'm making an Xcode feature request, but radars give zero room for wider discussion and feedback on the idea. Plus I hate filing radars and never hearing back about them. Waste of everyone's time imo.

But anyway yes, you're right, this is Xcode's job, not SPM's. I never said SPM should do it!

2 Likes

I'm maintaining a script based on emergetools's script to generate xcframework from swift package. And you can check it here.

Have experienced some bug on xcframework.

And my try to fix on swiftpm side is not very satisfactory due to the fact that it contains logic related with Xcode.