Skip to content

Commit

Permalink
Capture output from rustc and rustdoc.
Browse files Browse the repository at this point in the history
  • Loading branch information
kennytm committed Aug 3, 2018
1 parent f1c783b commit 641f7ff
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ core-foundation = { version = "0.6.0", features = ["mac_os_10_7_support"] }

[target.'cfg(windows)'.dependencies]
miow = "0.3.1"
fwdansi = "1"

[target.'cfg(windows)'.dependencies.winapi]
version = "0.3"
Expand Down
4 changes: 3 additions & 1 deletion src/cargo/core/compiler/job_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ impl<'a> JobQueue<'a> {
println!("{}", out);
}
Message::Stderr(err) => {
writeln!(cx.bcx.config.shell().err(), "{}", err)?;
let mut shell = cx.bcx.config.shell();
shell.print_ansi(err.as_bytes())?;
shell.err().write(b"\n")?;
}
Message::FixDiagnostic(msg) => {
print.print(&msg)?;
Expand Down
46 changes: 41 additions & 5 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ pub trait Executor: Send + Sync + 'static {
Ok(())
}

fn exec_and_capture_output(
&self,
cmd: ProcessBuilder,
id: &PackageId,
target: &Target,
mode: CompileMode,
_state: &job_queue::JobState<'_>,
) -> CargoResult<()> {
// we forward to exec() to keep RLS working.
self.exec(cmd, id, target, mode)
}

fn exec_json(
&self,
cmd: ProcessBuilder,
Expand All @@ -97,7 +109,18 @@ pub trait Executor: Send + Sync + 'static {
#[derive(Copy, Clone)]
pub struct DefaultExecutor;

impl Executor for DefaultExecutor {}
impl Executor for DefaultExecutor {
fn exec_and_capture_output(
&self,
cmd: ProcessBuilder,
_id: &PackageId,
_target: &Target,
_mode: CompileMode,
state: &job_queue::JobState<'_>,
) -> CargoResult<()> {
state.capture_output(cmd, false).map(drop)
}
}

fn compile<'a, 'cfg: 'a>(
cx: &mut Context<'a, 'cfg>,
Expand Down Expand Up @@ -216,6 +239,8 @@ fn rustc<'a, 'cfg>(
.unwrap_or_else(|| cx.bcx.config.cwd())
.to_path_buf();

let should_capture_output = cx.bcx.config.cli_unstable().compile_progress;

return Ok(Work::new(move |state| {
// Only at runtime have we discovered what the extra -L and -l
// arguments are for native libraries, so we process those here. We
Expand Down Expand Up @@ -291,7 +316,12 @@ fn rustc<'a, 'cfg>(
} else if build_plan {
state.build_plan(buildkey, rustc.clone(), outputs.clone());
} else {
exec.exec(rustc, &package_id, &target, mode)
let exec_result = if should_capture_output {
exec.exec_and_capture_output(rustc, &package_id, &target, mode, state)
} else {
exec.exec(rustc, &package_id, &target, mode)
};
exec_result
.map_err(Internal::new)
.chain_err(|| format!("Could not compile `{}`.", name))?;
}
Expand Down Expand Up @@ -613,6 +643,8 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
let build_state = cx.build_state.clone();
let key = (unit.pkg.package_id().clone(), unit.kind);

let should_capture_output = cx.bcx.config.cli_unstable().compile_progress;

Ok(Work::new(move |state| {
if let Some(output) = build_state.outputs.lock().unwrap().get(&key) {
for cfg in output.cfgs.iter() {
Expand All @@ -623,9 +655,13 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
}
}
state.running(&rustdoc);
rustdoc
.exec()
.chain_err(|| format!("Could not document `{}`.", name))?;

let exec_result = if should_capture_output {
state.capture_output(rustdoc, false).map(drop)
} else {
rustdoc.exec()
};
exec_result.chain_err(|| format!("Could not document `{}`.", name))?;
Ok(())
}))
}
Expand Down
13 changes: 13 additions & 0 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,19 @@ impl Shell {
ShellOut::Stream { stream, .. } => stream.supports_color(),
}
}

/// Prints a message and translates ANSI escape code into console colors.
pub fn print_ansi(&mut self, message: &[u8]) -> CargoResult<()> {
#[cfg(windows)]
{
if let ShellOut::Stream { stream, .. } = &mut self.err {
::fwdansi::write_ansi(stream, message)?;
return Ok(());
}
}
self.err().write_all(message)?;
Ok(())
}
}

impl Default for Shell {
Expand Down
2 changes: 2 additions & 0 deletions src/cargo/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ extern crate failure;
extern crate filetime;
extern crate flate2;
extern crate fs2;
#[cfg(windows)]
extern crate fwdansi;
extern crate git2;
extern crate glob;
extern crate hex;
Expand Down

0 comments on commit 641f7ff

Please sign in to comment.