From 3e24444aee9db36d8de530dee9c0283ce793c6fd Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 24 Apr 2020 10:23:01 +0800 Subject: Cacheproc-macro dlls --- crates/ra_proc_macro_srv/src/cli.rs | 8 ++-- crates/ra_proc_macro_srv/src/dylib.rs | 68 ++++++++++++++--------------- crates/ra_proc_macro_srv/src/lib.rs | 46 +++++++++++-------- crates/ra_proc_macro_srv/src/tests/utils.rs | 6 +-- 4 files changed, 70 insertions(+), 58 deletions(-) (limited to 'crates') diff --git a/crates/ra_proc_macro_srv/src/cli.rs b/crates/ra_proc_macro_srv/src/cli.rs index 7282e5b9b..1437794c9 100644 --- a/crates/ra_proc_macro_srv/src/cli.rs +++ b/crates/ra_proc_macro_srv/src/cli.rs @@ -1,15 +1,17 @@ //! Driver for proc macro server -use crate::{expand_task, list_macros}; +use crate::ProcMacroSrv; use ra_proc_macro::msg::{self, Message}; use std::io; pub fn run() -> io::Result<()> { + let mut srv = ProcMacroSrv::default(); + while let Some(req) = read_request()? { let res = match req { - msg::Request::ListMacro(task) => Ok(msg::Response::ListMacro(list_macros(&task))), + msg::Request::ListMacro(task) => srv.list_macros(&task).map(msg::Response::ListMacro), msg::Request::ExpansionMacro(task) => { - expand_task(&task).map(msg::Response::ExpansionMacro) + srv.expand(&task).map(msg::Response::ExpansionMacro) } }; diff --git a/crates/ra_proc_macro_srv/src/dylib.rs b/crates/ra_proc_macro_srv/src/dylib.rs index d202eb0fd..476bc5c01 100644 --- a/crates/ra_proc_macro_srv/src/dylib.rs +++ b/crates/ra_proc_macro_srv/src/dylib.rs @@ -112,7 +112,7 @@ impl ProcMacroLibraryLibloading { type ProcMacroLibraryImpl = ProcMacroLibraryLibloading; pub struct Expander { - libs: Vec, + inner: ProcMacroLibraryImpl, } impl Expander { @@ -125,7 +125,7 @@ impl Expander { let library = ProcMacroLibraryImpl::open(&lib).map_err(|e| e.to_string())?; - Ok(Expander { libs: vec![library] }) + Ok(Expander { inner: library }) } pub fn expand( @@ -141,38 +141,36 @@ impl Expander { TokenStream::with_subtree(attr.clone()) }); - for lib in &self.libs { - for proc_macro in &lib.exported_macros { - match proc_macro { - bridge::client::ProcMacro::CustomDerive { trait_name, client, .. } - if *trait_name == macro_name => - { - let res = client.run( - &crate::proc_macro::bridge::server::SameThread, - crate::rustc_server::Rustc::default(), - parsed_body, - ); - return res.map(|it| it.subtree); - } - bridge::client::ProcMacro::Bang { name, client } if *name == macro_name => { - let res = client.run( - &crate::proc_macro::bridge::server::SameThread, - crate::rustc_server::Rustc::default(), - parsed_body, - ); - return res.map(|it| it.subtree); - } - bridge::client::ProcMacro::Attr { name, client } if *name == macro_name => { - let res = client.run( - &crate::proc_macro::bridge::server::SameThread, - crate::rustc_server::Rustc::default(), - parsed_attributes, - parsed_body, - ); - return res.map(|it| it.subtree); - } - _ => continue, + for proc_macro in &self.inner.exported_macros { + match proc_macro { + bridge::client::ProcMacro::CustomDerive { trait_name, client, .. } + if *trait_name == macro_name => + { + let res = client.run( + &crate::proc_macro::bridge::server::SameThread, + crate::rustc_server::Rustc::default(), + parsed_body, + ); + return res.map(|it| it.subtree); + } + bridge::client::ProcMacro::Bang { name, client } if *name == macro_name => { + let res = client.run( + &crate::proc_macro::bridge::server::SameThread, + crate::rustc_server::Rustc::default(), + parsed_body, + ); + return res.map(|it| it.subtree); + } + bridge::client::ProcMacro::Attr { name, client } if *name == macro_name => { + let res = client.run( + &crate::proc_macro::bridge::server::SameThread, + crate::rustc_server::Rustc::default(), + parsed_attributes, + parsed_body, + ); + return res.map(|it| it.subtree); } + _ => continue, } } @@ -180,9 +178,9 @@ impl Expander { } pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> { - self.libs + self.inner + .exported_macros .iter() - .flat_map(|it| &it.exported_macros) .map(|proc_macro| match proc_macro { bridge::client::ProcMacro::CustomDerive { trait_name, .. } => { (trait_name.to_string(), ProcMacroKind::CustomDerive) diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs index 3aca859db..0954bd6b5 100644 --- a/crates/ra_proc_macro_srv/src/lib.rs +++ b/crates/ra_proc_macro_srv/src/lib.rs @@ -21,28 +21,40 @@ mod dylib; use proc_macro::bridge::client::TokenStream; use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask}; -use std::path::Path; - -pub(crate) fn expand_task(task: &ExpansionTask) -> Result { - let expander = create_expander(&task.lib); +use std::{ + collections::{hash_map::Entry, HashMap}, + path::{Path, PathBuf}, +}; + +#[derive(Default)] +pub(crate) struct ProcMacroSrv { + expanders: HashMap, +} - match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) { - Ok(expansion) => Ok(ExpansionResult { expansion }), - Err(msg) => { - Err(format!("Cannot perform expansion for {}: error {:?}", &task.macro_name, msg)) +impl ProcMacroSrv { + pub fn expand(&mut self, task: &ExpansionTask) -> Result { + let expander = self.expander(&task.lib)?; + match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) { + Ok(expansion) => Ok(ExpansionResult { expansion }), + Err(msg) => { + Err(format!("Cannot perform expansion for {}: error {:?}", &task.macro_name, msg)) + } } } -} - -pub(crate) fn list_macros(task: &ListMacrosTask) -> ListMacrosResult { - let expander = create_expander(&task.lib); - ListMacrosResult { macros: expander.list_macros() } -} + pub fn list_macros(&mut self, task: &ListMacrosTask) -> Result { + let expander = self.expander(&task.lib)?; + Ok(ListMacrosResult { macros: expander.list_macros() }) + } -fn create_expander(lib: &Path) -> dylib::Expander { - dylib::Expander::new(lib) - .unwrap_or_else(|err| panic!("Cannot create expander for {}: {:?}", lib.display(), err)) + fn expander(&mut self, path: &Path) -> Result<&dylib::Expander, String> { + Ok(match self.expanders.entry(path.to_path_buf()) { + 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(), + }) + } } pub mod cli; diff --git a/crates/ra_proc_macro_srv/src/tests/utils.rs b/crates/ra_proc_macro_srv/src/tests/utils.rs index 2139ec7a4..646a427c5 100644 --- a/crates/ra_proc_macro_srv/src/tests/utils.rs +++ b/crates/ra_proc_macro_srv/src/tests/utils.rs @@ -1,7 +1,7 @@ //! utils used in proc-macro tests use crate::dylib; -use crate::list_macros; +use crate::ProcMacroSrv; pub use difference::Changeset as __Changeset; use ra_proc_macro::ListMacrosTask; use std::str::FromStr; @@ -59,7 +59,7 @@ pub fn assert_expand( pub fn list(crate_name: &str, version: &str) -> Vec { let path = fixtures::dylib_path(crate_name, version); let task = ListMacrosTask { lib: path }; - - let res = list_macros(&task); + let mut srv = ProcMacroSrv::default(); + let res = srv.list_macros(&task).unwrap(); res.macros.into_iter().map(|(name, kind)| format!("{} [{:?}]", name, kind)).collect() } -- cgit v1.2.3 From 5a5bba5a4655be4ee7978f8577ce3351526655a5 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 25 Apr 2020 12:29:49 +0800 Subject: Copy dylib to temp directory --- crates/ra_proc_macro_srv/src/dylib.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/ra_proc_macro_srv/src/dylib.rs b/crates/ra_proc_macro_srv/src/dylib.rs index 476bc5c01..018cc7bb8 100644 --- a/crates/ra_proc_macro_srv/src/dylib.rs +++ b/crates/ra_proc_macro_srv/src/dylib.rs @@ -2,7 +2,7 @@ use crate::{proc_macro::bridge, rustc_server::TokenStream}; use std::fs::File; -use std::path::Path; +use std::path::{Path, PathBuf}; use goblin::{mach::Mach, Object}; use libloading::Library; @@ -123,6 +123,9 @@ impl Expander { .canonicalize() .unwrap_or_else(|err| panic!("Cannot canonicalize {}: {:?}", lib.display(), err)); + // Copy the dylib to temp directory to prevent locking in Windows + let lib = copy_to_temp_dir(&lib).map_err(|e| e.to_string())?; + let library = ProcMacroLibraryImpl::open(&lib).map_err(|e| e.to_string())?; Ok(Expander { inner: library }) @@ -195,3 +198,17 @@ impl Expander { .collect() } } + +fn copy_to_temp_dir(path: &Path) -> io::Result { + let mut to = std::env::temp_dir(); + let file_name = path.file_name().ok_or_else(|| { + io::Error::new( + io::ErrorKind::InvalidInput, + format!("File path is invalid: {}", path.display()), + ) + })?; + + to.push(file_name); + std::fs::copy(path, &to)?; + Ok(to) +} -- cgit v1.2.3 From 3bad5587c8cd17cd7dfae9796c8a01f47307214f Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 25 Apr 2020 12:30:28 +0800 Subject: Compare timestamp --- crates/ra_proc_macro_srv/src/lib.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs index 0954bd6b5..121f531fd 100644 --- a/crates/ra_proc_macro_srv/src/lib.rs +++ b/crates/ra_proc_macro_srv/src/lib.rs @@ -23,12 +23,14 @@ use proc_macro::bridge::client::TokenStream; use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask}; use std::{ collections::{hash_map::Entry, HashMap}, + fs::metadata, path::{Path, PathBuf}, + time::SystemTime, }; #[derive(Default)] pub(crate) struct ProcMacroSrv { - expanders: HashMap, + expanders: HashMap<(PathBuf, SystemTime), dylib::Expander>, } impl ProcMacroSrv { @@ -48,7 +50,11 @@ impl ProcMacroSrv { } fn expander(&mut self, path: &Path) -> Result<&dylib::Expander, String> { - Ok(match self.expanders.entry(path.to_path_buf()) { + let time = metadata(path) + .and_then(|it| it.modified()) + .map_err(|err| format!("Failed to file metadata for {}: {:?}", path.display(), err))?; + + Ok(match self.expanders.entry((path.to_path_buf(), time)) { Entry::Vacant(v) => v.insert(dylib::Expander::new(path).map_err(|err| { format!("Cannot create expander for {}: {:?}", path.display(), err) })?), -- cgit v1.2.3 From fb996cae6bf5bb5f60176a4a190311d4363b3e10 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 25 Apr 2020 12:48:59 +0800 Subject: Fix --- crates/ra_proc_macro_srv/src/dylib.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'crates') diff --git a/crates/ra_proc_macro_srv/src/dylib.rs b/crates/ra_proc_macro_srv/src/dylib.rs index 018cc7bb8..99c83481a 100644 --- a/crates/ra_proc_macro_srv/src/dylib.rs +++ b/crates/ra_proc_macro_srv/src/dylib.rs @@ -199,6 +199,7 @@ impl Expander { } } +#[cfg(windows)] fn copy_to_temp_dir(path: &Path) -> io::Result { let mut to = std::env::temp_dir(); let file_name = path.file_name().ok_or_else(|| { @@ -212,3 +213,8 @@ fn copy_to_temp_dir(path: &Path) -> io::Result { std::fs::copy(path, &to)?; Ok(to) } + +#[cfg(unix)] +fn copy_to_temp_dir(path: &Path) -> io::Result { + Ok(path.to_path_buf()) +} -- cgit v1.2.3 From 183673655f72250ee05ab1c5864feeae33a3c662 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 26 Apr 2020 17:58:56 +0800 Subject: Simpify code --- crates/ra_proc_macro_srv/src/dylib.rs | 20 ++++++++------------ crates/ra_proc_macro_srv/src/lib.rs | 8 ++++---- 2 files changed, 12 insertions(+), 16 deletions(-) (limited to 'crates') diff --git a/crates/ra_proc_macro_srv/src/dylib.rs b/crates/ra_proc_macro_srv/src/dylib.rs index 99c83481a..209f61493 100644 --- a/crates/ra_proc_macro_srv/src/dylib.rs +++ b/crates/ra_proc_macro_srv/src/dylib.rs @@ -109,24 +109,19 @@ impl ProcMacroLibraryLibloading { } } -type ProcMacroLibraryImpl = ProcMacroLibraryLibloading; - pub struct Expander { - inner: ProcMacroLibraryImpl, + inner: ProcMacroLibraryLibloading, } impl Expander { - pub fn new(lib: &Path) -> Result { + pub fn new(lib: &Path) -> io::Result { // Some libraries for dynamic loading require canonicalized path even when it is // already absolute - let lib = lib - .canonicalize() - .unwrap_or_else(|err| panic!("Cannot canonicalize {}: {:?}", lib.display(), err)); + let lib = lib.canonicalize()?; - // Copy the dylib to temp directory to prevent locking in Windows - let lib = copy_to_temp_dir(&lib).map_err(|e| e.to_string())?; + let lib = ensure_file_with_lock_free_access(&lib)?; - let library = ProcMacroLibraryImpl::open(&lib).map_err(|e| e.to_string())?; + let library = ProcMacroLibraryLibloading::open(&lib)?; Ok(Expander { inner: library }) } @@ -199,8 +194,9 @@ impl Expander { } } +/// Copy the dylib to temp directory to prevent locking in Windows #[cfg(windows)] -fn copy_to_temp_dir(path: &Path) -> io::Result { +fn ensure_file_with_lock_free_access(path: &Path) -> io::Result { let mut to = std::env::temp_dir(); let file_name = path.file_name().ok_or_else(|| { io::Error::new( @@ -215,6 +211,6 @@ fn copy_to_temp_dir(path: &Path) -> io::Result { } #[cfg(unix)] -fn copy_to_temp_dir(path: &Path) -> io::Result { +fn ensure_file_with_lock_free_access(path: &Path) -> io::Result { Ok(path.to_path_buf()) } diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs index 121f531fd..922bb84bb 100644 --- a/crates/ra_proc_macro_srv/src/lib.rs +++ b/crates/ra_proc_macro_srv/src/lib.rs @@ -23,7 +23,7 @@ use proc_macro::bridge::client::TokenStream; use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask}; use std::{ collections::{hash_map::Entry, HashMap}, - fs::metadata, + fs, path::{Path, PathBuf}, time::SystemTime, }; @@ -50,9 +50,9 @@ impl ProcMacroSrv { } fn expander(&mut self, path: &Path) -> Result<&dylib::Expander, String> { - let time = metadata(path) - .and_then(|it| it.modified()) - .map_err(|err| format!("Failed to file metadata for {}: {:?}", path.display(), err))?; + let time = fs::metadata(path).and_then(|it| it.modified()).map_err(|err| { + format!("Failed to get file metadata for {}: {:?}", path.display(), err) + })?; Ok(match self.expanders.entry((path.to_path_buf(), time)) { Entry::Vacant(v) => v.insert(dylib::Expander::new(path).map_err(|err| { -- cgit v1.2.3 From bfce6573772ebb91a9b1054864c0f53669ceee2f Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 26 Apr 2020 18:52:21 +0800 Subject: Generate uniq name --- crates/ra_proc_macro_srv/src/dylib.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'crates') diff --git a/crates/ra_proc_macro_srv/src/dylib.rs b/crates/ra_proc_macro_srv/src/dylib.rs index 209f61493..aa84e951c 100644 --- a/crates/ra_proc_macro_srv/src/dylib.rs +++ b/crates/ra_proc_macro_srv/src/dylib.rs @@ -8,7 +8,6 @@ use goblin::{mach::Mach, Object}; use libloading::Library; use memmap::Mmap; use ra_proc_macro::ProcMacroKind; - use std::io; const NEW_REGISTRAR_SYMBOL: &str = "_rustc_proc_macro_decls_"; @@ -197,7 +196,10 @@ impl Expander { /// Copy the dylib to temp directory to prevent locking in Windows #[cfg(windows)] fn ensure_file_with_lock_free_access(path: &Path) -> io::Result { + use std::{ffi::OsString, time::SystemTime}; + let mut to = std::env::temp_dir(); + let file_name = path.file_name().ok_or_else(|| { io::Error::new( io::ErrorKind::InvalidInput, @@ -205,8 +207,14 @@ fn ensure_file_with_lock_free_access(path: &Path) -> io::Result { ) })?; - to.push(file_name); - std::fs::copy(path, &to)?; + // generate a time deps unique number + let t = SystemTime::now().duration_since(std::time::UNIX_EPOCH).expect("Time went backwards"); + + let mut unique_name = OsString::from(t.as_millis().to_string()); + unique_name.push(file_name); + + to.push(unique_name); + std::fs::copy(path, &to).unwrap(); Ok(to) } -- cgit v1.2.3