Skip to content

Commit 735f49f

Browse files
committed
Auto merge of #6632 - ehuss:beta-fix-macos-fallback-path, r=alexcrichton
[BETA] Fix default DYLD_FALLBACK_LIBRARY_PATH on MacOS. Backport of #6625.
2 parents a8e5481 + 819e7e2 commit 735f49f

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

src/cargo/core/compiler/compilation.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,17 @@ impl<'cfg> Compilation<'cfg> {
196196
};
197197

198198
search_path.extend(util::dylib_path().into_iter());
199+
if cfg!(target_os = "macos") {
200+
// These are the defaults when DYLD_FALLBACK_LIBRARY_PATH isn't
201+
// set. Since Cargo is explicitly setting the value, make sure the
202+
// defaults still work.
203+
if let Ok(home) = env::var("HOME") {
204+
search_path.push(PathBuf::from(home).join("lib"));
205+
}
206+
search_path.push(PathBuf::from("/usr/local/lib"));
207+
search_path.push(PathBuf::from("/lib"));
208+
search_path.push(PathBuf::from("/usr/lib"));
209+
}
199210
let search_path = join_paths(&search_path, util::dylib_path_envvar())?;
200211

201212
cmd.env(util::dylib_path_envvar(), &search_path);

src/cargo/util/paths.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ pub fn dylib_path_envvar() -> &'static str {
4545
// find the library in the install path.
4646
// Setting DYLD_LIBRARY_PATH can easily have unintended
4747
// consequences.
48+
//
49+
// Also, DYLD_LIBRARY_PATH appears to have significant performance
50+
// penalty starting in 10.13. Cargo's testsuite ran more than twice as
51+
// slow with it on CI.
4852
"DYLD_FALLBACK_LIBRARY_PATH"
4953
} else {
5054
"LD_LIBRARY_PATH"

tests/testsuite/run.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,3 +1136,88 @@ fn default_run_workspace() {
11361136
.with_stdout("run-a")
11371137
.run();
11381138
}
1139+
1140+
#[test]
1141+
#[cfg(target_os = "macos")]
1142+
fn run_link_system_path_macos() {
1143+
use crate::support::paths::{self, CargoPathExt};
1144+
use std::fs;
1145+
// Check that the default system library path is honored.
1146+
// First, build a shared library that will be accessed from
1147+
// DYLD_FALLBACK_LIBRARY_PATH.
1148+
let p = project()
1149+
.file(
1150+
"Cargo.toml",
1151+
r#"
1152+
[project]
1153+
name = "foo"
1154+
version = "0.0.1"
1155+
[lib]
1156+
crate-type = ["cdylib"]
1157+
"#,
1158+
)
1159+
.file(
1160+
"src/lib.rs",
1161+
"#[no_mangle] pub extern fn something_shared() {}",
1162+
)
1163+
.build();
1164+
p.cargo("build").run();
1165+
1166+
// This is convoluted. Since this test can't modify things in /usr,
1167+
// this needs to dance around to check that things work.
1168+
//
1169+
// The default DYLD_FALLBACK_LIBRARY_PATH is:
1170+
// $(HOME)/lib:/usr/local/lib:/lib:/usr/lib
1171+
//
1172+
// This will make use of ~/lib in the path, but the default cc link
1173+
// path is /usr/lib:/usr/local/lib. So first need to build in one
1174+
// location, and then move it to ~/lib.
1175+
//
1176+
// 1. Build with rustc-link-search pointing to libfoo so the initial
1177+
// binary can be linked.
1178+
// 2. Move the library to ~/lib
1179+
// 3. Run `cargo run` to make sure it can still find the library in
1180+
// ~/lib.
1181+
//
1182+
// This should be equivalent to having the library in /usr/local/lib.
1183+
let p2 = project()
1184+
.at("bar")
1185+
.file("Cargo.toml", &basic_bin_manifest("bar"))
1186+
.file(
1187+
"src/main.rs",
1188+
r#"
1189+
extern {
1190+
fn something_shared();
1191+
}
1192+
fn main() {
1193+
unsafe { something_shared(); }
1194+
}
1195+
"#,
1196+
)
1197+
.file(
1198+
"build.rs",
1199+
&format!(
1200+
r#"
1201+
fn main() {{
1202+
println!("cargo:rustc-link-lib=foo");
1203+
println!("cargo:rustc-link-search={}");
1204+
}}
1205+
"#,
1206+
p.target_debug_dir().display()
1207+
),
1208+
)
1209+
.build();
1210+
p2.cargo("build").run();
1211+
p2.cargo("test").run();
1212+
1213+
let libdir = paths::home().join("lib");
1214+
fs::create_dir(&libdir).unwrap();
1215+
fs::rename(
1216+
p.target_debug_dir().join("libfoo.dylib"),
1217+
libdir.join("libfoo.dylib"),
1218+
)
1219+
.unwrap();
1220+
p.root().rm_rf();
1221+
p2.cargo("run").run();
1222+
p2.cargo("test").run();
1223+
}

0 commit comments

Comments
 (0)