Skip to content

Commit

Permalink
rust: Link with rlib external dependencies
Browse files Browse the repository at this point in the history
When linking with a Rust rlib, we should also link with its external
system dependencies. This was currently done only for C ABI crates, do
it for both rlib and staticlib now.
  • Loading branch information
xclaesse committed Mar 5, 2024
1 parent a9d42a7 commit 5e0a307
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
5 changes: 2 additions & 3 deletions mesonbuild/backend/ninjabackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,8 @@ def _link_library(libname: str, static: bool, bundle: bool = False):
for d in target_deps:
linkdirs.add(d.subdir)
deps.append(self.get_dependency_filename(d))
if isinstance(d, build.StaticLibrary):
external_deps.extend(d.external_deps)
if d.uses_rust_abi():
if d not in itertools.chain(target.link_targets, target.link_whole_targets):
# Indirect Rust ABI dependency, we only need its path in linkdirs.
Expand All @@ -1993,9 +1995,6 @@ def _link_library(libname: str, static: bool, bundle: bool = False):

# Link a C ABI library

if isinstance(d, build.StaticLibrary):
external_deps.extend(d.external_deps)

# Pass native libraries directly to the linker with "-C link-arg"
# because rustc's "-l:+verbatim=" is not portable and we cannot rely
# on linker to find the right library without using verbatim filename.
Expand Down
5 changes: 5 additions & 0 deletions test cases/rust/24 system deps/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extern crate wrapper;

fn main() {
wrapper::func();
}
9 changes: 9 additions & 0 deletions test cases/rust/24 system deps/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
project('system deps', 'rust')

glib = dependency('glib-2.0', required: false)
if not glib.found()
error('MESON_SKIP_TEST: Need glib system dependency')
endif

rlib = static_library('wrapper', 'wrapper.rs', dependencies: glib)
exe = executable('main', 'main.rs', link_with: rlib)
9 changes: 9 additions & 0 deletions test cases/rust/24 system deps/wrapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extern "C" {
fn g_hash_table_new() -> *mut std::ffi::c_void;
}

pub fn func() {
unsafe {
g_hash_table_new();
}
}

0 comments on commit 5e0a307

Please sign in to comment.