Skip to content

Commit 3f57277

Browse files
committed
rewrite raw-dylib-alt-calling-conventions to rmake
1 parent d53dc75 commit 3f57277

File tree

5 files changed

+64
-28
lines changed

5 files changed

+64
-28
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

-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ 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
6362
run-make/raw-dylib-c/Makefile
6463
run-make/raw-dylib-import-name-type/Makefile
6564
run-make/raw-dylib-link-ordinal/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+
}

0 commit comments

Comments
 (0)