Skip to content

Commit

Permalink
Auto merge of #3651 - pkgw:pr-issue-3366, r=alexcrichton
Browse files Browse the repository at this point in the history
Attempt to solve #3366, regarding spurious shared library search paths.

This drops `native_dirs` entries that are not within the target output when modifying the (DY)LD_LIBRARY_PATH environment variable before running programs.

CC #3366
  • Loading branch information
bors committed Feb 7, 2017
2 parents ce9ddf3 + d545a9f commit fa1b12a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
12 changes: 9 additions & 3 deletions src/cargo/ops/cargo_rustc/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct Compilation<'cfg> {
/// An array of all binaries created.
pub binaries: Vec<PathBuf>,

/// All directires for the output of native build commands.
/// All directories for the output of native build commands.
///
/// This is currently used to drive some entries which are added to the
/// LD_LIBRARY_PATH as appropriate.
Expand Down Expand Up @@ -104,7 +104,10 @@ impl<'cfg> Compilation<'cfg> {
} else {
let mut search_path = vec![];

// Add -L arguments, after stripping off prefixes like "native=" or "framework=".
// Add -L arguments, after stripping off prefixes like "native="
// or "framework=" and filtering out directories *not* inside our
// output directory, since they are likely spurious and can cause
// clashes with system shared libraries (issue #3366).
for dir in self.native_dirs.iter() {
let dir = match dir.to_str() {
Some(s) => {
Expand All @@ -120,7 +123,10 @@ impl<'cfg> Compilation<'cfg> {
}
None => dir.clone(),
};
search_path.push(dir);

if dir.starts_with(&self.root_output) {
search_path.push(dir);
}
}
search_path.push(self.root_output.clone());
search_path.push(self.deps_output.clone());
Expand Down
35 changes: 21 additions & 14 deletions tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,30 +597,37 @@ hello

#[test]
fn run_with_library_paths() {
let p = project("foo")
.file("Cargo.toml", r#"
let mut p = project("foo");

// Only link search directories within the target output directory are
// propagated through to dylib_path_envvar() (see #3366).
let mut dir1 = p.target_debug_dir();
dir1.push("foo\\backslash");

let mut dir2 = p.target_debug_dir();
dir2.push("dir=containing=equal=signs");

p = p.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
build = "build.rs"
"#)
.file("build.rs", r#"
fn main() {
println!("cargo:rustc-link-search=native=foo");
println!("cargo:rustc-link-search=bar");
println!("cargo:rustc-link-search=/path=containing=equal=signs");
}
"#)
.file("src/main.rs", &format!(r#"
.file("build.rs", &format!(r##"
fn main() {{
println!(r#"cargo:rustc-link-search=native={}"#);
println!(r#"cargo:rustc-link-search={}"#);
}}
"##, dir1.display(), dir2.display()))
.file("src/main.rs", &format!(r##"
fn main() {{
let search_path = std::env::var_os("{}").unwrap();
let paths = std::env::split_paths(&search_path).collect::<Vec<_>>();
assert!(paths.contains(&"foo".into()));
assert!(paths.contains(&"bar".into()));
assert!(paths.contains(&"/path=containing=equal=signs".into()));
assert!(paths.contains(&r#"{}"#.into()));
assert!(paths.contains(&r#"{}"#.into()));
}}
"#, dylib_path_envvar()));
"##, dylib_path_envvar(), dir1.display(), dir2.display()));

assert_that(p.cargo_process("run"), execs().with_status(0));
}
Expand Down

0 comments on commit fa1b12a

Please sign in to comment.