diff options
| author | Dylan Baker <dylan@pnwbakers.com> | 2025-07-03 15:38:32 -0700 |
|---|---|---|
| committer | Dylan Baker <dylan@pnwbakers.com> | 2025-10-06 09:03:07 -0700 |
| commit | 806289a5d27958a084bc6cba41b7cf9ccee4ecf4 (patch) | |
| tree | d6193d9aa67a6fb26e539d8b936f58aea19f2958 /mesonbuild/compilers/rust.py | |
| parent | be50d0e23737dc0fc5f074a291644d7fde39ef7b (diff) | |
| download | meson-806289a5d27958a084bc6cba41b7cf9ccee4ecf4.tar.gz | |
compilers: refactor sanity checking code
The goal is to reduce code duplication, and allow each language to
implement as little as possible to get good checking. The main
motivation is that half of the checks are fragile, as they add the work
directory to the paths of the generated files they want to use. This
works when run inside mesonmain because we always have an absolute build
directory, but when put into run_project_tests.py it doesn't work
because that gives a relative build directory.
Diffstat (limited to 'mesonbuild/compilers/rust.py')
| -rw-r--r-- | mesonbuild/compilers/rust.py | 63 |
1 files changed, 31 insertions, 32 deletions
diff --git a/mesonbuild/compilers/rust.py b/mesonbuild/compilers/rust.py index d0f92668a..07faba840 100644 --- a/mesonbuild/compilers/rust.py +++ b/mesonbuild/compilers/rust.py @@ -107,44 +107,43 @@ class RustCompiler(Compiler): def needs_static_linker(self) -> bool: return False - 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.exe') + def _sanity_check_compile_args(self, env: Environment, sourcename: str, binname: str) -> T.List[str]: cmdlist = self.exelist.copy() + assert self.linker is not None, 'for mypy' + if self.info.kernel == 'none' and 'ld.' in self.linker.id: + cmdlist.extend(['-C', 'link-arg=-nostartfiles']) + cmdlist.extend(self.get_output_args(binname)) + cmdlist.append(sourcename) + return cmdlist + + def _sanity_check_source_code(self) -> str: + if self.info.kernel != 'none': + return textwrap.dedent( + '''fn main() { + } + ''') + return textwrap.dedent( + '''#![no_std] + #![no_main] + #[no_mangle] + pub fn _start() { + } + #[panic_handler] + fn panic(_info: &core::panic::PanicInfo) -> ! { + loop {} + } + ''') - with open(source_name, 'w', encoding='utf-8') as ofile: - # If machine kernel is not `none`, try to compile a dummy program. - # If 'none', this is likely a `no-std`(i.e. bare metal) project. - if self.info.kernel != 'none': - ofile.write(textwrap.dedent( - '''fn main() { - } - ''')) - else: - # If rustc linker is gcc, add `-nostartfiles` - if 'ld.' in self.linker.id: - cmdlist.extend(['-C', 'link-arg=-nostartfiles']) - ofile.write(textwrap.dedent( - '''#![no_std] - #![no_main] - #[no_mangle] - pub fn _start() { - } - #[panic_handler] - fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} - } - ''')) - - cmdlist.extend(['-o', output_name, source_name]) - pc, stdo, stde = Popen_safe_logged(cmdlist, cwd=work_dir) - if pc.returncode != 0: - raise EnvironmentException(f'Rust compiler {self.name_string()} cannot compile programs.') + def sanity_check(self, work_dir: str, environment: Environment) -> None: + super().sanity_check(work_dir, environment) + source_name = self._sanity_check_filenames()[0] self._native_static_libs(work_dir, source_name) - self.run_sanity_check(environment, [output_name], work_dir) def _native_static_libs(self, work_dir: str, source_name: str) -> None: # Get libraries needed to link with a Rust staticlib + if self.native_static_libs: + return + cmdlist = self.exelist + ['--crate-type', 'staticlib', '--print', 'native-static-libs', source_name] p, stdo, stde = Popen_safe_logged(cmdlist, cwd=work_dir) if p.returncode != 0: |
