Add support for rust proc-macro crates

This commit is contained in:
Dylan Baker 2022-02-28 11:32:43 -08:00 committed by Eli Schwartz
parent 6ddba5c542
commit c08b6e29d9
6 changed files with 55 additions and 3 deletions

View File

@ -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)
```

View File

@ -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]:
"""

View File

@ -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\""
}
]
}

View File

@ -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)

View File

@ -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()
}

View File

@ -0,0 +1,8 @@
extern crate proc_macro_examples;
use proc_macro_examples::make_answer;
make_answer!();
fn main() {
assert_eq!(42, answer());
}