Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cargo/ops/cargo_rustc/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct Compilation<'cfg> {
pub to_doc_test: Vec<Package>,

/// Features enabled during this compilation.
pub features: HashSet<String>,
pub cfgs: HashSet<String>,

config: &'cfg Config,
}
Expand All @@ -58,7 +58,7 @@ impl<'cfg> Compilation<'cfg> {
binaries: Vec::new(),
extra_env: HashMap::new(),
to_doc_test: Vec::new(),
features: HashSet::new(),
cfgs: HashSet::new(),
config: config,
}
}
Expand Down
20 changes: 16 additions & 4 deletions src/cargo/ops/cargo_rustc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,17 @@ pub fn compile_targets<'a, 'cfg: 'a>(pkg_targets: &'a PackagesToBuild<'a>,
}
}

if let Some(feats) = cx.resolve.features(root.package_id()) {
cx.compilation.features.extend(feats.iter().cloned());
let root_pkg = root.package_id();
if let Some(feats) = cx.resolve.features(root_pkg) {
cx.compilation.cfgs.extend(feats.iter().map(|feat| {
format!("feature=\"{}\"", feat)
}));
}

for (&(ref pkg, _), output) in cx.build_state.outputs.lock().unwrap().iter() {
if pkg == root_pkg {
cx.compilation.cfgs.extend(output.cfgs.iter().cloned());
}
let any_dylib = output.library_links.iter().any(|l| {
!l.starts_with("static=") && !l.starts_with("framework=")
});
Expand Down Expand Up @@ -392,11 +398,17 @@ fn rustdoc(cx: &mut Context, unit: &Unit) -> CargoResult<Work> {
}

let name = unit.pkg.name().to_string();
let desc = rustdoc.to_string();
let build_state = cx.build_state.clone();
let key = (unit.pkg.package_id().clone(), unit.kind);
let exec_engine = cx.exec_engine.clone();

Ok(Work::new(move |desc_tx| {
desc_tx.send(desc).unwrap();
if let Some(output) = build_state.outputs.lock().unwrap().get(&key) {
for cfg in output.cfgs.iter() {
rustdoc.arg("--cfg").arg(cfg);
}
}
desc_tx.send(rustdoc.to_string()).unwrap();
exec_engine.exec(rustdoc).chain_error(|| {
human(format!("Could not document `{}`.", name))
})
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/ops/cargo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ fn run_doc_tests(options: &TestOptions,
p.arg("--test-args").arg(&test_args.connect(" "));
}

for feat in compilation.features.iter() {
p.arg("--cfg").arg(&format!("feature=\"{}\"", feat));
for cfg in compilation.cfgs.iter() {
p.arg("--cfg").arg(cfg);
}

for (_, libs) in compilation.libraries.iter() {
Expand Down
233 changes: 232 additions & 1 deletion tests/test_cargo_compile_custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io::prelude::*;
use support::{project, execs};
use support::{COMPILING, RUNNING, DOCTEST, FRESH};
use support::paths::CargoPathExt;
use hamcrest::{assert_that};
use hamcrest::{assert_that, existing_file, existing_dir};

fn setup() {
}
Expand Down Expand Up @@ -1281,6 +1281,237 @@ test!(cfg_override {
execs().with_status(0));
});

test!(cfg_test {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
build = "build.rs"
"#)
.file("build.rs", r#"
fn main() {
println!("cargo:rustc-cfg=foo");
}
"#)
.file("src/lib.rs", r#"
///
/// ```
/// extern crate foo;
///
/// fn main() {
/// foo::foo()
/// }
/// ```
///
#[cfg(foo)]
pub fn foo() {}

#[cfg(foo)]
#[test]
fn test_foo() {
foo()
}
"#)
.file("tests/test.rs", r#"
#[cfg(foo)]
#[test]
fn test_bar() {}
"#);
assert_that(p.cargo_process("test").arg("-v"),
execs().with_stdout(format!("\
{compiling} foo v0.0.1 ({dir})
{running} [..] build.rs [..]
{running} [..]build-script-build[..]
{running} [..] src[..]lib.rs [..] --cfg foo[..]
{running} [..] src[..]lib.rs [..] --cfg foo[..]
{running} [..] tests[..]test.rs [..] --cfg foo[..]
{running} [..]foo-[..]

running 1 test
test test_foo ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured

{running} [..]test-[..]

running 1 test
test test_bar ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured

{doctest} foo
{running} [..] --cfg foo[..]

running 1 test
test foo_0 ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured

",
compiling = COMPILING, dir = p.url(), running = RUNNING, doctest = DOCTEST)));
});

test!(cfg_doc {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
build = "build.rs"

[dependencies.bar]
path = "bar"
"#)
.file("build.rs", r#"
fn main() {
println!("cargo:rustc-cfg=foo");
}
"#)
.file("src/lib.rs", r#"
#[cfg(foo)]
pub fn foo() {}
"#)
.file("bar/Cargo.toml", r#"
[package]
name = "bar"
version = "0.0.1"
authors = []
build = "build.rs"
"#)
.file("bar/build.rs", r#"
fn main() {
println!("cargo:rustc-cfg=bar");
}
"#)
.file("bar/src/lib.rs", r#"
#[cfg(bar)]
pub fn bar() {}
"#);
assert_that(p.cargo_process("doc"),
execs().with_status(0));
assert_that(&p.root().join("target/doc"), existing_dir());
assert_that(&p.root().join("target/doc/foo/fn.foo.html"), existing_file());
assert_that(&p.root().join("target/doc/bar/fn.bar.html"), existing_file());
});

test!(cfg_override_test {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
build = "build.rs"
links = "a"
"#)
.file("build.rs", "")
.file(".cargo/config", &format!(r#"
[target.{}.a]
rustc-cfg = ["foo"]
"#, ::rustc_host()))
.file("src/lib.rs", r#"
///
/// ```
/// extern crate foo;
///
/// fn main() {
/// foo::foo()
/// }
/// ```
///
#[cfg(foo)]
pub fn foo() {}

#[cfg(foo)]
#[test]
fn test_foo() {
foo()
}
"#)
.file("tests/test.rs", r#"
#[cfg(foo)]
#[test]
fn test_bar() {}
"#);
assert_that(p.cargo_process("test").arg("-v"),
execs().with_stdout(format!("\
{compiling} foo v0.0.1 ({dir})
{running} [..] src[..]lib.rs [..] --cfg foo[..]
{running} [..] src[..]lib.rs [..] --cfg foo[..]
{running} [..] tests[..]test.rs [..] --cfg foo[..]
{running} [..]foo-[..]

running 1 test
test test_foo ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured

{running} [..]test-[..]

running 1 test
test test_bar ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured

{doctest} foo
{running} [..] --cfg foo[..]

running 1 test
test foo_0 ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured

",
compiling = COMPILING, dir = p.url(), running = RUNNING, doctest = DOCTEST)));
});

test!(cfg_override_doc {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
build = "build.rs"
links = "a"

[dependencies.bar]
path = "bar"
"#)
.file(".cargo/config", &format!(r#"
[target.{target}.a]
rustc-cfg = ["foo"]
[target.{target}.b]
rustc-cfg = ["bar"]
"#, target = ::rustc_host()))
.file("build.rs", "")
.file("src/lib.rs", r#"
#[cfg(foo)]
pub fn foo() {}
"#)
.file("bar/Cargo.toml", r#"
[package]
name = "bar"
version = "0.0.1"
authors = []
build = "build.rs"
links = "b"
"#)
.file("bar/build.rs", "")
.file("bar/src/lib.rs", r#"
#[cfg(bar)]
pub fn bar() {}
"#) ;
assert_that(p.cargo_process("doc"),
execs().with_status(0));
assert_that(&p.root().join("target/doc"), existing_dir());
assert_that(&p.root().join("target/doc/foo/fn.foo.html"), existing_file());
assert_that(&p.root().join("target/doc/bar/fn.bar.html"), existing_file());
});

test!(flags_go_into_tests {
let p = project("foo")
.file("Cargo.toml", r#"
Expand Down
43 changes: 43 additions & 0 deletions tests/test_cargo_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,46 @@ test!(doc_multiple_deps {
assert_that(&p.root().join("target/doc/bar/index.html"), existing_file());
assert_that(&p.root().join("target/doc/baz/index.html"), existing_file());
});

test!(features {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = []

[dependencies.bar]
path = "bar"

[features]
foo = ["bar/bar"]
"#)
.file("src/lib.rs", r#"
#[cfg(feature = "foo")]
pub fn foo() {}
"#)
.file("bar/Cargo.toml", r#"
[package]
name = "bar"
version = "0.0.1"
authors = []

[features]
bar = []
"#)
.file("bar/build.rs", r#"
fn main() {
println!("cargo:rustc-cfg=bar");
}
"#)
.file("bar/src/lib.rs", r#"
#[cfg(feature = "bar")]
pub fn bar() {}
"#);
assert_that(p.cargo_process("doc").arg("--features").arg("foo"),
execs().with_status(0));
assert_that(&p.root().join("target/doc"), existing_dir());
assert_that(&p.root().join("target/doc/foo/fn.foo.html"), existing_file());
assert_that(&p.root().join("target/doc/bar/fn.bar.html"), existing_file());
});