Skip to content

rust: rlib/dylib dependencies of dependencies only need -L added but not --extern #11694

@sdroege

Description

Describe the bug

When linking in a Rust rlib/dylib dependency into some target, currently meson is adding an --extern commandline parameter to rustc too for all dependencies of the dependency. This is not necessary: it is only required when the target is directly using a crate.

More importantly, it is adding the transitive dependencies all to the global namespace.

To Reproduce

  • meson.build:
project('test-rs', 'rust',
  version : '1.0.0',
  meson_version : '>= 1.0.0',
  default_options : ['buildtype=debugoptimized',
                     'rust_std=2021'
                    ]
)

rustc = meson.get_compiler('rust')

a = static_library('a', 'a/lib.rs',
  rust_crate_type : 'rlib',
)

a_dep = declare_dependency(link_with : a)

b = static_library('b', 'b/lib.rs',
  rust_crate_type : 'rlib',
  dependencies : [a_dep],
)

b_dep = declare_dependency(link_with : b,
  dependencies : [a_dep],
)

c = executable('c', 'c/main.rs',
  dependencies : [b_dep],
)
  • a/lib.rs:
pub fn foo() -> i32 {
    123
}
  • b/lib.rs:
pub fn bar() -> i32 {
    a::foo() * 2
}
  • c/main.rs:
fn main() {
    println!("{}", b::bar());
}

This results in the following rustc invocations

[1/3] rustc -C linker=cc --color=always --crate-type rlib --edition=2021 -C opt-level=2 -g --crate-name a --emit dep-info=a.d --emit link -o liba.rlib ../a/lib.rs
[2/3] rustc -C linker=cc --color=always --crate-type rlib --edition=2021 -C opt-level=2 -g --crate-name b --emit dep-info=b.d --emit link -o libb.rlib --extern a=liba.rlib -L . ../b/lib.rs
[3/3] rustc -C linker=cc --color=always --crate-type bin --edition=2021 -C opt-level=2 -g --crate-name c --emit dep-info=c.d --emit link -o c --extern b=libb.rlib --extern a=liba.rlib -L . ../c/main.rs

Expected behavior

Only adding -L parameters as needed (i.e. not at all in this example because all build results are in the same directory).

[1/3] rustc -C linker=cc --color=always --crate-type rlib --edition=2021 -C opt-level=2 -g --crate-name a --emit dep-info=a.d --emit link -o liba.rlib ../a/lib.rs
[2/3] rustc -C linker=cc --color=always --crate-type rlib --edition=2021 -C opt-level=2 -g --crate-name b --emit dep-info=b.d --emit link -o libb.rlib --extern a=liba.rlib -L . ../b/lib.rs
[3/3] rustc -C linker=cc --color=always --crate-type bin --edition=2021 -C opt-level=2 -g --crate-name c --emit dep-info=c.d --emit link -o c --extern b=libb.rlib -L . ../c/main.rs

CC @dcbaker Am I missing anything here? Where would this best be fixed, I guess already when declaring the dependency?

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