Skip to content

Commit

Permalink
Auto merge of #6167 - ehuss:fix-dylib-feature-flip, r=alexcrichton
Browse files Browse the repository at this point in the history
Fix dylib reuse with uplift.

Due to #5460, the files that are copied to the root of the target dir depend on whether or not it is a direct target. This causes a problem with dylibs if you build a dylib directly, and then separately run a target that depends on that dylib.  It will run with the dylib path preferring the root dir, which picks up the old dylib.  This PR swaps the preference of the dylib search path so that the dylib in the `deps` dir is preferred.

This is maybe not the best solution for dynamic dependencies. I can imagine if you are trying to create a package, you'll want all of the shared libs along with the binary, and digging through the `deps` dir is not optimal.  However, unconditionally linking dependencies into the root has issues (which #5460 fixed).  There are other issues because shared libs do not include a hash in the filename.  A grander solution might involve coming up with a better strategy for organizing the `target` directory to avoid conflicts between targets.

Fixes #6162
  • Loading branch information
bors committed Oct 12, 2018
2 parents b490af4 + b29d4d4 commit 9801c50
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ impl<'cfg> Compilation<'cfg> {
} else {
let mut search_path =
super::filter_dynamic_search_path(self.native_dirs.iter(), &self.root_output);
search_path.push(self.root_output.clone());
search_path.push(self.deps_output.clone());
search_path.push(self.root_output.clone());
search_path.extend(self.target_dylib_path.clone());
search_path
};
Expand Down
61 changes: 61 additions & 0 deletions tests/testsuite/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1669,3 +1669,64 @@ fn all_features_all_crates() {

p.cargo("build --all-features --all").run();
}

#[test]
fn feature_off_dylib() {
let p = project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["bar"]
[package]
name = "foo"
version = "0.0.1"
[lib]
crate-type = ["dylib"]
[features]
f1 = []
"#,
)
.file(
"src/lib.rs",
r#"
pub fn hello() -> &'static str {
if cfg!(feature = "f1") {
"f1"
} else {
"no f1"
}
}
"#,
)
.file(
"bar/Cargo.toml",
r#"
[package]
name = "bar"
version = "0.0.1"
[dependencies]
foo = { path = ".." }
"#,
)
.file(
"bar/src/main.rs",
r#"
extern crate foo;
fn main() {
assert_eq!(foo::hello(), "no f1");
}
"#,
)
.build();

// Build the dylib with `f1` feature.
p.cargo("build --features f1").run();
// Check that building without `f1` uses a dylib without `f1`.
p.cargo("run -p bar").run();
}

0 comments on commit 9801c50

Please sign in to comment.