Skip to content

Commit

Permalink
Auto merge of #4447 - lucaskolstad:proc-macro-crates-host-deps-linkag…
Browse files Browse the repository at this point in the history
…e, 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: a298346

**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.
  • Loading branch information
bors committed Aug 31, 2017
2 parents 62f800f + 95c6e68 commit f519d3d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/cargo/ops/cargo_rustc/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_rustc/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions src/cargo/ops/cargo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
57 changes: 56 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

0 comments on commit f519d3d

Please sign in to comment.