Skip to content

Commit ef5357b

Browse files
committed
Auto merge of #128107 - Oneirical:tomato-hartester, r=<try>
Migrate `raw-dylib-alt-calling-convention`, `raw-dylib-c` and `redundant-libs` `run-make` tests to rmake Part of #121876 and the associated [Google Summer of Code project](https://blog.rust-lang.org/2024/05/01/gsoc-2024-selected-projects.html). Please try: try-job: x86_64-msvc try-job: x86_64-mingw
2 parents 84c257e + 2c740a2 commit ef5357b

File tree

9 files changed

+121
-82
lines changed

9 files changed

+121
-82
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/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_static_lib, build_native_dynamic_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

-3
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,9 @@ run-make/pgo-indirect-call-promotion/Makefile
5959
run-make/pointer-auth-link-with-c/Makefile
6060
run-make/print-calling-conventions/Makefile
6161
run-make/print-target-list/Makefile
62-
run-make/raw-dylib-alt-calling-convention/Makefile
63-
run-make/raw-dylib-c/Makefile
6462
run-make/raw-dylib-import-name-type/Makefile
6563
run-make/raw-dylib-link-ordinal/Makefile
6664
run-make/raw-dylib-stdcall-ordinal/Makefile
67-
run-make/redundant-libs/Makefile
6865
run-make/remap-path-prefix-dwarf/Makefile
6966
run-make/reproducible-build-2/Makefile
7067
run-make/reproducible-build/Makefile

tests/run-make/raw-dylib-alt-calling-convention/Makefile

-24
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// `raw-dylib` is a Windows-specific attribute which emits idata sections for the items in the
2+
// attached extern block,
3+
// so they may be linked against without linking against an import library.
4+
// To learn more, read https://github.com/rust-lang/rfcs/blob/master/text/2627-raw-dylib-kind.md
5+
// This test uses this feature alongside alternative calling conventions, checking that both
6+
// features are compatible and result in the expected output upon execution of the binary.
7+
// See https://github.com/rust-lang/rust/pull/84171
8+
9+
//@ only-x86
10+
//@ only-windows
11+
12+
use run_make_support::{build_native_dynamic_lib, diff, is_msvc, run, run_with_args, rustc};
13+
14+
fn main() {
15+
rustc()
16+
.crate_type("lib")
17+
.crate_name("raw_dylib_alt_calling_convention_test")
18+
.input("lib.rs")
19+
.run();
20+
rustc().crate_type("bin").input("driver.rs").run();
21+
build_native_dynamic_lib("extern");
22+
let out = run("driver").stdout_utf8();
23+
diff().expected_file("output.txt").actual_text("actual", out).run();
24+
if is_msvc() {
25+
let out_msvc = run_with_args("driver", &["true"]).stdout_utf8();
26+
diff().expected_file("output.msvc.txt").actual_text("actual", out_msvc).run();
27+
}
28+
}

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

-28
This file was deleted.

tests/run-make/raw-dylib-c/rmake.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// `raw-dylib` is a Windows-specific attribute which emits idata sections for the items in the
2+
// attached extern block,
3+
// so they may be linked against without linking against an import library.
4+
// To learn more, read https://github.com/rust-lang/rfcs/blob/master/text/2627-raw-dylib-kind.md
5+
// This test is the simplest of the raw-dylib tests, simply smoke-testing that the feature
6+
// can be used to build an executable binary with an expected output with native C files
7+
// compiling into dynamic libraries.
8+
// See https://github.com/rust-lang/rust/pull/86419
9+
10+
//@ only-windows
11+
12+
use run_make_support::{build_native_dynamic_lib, diff, run, rustc};
13+
14+
fn main() {
15+
rustc().crate_type("lib").crate_name("raw_dylib_test").input("lib.rs").run();
16+
rustc().crate_type("bin").input("driver.rs").run();
17+
rustc().crate_type("bin").crate_name("raw_dylib_test_bin").input("lib.rs").run();
18+
build_native_dynamic_lib("extern_1");
19+
build_native_dynamic_lib("extern_2");
20+
let out_driver = run("driver").stdout_utf8();
21+
let out_raw = run("raw_dylib_test_bin").stdout_utf8();
22+
23+
diff()
24+
.expected_file("output.txt")
25+
.actual_text("actual", out_driver)
26+
.normalize(r#"\r"#, "")
27+
.run();
28+
diff().expected_file("output.txt").actual_text("actual", out_raw).run();
29+
}

tests/run-make/redundant-libs/Makefile

-24
This file was deleted.
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// rustc will remove one of the two redundant references to foo below. Depending
2+
// on which one gets removed, we'll get a linker error on SOME platforms (like
3+
// Linux). On these platforms, when a library is referenced, the linker will
4+
// only pull in the symbols needed _at that point in time_. If a later library
5+
// depends on additional symbols from the library, they will not have been pulled
6+
// in, and you'll get undefined symbols errors.
7+
//
8+
// So in this example, we need to ensure that rustc keeps the _later_ reference
9+
// to foo, and not the former one.
10+
11+
//@ ignore-cross-compile
12+
// Reason: the compiled binary is executed
13+
14+
//FIXME(Oneirical): msvc
15+
16+
use run_make_support::{build_native_dynamic_lib, build_native_static_lib, run, rustc};
17+
18+
fn main() {
19+
build_native_dynamic_lib("foo");
20+
build_native_static_lib("bar");
21+
build_native_static_lib("baz");
22+
rustc()
23+
.input("main.rs")
24+
.args(&["-lstatic=bar", "-lfoo", "-lstatic=baz", "-lfoo"])
25+
.print("link-args")
26+
.run();
27+
run("main");
28+
}

0 commit comments

Comments
 (0)