Debian Codegraph @

mesonbuild/templates/rusttemplates.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2019 The Meson development team
3 # Copyright © 2023-2025 Intel Corporation
4
5 from __future__ import annotations
6
7 import typing as T
8
9 from mesonbuild.templates.sampleimpl import FileImpl
10
11 if T.TYPE_CHECKING:
12 from ..minit import Arguments
13
14
15 lib_rust_template = '''#![crate_name = "{crate_file}"]
16
17 /* This function will not be exported and is not
18 * directly callable by users of this library.
19 */
20 fn internal_function() -> i32 {{
21 return 0;
22 }}
23
24 pub fn {function_name}() -> i32 {{
25 return internal_function();
26 }}
27
28 #[cfg(test)]
29 mod tests {{
30 use super::*;
31
32 #[test]
33 fn test_function() {{
34 assert_eq!({function_name}(), 0);
35 }}
36 }}
37 '''
38
39
40 lib_rust_meson_template = '''project(
41 '{project_name}',
42 'rust',
43 version : '{version}',
44 meson_version : '>= {meson_version}',
45 default_options : ['rust_std=2021', 'warning_level=3'],
46 )
47
48 rust = import('rust')
49
50 dependencies = [{dependencies}
51 ]
52
53 sources = [{source_files}
54 ]
55
56 lib = static_library(
57 '{lib_name}',
58 sources,
59 dependencies : dependencies,
60 install : true,
61 )
62
63 rust.test('{test_name}', lib)
64
65 # Make this library usable as a Meson subproject.
66 {ltoken}_dep = declare_dependency(
67 include_directories : include_directories('.'),
68 dependencies : dependencies,
69 link_with : lib,
70 )
71 meson.override_dependency('{project_name}', {ltoken}_dep)
72 '''
73
74 hello_rust_template = '''
75 fn main() {{
76 let project_name = "{project_name}";
77 println!("This is project {{}}.\\n", project_name);
78 }}
79 '''
80
81 hello_rust_meson_template = '''project(
82 '{project_name}',
83 'rust',
84 version : '{version}',
85 meson_version : '>= {meson_version}',
86 default_options : ['rust_std=2021', 'warning_level=3'],
87 )
88
89 dependencies = [{dependencies}
90 ]
91
92 sources = [{source_files}
93 ]
94
95 exe = executable(
96 '{exe_name}',
97 sources,
98 dependencies : dependencies,
99 install : true,
100 )
101
102 test('basic', exe)
103 '''
104
105
106 class RustProject(FileImpl):
107
108 source_ext = 'rs'
109 exe_template = hello_rust_template
110 exe_meson_template = hello_rust_meson_template
111 lib_template = lib_rust_template
112 lib_test_template = None
113 lib_meson_template = lib_rust_meson_template
114
115 def __init__(self, args: Arguments):
116 super().__init__(args)
117 self.meson_version = '1.3.0'
118
119 def lib_kwargs(self) -> T.Dict[str, str]:
120 kwargs = super().lib_kwargs()
121 kwargs['crate_file'] = self.lowercase_token
122 return kwargs