From 5be647ef2e150880931c05f6d14809fb191e0c0b Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 4 Jan 2015 01:16:33 -0800 Subject: [PATCH] Start using precise -L flags Now that the compiler supports the notion for a "dependency lookup path" Cargo can specify this information to the compiler in order to prevent transitive dependencies from being imported. Closes #1037 --- Cargo.lock | 4 +- src/cargo/ops/cargo_rustc/mod.rs | 12 +++--- tests/test_cargo_build_lib.rs | 4 +- tests/test_cargo_compile.rs | 66 +++++++++++++++++++++++++------ tests/test_cargo_cross_compile.rs | 4 +- tests/test_cargo_profiles.rs | 12 +++--- 6 files changed, 74 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0f08ca28499..c761cc25fba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,7 +3,7 @@ name = "cargo" version = "0.1.0" dependencies = [ "curl 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", - "docopt 0.6.22 (registry+https://github.com/rust-lang/crates.io-index)", + "docopt 0.6.23 (registry+https://github.com/rust-lang/crates.io-index)", "flate2 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "git2 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "glob 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -40,7 +40,7 @@ dependencies = [ [[package]] name = "docopt" -version = "0.6.22" +version = "0.6.23" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "regex 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index a9902f88041..c3d43e2e541 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -727,8 +727,8 @@ fn build_deps_args(mut cmd: CommandPrototype, target: &Target, package: &Package cx: &Context, kind: Kind) -> CargoResult { let layout = cx.layout(package, kind); - cmd = cmd.arg("-L").arg(layout.root()); - cmd = cmd.arg("-L").arg(layout.deps()); + cmd = cmd.arg("-L").arg(format!("dependency={}", layout.root().display())); + cmd = cmd.arg("-L").arg(format!("dependency={}", layout.deps().display())); cmd = cmd.env("OUT_DIR", if package.has_custom_build() { Some(layout.build_out(package)) @@ -746,7 +746,7 @@ fn build_deps_args(mut cmd: CommandPrototype, target: &Target, package: &Package } }); for dir in dirs.into_iter() { - cmd = cmd.arg("-L").arg(dir); + cmd = cmd.arg("-L").arg(format!("native={}", dir.display())); } for &(pkg, target) in cx.dep_targets(package, target).iter() { @@ -757,8 +757,9 @@ fn build_deps_args(mut cmd: CommandPrototype, target: &Target, package: &Package target.is_lib() && target.get_profile().is_compile() }); - if target.is_bin() && !target.get_profile().is_custom_build() { - for target in targets.filter(|f| !f.is_staticlib()) { + if (target.is_bin() || target.is_example()) && + !target.get_profile().is_custom_build() { + for target in targets.filter(|f| f.is_rlib() || f.is_dylib()) { cmd = try!(link_to(cmd, package, target, cx, kind)); } } @@ -777,6 +778,7 @@ fn build_deps_args(mut cmd: CommandPrototype, target: &Target, package: &Package }); for filename in try!(cx.target_filenames(target)).iter() { + if filename.as_bytes().ends_with(b".a") { continue } let mut v = Vec::new(); v.push_all(target.get_name().as_bytes()); v.push(b'='); diff --git a/tests/test_cargo_build_lib.rs b/tests/test_cargo_build_lib.rs index f559b53e8c8..6da91566935 100644 --- a/tests/test_cargo_build_lib.rs +++ b/tests/test_cargo_build_lib.rs @@ -14,8 +14,8 @@ fn verbose_output_for_lib(p: &ProjectBuilder) -> String { -C extra-filename=-[..] \ --out-dir {dir}{sep}target \ --emit=dep-info,link \ - -L {dir}{sep}target \ - -L {dir}{sep}target{sep}deps` + -L dependency={dir}{sep}target \ + -L dependency={dir}{sep}target{sep}deps` ", running = RUNNING, compiling = COMPILING, sep = path::SEP, dir = p.root().display(), url = p.url(), diff --git a/tests/test_cargo_compile.rs b/tests/test_cargo_compile.rs index 2572cf0ef00..4c333dcc39a 100644 --- a/tests/test_cargo_compile.rs +++ b/tests/test_cargo_compile.rs @@ -757,8 +757,8 @@ test!(lto_build { --cfg ndebug \ --out-dir {dir}{sep}target{sep}release \ --emit=dep-info,link \ - -L {dir}{sep}target{sep}release \ - -L {dir}{sep}target{sep}release{sep}deps` + -L dependency={dir}{sep}target{sep}release \ + -L dependency={dir}{sep}target{sep}release{sep}deps` ", running = RUNNING, compiling = COMPILING, sep = path::SEP, dir = p.root().display(), @@ -785,8 +785,8 @@ test!(verbose_build { -C extra-filename=-[..] \ --out-dir {dir}{sep}target \ --emit=dep-info,link \ - -L {dir}{sep}target \ - -L {dir}{sep}target{sep}deps` + -L dependency={dir}{sep}target \ + -L dependency={dir}{sep}target{sep}deps` ", running = RUNNING, compiling = COMPILING, sep = path::SEP, dir = p.root().display(), @@ -815,8 +815,8 @@ test!(verbose_release_build { -C extra-filename=-[..] \ --out-dir {dir}{sep}target{sep}release \ --emit=dep-info,link \ - -L {dir}{sep}target{sep}release \ - -L {dir}{sep}target{sep}release{sep}deps` + -L dependency={dir}{sep}target{sep}release \ + -L dependency={dir}{sep}target{sep}release{sep}deps` ", running = RUNNING, compiling = COMPILING, sep = path::SEP, dir = p.root().display(), @@ -861,8 +861,8 @@ test!(verbose_release_build_deps { -C extra-filename=-[..] \ --out-dir {dir}{sep}target{sep}release{sep}deps \ --emit=dep-info,link \ - -L {dir}{sep}target{sep}release{sep}deps \ - -L {dir}{sep}target{sep}release{sep}deps` + -L dependency={dir}{sep}target{sep}release{sep}deps \ + -L dependency={dir}{sep}target{sep}release{sep}deps` {compiling} test v0.0.0 ({url}) {running} `rustc {dir}{sep}src{sep}lib.rs --crate-name test --crate-type lib \ -C opt-level=3 \ @@ -871,8 +871,8 @@ test!(verbose_release_build_deps { -C extra-filename=-[..] \ --out-dir {dir}{sep}target{sep}release \ --emit=dep-info,link \ - -L {dir}{sep}target{sep}release \ - -L {dir}{sep}target{sep}release{sep}deps \ + -L dependency={dir}{sep}target{sep}release \ + -L dependency={dir}{sep}target{sep}release{sep}deps \ --extern foo={dir}{sep}target{sep}release{sep}deps/\ {prefix}foo-[..]{suffix} \ --extern foo={dir}{sep}target{sep}release{sep}deps/libfoo-[..].rlib` @@ -1119,7 +1119,7 @@ test!(staticlib_rlib_and_bin { foo::foo(); }"#); - assert_that(p.cargo_process("build"), execs().with_status(0)); + assert_that(p.cargo_process("build").arg("-v"), execs().with_status(0)); }); test!(opt_out_of_lib { @@ -1523,3 +1523,47 @@ test!(compile_then_delete { assert_that(p.process(cargo_dir().join("cargo")).arg("run"), execs().with_status(0)); }); + +test!(transitive_dependencies_not_available { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + + [dependencies.a] + path = "a" + "#) + .file("src/main.rs", "extern crate b; extern crate a; fn main() {}") + .file("a/Cargo.toml", r#" + [package] + name = "a" + version = "0.0.1" + authors = [] + + [dependencies.b] + path = "../b" + "#) + .file("a/src/lib.rs", "extern crate b;") + .file("b/Cargo.toml", r#" + [package] + name = "b" + version = "0.0.1" + authors = [] + "#) + .file("b/src/lib.rs", ""); + + assert_that(p.cargo_process("build").arg("-v"), + execs().with_status(101) + .with_stderr("\ +[..] can't find crate for `b` +[..] extern crate b; [..] +[..] +error: aborting due to previous error +Could not compile `foo`. + +Caused by: + [..] +")); +}); diff --git a/tests/test_cargo_cross_compile.rs b/tests/test_cargo_cross_compile.rs index b55cc75d9c9..e33eeb5eb11 100644 --- a/tests/test_cargo_cross_compile.rs +++ b/tests/test_cargo_cross_compile.rs @@ -303,8 +303,8 @@ test!(linker_and_ar { --emit=dep-info,link \ --target {target} \ -C ar=my-ar-tool -C linker=my-linker-tool \ - -L {dir}{sep}target{sep}{target} \ - -L {dir}{sep}target{sep}{target}{sep}deps` + -L dependency={dir}{sep}target{sep}{target} \ + -L dependency={dir}{sep}target{sep}{target}{sep}deps` ", running = RUNNING, compiling = COMPILING, diff --git a/tests/test_cargo_profiles.rs b/tests/test_cargo_profiles.rs index ccf531a540f..5479fafe98c 100644 --- a/tests/test_cargo_profiles.rs +++ b/tests/test_cargo_profiles.rs @@ -35,8 +35,8 @@ test!(profile_overrides { -C rpath \ --out-dir {dir}{sep}target \ --emit=dep-info,link \ - -L {dir}{sep}target \ - -L {dir}{sep}target{sep}deps` + -L dependency={dir}{sep}target \ + -L dependency={dir}{sep}target{sep}deps` ", running = RUNNING, compiling = COMPILING, sep = path::SEP, dir = p.root().display(), @@ -89,8 +89,8 @@ test!(top_level_overrides_deps { -C extra-filename=-[..] \ --out-dir {dir}{sep}target{sep}release{sep}deps \ --emit=dep-info,link \ - -L {dir}{sep}target{sep}release{sep}deps \ - -L {dir}{sep}target{sep}release{sep}deps` + -L dependency={dir}{sep}target{sep}release{sep}deps \ + -L dependency={dir}{sep}target{sep}release{sep}deps` {compiling} test v0.0.0 ({url}) {running} `rustc {dir}{sep}src{sep}lib.rs --crate-name test --crate-type lib \ -C opt-level=1 \ @@ -99,8 +99,8 @@ test!(top_level_overrides_deps { -C extra-filename=-[..] \ --out-dir {dir}{sep}target{sep}release \ --emit=dep-info,link \ - -L {dir}{sep}target{sep}release \ - -L {dir}{sep}target{sep}release{sep}deps \ + -L dependency={dir}{sep}target{sep}release \ + -L dependency={dir}{sep}target{sep}release{sep}deps \ --extern foo={dir}{sep}target{sep}release{sep}deps/\ {prefix}foo-[..]{suffix} \ --extern foo={dir}{sep}target{sep}release{sep}deps/libfoo-[..].rlib`