2020-03-26 16:12:17 -05:00
|
|
|
//! Handle process life-time and message passing for proc-macro client
|
|
|
|
|
2020-03-26 15:26:34 -05:00
|
|
|
use std::{
|
2020-03-28 05:12:51 -05:00
|
|
|
convert::{TryFrom, TryInto},
|
2020-04-20 15:57:55 -05:00
|
|
|
ffi::{OsStr, OsString},
|
2020-08-12 09:46:20 -05:00
|
|
|
io::{self, BufRead, BufReader, Write},
|
2021-07-08 09:40:14 -05:00
|
|
|
process::{Child, ChildStdin, ChildStdout, Command, Stdio},
|
2020-03-26 15:26:34 -05:00
|
|
|
};
|
|
|
|
|
2021-07-17 08:54:48 -05:00
|
|
|
use paths::{AbsPath, AbsPathBuf};
|
2021-02-01 13:24:09 -06:00
|
|
|
use stdx::JodChild;
|
2020-08-12 09:46:20 -05:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
msg::{ErrorCode, Message, Request, Response, ResponseError},
|
2020-12-07 06:55:41 -06:00
|
|
|
rpc::{ListMacrosResult, ListMacrosTask, ProcMacroKind},
|
2020-08-12 09:46:20 -05:00
|
|
|
};
|
|
|
|
|
2021-07-08 10:10:35 -05:00
|
|
|
#[derive(Debug)]
|
2020-03-26 15:26:34 -05:00
|
|
|
pub(crate) struct ProcMacroProcessSrv {
|
2021-07-12 08:19:53 -05:00
|
|
|
process: Process,
|
|
|
|
stdin: ChildStdin,
|
|
|
|
stdout: BufReader<ChildStdout>,
|
2020-03-26 15:26:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ProcMacroProcessSrv {
|
2020-11-02 06:13:32 -06:00
|
|
|
pub(crate) fn run(
|
2021-07-17 08:54:48 -05:00
|
|
|
process_path: AbsPathBuf,
|
2020-04-20 13:26:10 -05:00
|
|
|
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
|
2021-07-08 09:40:14 -05:00
|
|
|
) -> io::Result<ProcMacroProcessSrv> {
|
|
|
|
let mut process = Process::run(process_path, args)?;
|
|
|
|
let (stdin, stdout) = process.stdio().expect("couldn't access child stdio");
|
|
|
|
|
2021-07-12 08:19:53 -05:00
|
|
|
let srv = ProcMacroProcessSrv { process, stdin, stdout };
|
2021-07-08 09:40:14 -05:00
|
|
|
|
|
|
|
Ok(srv)
|
2020-03-26 15:26:34 -05:00
|
|
|
}
|
|
|
|
|
2020-11-02 06:13:32 -06:00
|
|
|
pub(crate) fn find_proc_macros(
|
2021-07-12 08:19:53 -05:00
|
|
|
&mut self,
|
2021-07-17 08:54:48 -05:00
|
|
|
dylib_path: &AbsPath,
|
2020-08-12 09:46:20 -05:00
|
|
|
) -> Result<Vec<(String, ProcMacroKind)>, tt::ExpansionError> {
|
2021-08-28 14:03:06 -05:00
|
|
|
let task = ListMacrosTask { lib: dylib_path.to_path_buf().into() };
|
2020-03-26 15:26:34 -05:00
|
|
|
|
2020-03-28 05:12:51 -05:00
|
|
|
let result: ListMacrosResult = self.send_task(Request::ListMacro(task))?;
|
2020-03-26 15:26:34 -05:00
|
|
|
Ok(result.macros)
|
|
|
|
}
|
|
|
|
|
2021-07-12 08:19:53 -05:00
|
|
|
pub(crate) fn send_task<R>(&mut self, req: Request) -> Result<R, tt::ExpansionError>
|
2020-03-26 15:26:34 -05:00
|
|
|
where
|
2020-03-28 05:12:51 -05:00
|
|
|
R: TryFrom<Response, Error = &'static str>,
|
2020-03-26 15:26:34 -05:00
|
|
|
{
|
2021-07-08 09:40:14 -05:00
|
|
|
let mut buf = String::new();
|
2021-07-12 08:19:53 -05:00
|
|
|
let res = match send_request(&mut self.stdin, &mut self.stdout, req, &mut buf) {
|
2021-07-08 09:40:14 -05:00
|
|
|
Ok(res) => res,
|
2021-01-01 01:09:27 -06:00
|
|
|
Err(err) => {
|
2021-07-12 08:19:53 -05:00
|
|
|
let result = self.process.child.try_wait();
|
2021-08-15 07:46:13 -05:00
|
|
|
tracing::error!(
|
2021-01-01 01:09:27 -06:00
|
|
|
"proc macro server crashed, server process state: {:?}, server request error: {:?}",
|
2021-07-12 08:19:53 -05:00
|
|
|
result,
|
2021-01-01 01:09:27 -06:00
|
|
|
err
|
2020-12-04 12:59:58 -06:00
|
|
|
);
|
2020-03-28 05:12:51 -05:00
|
|
|
let res = Response::Error(ResponseError {
|
|
|
|
code: ErrorCode::ServerErrorEnd,
|
2020-12-04 12:59:58 -06:00
|
|
|
message: "proc macro server crashed".into(),
|
2020-03-28 05:12:51 -05:00
|
|
|
});
|
2021-07-08 09:40:14 -05:00
|
|
|
Some(res)
|
2020-03-26 15:26:34 -05:00
|
|
|
}
|
2021-07-08 09:40:14 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Some(Response::Error(err)) => Err(tt::ExpansionError::ExpansionError(err.message)),
|
|
|
|
Some(res) => Ok(res.try_into().map_err(|err| {
|
|
|
|
tt::ExpansionError::Unknown(format!("Fail to get response, reason : {:#?} ", err))
|
|
|
|
})?),
|
|
|
|
None => Err(tt::ExpansionError::Unknown("Empty result".into())),
|
2020-03-26 15:26:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 09:40:14 -05:00
|
|
|
#[derive(Debug)]
|
2020-04-20 16:22:17 -05:00
|
|
|
struct Process {
|
2021-02-01 13:24:09 -06:00
|
|
|
child: JodChild,
|
2020-04-20 16:22:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Process {
|
|
|
|
fn run(
|
2021-07-17 08:54:48 -05:00
|
|
|
path: AbsPathBuf,
|
2020-04-20 16:22:17 -05:00
|
|
|
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
|
|
|
|
) -> io::Result<Process> {
|
2020-12-04 12:59:58 -06:00
|
|
|
let args: Vec<OsString> = args.into_iter().map(|s| s.as_ref().into()).collect();
|
2021-02-01 13:24:09 -06:00
|
|
|
let child = JodChild(mk_child(&path, &args)?);
|
2020-12-04 12:59:58 -06:00
|
|
|
Ok(Process { child })
|
2020-04-20 16:22:17 -05:00
|
|
|
}
|
|
|
|
|
2021-07-08 09:40:14 -05:00
|
|
|
fn stdio(&mut self) -> Option<(ChildStdin, BufReader<ChildStdout>)> {
|
2020-04-20 16:22:17 -05:00
|
|
|
let stdin = self.child.stdin.take()?;
|
|
|
|
let stdout = self.child.stdout.take()?;
|
|
|
|
let read = BufReader::new(stdout);
|
|
|
|
|
|
|
|
Some((stdin, read))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 08:54:48 -05:00
|
|
|
fn mk_child(
|
|
|
|
path: &AbsPath,
|
|
|
|
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
|
|
|
|
) -> io::Result<Child> {
|
|
|
|
Command::new(path.as_os_str())
|
2020-04-20 16:22:17 -05:00
|
|
|
.args(args)
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdout(Stdio::piped())
|
2020-04-22 17:57:02 -05:00
|
|
|
.stderr(Stdio::inherit())
|
2020-04-20 16:22:17 -05:00
|
|
|
.spawn()
|
|
|
|
}
|
|
|
|
|
2020-03-28 05:12:51 -05:00
|
|
|
fn send_request(
|
2020-03-26 15:26:34 -05:00
|
|
|
mut writer: &mut impl Write,
|
|
|
|
mut reader: &mut impl BufRead,
|
2020-03-28 05:12:51 -05:00
|
|
|
req: Request,
|
2021-03-23 14:47:08 -05:00
|
|
|
buf: &mut String,
|
2020-04-20 14:42:36 -05:00
|
|
|
) -> io::Result<Option<Response>> {
|
2020-03-28 05:12:51 -05:00
|
|
|
req.write(&mut writer)?;
|
2021-03-23 14:47:08 -05:00
|
|
|
Response::read(&mut reader, buf)
|
2020-03-26 15:26:34 -05:00
|
|
|
}
|