-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #12835 - fasterthanlime:sysroot-abi, r=fasterthanlime
Introduce proc-macro-srv/sysroot-abi Still a WIP. This change is tracked by: * #12818
- Loading branch information
Showing
14 changed files
with
1,053 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//! Determine rustc version `proc-macro-srv` (and thus the sysroot ABI) is | ||
//! build with and make it accessible at runtime for ABI selection. | ||
use std::{env, fs::File, io::Write, path::PathBuf, process::Command}; | ||
|
||
fn main() { | ||
let mut path = PathBuf::from(env::var_os("OUT_DIR").unwrap()); | ||
path.push("rustc_version.rs"); | ||
let mut f = File::create(&path).unwrap(); | ||
|
||
let rustc = env::var("RUSTC").expect("proc-macro-srv's build script expects RUSTC to be set"); | ||
let output = Command::new(rustc).arg("--version").output().expect("rustc --version must run"); | ||
let version_string = std::str::from_utf8(&output.stdout[..]) | ||
.expect("rustc --version output must be UTF-8") | ||
.trim(); | ||
|
||
write!( | ||
f, | ||
" | ||
#[allow(dead_code)] | ||
pub(crate) const RUSTC_VERSION_STRING: &str = {version_string:?}; | ||
" | ||
) | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
//! Proc macro ABI | ||
extern crate proc_macro; | ||
|
||
#[allow(dead_code)] | ||
#[doc(hidden)] | ||
mod ra_server; | ||
|
||
use libloading::Library; | ||
use proc_macro_api::ProcMacroKind; | ||
|
||
use super::PanicMessage; | ||
|
||
pub(crate) struct Abi { | ||
exported_macros: Vec<proc_macro::bridge::client::ProcMacro>, | ||
} | ||
|
||
impl From<proc_macro::bridge::PanicMessage> for PanicMessage { | ||
fn from(p: proc_macro::bridge::PanicMessage) -> Self { | ||
Self { message: p.as_str().map(|s| s.to_string()) } | ||
} | ||
} | ||
|
||
impl Abi { | ||
pub unsafe fn from_lib(lib: &Library, symbol_name: String) -> Result<Abi, libloading::Error> { | ||
let macros: libloading::Symbol<'_, &&[proc_macro::bridge::client::ProcMacro]> = | ||
lib.get(symbol_name.as_bytes())?; | ||
Ok(Self { exported_macros: macros.to_vec() }) | ||
} | ||
|
||
pub fn expand( | ||
&self, | ||
macro_name: &str, | ||
macro_body: &tt::Subtree, | ||
attributes: Option<&tt::Subtree>, | ||
) -> Result<tt::Subtree, PanicMessage> { | ||
let parsed_body = ra_server::TokenStream::with_subtree(macro_body.clone()); | ||
|
||
let parsed_attributes = attributes.map_or(ra_server::TokenStream::new(), |attr| { | ||
ra_server::TokenStream::with_subtree(attr.clone()) | ||
}); | ||
|
||
for proc_macro in &self.exported_macros { | ||
match proc_macro { | ||
proc_macro::bridge::client::ProcMacro::CustomDerive { | ||
trait_name, client, .. | ||
} if *trait_name == macro_name => { | ||
let res = client.run( | ||
&proc_macro::bridge::server::SameThread, | ||
ra_server::RustAnalyzer::default(), | ||
parsed_body, | ||
true, | ||
); | ||
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from); | ||
} | ||
proc_macro::bridge::client::ProcMacro::Bang { name, client } | ||
if *name == macro_name => | ||
{ | ||
let res = client.run( | ||
&proc_macro::bridge::server::SameThread, | ||
ra_server::RustAnalyzer::default(), | ||
parsed_body, | ||
true, | ||
); | ||
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from); | ||
} | ||
proc_macro::bridge::client::ProcMacro::Attr { name, client } | ||
if *name == macro_name => | ||
{ | ||
let res = client.run( | ||
&proc_macro::bridge::server::SameThread, | ||
ra_server::RustAnalyzer::default(), | ||
parsed_attributes, | ||
parsed_body, | ||
true, | ||
); | ||
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from); | ||
} | ||
_ => continue, | ||
} | ||
} | ||
|
||
Err(proc_macro::bridge::PanicMessage::String("Nothing to expand".to_string()).into()) | ||
} | ||
|
||
pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> { | ||
self.exported_macros | ||
.iter() | ||
.map(|proc_macro| match proc_macro { | ||
proc_macro::bridge::client::ProcMacro::CustomDerive { trait_name, .. } => { | ||
(trait_name.to_string(), ProcMacroKind::CustomDerive) | ||
} | ||
proc_macro::bridge::client::ProcMacro::Bang { name, .. } => { | ||
(name.to_string(), ProcMacroKind::FuncLike) | ||
} | ||
proc_macro::bridge::client::ProcMacro::Attr { name, .. } => { | ||
(name.to_string(), ProcMacroKind::Attr) | ||
} | ||
}) | ||
.collect() | ||
} | ||
} |
Oops, something went wrong.