cargo/tests/build-lib.rs
Alex Crichton 0863469c8b Always build libraries into the same location
Previously Cargo would compile a library into a different location depending on
whether it was the "root crate" or not. In the ongoing saga of reducing Cargo's
reliance on the idea of a "root crate" this PR is the next step. With workspaces
the root crate of a compliation changes all the time, so the output needs to be
the same whether a crate is at the root or not.

Fixing this inconsistence in turn fixes bugs like #2855 and #2897 which arise
due to this discrepancy. Additionally, Cargo will no longer recompile a library
when it's used as a "root crate" or not.

This is fixed by taking a few steps:

* Everything is now compiled into the `deps` directory, regardless of whether
  it's a root output or not.
* If a "root crate" is being compiled, then the relevant outputs are hard-linked
  up one level to where they are today. This means that your binaries, dylibs,
  staticlibs, etc, will all show up where they used to.
* The `-C metadata` flag is always omitted for path dependencies now. These
  dependencies are always path dependencies and already all have unique crate
  names. Additionally, they're the only crates in the DAG without metadata, so
  there's no need to provide additional metadata. This in turn means that none
  of the file names of the generated crates are mangled.

Closes #2855
2016-07-26 17:52:45 -07:00

87 lines
2.3 KiB
Rust

extern crate cargotest;
extern crate hamcrest;
use std::path::MAIN_SEPARATOR as SEP;
use cargotest::support::{basic_bin_manifest, execs, project, ProjectBuilder};
use hamcrest::{assert_that};
fn verbose_output_for_lib(p: &ProjectBuilder) -> String {
format!("\
[COMPILING] {name} v{version} ({url})
[RUNNING] `rustc src{sep}lib.rs --crate-name {name} --crate-type lib -g \
--out-dir [..] \
--emit=dep-info,link \
-L dependency={dir}{sep}target{sep}debug{sep}deps`
[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
", sep = SEP,
dir = p.root().display(), url = p.url(),
name = "foo", version = "0.0.1")
}
#[test]
fn build_lib_only() {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = ["wycats@example.com"]
"#)
.file("src/main.rs", r#"
fn main() {}
"#)
.file("src/lib.rs", r#" "#);
assert_that(p.cargo_process("build").arg("--lib").arg("-v"),
execs()
.with_status(0)
.with_stderr(verbose_output_for_lib(&p)));
}
#[test]
fn build_with_no_lib() {
let p = project("foo")
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/main.rs", r#"
fn main() {}
"#);
assert_that(p.cargo_process("build").arg("--lib"),
execs().with_status(101)
.with_stderr("[ERROR] no library targets found"));
}
#[test]
fn build_with_relative_cargo_home_path() {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = ["wycats@example.com"]
[dependencies]
"test-dependency" = { path = "src/test_dependency" }
"#)
.file("src/main.rs", r#"
fn main() {}
"#)
.file("src/test_dependency/src/lib.rs", r#" "#)
.file("src/test_dependency/Cargo.toml", r#"
[package]
name = "test-dependency"
version = "0.0.1"
authors = ["wycats@example.com"]
"#);
assert_that(p.cargo_process("build").env("CARGO_HOME", "./cargo_home/"),
execs()
.with_status(0));
}