Skip to content

Commit 641f7ff

Browse files
committed
Capture output from rustc and rustdoc.
1 parent f1c783b commit 641f7ff

File tree

5 files changed

+60
-6
lines changed

5 files changed

+60
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ core-foundation = { version = "0.6.0", features = ["mac_os_10_7_support"] }
6565

6666
[target.'cfg(windows)'.dependencies]
6767
miow = "0.3.1"
68+
fwdansi = "1"
6869

6970
[target.'cfg(windows)'.dependencies.winapi]
7071
version = "0.3"

src/cargo/core/compiler/job_queue.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,9 @@ impl<'a> JobQueue<'a> {
306306
println!("{}", out);
307307
}
308308
Message::Stderr(err) => {
309-
writeln!(cx.bcx.config.shell().err(), "{}", err)?;
309+
let mut shell = cx.bcx.config.shell();
310+
shell.print_ansi(err.as_bytes())?;
311+
shell.err().write(b"\n")?;
310312
}
311313
Message::FixDiagnostic(msg) => {
312314
print.print(&msg)?;

src/cargo/core/compiler/mod.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ pub trait Executor: Send + Sync + 'static {
7272
Ok(())
7373
}
7474

75+
fn exec_and_capture_output(
76+
&self,
77+
cmd: ProcessBuilder,
78+
id: &PackageId,
79+
target: &Target,
80+
mode: CompileMode,
81+
_state: &job_queue::JobState<'_>,
82+
) -> CargoResult<()> {
83+
// we forward to exec() to keep RLS working.
84+
self.exec(cmd, id, target, mode)
85+
}
86+
7587
fn exec_json(
7688
&self,
7789
cmd: ProcessBuilder,
@@ -97,7 +109,18 @@ pub trait Executor: Send + Sync + 'static {
97109
#[derive(Copy, Clone)]
98110
pub struct DefaultExecutor;
99111

100-
impl Executor for DefaultExecutor {}
112+
impl Executor for DefaultExecutor {
113+
fn exec_and_capture_output(
114+
&self,
115+
cmd: ProcessBuilder,
116+
_id: &PackageId,
117+
_target: &Target,
118+
_mode: CompileMode,
119+
state: &job_queue::JobState<'_>,
120+
) -> CargoResult<()> {
121+
state.capture_output(cmd, false).map(drop)
122+
}
123+
}
101124

102125
fn compile<'a, 'cfg: 'a>(
103126
cx: &mut Context<'a, 'cfg>,
@@ -216,6 +239,8 @@ fn rustc<'a, 'cfg>(
216239
.unwrap_or_else(|| cx.bcx.config.cwd())
217240
.to_path_buf();
218241

242+
let should_capture_output = cx.bcx.config.cli_unstable().compile_progress;
243+
219244
return Ok(Work::new(move |state| {
220245
// Only at runtime have we discovered what the extra -L and -l
221246
// arguments are for native libraries, so we process those here. We
@@ -291,7 +316,12 @@ fn rustc<'a, 'cfg>(
291316
} else if build_plan {
292317
state.build_plan(buildkey, rustc.clone(), outputs.clone());
293318
} else {
294-
exec.exec(rustc, &package_id, &target, mode)
319+
let exec_result = if should_capture_output {
320+
exec.exec_and_capture_output(rustc, &package_id, &target, mode, state)
321+
} else {
322+
exec.exec(rustc, &package_id, &target, mode)
323+
};
324+
exec_result
295325
.map_err(Internal::new)
296326
.chain_err(|| format!("Could not compile `{}`.", name))?;
297327
}
@@ -613,6 +643,8 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
613643
let build_state = cx.build_state.clone();
614644
let key = (unit.pkg.package_id().clone(), unit.kind);
615645

646+
let should_capture_output = cx.bcx.config.cli_unstable().compile_progress;
647+
616648
Ok(Work::new(move |state| {
617649
if let Some(output) = build_state.outputs.lock().unwrap().get(&key) {
618650
for cfg in output.cfgs.iter() {
@@ -623,9 +655,13 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
623655
}
624656
}
625657
state.running(&rustdoc);
626-
rustdoc
627-
.exec()
628-
.chain_err(|| format!("Could not document `{}`.", name))?;
658+
659+
let exec_result = if should_capture_output {
660+
state.capture_output(rustdoc, false).map(drop)
661+
} else {
662+
rustdoc.exec()
663+
};
664+
exec_result.chain_err(|| format!("Could not document `{}`.", name))?;
629665
Ok(())
630666
}))
631667
}

src/cargo/core/shell.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,19 @@ impl Shell {
239239
ShellOut::Stream { stream, .. } => stream.supports_color(),
240240
}
241241
}
242+
243+
/// Prints a message and translates ANSI escape code into console colors.
244+
pub fn print_ansi(&mut self, message: &[u8]) -> CargoResult<()> {
245+
#[cfg(windows)]
246+
{
247+
if let ShellOut::Stream { stream, .. } = &mut self.err {
248+
::fwdansi::write_ansi(stream, message)?;
249+
return Ok(());
250+
}
251+
}
252+
self.err().write_all(message)?;
253+
Ok(())
254+
}
242255
}
243256

244257
impl Default for Shell {

src/cargo/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ extern crate failure;
2626
extern crate filetime;
2727
extern crate flate2;
2828
extern crate fs2;
29+
#[cfg(windows)]
30+
extern crate fwdansi;
2931
extern crate git2;
3032
extern crate glob;
3133
extern crate hex;

0 commit comments

Comments
 (0)