modules/rust: Add a `link_with` kwarg to the test method

This was requested by Mesa, where a bunch of `declare_dependency`
objects are being created as a workaround for the lack of this keyword
pull/11902/head
Dylan Baker 2 years ago
parent 78b8d447ee
commit 43f24060f3
  1. 5
      docs/markdown/Rust-module.md
  2. 4
      docs/markdown/snippets/rust_test_link_with.md
  3. 13
      mesonbuild/modules/rust.py
  4. 3
      test cases/rust/9 unit tests/helper.rs
  5. 4
      test cases/rust/9 unit tests/meson.build
  6. 16
      test cases/rust/9 unit tests/test3.rs

@ -18,7 +18,7 @@ like Meson, rather than Meson work more like rust.
## Functions ## Functions
### test(name: string, target: library | executable, dependencies: []Dependency) ### test(name: string, target: library | executable, dependencies: []Dependency, link_with: []targets)
This function creates a new rust unittest target from an existing rust This function creates a new rust unittest target from an existing rust
based target, which may be a library or executable. It does this by based target, which may be a library or executable. It does this by
@ -33,6 +33,9 @@ that automatically.
Additional, test only dependencies may be passed via the dependencies Additional, test only dependencies may be passed via the dependencies
argument. argument.
*(since 1.2.0)* the link_with argument can be used to pass additional build
targets to link with
### bindgen(*, input: string | BuildTarget | [](string | BuildTarget), output: string, include_directories: [](include_directories | string), c_args: []string, args: []string, dependencies: []Dependency) ### bindgen(*, input: string | BuildTarget | [](string | BuildTarget), output: string, include_directories: [](include_directories | string), c_args: []string, args: []string, dependencies: []Dependency)
This function wraps bindgen to simplify creating rust bindings around C This function wraps bindgen to simplify creating rust bindings around C

@ -0,0 +1,4 @@
## Add a `link_with` keyword to `rust.test()`
This can already be be worked around by creating `declare_dependency()` objects
to pass to the `dependencies` keyword, but this cuts out the middle man.

@ -1,4 +1,4 @@
# Copyright © 2020-2022 Intel Corporation # Copyright © 2020-2023 Intel Corporation
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -18,15 +18,16 @@ import typing as T
from . import ExtensionModule, ModuleReturnValue, ModuleInfo from . import ExtensionModule, ModuleReturnValue, ModuleInfo
from .. import mlog from .. import mlog
from ..build import BothLibraries, BuildTarget, CustomTargetIndex, Executable, ExtractedObjects, GeneratedList, IncludeDirs, CustomTarget, StructuredSources from ..build import BothLibraries, BuildTarget, CustomTargetIndex, Executable, ExtractedObjects, GeneratedList, IncludeDirs, CustomTarget, InvalidArguments, Jar, StructuredSources
from ..compilers.compilers import are_asserts_disabled from ..compilers.compilers import are_asserts_disabled
from ..dependencies import Dependency, ExternalLibrary from ..dependencies import Dependency, ExternalLibrary
from ..interpreter.type_checking import DEPENDENCIES_KW, TEST_KWS, OUTPUT_KW, INCLUDE_DIRECTORIES from ..interpreter.type_checking import DEPENDENCIES_KW, LINK_WITH_KW, TEST_KWS, OUTPUT_KW, INCLUDE_DIRECTORIES
from ..interpreterbase import ContainerTypeInfo, InterpreterException, KwargInfo, typed_kwargs, typed_pos_args, noPosargs from ..interpreterbase import ContainerTypeInfo, InterpreterException, KwargInfo, typed_kwargs, typed_pos_args, noPosargs
from ..mesonlib import File from ..mesonlib import File
if T.TYPE_CHECKING: if T.TYPE_CHECKING:
from . import ModuleState from . import ModuleState
from ..build import LibTypes
from ..interpreter import Interpreter from ..interpreter import Interpreter
from ..interpreter import kwargs as _kwargs from ..interpreter import kwargs as _kwargs
from ..interpreter.interpreter import SourceInputs, SourceOutputs from ..interpreter.interpreter import SourceInputs, SourceOutputs
@ -38,6 +39,7 @@ if T.TYPE_CHECKING:
dependencies: T.List[T.Union[Dependency, ExternalLibrary]] dependencies: T.List[T.Union[Dependency, ExternalLibrary]]
is_parallel: bool is_parallel: bool
link_with: T.List[LibTypes]
class FuncBindgen(TypedDict): class FuncBindgen(TypedDict):
@ -68,6 +70,7 @@ class RustModule(ExtensionModule):
'rust.test', 'rust.test',
*TEST_KWS, *TEST_KWS,
DEPENDENCIES_KW, DEPENDENCIES_KW,
LINK_WITH_KW.evolve(since='1.2.0'),
KwargInfo('is_parallel', bool, default=False), KwargInfo('is_parallel', bool, default=False),
) )
def test(self, state: ModuleState, args: T.Tuple[str, BuildTarget], kwargs: FuncTest) -> ModuleReturnValue: def test(self, state: ModuleState, args: T.Tuple[str, BuildTarget], kwargs: FuncTest) -> ModuleReturnValue:
@ -112,6 +115,9 @@ class RustModule(ExtensionModule):
rust.test('rust_lib_test', rust_lib) rust.test('rust_lib_test', rust_lib)
``` ```
""" """
if any(isinstance(t, Jar) for t in kwargs.get('link_with', [])):
raise InvalidArguments('Rust tests cannot link with Jar targets')
name = args[0] name = args[0]
base_target: BuildTarget = args[1] base_target: BuildTarget = args[1]
if not base_target.uses_rust(): if not base_target.uses_rust():
@ -145,6 +151,7 @@ class RustModule(ExtensionModule):
new_target_kwargs['rust_args'] = new_target_kwargs.get('rust_args', []) + ['--test'] new_target_kwargs['rust_args'] = new_target_kwargs.get('rust_args', []) + ['--test']
new_target_kwargs['install'] = False new_target_kwargs['install'] = False
new_target_kwargs['dependencies'] = new_target_kwargs.get('dependencies', []) + kwargs['dependencies'] new_target_kwargs['dependencies'] = new_target_kwargs.get('dependencies', []) + kwargs['dependencies']
new_target_kwargs['link_with'] = new_target_kwargs.get('link_with', []) + kwargs['link_with']
sources = T.cast('T.List[SourceOutputs]', base_target.sources.copy()) sources = T.cast('T.List[SourceOutputs]', base_target.sources.copy())
sources.extend(base_target.generated) sources.extend(base_target.generated)

@ -0,0 +1,3 @@
pub fn subtract(a: i32, b: i32) -> i32 {
a - b
}

@ -41,3 +41,7 @@ rust.test('rust_test_from_static', lib, args: ['--skip', 'test_add_intentional_f
lib = shared_library('rust_shared', ['test.rs']) lib = shared_library('rust_shared', ['test.rs'])
rust.test('rust_test_from_shared', lib, args: ['--skip', 'test_add_intentional_fail']) rust.test('rust_test_from_shared', lib, args: ['--skip', 'test_add_intentional_fail'])
helper = static_library('helper', 'helper.rs')
lib = static_library('rust_link_with', 'test3.rs')
rust.test('rust_test_link_with', lib, link_with : helper)

@ -0,0 +1,16 @@
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
#[cfg(test)]
mod tests {
extern crate helper;
use super::*;
#[test]
fn test_add_sub() {
let x = helper::subtract(6, 5);
assert_eq!(add(x, 5), 6);
}
}
Loading…
Cancel
Save