SwiftPM and other languages/build systems

I have a library I want to expose as a swift package. The library is written in Rust (though that's not important to the problem), and the library is applicable to both Linux and macOS.

So I'd ideally like

  • my Swift Package to build the Rust code from source with cargo
  • generate some C headers to import into my Swift code
  • write Swift code to provide a nice API
  • put the whole mess on github for everyone to use as a normal swift package (they'd need rust installed to build it, but that seems fine...)

I could definitely compromise on some of that, though!

Currently, I can do some of what I need:

  • I can build the rust code myself (a build tool plugin will probably work too)
  • I can generate the headers myself (a build tool plugin will probably work too)
  • I can compile the C target with the headers
  • I can import that into the Swift target, and build the Swift wrapper

What I'm completely stuck on is, how can I link my Rust library with my Swift code?

  • I don't think I can do it with a build tool — they only seem to be able to output source files and resources, where I have a .a (or maybe a .dylib/.so or .o)
    • side note: they do accept .s files — seems like a heinous hack, but maybe rust could be convinced to spit out assembly?
  • I don't see how to do it with (unsafe) linker flags — I need an absolute path for -L, since the build system might do the link with any old cwd, and I don't see how to get one?
  • I can possibly do it with "system library" targets, but that seems to require installing the static library from rust, to some global prefix (which might fly in Docker, but won't in general on either macOS or Linux)
2 Likes