Add support for rust proc-macro crates

pull/10072/head
Dylan Baker 3 years ago committed by Eli Schwartz
parent 6ddba5c542
commit c08b6e29d9
  1. 16
      docs/markdown/snippets/rust_proc_macro_crates.md
  2. 6
      mesonbuild/build.py
  3. 2
      test cases/failing/54 wrong shared crate type/test.json
  4. 19
      test cases/rust/18 proc-macro/meson.build
  5. 7
      test cases/rust/18 proc-macro/proc.rs
  6. 8
      test cases/rust/18 proc-macro/use.rs

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

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

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

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

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

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