The initial pitched plan for how C++ interoperability should be support in Swift package manager in Swift 5.9 uncovered several issues. Most notably we do not have any officially supported feature that lets a Swift module import a C++ module without exposing that fact to its dependencies. As such, any client that wants to import a Swift target that enables C++ interoperability must also enable C++ interoperability. Unfortunately after some discussions we reached the conclusion that we will not be able to provide an alternative solution for this issue in Swift 5.9 . Therefore, I would like to propose a new, more limited plan for how C++ interoperability is going to be supported in Swift 5.9 .
Here's the updated proposed guidance we would like to document on the Swift website:
Enabling C++ interoperability for a SwiftPM target will need other targets that depend on such a target to enable C++ interoperability as well.
If you’d like to vend a package with a target that enables C++ interoperability, we recommend:
Communicating to clients that they have to enable C++ interoperability when depending on targets from such package.
Bumping up the major version, if you adopt C++ interoperability in an existing package. This will avoid breaking existing clients of the package.
Here's a target diagram illustrating how C++ interoperability will propagate through a Swift PM target graph:
Getting there in Swift 5.9
In Swift 5.9, We would like to augment the Swift compiler to always report an error when a Swift module that doesn't have C++ interoperability enabled imports another Swift module that has C++ interoperability enabled, except if the imported module is built with library evolution enabled.
Additionally, we still require the following SwiftPM fix:
- [C++ interop] c++ language standard is not passed to Swift's clang importer when C++ interop is enabled swift-package-manager#6565
Vision for beyond Swift 5.9
In order to allow a Swift target to import C++ clang modules without forcing C++ interoperability onto the dependencies of such Swift target, we would like to see Swift officially support the following features:
- resilience / library-evolution support for all supported platforms.
- build setting that enables it in SwiftPM as well.
This request is tracked by the following tracking issue on GitHub:
- ✨ Swift should provide support for internal imports and resilience for all platforms (that can be enabled in SwiftPM) to allow Swift modules to depend on C++ modules without requiring that the clients enable C++ interoperability · Issue #66156 · apple/swift · GitHub
The C++ interoperability workgroup investigated potential options for supporting C++ interoperability in SwiftPM in Swift 5.9 . After getting the feedback on the initial plan, we've put together an updated proposed guidance for SwiftPM. This guidance clearly states the constraints imposed by C++ interoperability, most notably that Swift package manager targets that enable C++ interoperability force their dependencies to enable C++ interoperability as well.