mesonbuild / meson

The Meson Build System
http://mesonbuild.com
Apache License 2.0
5.54k stars 1.61k forks source link

Meson exception when generate Rust test from Rust library when external dependency with `link_args` is used #11741

Open vanc opened 1 year ago

vanc commented 1 year ago

Describe the bug When b_sanitize is set to address, if use the Rust's module test method to generate a test from a Rust rlib target, linking would fail because of missing all symbols from the asan library.

With Cargo, I can use build.rs to inject a link command cargo:rustc-link-lib=asan based on an environment variable passed from meson.build. But with rust.test, I tried to use an external dependency with only a link_args: '-lasan' and got an exception.

Traceback (most recent call last):
  File "/home/user/.local/lib/python3.8/site-packages/mesonbuild/mesonmain.py", line 271, in run
    return msetup.run(['--reconfigure'] + args[2:])
  File "/home/user/.local/lib/python3.8/site-packages/mesonbuild/msetup.py", line 310, in run
    app.generate()
  File "/home/user/.local/lib/python3.8/site-packages/mesonbuild/msetup.py", line 187, in generate
    self._generate(env)
  File "/home/user/.local/lib/python3.8/site-packages/mesonbuild/msetup.py", line 251, in _generate
    intr.backend.generate()
  File "/home/user/.local/lib/python3.8/site-packages/mesonbuild/backend/ninjabackend.py", line 615, in generate
    self.generate_target(t)
  File "/home/user/.local/lib/python3.8/site-packages/mesonbuild/backend/ninjabackend.py", line 824, in generate_target
    self.generate_rust_target(target)
  File "/home/user/.local/lib/python3.8/site-packages/mesonbuild/backend/ninjabackend.py", line 1990, in generate_rust_target
    _type = 'static' if e.static else 'dylib'
AttributeError: 'InternalDependency' object has no attribute 'static'

To Reproduce

  1. Create a Rust library with rust-crate-type as rlib and generate some unit tests.
  2. Import rust module and use rust.test() to convert the rlib to a meson test.
  3. Enable sanitizer with meson setup builddir -Db_sanitize=address
  4. meson test would fail because of missing symbols from asan.

Now declare an external dependency with:

    dep_asan = declare_dependency(link_args: '-lasan')

And add the dependency to the dependency list of the test target. For example:

   rust = import('rust')
   rust.test('myrlib', myrlib, dependencies: [dep_asan])

Run meson setup again and got the backtrace.

Expected behavior There should be some way to allow Rust test to link with some extra libraries.

system parameters Meson version: 1.0.2 Python: 3.8

gfxstrand commented 2 months ago

cc @dcbaker I just hit this in Mesa