Hi,
this should just be a discussion around possible future improvements around C/C++ Interop in SPM for libraries.
Basically every library for common technologies in the server-side space has a C++ library available. With C++ Interop now there, in theory, these libraries could also be available for Swift users, with (if at all) just a small wrapper library necessary. This would boost the possibilities for adoption for Swift on Server massively as all the technologies would be available.
But this is, at least in my experience, only theory. Many of the bigger libraries donât have a nice /include
directory with all the headers but use a rather complicated build system, mostly done with Cmake, also including dependencies. Replicating the build in SPM is nearly impossible for such systems which limits the possibilities for the use of C++ libraries tremendously.
Currently there are 2 options I know of available:
-
Using unsafe options to statically link the libraries. This works but nobody can use your package as it includes unsafe options.
-
Completely refactor the library to bring it into a format SPM can work with. This has the drawback that it takes a. much time to do and b. you have to port all the changes over to your version of the package manually everytime there is an update.
Both options are not really usable. 1. is literally unusable and 2. can take so much time that you could ask yourself why not just stick with C++ and/or Java instead of refactoring possibly dozens of libraries.
My wishes
First I have to say Iâm not a C/C++ developer, nor Cmake user and not an expert in the Swift build system, nevertheless I would suggest some points we can at least discuss to make the situation better for such usecases.
1. Directly support Cmake with SPM
Possibly the easiest solution from a Package Developerâs view would be to directly support Cmake or at least make builds. This support could provide SPM with all the necessary public headers and the exact way on how to build and link the C/C++ target. While I can imagine that this is a complicated step from SPM perspective, it would make the usability nearly perfect
2. Allow the linking args as a safe flag
This would allow statically linking C++/C binaries into the Swift Package. This is currently possible with stating the direct compiler options but only as an unsafe flag which makes the package not usable as a dependency. If we would find a way to allow some advanced linker options as safe flags, that would make it possible to rather easily work with C++ libraries in Swift. If the user doesnât trust the binaries, they can just rebuild from source and switch out the binaries for their own.
3. Extend the build system of SPM massively
This idea is related to 2 but goes into all directions. The problem now is that we are not really able to replicate the Cmake files in SPMâs build system. If we were to massively extend the options for SPM C/C++ builds, it would probably be possible, at some point, to just build these libraries with native options.
These would be some ideas of mine to make the C++ Interop for libraries much better to support my suggested use case. Hope we can discuss my ideas/bring new ideas from you into the mix. Hopefully the one or another proposal will come out of this thread.
Happy holidays!