Skip to content

Support linking Rust into C++ as rlibs #14215

Open
@lf-

Description

Yes, this is unsupported, but it is effectively critical for usable linking to Rust targets in C++ static libs without substantial pain since without the ability to link rlibs, you cannot combine C++ targets arbitrarily anymore if there is any rust in-tree. One of the unintended consequences of #11722 is that it prevents linking rlibs into C++ targets.

Specifically, you cannot combine two static_library C++ targets together if they depend on rust staticlib crates due to duplicate copies of libstd or other crates. The linking of libstd and other stuff from the sysroot must be deferred to the end at the time of linking the executable.

There's a few documented hacks at rust-lang/rust#73632 and bazel/buck2 rules_rust implement rust linking of staticlibs via these methods.

The following is possible but it still needs to correctly generate the link args to link to libstd and the sysroot as a whole, which is not yet a thing.

diff --git a/mesonbuild/build.py b/mesonbuild/build.py
index 460ed549b..8d6a7e6cb 100644
--- a/mesonbuild/build.py
+++ b/mesonbuild/build.py
@@ -1490,8 +1490,6 @@ class BuildTarget(Target):
 
     def check_can_link_together(self, t: BuildTargetTypes) -> None:
         links_with_rust_abi = isinstance(t, BuildTarget) and t.uses_rust_abi()
-        if not self.uses_rust() and links_with_rust_abi:
-            raise InvalidArguments(f'Try to link Rust ABI library {t.name!r} with a non-Rust target {self.name!r}')
         if self.for_machine is not t.for_machine and (not links_with_rust_abi or t.rust_crate_type != 'proc-macro'):
             msg = f'Tried to tied to mix a {t.for_machine} library ("{t.name}") with a {self.for_machine} target "{self.name}"'
             if self.environment.is_cross_build():

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions