about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJubilee <46493976+workingjubilee@users.noreply.github.com>2024-06-02 12:58:08 -0700
committerGitHub <noreply@github.com>2024-06-02 12:58:08 -0700
commit800b2f8b64151933c9b60685b8a1f1bfbb8f46a3 (patch)
tree0213f425cd4771b13961986bdfd8caad3ae818bb
parent713cdcd8036fe6a8639100efc93a31a58756356e (diff)
parent32933a6869281aaa7718be4c42f5a2b0c9c9c4cf (diff)
downloadrust-800b2f8b64151933c9b60685b8a1f1bfbb8f46a3.tar.gz
rust-800b2f8b64151933c9b60685b8a1f1bfbb8f46a3.zip
Rollup merge of #125808 - GuillaumeGomez:migrate-run-make-c-link-to-rust-dylib, r=jieyouxu
Migrate `run-make/c-link-to-rust-dylib` to `rmake.rs`

Part of https://github.com/rust-lang/rust/issues/121876.

First commit comes from https://github.com/rust-lang/rust/pull/125773.

r? `@jieyouxu`
-rw-r--r--src/tools/run-make-support/src/lib.rs38
-rw-r--r--src/tools/tidy/src/allowed_run_make_makefiles.txt1
-rw-r--r--tests/run-make/c-link-to-rust-dylib/Makefile21
-rw-r--r--tests/run-make/c-link-to-rust-dylib/rmake.rs41
4 files changed, 67 insertions, 34 deletions
diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs
index 0cf64db6ac9..323fc40e648 100644
--- a/src/tools/run-make-support/src/lib.rs
+++ b/src/tools/run-make-support/src/lib.rs
@@ -123,12 +123,23 @@ pub fn dynamic_lib_name(name: &str) -> String {
     // ```
     assert!(!name.contains(char::is_whitespace), "dynamic library name cannot contain whitespace");
 
+    let extension = dynamic_lib_extension();
     if is_darwin() {
-        format!("lib{name}.dylib")
+        format!("lib{name}.{extension}")
     } else if is_windows() {
-        format!("{name}.dll")
+        format!("{name}.{extension}")
     } else {
-        format!("lib{name}.so")
+        format!("lib{name}.{extension}")
+    }
+}
+
+pub fn dynamic_lib_extension() -> &'static str {
+    if is_darwin() {
+        "dylib"
+    } else if is_windows() {
+        "dll"
+    } else {
+        "so"
     }
 }
 
@@ -249,16 +260,13 @@ pub fn recursive_diff(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
     }
 
     let dir2 = dir2.as_ref();
-    for entry in fs::read_dir(dir1).unwrap() {
-        let entry = entry.unwrap();
-        let entry_name = entry.file_name();
-        let path = entry.path();
-
-        if path.is_dir() {
-            recursive_diff(&path, &dir2.join(entry_name));
+    read_dir(dir1, |entry_path| {
+        let entry_name = entry_path.file_name().unwrap();
+        if entry_path.is_dir() {
+            recursive_diff(&entry_path, &dir2.join(entry_name));
         } else {
             let path2 = dir2.join(entry_name);
-            let file1 = read_file(&path);
+            let file1 = read_file(&entry_path);
             let file2 = read_file(&path2);
 
             // We don't use `assert_eq!` because they are `Vec<u8>`, so not great for display.
@@ -267,10 +275,16 @@ pub fn recursive_diff(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
             assert!(
                 file1 == file2,
                 "`{}` and `{}` have different content",
-                path.display(),
+                entry_path.display(),
                 path2.display(),
             );
         }
+    });
+}
+
+pub fn read_dir<F: Fn(&Path)>(dir: impl AsRef<Path>, callback: F) {
+    for entry in fs::read_dir(dir).unwrap() {
+        callback(&entry.unwrap().path());
     }
 }
 
diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt
index b36cde72ff4..96a27610500 100644
--- a/src/tools/tidy/src/allowed_run_make_makefiles.txt
+++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt
@@ -6,7 +6,6 @@ run-make/bare-outfile/Makefile
 run-make/branch-protection-check-IBT/Makefile
 run-make/c-dynamic-dylib/Makefile
 run-make/c-dynamic-rlib/Makefile
-run-make/c-link-to-rust-dylib/Makefile
 run-make/c-static-dylib/Makefile
 run-make/c-static-rlib/Makefile
 run-make/c-unwind-abi-catch-lib-panic/Makefile
diff --git a/tests/run-make/c-link-to-rust-dylib/Makefile b/tests/run-make/c-link-to-rust-dylib/Makefile
deleted file mode 100644
index 201f717ece4..00000000000
--- a/tests/run-make/c-link-to-rust-dylib/Makefile
+++ /dev/null
@@ -1,21 +0,0 @@
-# This test checks that C linking with Rust does not encounter any errors, with dynamic libraries.
-# See https://github.com/rust-lang/rust/issues/10434
-
-# ignore-cross-compile
-include ../tools.mk
-
-all: $(TMPDIR)/$(call BIN,bar)
-	$(call RUN,bar)
-	$(call REMOVE_DYLIBS,foo)
-	$(call FAIL,bar)
-
-ifdef IS_MSVC
-$(TMPDIR)/$(call BIN,bar): $(call DYLIB,foo)
-	$(CC) bar.c $(TMPDIR)/foo.dll.lib $(call OUT_EXE,bar)
-else
-$(TMPDIR)/$(call BIN,bar): $(call DYLIB,foo)
-	$(CC) bar.c -lfoo -o $(call RUN_BINFILE,bar) -L $(TMPDIR)
-endif
-
-$(call DYLIB,foo): foo.rs
-	$(RUSTC) foo.rs
diff --git a/tests/run-make/c-link-to-rust-dylib/rmake.rs b/tests/run-make/c-link-to-rust-dylib/rmake.rs
new file mode 100644
index 00000000000..5c4b6d78649
--- /dev/null
+++ b/tests/run-make/c-link-to-rust-dylib/rmake.rs
@@ -0,0 +1,41 @@
+// This test checks that C linking with Rust does not encounter any errors, with dynamic libraries.
+// See <https://github.com/rust-lang/rust/issues/10434>.
+
+//@ ignore-cross-compile
+
+use std::fs::remove_file;
+
+use run_make_support::{
+    cc, dynamic_lib_extension, is_msvc, read_dir, run, run_fail, rustc, tmp_dir,
+};
+
+fn main() {
+    rustc().input("foo.rs").run();
+
+    if is_msvc() {
+        let lib = tmp_dir().join("foo.dll.lib");
+
+        cc().input("bar.c").arg(lib).out_exe("bar").run();
+    } else {
+        cc().input("bar.c")
+            .arg("-lfoo")
+            .output(tmp_dir().join("bar"))
+            .library_search_path(tmp_dir())
+            .run();
+    }
+
+    run("bar");
+
+    let expected_extension = dynamic_lib_extension();
+    read_dir(tmp_dir(), |path| {
+        if path.is_file()
+            && path.extension().is_some_and(|ext| ext == expected_extension)
+            && path.file_name().and_then(|name| name.to_str()).is_some_and(|name| {
+                name.ends_with(".so") || name.ends_with(".dll") || name.ends_with(".dylib")
+            })
+        {
+            remove_file(path).unwrap();
+        }
+    });
+    run_fail("bar");
+}