Skip to content

Commit 9597bce

Browse files
committed
Do not add --color to rustdoc if it doesn't support it.
We detect this by executing `rustdoc --color never -V` and see if the result is successful. To avoid repeatedly creating a new process, we cache the result into `.rustc_info.json`.
1 parent 4779dbf commit 9597bce

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use core::profiles::{Lto, Profile};
1313
use core::{PackageId, Target};
1414
use util::errors::{CargoResult, CargoResultExt, Internal};
1515
use util::paths;
16-
use util::{self, machine_message, Freshness, ProcessBuilder};
16+
use util::{self, machine_message, Freshness, ProcessBuilder, process};
1717
use util::{internal, join_paths, profile};
1818

1919
use self::build_plan::BuildPlan;
@@ -583,7 +583,12 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
583583
rustdoc.arg("--crate-name").arg(&unit.target.crate_name());
584584
add_path_args(bcx, unit, &mut rustdoc);
585585
add_cap_lints(bcx, unit, &mut rustdoc);
586-
add_color(bcx, &mut rustdoc);
586+
587+
let mut can_add_color_process = process(&*bcx.config.rustdoc()?);
588+
can_add_color_process.args(&["--color", "never", "-V"]);
589+
if bcx.rustc.cached_success(&can_add_color_process)? {
590+
add_color(bcx, &mut rustdoc);
591+
}
587592

588593
if unit.kind != Kind::Host {
589594
if let Some(ref target) = bcx.build_config.requested_target {

src/cargo/util/rustc.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::path::{Path, PathBuf};
44
use std::hash::{Hash, Hasher, SipHasher};
55
use std::collections::hash_map::{Entry, HashMap};
66
use std::sync::Mutex;
7+
use std::process::Stdio;
78
use std::env;
89

910
use serde_json;
@@ -83,6 +84,10 @@ impl Rustc {
8384
pub fn cached_output(&self, cmd: &ProcessBuilder) -> CargoResult<(String, String)> {
8485
self.cache.lock().unwrap().cached_output(cmd)
8586
}
87+
88+
pub fn cached_success(&self, cmd: &ProcessBuilder) -> CargoResult<bool> {
89+
self.cache.lock().unwrap().cached_success(cmd)
90+
}
8691
}
8792

8893
/// It is a well known that `rustc` is not the fastest compiler in the world.
@@ -104,6 +109,7 @@ struct Cache {
104109
struct CacheData {
105110
rustc_fingerprint: u64,
106111
outputs: HashMap<u64, (String, String)>,
112+
successes: HashMap<u64, bool>,
107113
}
108114

109115
impl Cache {
@@ -113,6 +119,7 @@ impl Cache {
113119
let empty = CacheData {
114120
rustc_fingerprint,
115121
outputs: HashMap::new(),
122+
successes: HashMap::new(),
116123
};
117124
let mut dirty = true;
118125
let data = match read(&cache_location) {
@@ -177,6 +184,28 @@ impl Cache {
177184
}
178185
}
179186
}
187+
188+
fn cached_success(&mut self, cmd: &ProcessBuilder) -> CargoResult<bool> {
189+
let key = process_fingerprint(cmd);
190+
match self.data.successes.entry(key) {
191+
Entry::Occupied(entry) => {
192+
info!("rustc info cache hit");
193+
Ok(*entry.get())
194+
}
195+
Entry::Vacant(entry) => {
196+
info!("rustc info cache miss");
197+
let success = cmd
198+
.build_command()
199+
.stdout(Stdio::null())
200+
.stderr(Stdio::null())
201+
.status()?
202+
.success();
203+
entry.insert(success);
204+
self.dirty = true;
205+
Ok(success)
206+
}
207+
}
208+
}
180209
}
181210

182211
impl Drop for Cache {

tests/testsuite/rustdoc.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn rustdoc_simple() {
88
.with_stderr(
99
"\
1010
[DOCUMENTING] foo v0.0.1 ([CWD])
11-
[RUNNING] `rustdoc --crate-name foo src/lib.rs --color never \
11+
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
1212
-o [CWD]/target/doc \
1313
-L dependency=[CWD]/target/debug/deps`
1414
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
@@ -24,7 +24,7 @@ fn rustdoc_args() {
2424
.with_stderr(
2525
"\
2626
[DOCUMENTING] foo v0.0.1 ([CWD])
27-
[RUNNING] `rustdoc --crate-name foo src/lib.rs --color never \
27+
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
2828
-o [CWD]/target/doc \
2929
--cfg=foo \
3030
-L dependency=[CWD]/target/debug/deps`
@@ -61,7 +61,7 @@ fn rustdoc_foo_with_bar_dependency() {
6161
[CHECKING] bar v0.0.1 ([..])
6262
[RUNNING] `rustc [..]bar/src/lib.rs [..]`
6363
[DOCUMENTING] foo v0.0.1 ([CWD])
64-
[RUNNING] `rustdoc --crate-name foo src/lib.rs --color never \
64+
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
6565
-o [CWD]/target/doc \
6666
--cfg=foo \
6767
-L dependency=[CWD]/target/debug/deps \
@@ -97,7 +97,7 @@ fn rustdoc_only_bar_dependency() {
9797
.with_stderr(
9898
"\
9999
[DOCUMENTING] bar v0.0.1 ([..])
100-
[RUNNING] `rustdoc --crate-name bar [..]bar/src/lib.rs --color never \
100+
[RUNNING] `rustdoc --crate-name bar [..]bar/src/lib.rs [..]\
101101
-o [CWD]/target/doc \
102102
--cfg=foo \
103103
-L dependency=[CWD]/target/debug/deps`
@@ -117,7 +117,7 @@ fn rustdoc_same_name_documents_lib() {
117117
.with_stderr(
118118
"\
119119
[DOCUMENTING] foo v0.0.1 ([..])
120-
[RUNNING] `rustdoc --crate-name foo src/lib.rs --color never \
120+
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
121121
-o [CWD]/target/doc \
122122
--cfg=foo \
123123
-L dependency=[CWD]/target/debug/deps`
@@ -161,7 +161,7 @@ fn rustdoc_target() {
161161
.with_stderr(
162162
"\
163163
[DOCUMENTING] foo v0.0.1 ([..])
164-
[RUNNING] `rustdoc --crate-name foo src/lib.rs --color never \
164+
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
165165
--target x86_64-unknown-linux-gnu \
166166
-o [CWD]/target/x86_64-unknown-linux-gnu/doc \
167167
-L dependency=[CWD]/target/x86_64-unknown-linux-gnu/debug/deps \

0 commit comments

Comments
 (0)