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

Commit 7fa7c15

Browse files
committed
Properly link the library
1 parent 53eab6a commit 7fa7c15

File tree

7 files changed

+73
-29
lines changed

7 files changed

+73
-29
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ checked = []
2626
[workspace]
2727
members = [
2828
"crates/compiler-builtins-smoke-test",
29-
"crates/libm-cdylib",
3029
"crates/libm-bench",
30+
"crates/libm-cdylib",
3131
]
3232

3333
[dev-dependencies]

azure-pipelines.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ jobs:
3737
TARGET: powerpc64-unknown-linux-gnu
3838
powerpc64le:
3939
TARGET: powerpc64le-unknown-linux-gnu
40-
x86_64:
40+
x86_64_stable:
4141
TARGET: x86_64-unknown-linux-gnu
42+
x86_64_nightly:
43+
TARGET: x86_64-unknown-linux-gnu
44+
TOOLCHAIN: nightly
4245

4346
- job: OSX
4447
pool:

ci/run.sh

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ fi
2424
$CMD --features "stable checked"
2525
$CMD --release --features "stable checked ${TEST_MUSL}"
2626

27-
if [ "$TARGET" = "x86_64-unknown-linux-gnu" ] || [ "${TARGET}" = "x86_64-apple-darwin" ]; then
28-
(
29-
cd crates/libm-cdylib
30-
cargo test
31-
cargo test --release
32-
)
27+
if rustc --version | grep "nightly" ; then
28+
if [ "$TARGET" = "x86_64-unknown-linux-gnu" ] || [ "${TARGET}" = "x86_64-apple-darwin" ]; then
29+
(
30+
cd crates/libm-cdylib
31+
cargo test
32+
cargo test --release
33+
)
34+
fi
3335
fi

crates/libm-cdylib/build.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,13 @@ fn main() {
55
if profile == "release" {
66
println!("cargo:rustc-cfg=release_profile");
77
}
8+
let nightly = {
9+
let mut cmd = std::process::Command::new("rustc");
10+
cmd.arg("--version");
11+
let output = String::from_utf8(cmd.output().unwrap().stdout).unwrap();
12+
output.contains("nightly")
13+
};
14+
if nightly {
15+
println!("cargo:rustc-cfg=unstable_rust");
16+
}
817
}

crates/libm-cdylib/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
#![cfg(
2+
// The tests are only enabled on x86 32/64-bit linux/macos:
3+
all(unstable_rust,
4+
any(target_os = "linux", target_os = "macos"),
5+
any(target_arch = "x86", target_arch = "x86_64")
6+
)
7+
)]
18
#![allow(dead_code)]
29
#![cfg_attr(not(test), feature(core_intrinsics, lang_items))]
310
#![cfg_attr(not(test), no_std)]

crates/libm-cdylib/src/macros.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ macro_rules! export {
2525
fn [<$id _link_test>]() {
2626
use crate::test_utils::*;
2727

28+
// This re-compiles the dynamic library:
29+
compile_cdylib();
30+
2831
// Generate a small C program that calls the C API from
2932
// <math.h>. This program prints the result into an appropriate
3033
// type, that is then printed to stdout.
@@ -55,10 +58,9 @@ macro_rules! export {
5558

5659
// We now compile the C program into an executable, make sure
5760
// that the libm-cdylib has been generated (and generate it if
58-
// it isn't), and then we run the program, override the libm
59-
// dynamically, and verify the result.
61+
// it isn't), and then we run the program, override the libm,
62+
// and verify the result.
6063
compile_file(&src_path, &bin_path);
61-
compile_cdylib();
6264
check(&bin_path, $test_ret as $ret_ty)
6365
}
6466
}

crates/libm-cdylib/src/test_utils.rs

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ pub(crate) fn compile_file(src_path: &Path, bin_path: &Path) {
4545
.arg("-o")
4646
.arg(bin_path)
4747
.arg(src_path);
48+
49+
// Link our libm
50+
let lib_path = cdylib_dir();
51+
{
52+
let mut ls = process::Command::new("ls");
53+
ls.arg(lib_path.clone());
54+
let output = ls.output().unwrap();
55+
let output = String::from_utf8(output.stdout).unwrap();
56+
println!("ls\n{}]n", output);
57+
}
58+
cmd.arg(format!("-L{}", lib_path.display()));
59+
cmd.arg("-llibm");
60+
61+
eprintln!("compile cmd: {:?}", cmd);
62+
4863
handle_err(
4964
&format!("compile file: {}", src_path.display()),
5065
&cmd.output().unwrap(),
@@ -59,25 +74,12 @@ where
5974
{
6075
let mut cmd = process::Command::new(path);
6176

62-
// Find the cdylib - we just support standard locations for now.
63-
let libm_path = target_dir().join(if cfg!(release_profile) {
64-
"release"
65-
} else {
66-
"debug"
67-
});
68-
69-
// Replace libm at runtime
70-
if cfg!(target_os = "macos") {
71-
let lib_path = libm_path.join("liblibm.dylib");
72-
// for debugging:
73-
// cmd.env("DYLD_PRINT_LIBRARIES", "1");
74-
// cmd.env("X", "1");
75-
cmd.env("DYLD_FORCE_FLAT_NAMESPACE", "1");
76-
cmd.env("DYLD_INSERT_LIBRARIES", lib_path.display().to_string());
77-
} else if cfg!(target_os = "linux") {
78-
let lib_path = libm_path.join("liblibm.so");
79-
cmd.env("LD_PRELOAD", lib_path.display().to_string());
77+
if cfg!(target_os = "linux") {
78+
let ld_library_path = std::env::var("LD_LIBRARY_PATH").unwrap_or_default();
79+
let ld_library_path = format!("{}:{}", cdylib_dir().display(), ld_library_path);
80+
cmd.env("LD_LIBRARY_PATH", ld_library_path);
8081
}
82+
8183
// Run the binary:
8284
let output = cmd.output().unwrap();
8385
handle_err(&format!("run file: {}", path.display()), &output);
@@ -150,3 +152,22 @@ pub(crate) fn target_dir() -> std::path::PathBuf {
150152
Path::new("../../target").into()
151153
}
152154
}
155+
156+
pub(crate) fn cdylib_dir() -> std::path::PathBuf {
157+
target_dir().join(if cfg!(release_profile) {
158+
"release"
159+
} else {
160+
"debug"
161+
})
162+
}
163+
164+
pub(crate) fn cdylib_path() -> std::path::PathBuf {
165+
let libm_path = cdylib_dir();
166+
if cfg!(target_os = "macos") {
167+
libm_path.join("liblibm.dylib")
168+
} else if cfg!(target_os = "linux") {
169+
libm_path.join("liblibm.so")
170+
} else {
171+
panic!("unsupported target_os")
172+
}
173+
}

0 commit comments

Comments
 (0)