Skip to content

Commit

Permalink
Auto merge of rust-lang#128075 - Oneirical:try-your-damnetest, r=jiey…
Browse files Browse the repository at this point in the history
…ouxu

Migrate `rlib-format-packed-bundled-libs-2`, `native-link-modifier-whole-archive` and `no-builtins-attribute` `run-make` tests to rmake

Part of rust-lang#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: test-various
try-job: armhf-gnu
try-job: aarch64-apple
try-job: x86_64-gnu-llvm-18
  • Loading branch information
bors committed Jul 31, 2024
2 parents 249cf71 + 1a15d90 commit 83dcdb3
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 96 deletions.
32 changes: 31 additions & 1 deletion src/tools/run-make-support/src/assertion_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::panic;
use std::path::Path;

use crate::fs;
use crate::{fs, regex};

/// Assert that `actual` is equal to `expected`.
#[track_caller]
Expand Down Expand Up @@ -47,6 +47,36 @@ pub fn assert_not_contains<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N)
}
}

/// Assert that `haystack` contains the regex pattern `needle`.
#[track_caller]
pub fn assert_contains_regex<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N) {
let haystack = haystack.as_ref();
let needle = needle.as_ref();
let re = regex::Regex::new(needle).unwrap();
if !re.is_match(haystack) {
eprintln!("=== HAYSTACK ===");
eprintln!("{}", haystack);
eprintln!("=== NEEDLE ===");
eprintln!("{}", needle);
panic!("needle was not found in haystack");
}
}

/// Assert that `haystack` does not contain the regex pattern `needle`.
#[track_caller]
pub fn assert_not_contains_regex<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N) {
let haystack = haystack.as_ref();
let needle = needle.as_ref();
let re = regex::Regex::new(needle).unwrap();
if re.is_match(haystack) {
eprintln!("=== HAYSTACK ===");
eprintln!("{}", haystack);
eprintln!("=== NEEDLE ===");
eprintln!("{}", needle);
panic!("needle was unexpectedly found in haystack");
}
}

