rewrite c-dynamic-dylib to rmake · rust-lang/rust@2e3470e (original) (raw)

7 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
1 1 use std::path::PathBuf;
2 2
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};
4 5 use crate::external_deps::cc::cc;
5 6 use crate::external_deps::llvm::llvm_ar;
6 7 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.
8 11
9 12 /// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
10 13 #[track_caller]
@@ -25,3 +28,33 @@ pub fn build_native_static_lib(lib_name: &str) -> PathBuf {
25 28 llvm_ar().obj_to_ar().output_input(&lib_path, &obj_file).run();
26 29 path(lib_path)
27 30 }
31 +
32 +/// Builds a dynamic lib. The filename is computed in a target-dependent manner, relying on
33 +/// [`std::env::consts::DLL_PERFIX`] 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 +}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1 1 use std::io;
2 -use std::path::Path;
2 +use std::path::{Path, PathBuf};
3 3
4 4 // FIXME(jieyouxu): modify create_symlink to panic on windows.
5 5
@@ -176,3 +176,16 @@ pub fn set_permissions<P: AsRef>(path: P, perm: std::fs::Permissions) {
176 176 path.as_ref().display()
177 177 ));
178 178 }
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 +}
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ pub use wasmparser;
43 43 pub use external_deps::{c_build, cc, clang, htmldocck, llvm, python, rustc, rustdoc};
44 44
45 45 // 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};
47 47 pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
48 48 pub use clang::{clang, Clang};
49 49 pub use htmldocck::htmldocck;
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
1 1 run-make/branch-protection-check-IBT/Makefile
2 -run-make/c-dynamic-dylib/Makefile
3 2 run-make/c-unwind-abi-catch-lib-panic/Makefile
4 3 run-make/cat-and-grep-sanity-check/Makefile
5 4 run-make/cdylib-dylib-linkage/Makefile
Original file line number Diff line number Diff 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 +}
Original file line number Diff line number Diff line change
@@ -6,17 +6,13 @@
6 6 //@ ignore-cross-compile
7 7 // Reason: the compiled binary is executed
8 8
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};
14 10
15 11 fn main() {
16 12 build_native_dynamic_lib("cfoo");
17 13 rustc().input("foo.rs").run();
18 14 rustc().input("bar.rs").run();
19 15 run("bar");
20 -fs_wrapper::remove_file(dynamic_lib_name("cfoo"));
16 +rfs::remove_file(dynamic_lib_name("cfoo"));
21 17 run_fail("bar");
22 18 }