summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2024-11-20 14:42:29 +0100
committerDylan Baker <dylan@pnwbakers.com>2025-04-02 08:44:37 -0700
commit54118b6fbf5a508ee57d940d672ad2654a1cdf2c (patch)
tree656113a54ead2e0d2e2eb591d1d2ed131a9e2512
parentb13cd1051685d835179ad1396cbbeaba1fec4f96 (diff)
downloadmeson-54118b6fbf5a508ee57d940d672ad2654a1cdf2c.tar.gz
ninjabackend: generate command line for rust doctests
Adjust get_rust_compiler_args() to accept the crate-type externally, because rustdoc tests are an executable but are compiled with the parent target's --crate-type. Apart from that, the rustdoc arguments are very similar to the parent target, and are handled by the same functions that were split out of generate_rust_target. This concludes the backend implementation of doctests, only leaving the implementation of a doctest() function in the rust module. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
-rw-r--r--mesonbuild/backend/ninjabackend.py18
-rw-r--r--mesonbuild/compilers/rust.py11
2 files changed, 24 insertions, 5 deletions
diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py
index a3bc89c49..2faf8c1d3 100644
--- a/mesonbuild/backend/ninjabackend.py
+++ b/mesonbuild/backend/ninjabackend.py
@@ -45,6 +45,7 @@ if T.TYPE_CHECKING:
from ..linkers.linkers import DynamicLinker, StaticLinker
from ..compilers.cs import CsCompiler
from ..compilers.fortran import FortranCompiler
+ from ..compilers.rust import RustCompiler
from ..mesonlib import FileOrString
from .backends import TargetIntrospectionData
@@ -1980,13 +1981,13 @@ class NinjaBackend(backends.Backend):
return orderdeps, main_rust_file
- def get_rust_compiler_args(self, target: build.BuildTarget, rustc: Compiler,
+ def get_rust_compiler_args(self, target: build.BuildTarget, rustc: Compiler, src_crate_type: str,
depfile: T.Optional[str] = None) -> T.List[str]:
# Compiler args for compiling this target
args = compilers.get_base_compile_args(target, rustc, self.environment)
target_name = self.get_target_filename(target)
- args.extend(['--crate-type', target.rust_crate_type])
+ args.extend(['--crate-type', src_crate_type])
# If we're dynamically linking, add those arguments
if target.rust_crate_type in {'bin', 'dylib'}:
@@ -2122,7 +2123,7 @@ class NinjaBackend(backends.Backend):
return deps, project_deps, args
def generate_rust_target(self, target: build.BuildTarget) -> None:
- rustc = target.compilers['rust']
+ rustc = T.cast('RustCompiler', target.compilers['rust'])
self.generate_generator_list_rules(target)
for i in target.get_sources():
@@ -2141,7 +2142,7 @@ class NinjaBackend(backends.Backend):
args = rustc.compiler_args()
depfile = os.path.join(self.get_target_private_dir(target), target.name + '.d')
- args += self.get_rust_compiler_args(target, rustc, depfile)
+ args += self.get_rust_compiler_args(target, rustc, target.rust_crate_type, depfile)
deps, project_deps, deps_args = self.get_rust_compiler_deps_and_args(target, rustc)
args += deps_args
@@ -2171,6 +2172,15 @@ class NinjaBackend(backends.Backend):
self.generate_shsym(target)
self.create_target_source_introspection(target, rustc, args, [main_rust_file], [])
+ if target.doctests:
+ assert target.doctests.target is not None
+ rustdoc = rustc.get_rustdoc(self.environment)
+ args = rustdoc.get_exe_args()
+ args += self.get_rust_compiler_args(target.doctests.target, rustdoc, target.rust_crate_type)
+ _, _, deps_args = self.get_rust_compiler_deps_and_args(target.doctests.target, rustdoc)
+ args += deps_args
+ target.doctests.cmd_args = args.to_native() + [main_rust_file] + target.doctests.cmd_args
+
@staticmethod
def get_rule_suffix(for_machine: MachineChoice) -> str:
return PerMachine('_FOR_BUILD', '')[for_machine]
diff --git a/mesonbuild/compilers/rust.py b/mesonbuild/compilers/rust.py
index 17908860e..249ae1fc7 100644
--- a/mesonbuild/compilers/rust.py
+++ b/mesonbuild/compilers/rust.py
@@ -107,7 +107,7 @@ class RustCompiler(Compiler):
def needs_static_linker(self) -> bool:
return False
- def sanity_check(self, work_dir: str, environment: 'Environment') -> None:
+ def sanity_check(self, work_dir: str, environment: Environment) -> None:
source_name = os.path.join(work_dir, 'sanity.rs')
output_name = os.path.join(work_dir, 'rusttest')
cmdlist = self.exelist.copy()
@@ -333,6 +333,15 @@ class RustCompiler(Compiler):
args = self.linker.fatal_warnings() + args
return self.compiles('fn main { std::process::exit(0) };\n', env, extra_args=args, mode=CompileCheckMode.LINK)
+ @functools.lru_cache(maxsize=None)
+ def get_rustdoc(self, env: 'Environment') -> T.Optional[RustdocTestCompiler]:
+ exelist = self.get_rust_tool('rustdoc', env)
+ if not exelist:
+ return None
+
+ return RustdocTestCompiler(exelist, self.version, self.for_machine,
+ self.is_cross, self.info, linker=self.linker)
+
class ClippyRustCompiler(RustCompiler):
"""Clippy is a linter that wraps Rustc.