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},
|
2020-03-26 15:26:34 -05:00
|
|
|
path::{Path, PathBuf},
|
|
|
|
process::{Child, Command, Stdio},
|
2020-03-31 08:51:43 -05:00
|
|
|
sync::{Arc, Weak},
|
2020-03-26 15:26:34 -05:00
|
|
|
};
|
|
|
|
|
2020-08-12 09:46:20 -05:00
|
|
|
use crossbeam_channel::{bounded, Receiver, Sender};
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2020-03-26 15:26:34 -05:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub(crate) struct ProcMacroProcessSrv {
|
2020-12-04 06:54:09 -06:00
|
|
|
inner: Weak<Sender<Task>>,
|
2020-03-26 15:26:34 -05:00
|
|
|
}
|
|
|
|
|
2020-03-26 23:55:58 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct ProcMacroProcessThread {
|
2020-03-28 07:52:45 -05:00
|
|
|
// XXX: drop order is significant
|
2020-03-31 08:51:43 -05:00
|
|
|
sender: Arc<Sender<Task>>,
|
2020-03-28 07:52:45 -05:00
|
|
|
handle: jod_thread::JoinHandle<()>,
|
|
|
|
}
|
|
|
|
|
2020-03-26 15:26:34 -05:00
|
|
|
impl ProcMacroProcessSrv {
|
2020-11-02 06:13:32 -06:00
|
|
|
pub(crate) fn run(
|
2020-04-20 13:26:10 -05:00
|
|
|
process_path: PathBuf,
|
|
|
|
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
|
|
|
|
) -> io::Result<(ProcMacroProcessThread, ProcMacroProcessSrv)> {
|
2020-04-16 08:13:57 -05:00
|
|
|
let process = Process::run(process_path, args)?;
|
2020-03-26 15:26:34 -05:00
|
|
|
|
|
|
|
let (task_tx, task_rx) = bounded(0);
|
2020-03-28 07:52:45 -05:00
|
|
|
let handle = jod_thread::spawn(move || {
|
2020-03-26 15:26:34 -05:00
|
|
|
client_loop(task_rx, process);
|
|
|
|
});
|
2020-03-26 23:55:58 -05:00
|
|
|
|
2020-03-31 08:51:43 -05:00
|
|
|
let task_tx = Arc::new(task_tx);
|
2020-12-04 06:54:09 -06:00
|
|
|
let srv = ProcMacroProcessSrv { inner: Arc::downgrade(&task_tx) };
|
2020-03-31 08:51:43 -05:00
|
|
|
let thread = ProcMacroProcessThread { handle, sender: task_tx };
|
2020-03-26 23:55:58 -05:00
|
|
|
|
|
|
|
Ok((thread, srv))
|
2020-03-26 15:26:34 -05:00
|
|
|
}
|
|
|
|
|
2020-11-02 06:13:32 -06:00
|
|
|
pub(crate) fn find_proc_macros(
|
2020-03-26 15:26:34 -05:00
|
|
|
&self,
|
|
|
|
dylib_path: &Path,
|
2020-08-12 09:46:20 -05:00
|
|
|
) -> Result<Vec<(String, ProcMacroKind)>, tt::ExpansionError> {
|
2020-03-26 15:26:34 -05:00
|
|
|
let task = ListMacrosTask { lib: dylib_path.to_path_buf() };
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-11-02 06:13:32 -06:00
|
|
|
pub(crate) fn send_task<R>(&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
|
|
|
{
|
|
|
|
let (result_tx, result_rx) = bounded(0);
|
2020-12-04 06:54:09 -06:00
|
|
|
let sender = match self.inner.upgrade() {
|
2020-12-04 12:59:58 -06:00
|
|
|
None => return Err(tt::ExpansionError::Unknown("proc macro process is closed".into())),
|
2020-03-31 08:51:43 -05:00
|
|
|
Some(it) => it,
|
|
|
|
};
|
2020-12-04 12:59:58 -06:00
|
|
|
sender
|
|
|
|
.send(Task { req, result_tx })
|
|
|
|
.map_err(|_| tt::ExpansionError::Unknown("proc macro server crashed".into()))?;
|
|
|
|
|
2020-03-31 09:01:34 -05:00
|
|
|
let res = result_rx
|
|
|
|
.recv()
|
2020-12-04 12:59:58 -06:00
|
|
|
.map_err(|_| tt::ExpansionError::Unknown("proc macro server crashed".into()))?;
|
2020-03-26 15:26:34 -05:00
|
|
|
|
2020-03-28 05:12:51 -05:00
|
|
|
match res {
|
2020-03-31 09:01:34 -05:00
|
|
|
Some(Response::Error(err)) => {
|
2020-08-12 09:46:20 -05:00
|
|
|
return Err(tt::ExpansionError::ExpansionError(err.message));
|
2020-03-26 15:26:34 -05:00
|
|
|
}
|
2020-03-31 09:01:34 -05:00
|
|
|
Some(res) => Ok(res.try_into().map_err(|err| {
|
2020-08-12 09:46:20 -05:00
|
|
|
tt::ExpansionError::Unknown(format!("Fail to get response, reason : {:#?} ", err))
|
2020-03-28 05:12:51 -05:00
|
|
|
})?),
|
2020-08-12 09:46:20 -05:00
|
|
|
None => Err(tt::ExpansionError::Unknown("Empty result".into())),
|
2020-03-26 15:26:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn client_loop(task_rx: Receiver<Task>, mut process: Process) {
|
2020-12-04 07:03:06 -06:00
|
|
|
let (mut stdin, mut stdout) = process.stdio().expect("couldn't access child stdio");
|
2020-03-26 15:26:34 -05:00
|
|
|
|
2020-12-04 06:57:33 -06:00
|
|
|
for Task { req, result_tx } in task_rx {
|
2020-03-31 09:01:34 -05:00
|
|
|
match send_request(&mut stdin, &mut stdout, req) {
|
|
|
|
Ok(res) => result_tx.send(res).unwrap(),
|
2020-03-26 15:26:34 -05:00
|
|
|
Err(_err) => {
|
2020-12-04 12:59:58 -06:00
|
|
|
log::error!(
|
|
|
|
"proc macro server crashed, server process state: {:?}",
|
|
|
|
process.child.try_wait()
|
|
|
|
);
|
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
|
|
|
});
|
2020-03-31 08:24:10 -05:00
|
|
|
result_tx.send(res.into()).unwrap();
|
2020-12-04 12:59:58 -06:00
|
|
|
// Exit the thread.
|
|
|
|
break;
|
2020-03-26 15:26:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 16:22:17 -05:00
|
|
|
struct Task {
|
|
|
|
req: Request,
|
|
|
|
result_tx: Sender<Option<Response>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Process {
|
|
|
|
child: Child,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Process {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let _ = self.child.kill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Process {
|
|
|
|
fn run(
|
|
|
|
path: PathBuf,
|
|
|
|
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();
|
2020-04-20 16:22:17 -05:00
|
|
|
let child = mk_child(&path, &args)?;
|
2020-12-04 12:59:58 -06:00
|
|
|
Ok(Process { child })
|
2020-04-20 16:22:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn stdio(&mut self) -> Option<(impl Write, impl BufRead)> {
|
|
|
|
let stdin = self.child.stdin.take()?;
|
|
|
|
let stdout = self.child.stdout.take()?;
|
|
|
|
let read = BufReader::new(stdout);
|
|
|
|
|
|
|
|
Some((stdin, read))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mk_child(path: &Path, args: impl IntoIterator<Item = impl AsRef<OsStr>>) -> io::Result<Child> {
|
|
|
|
Command::new(&path)
|
|
|
|
.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,
|
2020-04-20 14:42:36 -05:00
|
|
|
) -> io::Result<Option<Response>> {
|
2020-03-28 05:12:51 -05:00
|
|
|
req.write(&mut writer)?;
|
|
|
|
Ok(Response::read(&mut reader)?)
|
2020-03-26 15:26:34 -05:00
|
|
|
}
|