From: Lukas Lueg Date: Sat, 9 Dec 2017 14:24:08 +0000 (+0100) Subject: Warn when trying to link to a library that is not linkable. X-Git-Tag: archive/raspbian/0.35.0-2+rpi1~3^2^2^2^2^2^2^2~22^2~4^2~27^2 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=554f333948327ab2d92904a960277626c87e6ba4;p=cargo.git Warn when trying to link to a library that is not linkable. If a dependency is a library that provides no linkable targets, we warn about the impending error in rustc about an unresolvable `extern crate`. Fixes #3169. --- diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index bc0c812ca..d0def53d7 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -896,12 +896,31 @@ fn build_deps_args<'a, 'cfg>(cmd: &mut ProcessBuilder, }); } - for unit in cx.dep_targets(unit)?.iter() { + let dep_targets = cx.dep_targets(unit)?; + + // If there is not one linkable target but should, rustc fails later + // on if there is an `extern crate` for it. This may turn into a hard + // error in the future, see PR #4797 + if !dep_targets.iter().any(|u| !u.profile.doc && u.target.linkable()) { + if let Some(u) = dep_targets.iter() + .filter(|u| !u.profile.doc && u.target.is_lib()) + .next() { + cx.config.shell().warn(format!("The package `{}` \ +provides no linkable target. The compiler might raise an error while compiling \ +`{}`. Consider adding 'dylib' or 'rlib' to key `crate-type` in `{}`'s \ +Cargo.toml. This warning might turn into a hard error in the future.", + u.target.crate_name(), + unit.target.crate_name(), + u.target.crate_name()))?; + } + } + + for unit in dep_targets { if unit.profile.run_custom_build { - cmd.env("OUT_DIR", &cx.build_script_out_dir(unit)); + cmd.env("OUT_DIR", &cx.build_script_out_dir(&unit)); } if unit.target.linkable() && !unit.profile.doc { - link_to(cmd, cx, unit)?; + link_to(cmd, cx, &unit)?; } } diff --git a/tests/build.rs b/tests/build.rs index 523e42d91..796fe1fcd 100644 --- a/tests/build.rs +++ b/tests/build.rs @@ -3971,3 +3971,34 @@ fn all_targets_no_lib() { -C debuginfo=2 --test [..]") ); } + +#[test] +fn no_linkable_target() { + // Issue 3169. This is currently not an error as per discussion in PR #4797 + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.1.0" + authors = [] + [dependencies] + the_lib = { path = "the_lib" } + "#) + .file("src/main.rs", "fn main() {}") + .file("the_lib/Cargo.toml", r#" + [package] + name = "the_lib" + version = "0.1.0" + [lib] + name = "the_lib" + crate-type = ["staticlib"] + "#) + .file("the_lib/src/lib.rs", "pub fn foo() {}") + .build(); + assert_that(p.cargo("build"), + execs() + .with_status(0) + .with_stderr_contains("\ + [WARNING] The package `the_lib` provides no linkable [..] \ +while compiling `foo`. [..] in `the_lib`'s Cargo.toml. [..]")); +}