Skip to content

Commit

Permalink
Auto merge of #127872 - Oneirical:antestral-traditions, r=<try>
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Jul 17, 2024
2 parents f00f850 + 0fa1214 commit 0fdfa4c
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 56 deletions.
35 changes: 33 additions & 2 deletions src/tools/run-make-support/src/external_deps/c_build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::path::PathBuf;

use crate::artifact_names::static_lib_name;
use super::cygpath::get_windows_path;
use crate::artifact_names::{dynamic_lib_name, static_lib_name};
use crate::external_deps::cc::cc;
use crate::external_deps::llvm::llvm_ar;
use crate::path_helpers::path;
use crate::targets::is_msvc;
use crate::targets::{is_darwin, is_msvc, is_windows};

/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
#[track_caller]
Expand All @@ -25,3 +26,33 @@ pub fn build_native_static_lib(lib_name: &str) -> PathBuf {
llvm_ar().obj_to_ar().output_input(&lib_path, &obj_file).run();
path(lib_path)
}

/// Builds a dynamic lib (`.dll` on Windows, `.dylib` on OSX and `.so` for Linux) with the given
/// name.
#[track_caller]
pub fn build_native_dynamic_lib(lib_name: &str) -> PathBuf {
let obj_file = if is_msvc() { format!("{lib_name}") } else { format!("{lib_name}.o") };
let src = format!("{lib_name}.c");
let lib_path = dynamic_lib_name(lib_name);
if is_msvc() {
cc().arg("-c").out_exe(&obj_file).input(src).run();
} else {
cc().arg("-v").arg("-c").out_exe(&obj_file).input(src).run();
};
let obj_file = if is_msvc() { format!("{lib_name}.obj") } else { format!("{lib_name}.o") };
if is_msvc() {
let mut out_arg = "-out".to_owned();
out_arg.push_str(&get_windows_path(&lib_path));
cc().input(&obj_file).args(&["-link", "-dll", &out_arg]).run();
} else if is_darwin() {
cc().out_exe(&lib_path)
.input(&obj_file)
.args(&["-shared", &format!("-Wl,--out-implib={lib_path}.a")])
.run();
} else if is_windows() {
cc().out_exe(&lib_path).input(&obj_file).args(&["-dynamiclib", "-Wl,-dylib"]).run();
} else {
cc().out_exe(&lib_path).input(&obj_file).arg("-shared").run();
}
path(lib_path)
}
2 changes: 1 addition & 1 deletion src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub use wasmparser;
pub use external_deps::{c_build, cc, clang, htmldocck, llvm, python, rustc, rustdoc};

// These rely on external dependencies.
pub use c_build::build_native_static_lib;
pub use c_build::{build_native_dynamic_lib, build_native_static_lib};
pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
pub use clang::{clang, Clang};
pub use htmldocck::htmldocck;
Expand Down
3 changes: 0 additions & 3 deletions src/tools/tidy/src/allowed_run_make_makefiles.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
run-make/archive-duplicate-names/Makefile
run-make/atomic-lock-free/Makefile
run-make/branch-protection-check-IBT/Makefile
run-make/c-dynamic-dylib/Makefile
run-make/c-dynamic-rlib/Makefile
run-make/c-static-dylib/Makefile
run-make/c-static-rlib/Makefile
run-make/c-unwind-abi-catch-lib-panic/Makefile
Expand Down Expand Up @@ -88,7 +86,6 @@ run-make/pdb-buildinfo-cl-cmd/Makefile
run-make/pgo-gen-lto/Makefile
run-make/pgo-gen-no-imp-symbols/Makefile
run-make/pgo-indirect-call-promotion/Makefile
run-make/pointer-auth-link-with-c/Makefile
run-make/print-calling-conventions/Makefile
run-make/print-target-list/Makefile
run-make/raw-dylib-alt-calling-convention/Makefile
Expand Down
16 changes: 0 additions & 16 deletions tests/run-make/c-dynamic-dylib/Makefile

This file was deleted.

19 changes: 19 additions & 0 deletions tests/run-make/c-dynamic-dylib/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This test checks that dynamic Rust linking with C does not encounter any errors in both
// compilation and execution, with dynamic dependencies given preference over static.
// See https://github.com/rust-lang/rust/issues/10434

//@ ignore-cross-compile
// Reason: the compiled binary is executed

//FIXME(Oneirical): test on apple because older versions of osx are failing apparently

use run_make_support::{build_native_dynamic_lib, dynamic_lib_name, rfs, run, run_fail, rustc};

fn main() {
build_native_dynamic_lib("cfoo");
rustc().input("foo.rs").arg("-Cprefer-dynamic").run();
rustc().input("bar.rs").run();
run("bar");
rfs::remove_file(dynamic_lib_name("cfoo"));
run_fail("bar");
}
19 changes: 0 additions & 19 deletions tests/run-make/c-dynamic-rlib/Makefile

This file was deleted.

20 changes: 20 additions & 0 deletions tests/run-make/c-dynamic-rlib/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// This test checks that dynamic Rust linking with C does not encounter any errors in both
// compilation and execution, with static dependencies given preference over dynamic.
// (This is the default behaviour.)
// See https://github.com/rust-lang/rust/issues/10434

//@ ignore-cross-compile
// Reason: the compiled binary is executed

//FIXME(Oneirical): test on apple because older versions of osx are failing apparently

use run_make_support::{build_native_dynamic_lib, dynamic_lib_name, rfs, run, run_fail, rustc};

fn main() {
build_native_dynamic_lib("cfoo");
rustc().input("foo.rs").run();
rustc().input("bar.rs").run();
run("bar");
rfs::remove_file(dynamic_lib_name("cfoo"));
run_fail("bar");
}
15 changes: 0 additions & 15 deletions tests/run-make/pointer-auth-link-with-c/Makefile

This file was deleted.

28 changes: 28 additions & 0 deletions tests/run-make/pointer-auth-link-with-c/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// `-Z branch protection` is an unstable compiler feature which adds pointer-authentication
// code (PAC), a useful hashing measure for verifying that pointers have not been modified.
// This test checks that compilation and execution is successful when this feature is activated,
// with some of its possible extra arguments (bti, pac-ret, leaf).
// See https://github.com/rust-lang/rust/pull/88354

//@ only-aarch64
// Reason: branch protection is not supported on other architectures
//@ ignore-cross-compile
// Reason: the compiled binary is executed

use run_make_support::{build_native_static_lib, cc, is_msvc, llvm_ar, run, rustc};

fn main() {
build_native_static_lib("test");
rustc().arg("-Zbranch-protection=bti,pac-ret,leaf").input("test.rs").run();
run("test");
cc().arg("-v")
.arg("-c")
.out_exe("test")
.input("test.c")
.arg("-mbranch-protection=bti+pac-ret+leaf")
.run();
let obj_file = if is_msvc() { "test.obj" } else { "test" };
llvm_ar().obj_to_ar().output_input("libtest.a", &obj_file).run();
rustc().arg("-Zbranch-protection=bti,pac-ret,leaf").input("test.rs").run();
run("test");
}

0 comments on commit 0fdfa4c

Please # to comment.