Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 83bab81

Browse files
committed
Be a bit more robust around path handling; support CARGO_TARGET_DIR
1 parent 9beac39 commit 83bab81

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

crates/libm-cdylib/src/test_utils.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,30 @@ where
5151
let mut cmd = process::Command::new(path);
5252

5353
// Find the cdylib - we just support standard locations for now.
54-
let libm_path = format!(
55-
"../../target/{}/liblibm",
56-
if cfg!(release_profile) {
57-
"release"
58-
} else {
59-
"debug"
60-
},
61-
);
54+
let target_dir = if let Ok(dir) = std::env::var("CARGO_TARGET_DIR") {
55+
std::path::PathBuf::from(&dir)
56+
} else {
57+
Path::new("../../target").into()
58+
};
59+
let libm_path = target_dir.join(Path::new(if cfg!(release_profile) {
60+
"release"
61+
} else {
62+
"debug"
63+
}));
6264

6365
// Replace libm at runtime
6466
if cfg!(target_os = "macos") {
67+
let lib_name = format!("liblibm.{}", "dylib");
68+
let lib_path = libm_path.join(Path::new(&lib_name));
6569
// for debugging:
6670
// cmd.env("DYLD_PRINT_LIBRARIES", "1");
6771
// cmd.env("X", "1");
6872
cmd.env("DYLD_FORCE_FLAT_NAMESPACE", "1");
69-
cmd.env(
70-
"DYLD_INSERT_LIBRARIES",
71-
format!("{}.{}", libm_path, "dylib"),
72-
);
73+
cmd.env("DYLD_INSERT_LIBRARIES", lib_path.display().to_string());
7374
} else if cfg!(target_os = "linux") {
74-
cmd.env("LD_PRELOAD", format!("{}.{}", libm_path, "so"));
75+
let lib_name = format!("liblibm.{}", "so");
76+
let lib_path = libm_path.join(Path::new(&lib_name));
77+
cmd.env("LD_PRELOAD", lib_path.display().to_string());
7578
}
7679
// Run the binary:
7780
let output = cmd.output().unwrap();

0 commit comments

Comments
 (0)