How to create a linkable package from several static libraries with C and Swift code with SwiftPM?

I am trying to turn a public source library into a Swift Package Manager defined static library that is mixing existing static libraries with a .c file and several .swift files. I can compile it all in Xcode into one single static library. This procedure allows me to release my software later through the AppStore without running into any sandbox issues.

Basically, I have:

  • several static libraries (.a & .o) that I need to link eventually with the rest of the package. I have these under Sources/libraries. They are several public source libraries that I need.
  • the needed c header files for these libraries in Sources/headers
  • some C wrappers in order to resolve macros: builtins.c and builtins.h in Sources/headers
  • a Swift wrapper (several swift files) under Sources/SwiftWrapper

I have now tried to replicate this process with Swift Package Manager. I am struggling with specifying library locations, different languages and modulemap files. How to mix it all together?

Many thanks in advance!


This isn’t supported yet. (See SE‐0272.)

If their own source is available and you can turn them into packages too, you might be able to convert the entire dependency tree to source.

Or if they have dynamic library alternatives, you might be able to tell clients to install them separately so that you can treat them as system libraries.

Those will have to be separate modules, because you cannot mix sources. You can fake it by making the Swift module depend on the C module and then @_exported importing it. But that is an underscored attribute which hasn’t been finalized, so beware that it isn’t source‐stable from one release of Swift to the next.

1 Like