2020-04-01 00:11:26 -05:00
|
|
|
//! RA Proc Macro Server
|
|
|
|
//!
|
|
|
|
//! This library is able to call compiled Rust custom derive dynamic libraries on arbitrary code.
|
2021-06-13 23:57:10 -05:00
|
|
|
//! The general idea here is based on <https://github.com/fedochet/rust-proc-macro-expander>.
|
2020-04-01 00:11:26 -05:00
|
|
|
//!
|
2020-04-20 13:26:10 -05:00
|
|
|
//! But we adapt it to better fit RA needs:
|
2020-04-01 00:11:26 -05:00
|
|
|
//!
|
2020-08-12 09:46:20 -05:00
|
|
|
//! * We use `tt` for proc-macro `TokenStream` server, it is easier to manipulate and interact with
|
2020-04-20 13:26:10 -05:00
|
|
|
//! RA than `proc-macro2` token stream.
|
2020-04-01 00:11:26 -05:00
|
|
|
//! * By **copying** the whole rustc `lib_proc_macro` code, we are able to build this with `stable`
|
2020-08-13 05:07:28 -05:00
|
|
|
//! rustc rather than `unstable`. (Although in general ABI compatibility is still an issue)…
|
2020-11-02 06:13:32 -06:00
|
|
|
#![allow(unreachable_pub)]
|
2020-04-01 00:11:26 -05:00
|
|
|
|
2020-04-04 03:09:36 -05:00
|
|
|
mod dylib;
|
2021-07-12 09:47:47 -05:00
|
|
|
mod abis;
|
|
|
|
|
2020-04-23 21:23:01 -05:00
|
|
|
use std::{
|
|
|
|
collections::{hash_map::Entry, HashMap},
|
2020-12-11 07:57:50 -06:00
|
|
|
env, fs,
|
2020-04-23 21:23:01 -05:00
|
|
|
path::{Path, PathBuf},
|
2020-04-24 23:30:28 -05:00
|
|
|
time::SystemTime,
|
2020-04-23 21:23:01 -05:00
|
|
|
};
|
|
|
|
|
internal: cleanup proc macro server error handlig
When dealing with proc macros, there are two very different kinds of
errors:
* first, usual errors of "proc macro panicked on this particular input"
* second, the proc macro server might day if the user, eg, kills it
First kind of errors are expected and are a normal output, while the
second kind are genuine IO-errors.
For this reason, we use a curious nested result here: `Result<Result<T,
E1>, E2>` pattern, which is 100% inspired by http://sled.rs/errors.html
2021-08-31 11:01:39 -05:00
|
|
|
use proc_macro_api::{
|
|
|
|
msg::{ExpandMacro, FlatTree, PanicMessage},
|
|
|
|
ProcMacroKind,
|
|
|
|
};
|
|
|
|
|
2020-04-23 21:23:01 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
pub(crate) struct ProcMacroSrv {
|
2020-04-24 23:30:28 -05:00
|
|
|
expanders: HashMap<(PathBuf, SystemTime), dylib::Expander>,
|
2020-04-23 21:23:01 -05:00
|
|
|
}
|
2020-04-04 03:10:45 -05:00
|
|
|
|
2020-04-23 21:23:01 -05:00
|
|
|
impl ProcMacroSrv {
|
internal: cleanup proc macro server error handlig
When dealing with proc macros, there are two very different kinds of
errors:
* first, usual errors of "proc macro panicked on this particular input"
* second, the proc macro server might day if the user, eg, kills it
First kind of errors are expected and are a normal output, while the
second kind are genuine IO-errors.
For this reason, we use a curious nested result here: `Result<Result<T,
E1>, E2>` pattern, which is 100% inspired by http://sled.rs/errors.html
2021-08-31 11:01:39 -05:00
|
|
|
pub fn expand(&mut self, task: ExpandMacro) -> Result<FlatTree, PanicMessage> {
|
|
|
|
let expander = self.expander(task.lib.as_ref()).map_err(|err| {
|
|
|
|
debug_assert!(false, "should list macros before asking to expand");
|
|
|
|
PanicMessage(format!("failed to load macro: {}", err))
|
|
|
|
})?;
|
2020-12-11 07:57:50 -06:00
|
|
|
|
|
|
|
let mut prev_env = HashMap::new();
|
|
|
|
for (k, v) in &task.env {
|
|
|
|
prev_env.insert(k.as_str(), env::var_os(k));
|
|
|
|
env::set_var(k, v);
|
|
|
|
}
|
2022-01-27 06:54:06 -06:00
|
|
|
let prev_working_dir = match task.current_dir {
|
|
|
|
Some(dir) => {
|
|
|
|
let prev_working_dir = std::env::current_dir().ok();
|
|
|
|
if let Err(err) = std::env::set_current_dir(&dir) {
|
|
|
|
eprintln!("Failed to set the current working dir to {}. Error: {:?}", dir, err)
|
|
|
|
}
|
|
|
|
prev_working_dir
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
};
|
2020-12-11 07:57:50 -06:00
|
|
|
|
2021-08-28 12:36:41 -05:00
|
|
|
let macro_body = task.macro_body.to_subtree();
|
|
|
|
let attributes = task.attributes.map(|it| it.to_subtree());
|
|
|
|
let result = expander
|
|
|
|
.expand(&task.macro_name, ¯o_body, attributes.as_ref())
|
|
|
|
.map(|it| FlatTree::new(&it));
|
2020-12-11 07:57:50 -06:00
|
|
|
|
|
|
|
for (k, _) in &task.env {
|
|
|
|
match &prev_env[k.as_str()] {
|
|
|
|
Some(v) => env::set_var(k, v),
|
|
|
|
None => env::remove_var(k),
|
|
|
|
}
|
|
|
|
}
|
2022-01-27 06:54:06 -06:00
|
|
|
if let Some(dir) = prev_working_dir {
|
|
|
|
if let Err(err) = std::env::set_current_dir(&dir) {
|
|
|
|
eprintln!(
|
|
|
|
"Failed to set the current working dir to {}. Error: {:?}",
|
|
|
|
dir.display(),
|
|
|
|
err
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2020-12-11 07:57:50 -06:00
|
|
|
|
internal: cleanup proc macro server error handlig
When dealing with proc macros, there are two very different kinds of
errors:
* first, usual errors of "proc macro panicked on this particular input"
* second, the proc macro server might day if the user, eg, kills it
First kind of errors are expected and are a normal output, while the
second kind are genuine IO-errors.
For this reason, we use a curious nested result here: `Result<Result<T,
E1>, E2>` pattern, which is 100% inspired by http://sled.rs/errors.html
2021-08-31 11:01:39 -05:00
|
|
|
result.map_err(PanicMessage)
|
2020-04-04 03:10:45 -05:00
|
|
|
}
|
|
|
|
|
internal: cleanup proc macro server error handlig
When dealing with proc macros, there are two very different kinds of
errors:
* first, usual errors of "proc macro panicked on this particular input"
* second, the proc macro server might day if the user, eg, kills it
First kind of errors are expected and are a normal output, while the
second kind are genuine IO-errors.
For this reason, we use a curious nested result here: `Result<Result<T,
E1>, E2>` pattern, which is 100% inspired by http://sled.rs/errors.html
2021-08-31 11:01:39 -05:00
|
|
|
pub(crate) fn list_macros(
|
|
|
|
&mut self,
|
|
|
|
dylib_path: &Path,
|
|
|
|
) -> Result<Vec<(String, ProcMacroKind)>, String> {
|
|
|
|
let expander = self.expander(dylib_path)?;
|
|
|
|
Ok(expander.list_macros())
|
2020-04-23 21:23:01 -05:00
|
|
|
}
|
2020-04-20 13:26:10 -05:00
|
|
|
|
2020-04-23 21:23:01 -05:00
|
|
|
fn expander(&mut self, path: &Path) -> Result<&dylib::Expander, String> {
|
2020-04-26 04:58:56 -05:00
|
|
|
let time = fs::metadata(path).and_then(|it| it.modified()).map_err(|err| {
|
|
|
|
format!("Failed to get file metadata for {}: {:?}", path.display(), err)
|
|
|
|
})?;
|
2020-04-24 23:30:28 -05:00
|
|
|
|
|
|
|
Ok(match self.expanders.entry((path.to_path_buf(), time)) {
|
2020-04-23 21:23:01 -05:00
|
|
|
Entry::Vacant(v) => v.insert(dylib::Expander::new(path).map_err(|err| {
|
|
|
|
format!("Cannot create expander for {}: {:?}", path.display(), err)
|
|
|
|
})?),
|
|
|
|
Entry::Occupied(e) => e.into_mut(),
|
|
|
|
})
|
|
|
|
}
|
2020-04-01 00:11:26 -05:00
|
|
|
}
|
2020-04-09 12:43:47 -05:00
|
|
|
|
2020-04-16 08:13:57 -05:00
|
|
|
pub mod cli;
|
|
|
|
|
2020-04-09 12:43:47 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|