Skip to content

Commit 1dc1865

Browse files
committed
Auto merge of #2050 - gkoz:build_cfgs, r=alexcrichton
Fixes #1980
2 parents ec85eef + 807da6b commit 1dc1865

File tree

5 files changed

+295
-9
lines changed

5 files changed

+295
-9
lines changed

src/cargo/ops/cargo_rustc/compilation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub struct Compilation<'cfg> {
4242
pub to_doc_test: Vec<Package>,
4343

4444
/// Features enabled during this compilation.
45-
pub features: HashSet<String>,
45+
pub cfgs: HashSet<String>,
4646

4747
config: &'cfg Config,
4848
}
@@ -58,7 +58,7 @@ impl<'cfg> Compilation<'cfg> {
5858
binaries: Vec::new(),
5959
extra_env: HashMap::new(),
6060
to_doc_test: Vec::new(),
61-
features: HashSet::new(),
61+
cfgs: HashSet::new(),
6262
config: config,
6363
}
6464
}

src/cargo/ops/cargo_rustc/mod.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,17 @@ pub fn compile_targets<'a, 'cfg: 'a>(pkg_targets: &'a PackagesToBuild<'a>,
148148
}
149149
}
150150

151-
if let Some(feats) = cx.resolve.features(root.package_id()) {
152-
cx.compilation.features.extend(feats.iter().cloned());
151+
let root_pkg = root.package_id();
152+
if let Some(feats) = cx.resolve.features(root_pkg) {
153+
cx.compilation.cfgs.extend(feats.iter().map(|feat| {
154+
format!("feature=\"{}\"", feat)
155+
}));
153156
}
154157

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

394400
let name = unit.pkg.name().to_string();
395-
let desc = rustdoc.to_string();
401+
let build_state = cx.build_state.clone();
402+
let key = (unit.pkg.package_id().clone(), unit.kind);
396403
let exec_engine = cx.exec_engine.clone();
397404

398405
Ok(Work::new(move |desc_tx| {
399-
desc_tx.send(desc).unwrap();
406+
if let Some(output) = build_state.outputs.lock().unwrap().get(&key) {
407+
for cfg in output.cfgs.iter() {
408+
rustdoc.arg("--cfg").arg(cfg);
409+
}
410+
}
411+
desc_tx.send(rustdoc.to_string()).unwrap();
400412
exec_engine.exec(rustdoc).chain_error(|| {
401413
human(format!("Could not document `{}`.", name))
402414
})

src/cargo/ops/cargo_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ fn run_doc_tests(options: &TestOptions,
136136
p.arg("--test-args").arg(&test_args.connect(" "));
137137
}
138138

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

143143
for (_, libs) in compilation.libraries.iter() {

tests/test_cargo_compile_custom_build.rs

Lines changed: 232 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::io::prelude::*;
44
use support::{project, execs};
55
use support::{COMPILING, RUNNING, DOCTEST, FRESH};
66
use support::paths::CargoPathExt;
7-
use hamcrest::{assert_that};
7+
use hamcrest::{assert_that, existing_file, existing_dir};
88

99
fn setup() {
1010
}
@@ -1281,6 +1281,237 @@ test!(cfg_override {
12811281
execs().with_status(0));
12821282
});
12831283

1284+
test!(cfg_test {
1285+
let p = project("foo")
1286+
.file("Cargo.toml", r#"
1287+
[package]
1288+
name = "foo"
1289+
version = "0.0.1"
1290+
authors = []
1291+
build = "build.rs"
1292+
"#)
1293+
.file("build.rs", r#"
1294+
fn main() {
1295+
println!("cargo:rustc-cfg=foo");
1296+
}
1297+
"#)
1298+
.file("src/lib.rs", r#"
1299+
///
1300+
/// ```
1301+
/// extern crate foo;
1302+
///
1303+
/// fn main() {
1304+
/// foo::foo()
1305+
/// }
1306+
/// ```
1307+
///
1308+
#[cfg(foo)]
1309+
pub fn foo() {}
1310+
1311+
#[cfg(foo)]
1312+
#[test]
1313+
fn test_foo() {
1314+
foo()
1315+
}
1316+
"#)
1317+
.file("tests/test.rs", r#"
1318+
#[cfg(foo)]
1319+
#[test]
1320+
fn test_bar() {}
1321+
"#);
1322+
assert_that(p.cargo_process("test").arg("-v"),
1323+
execs().with_stdout(format!("\
1324+
{compiling} foo v0.0.1 ({dir})
1325+
{running} [..] build.rs [..]
1326+
{running} [..]build-script-build[..]
1327+
{running} [..] src[..]lib.rs [..] --cfg foo[..]
1328+
{running} [..] src[..]lib.rs [..] --cfg foo[..]
1329+
{running} [..] tests[..]test.rs [..] --cfg foo[..]
1330+
{running} [..]foo-[..]
1331+
1332+
running 1 test
1333+
test test_foo ... ok
1334+
1335+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
1336+
1337+
{running} [..]test-[..]
1338+
1339+
running 1 test
1340+
test test_bar ... ok
1341+
1342+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
1343+
1344+
{doctest} foo
1345+
{running} [..] --cfg foo[..]
1346+
1347+
running 1 test
1348+
test foo_0 ... ok
1349+
1350+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
1351+
1352+
",
1353+
compiling = COMPILING, dir = p.url(), running = RUNNING, doctest = DOCTEST)));
1354+
});
1355+
1356+
test!(cfg_doc {
1357+
let p = project("foo")
1358+
.file("Cargo.toml", r#"
1359+
[package]
1360+
name = "foo"
1361+
version = "0.0.1"
1362+
authors = []
1363+
build = "build.rs"
1364+
1365+
[dependencies.bar]
1366+
path = "bar"
1367+
"#)
1368+
.file("build.rs", r#"
1369+
fn main() {
1370+
println!("cargo:rustc-cfg=foo");
1371+
}
1372+
"#)
1373+
.file("src/lib.rs", r#"
1374+
#[cfg(foo)]
1375+
pub fn foo() {}
1376+
"#)
1377+
.file("bar/Cargo.toml", r#"
1378+
[package]
1379+
name = "bar"
1380+
version = "0.0.1"
1381+
authors = []
1382+
build = "build.rs"
1383+
"#)
1384+
.file("bar/build.rs", r#"
1385+
fn main() {
1386+
println!("cargo:rustc-cfg=bar");
1387+
}
1388+
"#)
1389+
.file("bar/src/lib.rs", r#"
1390+
#[cfg(bar)]
1391+
pub fn bar() {}
1392+
"#);
1393+
assert_that(p.cargo_process("doc"),
1394+
execs().with_status(0));
1395+
assert_that(&p.root().join("target/doc"), existing_dir());
1396+
assert_that(&p.root().join("target/doc/foo/fn.foo.html"), existing_file());
1397+
assert_that(&p.root().join("target/doc/bar/fn.bar.html"), existing_file());
1398+
});
1399+
1400+
test!(cfg_override_test {
1401+
let p = project("foo")
1402+
.file("Cargo.toml", r#"
1403+
[package]
1404+
name = "foo"
1405+
version = "0.0.1"
1406+
authors = []
1407+
build = "build.rs"
1408+
links = "a"
1409+
"#)
1410+
.file("build.rs", "")
1411+
.file(".cargo/config", &format!(r#"
1412+
[target.{}.a]
1413+
rustc-cfg = ["foo"]
1414+
"#, ::rustc_host()))
1415+
.file("src/lib.rs", r#"
1416+
///
1417+
/// ```
1418+
/// extern crate foo;
1419+
///
1420+
/// fn main() {
1421+
/// foo::foo()
1422+
/// }
1423+
/// ```
1424+
///
1425+
#[cfg(foo)]
1426+
pub fn foo() {}
1427+
1428+
#[cfg(foo)]
1429+
#[test]
1430+
fn test_foo() {
1431+
foo()
1432+
}
1433+
"#)
1434+
.file("tests/test.rs", r#"
1435+
#[cfg(foo)]
1436+
#[test]
1437+
fn test_bar() {}
1438+
"#);
1439+
assert_that(p.cargo_process("test").arg("-v"),
1440+
execs().with_stdout(format!("\
1441+
{compiling} foo v0.0.1 ({dir})
1442+
{running} [..] src[..]lib.rs [..] --cfg foo[..]
1443+
{running} [..] src[..]lib.rs [..] --cfg foo[..]
1444+
{running} [..] tests[..]test.rs [..] --cfg foo[..]
1445+
{running} [..]foo-[..]
1446+
1447+
running 1 test
1448+
test test_foo ... ok
1449+
1450+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
1451+
1452+
{running} [..]test-[..]
1453+
1454+
running 1 test
1455+
test test_bar ... ok
1456+
1457+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
1458+
1459+
{doctest} foo
1460+
{running} [..] --cfg foo[..]
1461+
1462+
running 1 test
1463+
test foo_0 ... ok
1464+
1465+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
1466+
1467+
",
1468+
compiling = COMPILING, dir = p.url(), running = RUNNING, doctest = DOCTEST)));
1469+
});
1470+
1471+
test!(cfg_override_doc {
1472+
let p = project("foo")
1473+
.file("Cargo.toml", r#"
1474+
[package]
1475+
name = "foo"
1476+
version = "0.0.1"
1477+
authors = []
1478+
build = "build.rs"
1479+
links = "a"
1480+
1481+
[dependencies.bar]
1482+
path = "bar"
1483+
"#)
1484+
.file(".cargo/config", &format!(r#"
1485+
[target.{target}.a]
1486+
rustc-cfg = ["foo"]
1487+
[target.{target}.b]
1488+
rustc-cfg = ["bar"]
1489+
"#, target = ::rustc_host()))
1490+
.file("build.rs", "")
1491+
.file("src/lib.rs", r#"
1492+
#[cfg(foo)]
1493+
pub fn foo() {}
1494+
"#)
1495+
.file("bar/Cargo.toml", r#"
1496+
[package]
1497+
name = "bar"
1498+
version = "0.0.1"
1499+
authors = []
1500+
build = "build.rs"
1501+
links = "b"
1502+
"#)
1503+
.file("bar/build.rs", "")
1504+
.file("bar/src/lib.rs", r#"
1505+
#[cfg(bar)]
1506+
pub fn bar() {}
1507+
"#) ;
1508+
assert_that(p.cargo_process("doc"),
1509+
execs().with_status(0));
1510+
assert_that(&p.root().join("target/doc"), existing_dir());
1511+
assert_that(&p.root().join("target/doc/foo/fn.foo.html"), existing_file());
1512+
assert_that(&p.root().join("target/doc/bar/fn.bar.html"), existing_file());
1513+
});
1514+
12841515
test!(flags_go_into_tests {
12851516
let p = project("foo")
12861517
.file("Cargo.toml", r#"

tests/test_cargo_doc.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,46 @@ test!(doc_multiple_deps {
477477
assert_that(&p.root().join("target/doc/bar/index.html"), existing_file());
478478
assert_that(&p.root().join("target/doc/baz/index.html"), existing_file());
479479
});
480+
481+
test!(features {
482+
let p = project("foo")
483+
.file("Cargo.toml", r#"
484+
[package]
485+
name = "foo"
486+
version = "0.0.1"
487+
authors = []
488+
489+
[dependencies.bar]
490+
path = "bar"
491+
492+
[features]
493+
foo = ["bar/bar"]
494+
"#)
495+
.file("src/lib.rs", r#"
496+
#[cfg(feature = "foo")]
497+
pub fn foo() {}
498+
"#)
499+
.file("bar/Cargo.toml", r#"
500+
[package]
501+
name = "bar"
502+
version = "0.0.1"
503+
authors = []
504+
505+
[features]
506+
bar = []
507+
"#)
508+
.file("bar/build.rs", r#"
509+
fn main() {
510+
println!("cargo:rustc-cfg=bar");
511+
}
512+
"#)
513+
.file("bar/src/lib.rs", r#"
514+
#[cfg(feature = "bar")]
515+
pub fn bar() {}
516+
"#);
517+
assert_that(p.cargo_process("doc").arg("--features").arg("foo"),
518+
execs().with_status(0));
519+
assert_that(&p.root().join("target/doc"), existing_dir());
520+
assert_that(&p.root().join("target/doc/foo/fn.foo.html"), existing_file());
521+
assert_that(&p.root().join("target/doc/bar/fn.bar.html"), existing_file());
522+
});

0 commit comments

Comments
 (0)