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

Commit 53eab6a

Browse files
committed
Be a bit more robust around path handling; support CARGO_TARGET_DIR
1 parent 164560a commit 53eab6a

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

ci/run.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#!/usr/bin/env sh
22

33
set -ex
4+
45
TARGET=$1
56

7+
export RUST_BACKTRACE=1
8+
export RUST_TEST_THREADS=1
9+
610
CMD="cargo test --all --no-default-features --target $TARGET"
711

812
$CMD

crates/libm-cdylib/src/macros.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ macro_rules! export {
4747
c_format_s = c_format_s
4848
);
4949

50-
let src = &format!("../../target/{}.c", stringify!($id));
51-
let bin = &format!("../../target/{}", stringify!($id));
52-
let src_path = std::path::Path::new(src);
53-
let bin_path = std::path::Path::new(bin);
50+
let target_dir = target_dir();
51+
eprintln!("target dir: {}", target_dir.display());
52+
let src_path = target_dir.clone().join(format!("{}.c", stringify!($id)));
53+
let bin_path = target_dir.clone().join(format!("{}", stringify!($id)));
5454
write_to_file(&src_path, &ctest);
5555

5656
// We now compile the C program into an executable, make sure

crates/libm-cdylib/src/test_utils.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,16 @@ pub(crate) fn compile_cdylib() {
2525
/// Compiles the test C program with source at `src_path` into
2626
/// an executable at `bin_path`.
2727
pub(crate) fn compile_file(src_path: &Path, bin_path: &Path) {
28-
let mut cmd = process::Command::new("CC");
28+
let cc = if std::env::var("CC").is_ok() {
29+
std::env::var("CC").unwrap().to_string()
30+
} else if cfg!(target_os = "linux") {
31+
"gcc".to_string()
32+
} else if cfg!(target_os = "macos") {
33+
"clang".to_string()
34+
} else {
35+
panic!("unknown platform - Ccompiler not found")
36+
};
37+
let mut cmd = process::Command::new(&cc);
2938
// We disable the usage of builtin functions, e.g., from libm.
3039
// This should ideally produce a link failure if libm is not dynamically
3140
// linked.
@@ -51,27 +60,23 @@ where
5160
let mut cmd = process::Command::new(path);
5261

5362
// 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-
);
63+
let libm_path = target_dir().join(if cfg!(release_profile) {
64+
"release"
65+
} else {
66+
"debug"
67+
});
6268

6369
// Replace libm at runtime
6470
if cfg!(target_os = "macos") {
71+
let lib_path = libm_path.join("liblibm.dylib");
6572
// for debugging:
6673
// cmd.env("DYLD_PRINT_LIBRARIES", "1");
6774
// cmd.env("X", "1");
6875
cmd.env("DYLD_FORCE_FLAT_NAMESPACE", "1");
69-
cmd.env(
70-
"DYLD_INSERT_LIBRARIES",
71-
format!("{}.{}", libm_path, "dylib"),
72-
);
76+
cmd.env("DYLD_INSERT_LIBRARIES", lib_path.display().to_string());
7377
} else if cfg!(target_os = "linux") {
74-
cmd.env("LD_PRELOAD", format!("{}.{}", libm_path, "so"));
78+
let lib_path = libm_path.join("liblibm.so");
79+
cmd.env("LD_PRELOAD", lib_path.display().to_string());
7580
}
7681
// Run the binary:
7782
let output = cmd.output().unwrap();
@@ -137,3 +142,11 @@ pub(crate) fn ctype_and_printf_format_specifier(x: &str) -> (&str, &str) {
137142
_ => panic!("unknown type: {}", x),
138143
}
139144
}
145+
146+
pub(crate) fn target_dir() -> std::path::PathBuf {
147+
if let Ok(dir) = std::env::var("CARGO_TARGET_DIR") {
148+
std::path::PathBuf::from(&dir)
149+
} else {
150+
Path::new("../../target").into()
151+
}
152+
}

0 commit comments

Comments
 (0)