Skip to content

Commit d8c4e27

Browse files
committed
Auto merge of #127872 - Oneirical:antestral-traditions, r=jieyouxu
Migrate `pointer-auth-link-with-c`, `c-dynamic-rlib` and `c-dynamic-dylib` `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: i686-mingw try-job: aarch64-apple
2 parents 8ded134 + 2e3470e commit d8c4e27

File tree

10 files changed

+113
-57
lines changed

10 files changed

+113
-57
lines changed

Diff for: 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_PERFIX`] 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+
}

Diff for: src/tools/run-make-support/src/fs.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::io;
2-
use std::path::Path;
2+
use std::path::{Path, PathBuf};
33

44
// FIXME(jieyouxu): modify create_symlink to panic on windows.
55

@@ -176,3 +176,16 @@ pub fn set_permissions<P: AsRef<Path>>(path: P, perm: std::fs::Permissions) {
176176
path.as_ref().display()
177177
));
178178
}
179+
180+
/// A function which prints all file names in the directory `dir` similarly to Unix's `ls`.
181+
/// Useful for debugging.
182+
/// Usage: `eprintln!("{:#?}", shallow_find_dir_entries(some_dir));`
183+
#[track_caller]
184+
pub fn shallow_find_dir_entries<P: AsRef<Path>>(dir: P) -> Vec<PathBuf> {
185+
let paths = read_dir(dir);
186+
let mut output = Vec::new();
187+
for path in paths {
188+
output.push(path.unwrap().path());
189+
}
190+
output
191+
}

Diff for: 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_dynamic_lib, build_native_static_lib};
4747
pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
4848
pub use clang::{clang, Clang};
4949
pub use htmldocck::htmldocck;

Diff for: src/tools/tidy/src/allowed_run_make_makefiles.txt

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
run-make/branch-protection-check-IBT/Makefile
2-
run-make/c-dynamic-dylib/Makefile
3-
run-make/c-dynamic-rlib/Makefile
42
run-make/c-unwind-abi-catch-lib-panic/Makefile
53
run-make/cat-and-grep-sanity-check/Makefile
64
run-make/cdylib-dylib-linkage/Makefile
@@ -56,7 +54,6 @@ run-make/pdb-buildinfo-cl-cmd/Makefile
5654
run-make/pgo-gen-lto/Makefile
5755
run-make/pgo-gen-no-imp-symbols/Makefile
5856
run-make/pgo-indirect-call-promotion/Makefile
59-
run-make/pointer-auth-link-with-c/Makefile
6057
run-make/print-calling-conventions/Makefile
6158
run-make/print-target-list/Makefile
6259
run-make/raw-dylib-alt-calling-convention/Makefile

Diff for: tests/run-make/c-dynamic-dylib/Makefile

-16
This file was deleted.

Diff for: tests/run-make/c-dynamic-dylib/rmake.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This test checks that dynamic Rust linking with C does not encounter any errors in both
2+
// compilation and execution, with dynamic dependencies given preference over static.
3+
// See https://github.com/rust-lang/rust/issues/10434
4+
5+
//@ ignore-cross-compile
6+
// Reason: the compiled binary is executed
7+
8+
use run_make_support::{build_native_dynamic_lib, dynamic_lib_name, rfs, run, run_fail, rustc};
9+
10+
fn main() {
11+
build_native_dynamic_lib("cfoo");
12+
rustc().input("foo.rs").arg("-Cprefer-dynamic").run();
13+
rustc().input("bar.rs").run();
14+
run("bar");
15+
rfs::remove_file(dynamic_lib_name("cfoo"));
16+
run_fail("bar");
17+
}

Diff for: tests/run-make/c-dynamic-rlib/Makefile

-19
This file was deleted.

Diff for: tests/run-make/c-dynamic-rlib/rmake.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This test checks that dynamic Rust linking with C does not encounter any errors in both
2+
// compilation and execution, with static dependencies given preference over dynamic.
3+
// (This is the default behaviour.)
4+
// See https://github.com/rust-lang/rust/issues/10434
5+
6+
//@ ignore-cross-compile
7+
// Reason: the compiled binary is executed
8+
9+
use run_make_support::{build_native_dynamic_lib, dynamic_lib_name, rfs, run, run_fail, rustc};
10+
11+
fn main() {
12+
build_native_dynamic_lib("cfoo");
13+
rustc().input("foo.rs").run();
14+
rustc().input("bar.rs").run();
15+
run("bar");
16+
rfs::remove_file(dynamic_lib_name("cfoo"));
17+
run_fail("bar");
18+
}

Diff for: tests/run-make/pointer-auth-link-with-c/Makefile

-15
This file was deleted.

Diff for: tests/run-make/pointer-auth-link-with-c/rmake.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// `-Z branch protection` is an unstable compiler feature which adds pointer-authentication
2+
// code (PAC), a useful hashing measure for verifying that pointers have not been modified.
3+
// This test checks that compilation and execution is successful when this feature is activated,
4+
// with some of its possible extra arguments (bti, pac-ret, leaf).
5+
// See https://github.com/rust-lang/rust/pull/88354
6+
7+
//@ only-aarch64
8+
// Reason: branch protection is not supported on other architectures
9+
//@ ignore-cross-compile
10+
// Reason: the compiled binary is executed
11+
12+
use run_make_support::{build_native_static_lib, cc, is_msvc, llvm_ar, run, rustc};
13+
14+
fn main() {
15+
build_native_static_lib("test");
16+
rustc().arg("-Zbranch-protection=bti,pac-ret,leaf").input("test.rs").run();
17+
run("test");
18+
cc().arg("-v")
19+
.arg("-c")
20+
.out_exe("test")
21+
.input("test.c")
22+
.arg("-mbranch-protection=bti+pac-ret+leaf")
23+
.run();
24+
let obj_file = if is_msvc() { "test.obj" } else { "test" };
25+
llvm_ar().obj_to_ar().output_input("libtest.a", &obj_file).run();
26+
rustc().arg("-Zbranch-protection=bti,pac-ret,leaf").input("test.rs").run();
27+
run("test");
28+
}

0 commit comments

Comments
 (0)