Skip to content

Commit 710e7b0

Browse files
committed
rewrite c-dynamic-dylib to rmake
1 parent 63286d8 commit 710e7b0

File tree

7 files changed

+69
-27
lines changed

7 files changed

+69
-27
lines changed

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

+35-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
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};
9+
10+
// FIXME(Oneirical): These native build functions should take a Path-based generic.
811

912
/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
1013
#[track_caller]
@@ -25,3 +28,33 @@ pub fn build_native_static_lib(lib_name: &str) -> PathBuf {
2528
llvm_ar().obj_to_ar().output_input(&lib_path, &obj_file).run();
2629
path(lib_path)
2730
}
31+
32+
/// Builds a dynamic lib. The filename is computed in a target-dependent manner, relying on
33+
/// [`std::env::consts::DLL_PREFIX`] and [`std::env::consts::DLL_EXTENSION`].
34+
#[track_caller]
35+
pub fn build_native_dynamic_lib(lib_name: &str) -> PathBuf {
36+
let obj_file = if is_msvc() { format!("{lib_name}") } else { format!("{lib_name}.o") };
37+
let src = format!("{lib_name}.c");
38+
let lib_path = dynamic_lib_name(lib_name);
39+
if is_msvc() {
40+
cc().arg("-c").out_exe(&obj_file).input(src).run();
41+
} else {
42+
cc().arg("-v").arg("-c").out_exe(&obj_file).input(src).run();
43+
};
44+
let obj_file = if is_msvc() { format!("{lib_name}.obj") } else { format!("{lib_name}.o") };
45+
if is_msvc() {
46+
let mut out_arg = "-out:".to_owned();
47+
out_arg.push_str(&get_windows_path(&lib_path));
48+
cc().input(&obj_file).args(&["-link", "-dll", &out_arg]).run();
49+
} else if is_darwin() {
50+
cc().out_exe(&lib_path).input(&obj_file).args(&["-dynamiclib", "-Wl,-dylib"]).run();
51+
} else if is_windows() {
52+
cc().out_exe(&lib_path)
53+
.input(&obj_file)
54+
.args(&["-shared", &format!("-Wl,--out-implib={lib_path}.a")])
55+
.run();
56+
} else {
57+
cc().out_exe(&lib_path).input(&obj_file).arg("-shared").run();
58+
}
59+
path(lib_path)
60+
}

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::io;
2-
use std::path::Path;
2+
use std::path::{Path, PathBuf};
33

44
// FIXME(jieyouxu): modify create_symlink to panic on windows.
55

@@ -176,3 +176,16 @@ pub fn set_permissions<P: AsRef<Path>>(path: P, perm: std::fs::Permissions) {
176176
path.as_ref().display()
177177
));
178178
}
179+
180+
/// A function which prints all file names in the directory `dir` similarly to Unix's `ls`.
181+
/// Useful for debugging.
182+
/// Usage: `eprintln!("{:#?}", shallow_find_dir_entries(some_dir));`
183+
#[track_caller]
184+
pub fn shallow_find_dir_entries<P: AsRef<Path>>(dir: P) -> Vec<PathBuf> {
185+
let paths = read_dir(dir);
186+
let mut output = Vec::new();
187+
for path in paths {
188+
output.push(path.unwrap().path());
189+
}
190+
output
191+
}

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

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

4545
// These rely on external dependencies.
46-
pub use c_build::build_native_static_lib;
46+
pub use c_build::{build_native_dynamic_lib, build_native_static_lib};
4747
pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
4848
pub use clang::{clang, Clang};
4949
pub use htmldocck::htmldocck;

src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
run-make/branch-protection-check-IBT/Makefile
2-
run-make/c-dynamic-dylib/Makefile
32
run-make/c-unwind-abi-catch-lib-panic/Makefile
43
run-make/cat-and-grep-sanity-check/Makefile
54
run-make/cdylib-dylib-linkage/Makefile

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

-16
This file was deleted.
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
use run_make_support::{build_native_dynamic_lib, dynamic_lib_name, rfs, run, run_fail, rustc};
9+
10+
fn main() {
11+
build_native_dynamic_lib("cfoo");
12+
rustc().input("foo.rs").arg("-Cprefer-dynamic").run();
13+
rustc().input("bar.rs").run();
14+
run("bar");
15+
rfs::remove_file(dynamic_lib_name("cfoo"));
16+
run_fail("bar");
17+
}

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@
66
//@ ignore-cross-compile
77
// Reason: the compiled binary is executed
88

9-
//FIXME(Oneirical): test on apple because older versions of osx are failing apparently
10-
11-
use run_make_support::{
12-
build_native_dynamic_lib, dynamic_lib_name, fs_wrapper, run, run_fail, rustc,
13-
};
9+
use run_make_support::{build_native_dynamic_lib, dynamic_lib_name, rfs, run, run_fail, rustc};
1410

1511
fn main() {
1612
build_native_dynamic_lib("cfoo");
1713
rustc().input("foo.rs").run();
1814
rustc().input("bar.rs").run();
1915
run("bar");
20-
fs_wrapper::remove_file(dynamic_lib_name("cfoo"));
16+
rfs::remove_file(dynamic_lib_name("cfoo"));
2117
run_fail("bar");
2218
}

0 commit comments

Comments
 (0)