Skip to content

Commit 0fa1214

Browse files
committed
rewrite c-dynamic-dylib to rmake
1 parent 77e80c1 commit 0fa1214

File tree

6 files changed

+55
-24
lines changed

6 files changed

+55
-24
lines changed

src/tools/run-make-support/src/external_deps/c_build.rs

+33-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::path::PathBuf;
22

3-
use crate::artifact_names::static_lib_name;
3+
use super::cygpath::get_windows_path;
4+
use crate::artifact_names::{dynamic_lib_name, static_lib_name};
45
use crate::external_deps::cc::cc;
56
use crate::external_deps::llvm::llvm_ar;
67
use crate::path_helpers::path;
7-
use crate::targets::is_msvc;
8+
use crate::targets::{is_darwin, is_msvc, is_windows};
89

910
/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
1011
#[track_caller]
@@ -25,3 +26,33 @@ pub fn build_native_static_lib(lib_name: &str) -> PathBuf {
2526
llvm_ar().obj_to_ar().output_input(&lib_path, &obj_file).run();
2627
path(lib_path)
2728
}
29+
30+
/// Builds a dynamic lib (`.dll` on Windows, `.dylib` on OSX and `.so` for Linux) with the given
31+
/// name.
32+
#[track_caller]
33+
pub fn build_native_dynamic_lib(lib_name: &str) -> PathBuf {
34+
let obj_file = if is_msvc() { format!("{lib_name}") } else { format!("{lib_name}.o") };
35+
let src = format!("{lib_name}.c");
36+
let lib_path = dynamic_lib_name(lib_name);
37+
if is_msvc() {
38+
cc().arg("-c").out_exe(&obj_file).input(src).run();
39+
} else {
40+
cc().arg("-v").arg("-c").out_exe(&obj_file).input(src).run();
41+
};
42+
let obj_file = if is_msvc() { format!("{lib_name}.obj") } else { format!("{lib_name}.o") };
43+
if is_msvc() {
44+
let mut out_arg = "-out".to_owned();
45+
out_arg.push_str(&get_windows_path(&lib_path));
46+
cc().input(&obj_file).args(&["-link", "-dll", &out_arg]).run();
47+
} else if is_darwin() {
48+
cc().out_exe(&lib_path)
49+
.input(&obj_file)
50+
.args(&["-shared", &format!("-Wl,--out-implib={lib_path}.a")])
51+
.run();
52+
} else if is_windows() {
53+
cc().out_exe(&lib_path).input(&obj_file).args(&["-dynamiclib", "-Wl,-dylib"]).run();
54+
} else {
55+
cc().out_exe(&lib_path).input(&obj_file).arg("-shared").run();
56+
}
57+
path(lib_path)
58+
}

src/tools/run-make-support/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub use wasmparser;
3737
pub use external_deps::{c_build, cc, clang, htmldocck, llvm, python, rustc, rustdoc};
3838

3939
// These rely on external dependencies.
40-
pub use c_build::build_native_static_lib;
40+
pub use c_build::{build_native_dynamic_lib, build_native_static_lib};
4141
pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
4242
pub use clang::{clang, Clang};
4343
pub use htmldocck::htmldocck;

src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
run-make/archive-duplicate-names/Makefile
22
run-make/atomic-lock-free/Makefile
33
run-make/branch-protection-check-IBT/Makefile
4-
run-make/c-dynamic-dylib/Makefile
54
run-make/c-static-dylib/Makefile
65
run-make/c-static-rlib/Makefile
76
run-make/c-unwind-abi-catch-lib-panic/Makefile

tests/run-make/c-dynamic-dylib/Makefile

-16
This file was deleted.
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This test checks that dynamic Rust linking with C does not encounter any errors in both
2+
// compilation and execution, with dynamic dependencies given preference over static.
3+
// See https://github.com/rust-lang/rust/issues/10434
4+
5+
//@ ignore-cross-compile
6+
// Reason: the compiled binary is executed
7+
8+
//FIXME(Oneirical): test on apple because older versions of osx are failing apparently
9+
10+
use run_make_support::{build_native_dynamic_lib, dynamic_lib_name, rfs, run, run_fail, rustc};
11+
12+
fn main() {
13+
build_native_dynamic_lib("cfoo");
14+
rustc().input("foo.rs").arg("-Cprefer-dynamic").run();
15+
rustc().input("bar.rs").run();
16+
run("bar");
17+
rfs::remove_file(dynamic_lib_name("cfoo"));
18+
run_fail("bar");
19+
}

tests/run-make/c-dynamic-rlib/rmake.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88

99
//FIXME(Oneirical): test on apple because older versions of osx are failing apparently
1010

11-
use run_make_support::{
12-
build_native_dynamic_lib, dynamic_lib_name, fs_wrapper, run, run_fail, rustc,
13-
};
11+
use run_make_support::{build_native_dynamic_lib, dynamic_lib_name, rfs, run, run_fail, rustc};
1412

1513
fn main() {
1614
build_native_dynamic_lib("cfoo");
1715
rustc().input("foo.rs").run();
1816
rustc().input("bar.rs").run();
1917
run("bar");
20-
fs_wrapper::remove_file(dynamic_lib_name("cfoo"));
18+
rfs::remove_file(dynamic_lib_name("cfoo"));
2119
run_fail("bar");
2220
}

0 commit comments

Comments
 (0)