Skip to content

Commit

Permalink
Do not add --color to rustdoc if it doesn't support it.
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
kennytm committed Sep 12, 2018
1 parent 4779dbf commit 9597bce
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use core::profiles::{Lto, Profile};
use core::{PackageId, Target};
use util::errors::{CargoResult, CargoResultExt, Internal};
use util::paths;
use util::{self, machine_message, Freshness, ProcessBuilder};
use util::{self, machine_message, Freshness, ProcessBuilder, process};
use util::{internal, join_paths, profile};

use self::build_plan::BuildPlan;
Expand Down Expand Up @@ -583,7 +583,12 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
rustdoc.arg("--crate-name").arg(&unit.target.crate_name());
add_path_args(bcx, unit, &mut rustdoc);
add_cap_lints(bcx, unit, &mut rustdoc);
add_color(bcx, &mut rustdoc);

let mut can_add_color_process = process(&*bcx.config.rustdoc()?);
can_add_color_process.args(&["--color", "never", "-V"]);
if bcx.rustc.cached_success(&can_add_color_process)? {
add_color(bcx, &mut rustdoc);
}

if unit.kind != Kind::Host {
if let Some(ref target) = bcx.build_config.requested_target {
Expand Down
29 changes: 29 additions & 0 deletions src/cargo/util/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::{Path, PathBuf};
use std::hash::{Hash, Hasher, SipHasher};
use std::collections::hash_map::{Entry, HashMap};
use std::sync::Mutex;
use std::process::Stdio;
use std::env;

use serde_json;
Expand Down Expand Up @@ -83,6 +84,10 @@ impl Rustc {
pub fn cached_output(&self, cmd: &ProcessBuilder) -> CargoResult<(String, String)> {
self.cache.lock().unwrap().cached_output(cmd)
}

pub fn cached_success(&self, cmd: &ProcessBuilder) -> CargoResult<bool> {
self.cache.lock().unwrap().cached_success(cmd)
}
}

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

impl Cache {
Expand All @@ -113,6 +119,7 @@ impl Cache {
let empty = CacheData {
rustc_fingerprint,
outputs: HashMap::new(),
successes: HashMap::new(),
};
let mut dirty = true;
let data = match read(&cache_location) {
Expand Down Expand Up @@ -177,6 +184,28 @@ impl Cache {
}
}
}

fn cached_success(&mut self, cmd: &ProcessBuilder) -> CargoResult<bool> {
let key = process_fingerprint(cmd);
match self.data.successes.entry(key) {
Entry::Occupied(entry) => {
info!("rustc info cache hit");
Ok(*entry.get())
}
Entry::Vacant(entry) => {
info!("rustc info cache miss");
let success = cmd
.build_command()
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()?
.success();
entry.insert(success);
self.dirty = true;
Ok(success)
}
}
}
}

impl Drop for Cache {
Expand Down
12 changes: 6 additions & 6 deletions tests/testsuite/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn rustdoc_simple() {
.with_stderr(
"\
[DOCUMENTING] foo v0.0.1 ([CWD])
[RUNNING] `rustdoc --crate-name foo src/lib.rs --color never \
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
-o [CWD]/target/doc \
-L dependency=[CWD]/target/debug/deps`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
Expand All @@ -24,7 +24,7 @@ fn rustdoc_args() {
.with_stderr(
"\
[DOCUMENTING] foo v0.0.1 ([CWD])
[RUNNING] `rustdoc --crate-name foo src/lib.rs --color never \
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
-o [CWD]/target/doc \
--cfg=foo \
-L dependency=[CWD]/target/debug/deps`
Expand Down Expand Up @@ -61,7 +61,7 @@ fn rustdoc_foo_with_bar_dependency() {
[CHECKING] bar v0.0.1 ([..])
[RUNNING] `rustc [..]bar/src/lib.rs [..]`
[DOCUMENTING] foo v0.0.1 ([CWD])
[RUNNING] `rustdoc --crate-name foo src/lib.rs --color never \
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
-o [CWD]/target/doc \
--cfg=foo \
-L dependency=[CWD]/target/debug/deps \
Expand Down Expand Up @@ -97,7 +97,7 @@ fn rustdoc_only_bar_dependency() {
.with_stderr(
"\
[DOCUMENTING] bar v0.0.1 ([..])
[RUNNING] `rustdoc --crate-name bar [..]bar/src/lib.rs --color never \
[RUNNING] `rustdoc --crate-name bar [..]bar/src/lib.rs [..]\
-o [CWD]/target/doc \
--cfg=foo \
-L dependency=[CWD]/target/debug/deps`
Expand All @@ -117,7 +117,7 @@ fn rustdoc_same_name_documents_lib() {
.with_stderr(
"\
[DOCUMENTING] foo v0.0.1 ([..])
[RUNNING] `rustdoc --crate-name foo src/lib.rs --color never \
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
-o [CWD]/target/doc \
--cfg=foo \
-L dependency=[CWD]/target/debug/deps`
Expand Down Expand Up @@ -161,7 +161,7 @@ fn rustdoc_target() {
.with_stderr(
"\
[DOCUMENTING] foo v0.0.1 ([..])
[RUNNING] `rustdoc --crate-name foo src/lib.rs --color never \
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]\
--target x86_64-unknown-linux-gnu \
-o [CWD]/target/x86_64-unknown-linux-gnu/doc \
-L dependency=[CWD]/target/x86_64-unknown-linux-gnu/debug/deps \
Expand Down

0 comments on commit 9597bce

Please sign in to comment.