aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/markdown/snippets/rust_proc_macro_crates.md16
-rw-r--r--mesonbuild/build.py6
-rw-r--r--test cases/failing/54 wrong shared crate type/test.json2
-rw-r--r--test cases/rust/18 proc-macro/meson.build19
-rw-r--r--test cases/rust/18 proc-macro/proc.rs7
-rw-r--r--test cases/rust/18 proc-macro/use.rs8
6 files changed, 55 insertions, 3 deletions
diff --git a/docs/markdown/snippets/rust_proc_macro_crates.md b/docs/markdown/snippets/rust_proc_macro_crates.md
new file mode 100644
index 0000000..780a5b3
--- /dev/null
+++ b/docs/markdown/snippets/rust_proc_macro_crates.md
@@ -0,0 +1,16 @@
+## Rust proc-macro crates
+
+Rust has these handy things called proc-macro crates, which are a bit like a
+compiler plugin. We can now support them, simply build a [[shared_library]] with
+the `rust_crate_type` set to `proc-macro`.
+
+```meson
+proc = shared_library(
+ 'proc',
+ 'proc.rs',
+ rust_crate_type : 'proc-macro',
+ install : false,
+)
+
+user = executable('user, 'user.rs', link_with : proc)
+```
diff --git a/mesonbuild/build.py b/mesonbuild/build.py
index 6916bcb..0f792df 100644
--- a/mesonbuild/build.py
+++ b/mesonbuild/build.py
@@ -1982,8 +1982,8 @@ class SharedLibrary(BuildTarget):
mlog.debug('Defaulting Rust dynamic library target crate type to "dylib"')
self.rust_crate_type = 'dylib'
# Don't let configuration proceed with a non-dynamic crate type
- elif self.rust_crate_type not in ['dylib', 'cdylib']:
- raise InvalidArguments(f'Crate type "{self.rust_crate_type}" invalid for dynamic libraries; must be "dylib" or "cdylib"')
+ elif self.rust_crate_type not in ['dylib', 'cdylib', 'proc-macro']:
+ raise InvalidArguments(f'Crate type "{self.rust_crate_type}" invalid for dynamic libraries; must be "dylib", "cdylib", or "proc-macro"')
if not hasattr(self, 'prefix'):
self.prefix = None
if not hasattr(self, 'suffix'):
@@ -2215,6 +2215,8 @@ class SharedLibrary(BuildTarget):
self.rust_crate_type = rust_crate_type
else:
raise InvalidArguments(f'Invalid rust_crate_type "{rust_crate_type}": must be a string.')
+ if rust_crate_type == 'proc-macro':
+ FeatureNew.single_use('Rust crate type "proc-macro"', '0.62.0', self.subproject)
def get_import_filename(self) -> T.Optional[str]:
"""
diff --git a/test cases/failing/54 wrong shared crate type/test.json b/test cases/failing/54 wrong shared crate type/test.json
index 5cced6f..a5b7961 100644
--- a/test cases/failing/54 wrong shared crate type/test.json
+++ b/test cases/failing/54 wrong shared crate type/test.json
@@ -1,7 +1,7 @@
{
"stdout": [
{
- "line": "test cases/failing/54 wrong shared crate type/meson.build:7:0: ERROR: Crate type \"staticlib\" invalid for dynamic libraries; must be \"dylib\" or \"cdylib\""
+ "line": "test cases/failing/54 wrong shared crate type/meson.build:7:0: ERROR: Crate type \"staticlib\" invalid for dynamic libraries; must be \"dylib\", \"cdylib\", or \"proc-macro\""
}
]
}
diff --git a/test cases/rust/18 proc-macro/meson.build b/test cases/rust/18 proc-macro/meson.build
new file mode 100644
index 0000000..01c4cbe
--- /dev/null
+++ b/test cases/rust/18 proc-macro/meson.build
@@ -0,0 +1,19 @@
+project('rust proc-macro', 'rust')
+
+if build_machine.system() != 'linux'
+ error('MESON_SKIP_TEST, this test only works on Linux. Patches welcome.')
+endif
+
+pm = shared_library(
+ 'proc_macro_examples',
+ 'proc.rs',
+ rust_crate_type : 'proc-macro',
+)
+
+main = executable(
+ 'main',
+ 'use.rs',
+ link_with : pm
+)
+
+test('main_test', main)
diff --git a/test cases/rust/18 proc-macro/proc.rs b/test cases/rust/18 proc-macro/proc.rs
new file mode 100644
index 0000000..53935e4
--- /dev/null
+++ b/test cases/rust/18 proc-macro/proc.rs
@@ -0,0 +1,7 @@
+extern crate proc_macro;
+use proc_macro::TokenStream;
+
+#[proc_macro]
+pub fn make_answer(_item: TokenStream) -> TokenStream {
+ "fn answer() -> u32 { 42 }".parse().unwrap()
+}
diff --git a/test cases/rust/18 proc-macro/use.rs b/test cases/rust/18 proc-macro/use.rs
new file mode 100644
index 0000000..0b6342b
--- /dev/null
+++ b/test cases/rust/18 proc-macro/use.rs
@@ -0,0 +1,8 @@
+extern crate proc_macro_examples;
+use proc_macro_examples::make_answer;
+
+make_answer!();
+
+fn main() {
+ assert_eq!(42, answer());
+}