Auto merge of #4447 - lucaskolstad:proc-macro-crates-host-deps-linkage, r=alexcrichton

Add host dependency path via -L for cargo_test.

Proc-macro crates' dependencies in the host dependency directory cannot
be found when running `cargo test` with the `--target {target}` flag
set as reported in #4224. This adds the host dependency directory to the
search path to find those missing dependencies with -L when building tests
and adds a test case that fails before and passes after this patch.

A new function `find_host_deps(..)` is added to accomplish this which can
determine the path of the host dependencies directory from within
`run_doc_tests(..)` where the missing library search path is needed.

Fixes #4224
Fixes #4254

Modeled after a similar patch: a298346d6e8862992be3fd93dd8a6c833cc72719

**Concerns**

The test case seems to require a non-local crate from crates.io to example the failure before this patch. Couldn't make it fail with simply another local subcrate, but if others think it's possible that'd be great. This means that during tests for the cargo project itself that this test case actually downloads and compiles a crate, which I don't think any other tests do and is obviously not ideal and is perhaps even unacceptable. I've used the base64 crate pretty arbitrarily, but which crate it is really doesn't matter to test the case's content. So if anyone knows a tiny or empty crate on crates.io to use instead that'd speed this up and if someone can figure out how to make it fail before this patch is applied without downloading an external crate that would help as well.

Also, I'm not 100% confident about the `find_host_deps(..)` style and whether it's the best way to find the host dependencies directory with just the `TestOptions` and `Compilation` structs available since I'm new to this project. Any comments are appreciated.
This commit is contained in:
bors 2017-08-31 20:26:56 +00:00
commit f519d3de31
4 changed files with 70 additions and 8 deletions

View File

@ -28,12 +28,12 @@ pub struct Compilation<'cfg> {
/// Root output directory (for the local package's artifacts)
pub root_output: PathBuf,
/// Output directory for rust dependencies
/// Output directory for rust dependencies.
/// May be for the host or for a specific target.
pub deps_output: PathBuf,
/// Library search path for compiler plugins and build scripts
/// which have dynamic dependencies.
pub plugins_dylib_path: PathBuf,
/// Output directory for the rust host dependencies.
pub host_deps_output: PathBuf,
/// The path to rustc's own libstd
pub host_dylib_path: Option<PathBuf>,
@ -64,7 +64,7 @@ impl<'cfg> Compilation<'cfg> {
native_dirs: HashSet::new(), // TODO: deprecated, remove
root_output: PathBuf::from("/"),
deps_output: PathBuf::from("/"),
plugins_dylib_path: PathBuf::from("/"),
host_deps_output: PathBuf::from("/"),
host_dylib_path: None,
target_dylib_path: None,
tests: Vec::new(),
@ -124,7 +124,7 @@ impl<'cfg> Compilation<'cfg> {
-> CargoResult<ProcessBuilder> {
let mut search_path = if is_host {
let mut search_path = vec![self.plugins_dylib_path.clone()];
let mut search_path = vec![self.host_deps_output.clone()];
search_path.extend(self.host_dylib_path.clone());
search_path
} else {

View File

@ -175,7 +175,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
None => {}
}
self.compilation.plugins_dylib_path = self.host.deps().to_path_buf();
self.compilation.host_deps_output = self.host.deps().to_path_buf();
let layout = self.target.as_ref().unwrap_or(&self.host);
self.compilation.root_output = layout.dest().to_path_buf();

View File

@ -149,10 +149,17 @@ fn run_doc_tests(options: &TestOptions,
arg.push(rust_dep);
p.arg("-L").arg(arg);
}
for native_dep in compilation.native_dirs.iter() {
p.arg("-L").arg(native_dep);
}
for &host_rust_dep in &[&compilation.host_deps_output] {
let mut arg = OsString::from("dependency=");
arg.push(host_rust_dep);
p.arg("-L").arg(arg);
}
for arg in test_args {
p.arg("--test-args").arg(arg);
}

View File

@ -6,7 +6,7 @@ use std::fs::File;
use std::io::prelude::*;
use std::str;
use cargotest::{sleep_ms, is_nightly};
use cargotest::{sleep_ms, is_nightly, rustc_host};
use cargotest::support::{project, execs, basic_bin_manifest, basic_lib_manifest, cargo_exe};
use cargotest::support::paths::CargoPathExt;
use cargotest::support::registry::Package;
@ -2782,3 +2782,58 @@ fn publish_a_crate_without_tests() {
assert_that(p.cargo("test").arg("--package").arg("testless"),
execs().with_status(0));
}
#[test]
fn find_dependency_of_proc_macro_dependency_with_target() {
let workspace = project("workspace")
.file("Cargo.toml", r#"
[workspace]
members = ["root", "proc_macro_dep"]
"#)
.file("root/Cargo.toml", r#"
[project]
name = "root"
version = "0.1.0"
authors = []
[dependencies]
proc_macro_dep = { path = "../proc_macro_dep" }
"#)
.file("root/src/lib.rs", r#"
#[macro_use]
extern crate proc_macro_dep;
#[derive(Noop)]
pub struct X;
"#)
.file("proc_macro_dep/Cargo.toml", r#"
[project]
name = "proc_macro_dep"
version = "0.1.0"
authors = []
[lib]
proc-macro = true
[dependencies]
bar = "^0.1"
"#)
.file("proc_macro_dep/src/lib.rs", r#"
extern crate bar;
extern crate proc_macro;
use proc_macro::TokenStream;
#[proc_macro_derive(Noop)]
pub fn noop(_input: TokenStream) -> TokenStream {
"".parse().unwrap()
}
"#);
Package::new("foo", "0.1.0").publish();
Package::new("bar", "0.1.0")
.dep("foo", "0.1")
.file("src/lib.rs", "extern crate foo;")
.publish();
workspace.build();
assert_that(workspace.cargo("test").arg("--all").arg("--target").arg(rustc_host()),
execs().with_status(0));
}