diff options
-rw-r--r-- | docs/markdown/Rust-module.md | 5 | ||||
-rw-r--r-- | docs/markdown/snippets/rust_test_link_with.md | 4 | ||||
-rw-r--r-- | mesonbuild/modules/rust.py | 13 | ||||
-rw-r--r-- | test cases/rust/9 unit tests/helper.rs | 3 | ||||
-rw-r--r-- | test cases/rust/9 unit tests/meson.build | 4 | ||||
-rw-r--r-- | test cases/rust/9 unit tests/test3.rs | 16 |
6 files changed, 41 insertions, 4 deletions
diff --git a/docs/markdown/Rust-module.md b/docs/markdown/Rust-module.md index 7617dbb..512c4ec 100644 --- a/docs/markdown/Rust-module.md +++ b/docs/markdown/Rust-module.md @@ -18,7 +18,7 @@ like Meson, rather than Meson work more like rust. ## 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 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 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) This function wraps bindgen to simplify creating rust bindings around C diff --git a/docs/markdown/snippets/rust_test_link_with.md b/docs/markdown/snippets/rust_test_link_with.md new file mode 100644 index 0000000..9c2b7d6 --- /dev/null +++ b/docs/markdown/snippets/rust_test_link_with.md @@ -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. diff --git a/mesonbuild/modules/rust.py b/mesonbuild/modules/rust.py index bf1fd1d..4153630 100644 --- a/mesonbuild/modules/rust.py +++ b/mesonbuild/modules/rust.py @@ -1,4 +1,4 @@ -# Copyright © 2020-2022 Intel Corporation +# Copyright © 2020-2023 Intel Corporation # Licensed under the Apache License, Version 2.0 (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 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 ..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 ..mesonlib import File if T.TYPE_CHECKING: from . import ModuleState + from ..build import LibTypes from ..interpreter import Interpreter from ..interpreter import kwargs as _kwargs from ..interpreter.interpreter import SourceInputs, SourceOutputs @@ -38,6 +39,7 @@ if T.TYPE_CHECKING: dependencies: T.List[T.Union[Dependency, ExternalLibrary]] is_parallel: bool + link_with: T.List[LibTypes] class FuncBindgen(TypedDict): @@ -68,6 +70,7 @@ class RustModule(ExtensionModule): 'rust.test', *TEST_KWS, DEPENDENCIES_KW, + LINK_WITH_KW.evolve(since='1.2.0'), KwargInfo('is_parallel', bool, default=False), ) 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) ``` """ + if any(isinstance(t, Jar) for t in kwargs.get('link_with', [])): + raise InvalidArguments('Rust tests cannot link with Jar targets') + name = args[0] base_target: BuildTarget = args[1] 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['install'] = False 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.extend(base_target.generated) diff --git a/test cases/rust/9 unit tests/helper.rs b/test cases/rust/9 unit tests/helper.rs new file mode 100644 index 0000000..afe3233 --- /dev/null +++ b/test cases/rust/9 unit tests/helper.rs @@ -0,0 +1,3 @@ +pub fn subtract(a: i32, b: i32) -> i32 { + a - b +} diff --git a/test cases/rust/9 unit tests/meson.build b/test cases/rust/9 unit tests/meson.build index b649abb..9096014 100644 --- a/test cases/rust/9 unit tests/meson.build +++ b/test cases/rust/9 unit tests/meson.build @@ -41,3 +41,7 @@ rust.test('rust_test_from_static', lib, args: ['--skip', 'test_add_intentional_f lib = shared_library('rust_shared', ['test.rs']) 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) diff --git a/test cases/rust/9 unit tests/test3.rs b/test cases/rust/9 unit tests/test3.rs new file mode 100644 index 0000000..ccb5f98 --- /dev/null +++ b/test cases/rust/9 unit tests/test3.rs @@ -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); + } +} |