Skip to content

Commit

Permalink
Be a bit more robust around path handling; support CARGO_TARGET_DIR
Browse files Browse the repository at this point in the history
  • Loading branch information
gnzlbg committed Jul 2, 2019
1 parent 9beac39 commit 83bab81
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions crates/libm-cdylib/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,30 @@ where
let mut cmd = process::Command::new(path);

// Find the cdylib - we just support standard locations for now.
let libm_path = format!(
"../../target/{}/liblibm",
if cfg!(release_profile) {
"release"
} else {
"debug"
},
);
let target_dir = if let Ok(dir) = std::env::var("CARGO_TARGET_DIR") {
std::path::PathBuf::from(&dir)
} else {
Path::new("../../target").into()
};
let libm_path = target_dir.join(Path::new(if cfg!(release_profile) {
"release"
} else {
"debug"
}));

// Replace libm at runtime
if cfg!(target_os = "macos") {
let lib_name = format!("liblibm.{}", "dylib");
let lib_path = libm_path.join(Path::new(&lib_name));
// for debugging:
// cmd.env("DYLD_PRINT_LIBRARIES", "1");
// cmd.env("X", "1");
cmd.env("DYLD_FORCE_FLAT_NAMESPACE", "1");
cmd.env(
"DYLD_INSERT_LIBRARIES",
format!("{}.{}", libm_path, "dylib"),
);
cmd.env("DYLD_INSERT_LIBRARIES", lib_path.display().to_string());
} else if cfg!(target_os = "linux") {
cmd.env("LD_PRELOAD", format!("{}.{}", libm_path, "so"));
let lib_name = format!("liblibm.{}", "so");
let lib_path = libm_path.join(Path::new(&lib_name));
cmd.env("LD_PRELOAD", lib_path.display().to_string());
}
// Run the binary:
let output = cmd.output().unwrap();
Expand Down

0 comments on commit 83bab81

Please sign in to comment.