diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index c6919840c7b..dbe854a09c6 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -25,6 +25,16 @@ pub enum ColorConfig { Never } +impl fmt::Display for ColorConfig { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + ColorConfig::Auto => "auto", + ColorConfig::Always => "always", + ColorConfig::Never => "never", + }.fmt(f) + } +} + #[derive(Clone, Copy)] pub struct ShellConfig { pub color_config: ColorConfig, @@ -129,6 +139,11 @@ impl MultiShell { pub fn get_verbose(&self) -> Verbosity { self.verbosity } + + pub fn color_config(&self) -> ColorConfig { + assert!(self.out.config.color_config == self.err.config.color_config); + self.out.config.color_config + } } impl Shell { diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index 9bf42a8513b..cc40730a84c 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -7,6 +7,7 @@ use std::sync::Arc; use core::{Package, PackageId, PackageSet, Target, Resolve}; use core::{Profile, Profiles}; +use core::shell::ColorConfig; use util::{self, CargoResult, human}; use util::{Config, internal, ChainError, profile, join_paths}; @@ -461,6 +462,11 @@ fn build_base_args(cx: &Context, cmd.arg(&root_path(cx, unit)); + let color_config = cx.config.shell().color_config(); + if color_config != ColorConfig::Auto { + cmd.arg("--color").arg(&color_config.to_string()); + } + cmd.arg("--crate-name").arg(&unit.target.crate_name()); for crate_type in crate_types.iter() { diff --git a/tests/build.rs b/tests/build.rs index a5ac1a63cf0..7c4876c7f56 100644 --- a/tests/build.rs +++ b/tests/build.rs @@ -2212,3 +2212,31 @@ fn panic_abort_compiles_with_panic_abort() { execs().with_status(0) .with_stderr_contains("[..] -C panic=abort [..]")); } + +#[test] +fn explicit_color_config_is_propagated_to_rustc() { + let mut p = project("foo"); + p = p + .file("Cargo.toml", r#" + [package] + + name = "test" + version = "0.0.0" + authors = [] + "#) + .file("src/lib.rs", ""); + + assert_that(p.cargo_process("build").arg("-v").arg("--color").arg("always"), + execs().with_status(0).with_stderr_contains( + "[..]rustc src[..]lib.rs --color always[..]")); + + assert_that(p.cargo_process("build").arg("-v").arg("--color").arg("never"), + execs().with_status(0).with_stderr("\ +[COMPILING] test v0.0.0 ([..]) +[RUNNING] `rustc src[..]lib.rs --color never --crate-name test --crate-type lib -g \ + --out-dir [..]target[..]debug \ + --emit=dep-info,link \ + -L dependency=[..]target[..]debug \ + -L dependency=[..]target[..]debug[..]deps` +")); +}