Skip to content

Commit

Permalink
modernize Rust template
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Bonzini <[email protected]>
  • Loading branch information
bonzini authored and jpakkane committed Dec 9, 2024
1 parent 83253cd commit 8578a99
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
27 changes: 15 additions & 12 deletions mesonbuild/templates/rusttemplates.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,28 @@
pub fn {function_name}() -> i32 {{
return internal_function();
}}
'''
lib_rust_test_template = '''extern crate {crate_file};
#[cfg(test)]
mod tests {{
use super::*;
fn main() {{
println!("printing: {{}}", {crate_file}::{function_name}());
#[test]
fn test_function() {{
assert_eq!({function_name}(), 0);
}}
}}
'''


lib_rust_meson_template = '''project('{project_name}', 'rust',
version : '{version}',
default_options : ['warning_level=3'])
version : '{version}', meson_version: '>=1.3.0',
default_options : ['rust_std=2021', 'warning_level=3'])
rust = import('rust')
shlib = static_library('{lib_name}', '{source_file}', install : true)
test_exe = executable('{test_exe_name}', '{test_source_file}',
link_with : shlib)
test('{test_name}', test_exe)
rust.test('{test_name}', shlib)
# Make this library usable as a Meson subproject.
{ltoken}_dep = declare_dependency(
Expand All @@ -54,8 +57,8 @@
'''

hello_rust_meson_template = '''project('{project_name}', 'rust',
version : '{version}',
default_options : ['warning_level=3'])
version : '{version}', meson_version: '>=1.3.0',
default_options : ['rust_std=2021', 'warning_level=3'])
exe = executable('{exe_name}', '{source_name}',
install : true)
Expand All @@ -70,7 +73,7 @@ class RustProject(FileImpl):
exe_template = hello_rust_template
exe_meson_template = hello_rust_meson_template
lib_template = lib_rust_template
lib_test_template = lib_rust_test_template
lib_test_template = None
lib_meson_template = lib_rust_meson_template

def lib_kwargs(self) -> T.Dict[str, str]:
Expand Down
12 changes: 7 additions & 5 deletions mesonbuild/templates/sampleimpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def lib_template(self) -> str:
pass

@abc.abstractproperty
def lib_test_template(self) -> str:
def lib_test_template(self) -> T.Optional[str]:
pass

@abc.abstractproperty
Expand Down Expand Up @@ -85,8 +85,9 @@ def create_library(self) -> None:
}
with open(lib_name, 'w', encoding='utf-8') as f:
f.write(self.lib_template.format(**kwargs))
with open(test_name, 'w', encoding='utf-8') as f:
f.write(self.lib_test_template.format(**kwargs))
if self.lib_test_template:
with open(test_name, 'w', encoding='utf-8') as f:
f.write(self.lib_test_template.format(**kwargs))
with open('meson.build', 'w', encoding='utf-8') as f:
f.write(self.lib_meson_template.format(**kwargs))

Expand Down Expand Up @@ -132,8 +133,9 @@ def create_library(self) -> None:
kwargs = self.lib_kwargs()
with open(lib_name, 'w', encoding='utf-8') as f:
f.write(self.lib_template.format(**kwargs))
with open(test_name, 'w', encoding='utf-8') as f:
f.write(self.lib_test_template.format(**kwargs))
if self.lib_test_template:
with open(test_name, 'w', encoding='utf-8') as f:
f.write(self.lib_test_template.format(**kwargs))
with open('meson.build', 'w', encoding='utf-8') as f:
f.write(self.lib_meson_template.format(**kwargs))

Expand Down

0 comments on commit 8578a99

Please sign in to comment.