Skip to content

Commit 8f599f3

Browse files
committed
rewrite cdylib-dylib-linkage to rmake
1 parent 2d5a628 commit 8f599f3

File tree

6 files changed

+61
-36
lines changed

6 files changed

+61
-36
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ pub fn dynamic_lib_name(name: &str) -> String {
3838
format!("{}{name}.{}", std::env::consts::DLL_PREFIX, std::env::consts::DLL_EXTENSION)
3939
}
4040

41+
/// Construct the import dynamic library name, exclusive to MSVC and accepted by link.exe.
42+
#[must_use]
43+
pub fn msvc_import_dynamic_lib_name(name: &str) -> String {
44+
assert!(is_msvc(), "this function is exclusive to MSVC");
45+
assert!(!name.contains(char::is_whitespace), "dynamic library name cannot contain whitespace");
46+
47+
format!("{name}.dll.lib")
48+
}
49+
4150
/// Construct the dynamic library extension based on the target.
4251
#[must_use]
4352
pub fn dynamic_lib_extension() -> &'static str {

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,14 @@ pub use targets::{is_darwin, is_msvc, is_windows, llvm_components_contain, targe
7171

7272
/// Helpers for building names of output artifacts that are potentially target-specific.
7373
pub use artifact_names::{
74-
bin_name, dynamic_lib_extension, dynamic_lib_name, rust_lib_name, static_lib_name,
74+
bin_name, dynamic_lib_extension, dynamic_lib_name, msvc_import_dynamic_lib_name, rust_lib_name,
75+
static_lib_name,
7576
};
7677

7778
/// Path-related helpers.
7879
pub use path_helpers::{
79-
cwd, filename_not_in_denylist, has_extension, has_prefix, has_suffix, not_contains, path,
80-
shallow_find_files, source_root,
80+
cwd, filename_contains, filename_not_in_denylist, has_extension, has_prefix, has_suffix,
81+
not_contains, path, shallow_find_files, source_root,
8182
};
8283

8384
/// Helpers for scoped test execution where certain properties are attempted to be maintained.

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::path::{Path, PathBuf};
44

55
use crate::env::env_var;
6+
use crate::rfs;
67

78
/// Return the current working directory.
89
///
@@ -40,7 +41,7 @@ pub fn shallow_find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
4041
filter: F,
4142
) -> Vec<PathBuf> {
4243
let mut matching_files = Vec::new();
43-
for entry in std::fs::read_dir(path).unwrap() {
44+
for entry in rfs::read_dir(path) {
4445
let entry = entry.expect("failed to read directory entry.");
4546
let path = entry.path();
4647

@@ -78,3 +79,8 @@ pub fn has_extension<P: AsRef<Path>>(path: P, extension: &str) -> bool {
7879
pub fn has_suffix<P: AsRef<Path>>(path: P, suffix: &str) -> bool {
7980
path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().ends_with(suffix))
8081
}
82+
83+
/// Returns true if the filename at `path` contains `needle`.
84+
pub fn filename_contains<P: AsRef<Path>>(path: P, needle: &str) -> bool {
85+
path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().contains(needle))
86+
}

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/branch-protection-check-IBT/Makefile
22
run-make/c-unwind-abi-catch-lib-panic/Makefile
33
run-make/cat-and-grep-sanity-check/Makefile
4-
run-make/cdylib-dylib-linkage/Makefile
54
run-make/cross-lang-lto-clang/Makefile
65
run-make/cross-lang-lto-pgo-smoketest/Makefile
76
run-make/cross-lang-lto-upstream-rlibs/Makefile

tests/run-make/cdylib-dylib-linkage/Makefile

-31
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Previously, rustc mandated that cdylibs could only link against rlibs as dependencies,
2+
// making linkage between cdylibs and dylibs impossible. After this was changed in #68448,
3+
// this test attempts to link both `foo` (a cdylib) and `bar` (a dylib) and checks that
4+
// both compilation and execution are successful.
5+
// See https://github.com/rust-lang/rust/pull/68448
6+
7+
//@ ignore-cross-compile
8+
// Reason: the compiled binary is executed
9+
10+
use run_make_support::{
11+
bin_name, cc, dynamic_lib_extension, dynamic_lib_name, filename_contains, has_extension,
12+
has_prefix, has_suffix, is_msvc, msvc_import_dynamic_lib_name, path, run, rustc,
13+
shallow_find_files, target,
14+
};
15+
16+
fn main() {
17+
rustc().arg("-Cprefer-dynamic").input("bar.rs").run();
18+
rustc().input("foo.rs").run();
19+
let sysroot = rustc().print("sysroot").run().stdout_utf8();
20+
let sysroot = sysroot.trim();
21+
let target_sysroot = path(sysroot).join("lib/rustlib").join(target()).join("lib");
22+
if is_msvc() {
23+
let mut libs = shallow_find_files(&target_sysroot, |path| {
24+
has_prefix(path, "libstd-") && has_suffix(path, ".dll.lib")
25+
});
26+
libs.push(path(msvc_import_dynamic_lib_name("foo")));
27+
libs.push(path(msvc_import_dynamic_lib_name("bar")));
28+
cc().input("foo.c").args(&libs).out_exe("foo").run();
29+
} else {
30+
let stdlibs = shallow_find_files(&target_sysroot, |path| {
31+
has_extension(path, dynamic_lib_extension()) && filename_contains(path, "std")
32+
});
33+
cc().input("foo.c")
34+
.args(&[dynamic_lib_name("foo"), dynamic_lib_name("bar")])
35+
.arg(stdlibs.get(0).unwrap())
36+
.library_search_path(&target_sysroot)
37+
.output(bin_name("foo"))
38+
.run();
39+
}
40+
run("foo");
41+
}

0 commit comments

Comments
 (0)