diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index bf458bcbf5c..964450e98e0 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -160,11 +160,15 @@ impl<'a, 'cfg> Context<'a, 'cfg> { crate_types: &BTreeSet, kind: Kind) -> CargoResult<()> { + let rustflags = try!(env_args(self.config, + &self.build_config, + kind, + "RUSTFLAGS")); let mut process = util::process(self.config.rustc()); process.arg("-") .arg("--crate-name").arg("_") .arg("--print=file-names") - .args(&try!(rustflags_args(self.config, &self.build_config, kind))) + .args(&rustflags) .env_remove("RUST_LOG"); for crate_type in crate_types { @@ -644,7 +648,11 @@ impl<'a, 'cfg> Context<'a, 'cfg> { } pub fn rustflags_args(&self, unit: &Unit) -> CargoResult> { - rustflags_args(self.config, &self.build_config, unit.kind) + env_args(self.config, &self.build_config, unit.kind, "RUSTFLAGS") + } + + pub fn rustdocflags_args(&self, unit: &Unit) -> CargoResult> { + env_args(self.config, &self.build_config, unit.kind, "RUSTDOCFLAGS") } pub fn show_warnings(&self, pkg: &PackageId) -> bool { @@ -655,9 +663,10 @@ impl<'a, 'cfg> Context<'a, 'cfg> { // Acquire extra flags to pass to the compiler from the // RUSTFLAGS environment variable and similar config values -fn rustflags_args(config: &Config, - build_config: &BuildConfig, - kind: Kind) -> CargoResult> { +fn env_args(config: &Config, + build_config: &BuildConfig, + kind: Kind, + name: &str) -> CargoResult> { // We *want* to apply RUSTFLAGS only to builds for the // requested target architecture, and not to things like build // scripts and plugins, which may be for an entirely different @@ -688,7 +697,7 @@ fn rustflags_args(config: &Config, } // First try RUSTFLAGS from the environment - if let Some(a) = env::var("RUSTFLAGS").ok() { + if let Some(a) = env::var(name).ok() { let args = a.split(" ") .map(str::trim) .filter(|s| !s.is_empty()) @@ -697,7 +706,9 @@ fn rustflags_args(config: &Config, } // Then the build.rustflags value - if let Some(args) = try!(config.get_list("build.rustflags")) { + let name = name.chars().flat_map(|c| c.to_lowercase()).collect::(); + let key = format!("build.{}", name); + if let Some(args) = try!(config.get_list(&key)) { let args = args.val.into_iter().map(|a| a.0); return Ok(args.collect()); } diff --git a/src/cargo/ops/cargo_rustc/fingerprint.rs b/src/cargo/ops/cargo_rustc/fingerprint.rs index 437474c498e..449e6966713 100644 --- a/src/cargo/ops/cargo_rustc/fingerprint.rs +++ b/src/cargo/ops/cargo_rustc/fingerprint.rs @@ -346,6 +346,11 @@ fn calculate<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) }; let mut deps = deps; deps.sort_by(|&(ref a, _), &(ref b, _)| a.cmp(b)); + let extra_flags = if unit.profile.doc { + try!(cx.rustdocflags_args(unit)) + } else { + try!(cx.rustflags_args(unit)) + }; let fingerprint = Arc::new(Fingerprint { rustc: util::hash_u64(&cx.config.rustc_info().verbose_version), target: util::hash_u64(&unit.target), @@ -354,7 +359,7 @@ fn calculate<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) deps: deps, local: local, memoized_hash: Mutex::new(None), - rustflags: try!(cx.rustflags_args(unit)), + rustflags: extra_flags, }); cx.fingerprints.insert(*unit, fingerprint.clone()); Ok(fingerprint) diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index 0c23b277bc9..705b03fae16 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -246,7 +246,7 @@ fn rustc(cx: &mut Context, unit: &Unit) -> CargoResult { let dep_info_loc = fingerprint::dep_info_loc(cx, unit); let cwd = cx.config.cwd().to_path_buf(); - let rustflags = try!(cx.rustflags_args(unit)); + rustc.args(&try!(cx.rustflags_args(unit))); return Ok(Work::new(move |state| { // Only at runtime have we discovered what the extra -L and -l @@ -270,9 +270,6 @@ fn rustc(cx: &mut Context, unit: &Unit) -> CargoResult { } } - // Add the arguments from RUSTFLAGS - rustc.args(&rustflags); - state.running(&rustc); try!(exec_engine.exec(rustc).chain_error(|| { human(format!("Could not compile `{}`.", name)) @@ -406,6 +403,8 @@ fn rustdoc(cx: &mut Context, unit: &Unit) -> CargoResult { .build_out(unit.pkg)); } + rustdoc.args(&try!(cx.rustdocflags_args(unit))); + let name = unit.pkg.name().to_string(); let build_state = cx.build_state.clone(); let key = (unit.pkg.package_id().clone(), unit.kind); diff --git a/tests/rustdocflags.rs b/tests/rustdocflags.rs new file mode 100644 index 00000000000..aa91471a6d8 --- /dev/null +++ b/tests/rustdocflags.rs @@ -0,0 +1,85 @@ +extern crate cargotest; +extern crate hamcrest; + +use cargotest::support::{project, execs}; +use hamcrest::assert_that; + +#[test] +fn parses_env() { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/lib.rs", ""); + p.build(); + + assert_that(p.cargo("doc").env("RUSTDOCFLAGS", "--cfg=foo").arg("-v"), + execs().with_status(0) + .with_stderr_contains("\ +[RUNNING] `rustdoc [..] --cfg=foo[..]` +")); +} + +#[test] +fn parses_config() { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/lib.rs", "") + .file(".cargo/config", r#" + [build] + rustdocflags = ["--cfg", "foo"] + "#); + p.build(); + + assert_that(p.cargo("doc").arg("-v"), + execs().with_status(0) + .with_stderr_contains("\ +[RUNNING] `rustdoc [..] --cfg foo[..]` +")); +} + +#[test] +fn bad_flags() { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/lib.rs", ""); + p.build(); + + assert_that(p.cargo("doc").env("RUSTDOCFLAGS", "--bogus"), + execs().with_status(101)); +} + +#[test] +fn rerun() { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/lib.rs", ""); + p.build(); + + assert_that(p.cargo("doc").env("RUSTDOCFLAGS", "--cfg=foo"), + execs().with_status(0)); + assert_that(p.cargo("doc").env("RUSTDOCFLAGS", "--cfg=foo"), + execs().with_status(0).with_stderr("")); + assert_that(p.cargo("doc").env("RUSTDOCFLAGS", "--cfg=bar"), + execs().with_status(0).with_stderr("\ +[DOCUMENTING] foo v0.0.1 ([..]) +")); +}