meson icon indicating copy to clipboard operation
meson copied to clipboard

Mixed Swift/C++ targets support

Open dblsaiko opened this issue 1 year ago • 0 comments

(Requires/follow-up to #13317. Actual diff of this PR without #13317.) Closes #13203. (Supersedes) Closes #14241.

This is a different approach to #14241 which does not require the incredibly ugly manual linking against Swift core libraries.

Makes Swift target generation use the main code path. This allows mixing Swift and C++ sources inside of a single target. (All Swift sources are still compiled with one swiftc invocation since per-file compilation is not supported.)

This also adds support for shared library Swift targets.

Also creates a Ninja target for a C++/Obj-C exported header, which those languages can use to call Swift code. This header is automatically added as a dependency to targets that can use it.

Caveat: using this to call Swift code from C++ requires manually setting CXX to the Swift-bundled clang++ (on non-macOS), since it requires special support in the compiler which the normal clang doesn't come with. Meson would automatically have to switch to using the Swift-bundled clang when a project uses both Swift and C/C++/Obj-C. Not sure if that is desirable or even doable. Could maybe throw an error instead when the mismatch is detected.

To do:

  • [ ] Test on macOS, specifically with Obj-C
  • [ ] Version checks (C++ bridging is available in 5.9 and above)
  • [ ] Test cases
  • [ ] Documentation
# Linking Swift lib with C++ executable
swiftlib = static_library('swiftlib', 'swiftlib.swift', swift_args: ['-parse-as-library'])
cpp_exe = executable('cpp_exe', 'main.cpp', link_with: [swiftlib], include_directories: [swiftlib.private_dir_include()])

# Single Swift/C++ target
combined_swift_cpp = executable('combined', 'combined.swift', 'combined.h', 'combined.cpp')

dblsaiko avatar Feb 17 '25 02:02 dblsaiko