Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate run-make/c-link-to-rust-dylib to rmake.rs #125808

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not-a-review-comment: I am always paranoid about these extensions, I feel like there's almost always edge cases I'm missing

if is_darwin() {
"dylib"
} else if is_windows() {
"dll"
} else {
"so"
}
}

Expand Down Expand Up @@ -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.
Expand All @@ -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());
}
}

Expand Down
1 change: 0 additions & 1 deletion src/tools/tidy/src/allowed_run_make_makefiles.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 0 additions & 21 deletions tests/run-make/c-link-to-rust-dylib/Makefile

This file was deleted.

41 changes: 41 additions & 0 deletions tests/run-make/c-link-to-rust-dylib/rmake.rs
Original file line number Diff line number Diff line change
@@ -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");
}
Loading