Skip to content

Commit

Permalink
Auto merge of #2794 - alexcrichton:rustdocflags, r=brson
Browse files Browse the repository at this point in the history
Add support for RUSTDOCFLAGS

Like with RUSTFLAGS, parse this variable to pass along extra arguments to
invocations of `rustdoc`.
  • Loading branch information
bors authored Jul 6, 2016
2 parents b036f19 + f639d38 commit 270200b
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 12 deletions.
25 changes: 18 additions & 7 deletions src/cargo/ops/cargo_rustc/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,15 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
crate_types: &BTreeSet<String>,
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 {
Expand Down Expand Up @@ -644,7 +648,11 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
}

pub fn rustflags_args(&self, unit: &Unit) -> CargoResult<Vec<String>> {
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<Vec<String>> {
env_args(self.config, &self.build_config, unit.kind, "RUSTDOCFLAGS")
}

pub fn show_warnings(&self, pkg: &PackageId) -> bool {
Expand All @@ -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<Vec<String>> {
fn env_args(config: &Config,
build_config: &BuildConfig,
kind: Kind,
name: &str) -> CargoResult<Vec<String>> {
// 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
Expand Down Expand Up @@ -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())
Expand All @@ -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::<String>();
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());
}
Expand Down
7 changes: 6 additions & 1 deletion src/cargo/ops/cargo_rustc/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions src/cargo/ops/cargo_rustc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ fn rustc(cx: &mut Context, unit: &Unit) -> CargoResult<Work> {
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
Expand All @@ -270,9 +270,6 @@ fn rustc(cx: &mut Context, unit: &Unit) -> CargoResult<Work> {
}
}

// Add the arguments from RUSTFLAGS
rustc.args(&rustflags);

state.running(&rustc);
try!(exec_engine.exec(rustc).chain_error(|| {
human(format!("Could not compile `{}`.", name))
Expand Down Expand Up @@ -406,6 +403,8 @@ fn rustdoc(cx: &mut Context, unit: &Unit) -> CargoResult<Work> {
.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);
Expand Down
85 changes: 85 additions & 0 deletions tests/rustdocflags.rs
Original file line number Diff line number Diff line change
@@ -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 ([..])
"));
}

0 comments on commit 270200b

Please sign in to comment.