/// Assert that all files in `dir1` exist and have the same content in `dir2`
pub fn assert_dirs_are_equal(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
let dir2 = dir2.as_ref();
Expand Down
33 changes: 32 additions & 1 deletion src/tools/run-make-support/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use std::{ffi, panic};
use build_helper::drop_bomb::DropBomb;

use crate::util::handle_failed_output;
use crate::{assert_contains, assert_equals, assert_not_contains};
use crate::{
assert_contains, assert_contains_regex, assert_equals, assert_not_contains,
assert_not_contains_regex,
};

/// This is a custom command wrapper that simplifies working with commands and makes it easier to
/// ensure that we check the exit status of executed processes.
Expand Down Expand Up @@ -191,13 +194,27 @@ impl CompletedProcess {
self
}

/// Checks that `stdout` does not contain the regex pattern `unexpected`.
#[track_caller]
pub fn assert_stdout_not_contains_regex<S: AsRef<str>>(&self, unexpected: S) -> &Self {
assert_not_contains_regex(&self.stdout_utf8(), unexpected);
self
}

/// Checks that `stdout` contains `expected`.
#[track_caller]
pub fn assert_stdout_contains<S: AsRef<str>>(&self, expected: S) -> &Self {
assert_contains(&self.stdout_utf8(), expected);
self
}

/// Checks that `stdout` contains the regex pattern `expected`.
#[track_caller]
pub fn assert_stdout_contains_regex<S: AsRef<str>>(&self, expected: S) -> &Self {
assert_contains_regex(&self.stdout_utf8(), expected);
self
}

/// Checks that trimmed `stderr` matches trimmed `expected`.
#[track_caller]
pub fn assert_stderr_equals<S: AsRef<str>>(&self, expected: S) -> &Self {
Expand All @@ -212,13 +229,27 @@ impl CompletedProcess {
self
}

/// Checks that `stderr` contains the regex pattern `expected`.
#[track_caller]
pub fn assert_stderr_contains_regex<S: AsRef<str>>(&self, expected: S) -> &Self {
assert_contains_regex(&self.stderr_utf8(), expected);
self
}

/// Checks that `stderr` does not contain `unexpected`.
#[track_caller]
pub fn assert_stderr_not_contains<S: AsRef<str>>(&self, unexpected: S) -> &Self {
assert_not_contains(&self.stdout_utf8(), unexpected);
self
}

/// Checks that `stderr` does not contain the regex pattern `unexpected`.
#[track_caller]
pub fn assert_stderr_not_contains_regex<S: AsRef<str>>(&self, unexpected: S) -> &Self {
assert_not_contains_regex(&self.stdout_utf8(), unexpected);
self
}

#[track_caller]
pub fn assert_exit_code(&self, code: i32) -> &Self {
assert!(self.output.status.code() == Some(code));
Expand Down
30 changes: 30 additions & 0 deletions src/tools/run-make-support/src/external_deps/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ pub fn llvm_ar() -> LlvmAr {
LlvmAr::new()
}

/// Construct a new `llvm-nm` invocation. This assumes that `llvm-nm` is available
/// at `$LLVM_BIN_DIR/llvm-nm`.
pub fn llvm_nm() -> LlvmNm {
LlvmNm::new()
}

/// A `llvm-readobj` invocation builder.
#[derive(Debug)]
#[must_use]
Expand Down Expand Up @@ -71,11 +77,19 @@ pub struct LlvmAr {
cmd: Command,
}

/// A `llvm-nm` invocation builder.
#[derive(Debug)]
#[must_use]
pub struct LlvmNm {
cmd: Command,
}

crate::macros::impl_common_helpers!(LlvmReadobj);
crate::macros::impl_common_helpers!(LlvmProfdata);
crate::macros::impl_common_helpers!(LlvmFilecheck);
crate::macros::impl_common_helpers!(LlvmObjdump);
crate::macros::impl_common_helpers!(LlvmAr);
crate::macros::impl_common_helpers!(LlvmNm);

/// Generate the path to the bin directory of LLVM.
#[must_use]
Expand Down Expand Up @@ -244,3 +258,19 @@ impl LlvmAr {
self
}
}

impl LlvmNm {
/// Construct a new `llvm-nm` invocation. This assumes that `llvm-nm` is available
/// at `$LLVM_BIN_DIR/llvm-nm`.
pub fn new() -> Self {
let llvm_nm = llvm_bin_dir().join("llvm-nm");
let cmd = Command::new(llvm_nm);
Self { cmd }
}

/// Provide an input file.
pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
self.cmd.arg(path.as_ref());
self
}
}
7 changes: 4 additions & 3 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ pub use cc::{cc, cxx, extra_c_flags, extra_cxx_flags, Cc};
pub use clang::{clang, Clang};
pub use htmldocck::htmldocck;
pub use llvm::{
llvm_ar, llvm_filecheck, llvm_objdump, llvm_profdata, llvm_readobj, LlvmAr, LlvmFilecheck,
LlvmObjdump, LlvmProfdata, LlvmReadobj,
llvm_ar, llvm_filecheck, llvm_nm, llvm_objdump, llvm_profdata, llvm_readobj, LlvmAr,
LlvmFilecheck, LlvmNm, LlvmObjdump, LlvmProfdata, LlvmReadobj,
};
pub use python::python_command;
pub use rustc::{aux_build, bare_rustc, rustc, Rustc};
Expand Down Expand Up @@ -84,7 +84,8 @@ pub use path_helpers::{
pub use scoped_run::{run_in_tmpdir, test_while_readonly};

pub use assertion_helpers::{
assert_contains, assert_dirs_are_equal, assert_equals, assert_not_contains,
assert_contains, assert_contains_regex, assert_dirs_are_equal, assert_equals,
assert_not_contains, assert_not_contains_regex,
};

pub use string::{
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
Expand Up @@ -31,9 +31,7 @@ run-make/long-linker-command-lines/Makefile
run-make/macos-deployment-target/Makefile
run-make/min-global-align/Makefile
run-make/native-link-modifier-bundle/Makefile
run-make/native-link-modifier-whole-archive/Makefile
run-make/no-alloc-shim/Makefile
run-make/no-builtins-attribute/Makefile
run-make/pdb-buildinfo-cl-cmd/Makefile
run-make/pgo-gen-lto/Makefile
run-make/pgo-indirect-call-promotion/Makefile
Expand All @@ -45,7 +43,6 @@ run-make/redundant-libs/Makefile
run-make/remap-path-prefix-dwarf/Makefile
run-make/reproducible-build-2/Makefile
run-make/reproducible-build/Makefile
run-make/rlib-format-packed-bundled-libs-2/Makefile
run-make/rlib-format-packed-bundled-libs/Makefile
run-make/simd-ffi/Makefile
run-make/split-debuginfo/Makefile
Expand Down
52 changes: 0 additions & 52 deletions tests/run-make/native-link-modifier-whole-archive/Makefile

This file was deleted.

86 changes: 86 additions & 0 deletions tests/run-make/native-link-modifier-whole-archive/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// This test case makes sure that native libraries are linked with appropriate semantics
// when the `[+-]bundle,[+-]whole-archive` modifiers are applied to them.
// The test works by checking that the resulting executables produce the expected output,
// part of which is emitted by otherwise unreferenced C code. If +whole-archive didn't work
// that code would never make it into the final executable and we'd thus be missing some
// of the output.
// See https://github.com/rust-lang/rust/issues/88085

//@ ignore-cross-compile
// Reason: compiling C++ code does not work well when cross-compiling
// plus, the compiled binary is executed

use run_make_support::{cxx, is_msvc, llvm_ar, run, run_with_args, rustc, static_lib_name};

fn main() {
let mut cxx = cxx();
if is_msvc() {
cxx.arg("-EHs");
}
cxx.input("c_static_lib_with_constructor.cpp")
.arg("-c")
.out_exe("libc_static_lib_with_constructor")
.run();

let mut llvm_ar = llvm_ar();
llvm_ar.obj_to_ar();
if is_msvc() {
llvm_ar
.output_input(
static_lib_name("c_static_lib_with_constructor"),
"libc_static_lib_with_constructor.obj",
)
.run();
} else {
llvm_ar
.output_input(
static_lib_name("c_static_lib_with_constructor"),
"libc_static_lib_with_constructor",
)
.run();
}

// Native lib linked directly into executable
rustc()
.input("directly_linked.rs")
.arg("-lstatic:+whole-archive=c_static_lib_with_constructor")
.run();

// Native lib linked into test executable, +whole-archive
rustc()
.input("directly_linked_test_plus_whole_archive.rs")
.arg("--test")
.arg("-lstatic:+whole-archive=c_static_lib_with_constructor")
.run();

// Native lib linked into test executable, -whole-archive
rustc()
.input("directly_linked_test_minus_whole_archive.rs")
.arg("--test")
.arg("-lstatic:-whole-archive=c_static_lib_with_constructor")
.run();

// Native lib linked into rlib with via commandline
rustc()
.input("rlib_with_cmdline_native_lib.rs")
.crate_type("rlib")
.arg("-lstatic:-bundle,+whole-archive=c_static_lib_with_constructor")
.run();
// Native lib linked into RLIB via `-l static:-bundle,+whole-archive`
// RLIB linked into executable
rustc().input("indirectly_linked.rs").run();

// Native lib linked into rlib via `#[link()]` attribute on extern block.
rustc().input("native_lib_in_src.rs").crate_type("rlib").run();
// Native lib linked into RLIB via #[link] attribute, RLIB linked into executable
rustc().input("indirectly_linked_via_attr.rs").run();

run("directly_linked").assert_stdout_contains("static-initializer.directly_linked.");
run_with_args("directly_linked_test_plus_whole_archive", &["--nocapture"])
.assert_stdout_contains("static-initializer.");
run_with_args("directly_linked_test_minus_whole_archive", &["--nocapture"])
.assert_stdout_not_contains("static-initializer.");
run("indirectly_linked").assert_stdout_contains("static-initializer.indirectly_linked.");
run("indirectly_linked_via_attr")
.assert_stdout_contains("static-initializer.native_lib_in_src.");
}
9 changes: 0 additions & 9 deletions tests/run-make/no-builtins-attribute/Makefile

This file was deleted.

13 changes: 13 additions & 0 deletions tests/run-make/no-builtins-attribute/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// `no_builtins` is an attribute related to LLVM's optimizations. In order to ensure that it has an
// effect on link-time optimizations (LTO), it should be added to function declarations in a crate.
// This test uses the `llvm-filecheck` tool to determine that this attribute is successfully
// being added to these function declarations.
// See https://github.com/rust-lang/rust/pull/113716

use run_make_support::{llvm_filecheck, rfs, rustc};

fn main() {
rustc().input("no_builtins.rs").emit("link").run();
rustc().input("main.rs").emit("llvm-ir").run();
llvm_filecheck().patterns("filecheck.main.txt").stdin(rfs::read("main.ll")).run();
}
27 changes: 0 additions & 27 deletions tests/run-make/rlib-format-packed-bundled-libs-2/Makefile

This file was deleted.

Loading

0 comments on commit 83dcdb3

Please sign in to comment.