From 7d3cbb3e9365b9a26df1dbbd812dd37a7cf2a757 Mon Sep 17 00:00:00 2001 From: Oneirical Date: Mon, 15 Jul 2024 14:02:03 -0400 Subject: [PATCH 1/3] rewrite staticlib-blank-lib to rmake --- src/tools/tidy/src/allowed_run_make_makefiles.txt | 1 - tests/run-make/staticlib-blank-lib/Makefile | 6 ------ tests/run-make/staticlib-blank-lib/rmake.rs | 13 +++++++++++++ 3 files changed, 13 insertions(+), 7 deletions(-) delete mode 100644 tests/run-make/staticlib-blank-lib/Makefile create mode 100644 tests/run-make/staticlib-blank-lib/rmake.rs diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index 745f00c4f52..7fffe7c6ea5 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -102,7 +102,6 @@ run-make/simd-ffi/Makefile run-make/split-debuginfo/Makefile run-make/stable-symbol-names/Makefile run-make/static-dylib-by-default/Makefile -run-make/staticlib-blank-lib/Makefile run-make/staticlib-dylib-linkage/Makefile run-make/symbol-mangling-hashed/Makefile run-make/symbol-visibility/Makefile diff --git a/tests/run-make/staticlib-blank-lib/Makefile b/tests/run-make/staticlib-blank-lib/Makefile deleted file mode 100644 index fcbf87758fb..00000000000 --- a/tests/run-make/staticlib-blank-lib/Makefile +++ /dev/null @@ -1,6 +0,0 @@ -include ../tools.mk - -all: - $(AR) crus $(TMPDIR)/libfoo.a foo.rs - $(AR) d $(TMPDIR)/libfoo.a foo.rs - $(RUSTC) foo.rs diff --git a/tests/run-make/staticlib-blank-lib/rmake.rs b/tests/run-make/staticlib-blank-lib/rmake.rs new file mode 100644 index 00000000000..11a85d102aa --- /dev/null +++ b/tests/run-make/staticlib-blank-lib/rmake.rs @@ -0,0 +1,13 @@ +// In this test, the static library foo is made blank, which used to cause +// a compilation error. As the compiler now returns Ok upon encountering a blank +// staticlib as of #12379, this test checks that compilation is successful despite +// the blank staticlib. +// See https://github.com/rust-lang/rust/pull/12379 + +use run_make_support::{llvm_ar, rustc, static_lib_name}; + +fn main() { + llvm_ar().obj_to_ar().output_input(static_lib_name("foo"), "foo.rs").run(); + llvm_ar().arg("d").output_input(static_lib_name("foo"), "foo.rs").run(); + rustc().input("foo.rs").run(); +} From f1747f40a32f5fcbe7cc684aacedaa5c2bcbb2c4 Mon Sep 17 00:00:00 2001 From: Oneirical Date: Mon, 15 Jul 2024 14:51:56 -0400 Subject: [PATCH 2/3] rewrite rlib-format-packed-bundled-libs-3 to rmake --- .../tidy/src/allowed_run_make_makefiles.txt | 1 - .../Makefile | 35 -------- .../rmake.rs | 88 +++++++++++++++++++ 3 files changed, 88 insertions(+), 36 deletions(-) delete mode 100644 tests/run-make/rlib-format-packed-bundled-libs-3/Makefile create mode 100644 tests/run-make/rlib-format-packed-bundled-libs-3/rmake.rs diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index 7fffe7c6ea5..63e96566a09 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -92,7 +92,6 @@ run-make/reproducible-build-2/Makefile run-make/reproducible-build/Makefile run-make/return-non-c-like-enum-from-c/Makefile run-make/rlib-format-packed-bundled-libs-2/Makefile -run-make/rlib-format-packed-bundled-libs-3/Makefile run-make/rlib-format-packed-bundled-libs/Makefile run-make/sanitizer-cdylib-link/Makefile run-make/sanitizer-dylib-link/Makefile diff --git a/tests/run-make/rlib-format-packed-bundled-libs-3/Makefile b/tests/run-make/rlib-format-packed-bundled-libs-3/Makefile deleted file mode 100644 index 9ba077b1854..00000000000 --- a/tests/run-make/rlib-format-packed-bundled-libs-3/Makefile +++ /dev/null @@ -1,35 +0,0 @@ -include ../tools.mk - -# ignore-cross-compile -# only-linux - -# Make sure -Zpacked_bundled_libs-like behavior activates with +bundle,+whole-archive. - -# We're using the llvm-nm instead of the system nm to ensure it is compatible -# with the LLVM bitcode generated by rustc. -NM = "$(LLVM_BIN_DIR)"/llvm-nm - -all: $(call NATIVE_STATICLIB,native_dep_1) $(call NATIVE_STATICLIB,native_dep_2) $(call NATIVE_STATICLIB,native_dep_3) $(call NATIVE_STATICLIB,native_dep_4) - # test cfg with packed bundle - $(RUSTC) rust_dep_cfg.rs --crate-type=rlib - $(RUSTC) main.rs --extern rust_dep=$(TMPDIR)/librust_dep_cfg.rlib --crate-type=staticlib --cfg should_add - $(AR) t $(TMPDIR)/librust_dep_cfg.rlib | $(CGREP) -e "libnative_dep_1.a" - $(AR) t $(TMPDIR)/librust_dep_cfg.rlib | $(CGREP) -e "libnative_dep_2.a" - $(AR) t $(TMPDIR)/libmain.a | $(CGREP) -e "libnative_dep_1.o" - $(AR) t $(TMPDIR)/libmain.a | $(CGREP) -ev "libnative_dep_2.o" - - - # test bundle with whole_archive - $(RUSTC) rust_dep.rs --crate-type=rlib - $(AR) t $(TMPDIR)/librust_dep.rlib | $(CGREP) -e "native_dep_1" - $(AR) t $(TMPDIR)/librust_dep.rlib | $(CGREP) -e "native_dep_3" - $(AR) t $(TMPDIR)/librust_dep.rlib | $(CGREP) -ev "native_dep_2" - $(AR) t $(TMPDIR)/librust_dep.rlib | $(CGREP) -ev "native_dep_4" - - # Make sure compiler doesn't use files, that it shouldn't know about. - rm $(TMPDIR)/libnative_dep_1.a - rm $(TMPDIR)/libnative_dep_3.a - - $(RUSTC) main.rs --extern rust_dep=$(TMPDIR)/librust_dep.rlib --print link-args > $(TMPDIR)/link_args - cat $(TMPDIR)/link_args | $(CGREP) -ev "native_dep_3" - cat $(TMPDIR)/link_args | $(CGREP) -e "--whole-archive.*native_dep_1.*--whole-archive.*lnative_dep_2.*no-whole-archive.*lnative_dep_4" diff --git a/tests/run-make/rlib-format-packed-bundled-libs-3/rmake.rs b/tests/run-make/rlib-format-packed-bundled-libs-3/rmake.rs new file mode 100644 index 00000000000..ede4d9f031d --- /dev/null +++ b/tests/run-make/rlib-format-packed-bundled-libs-3/rmake.rs @@ -0,0 +1,88 @@ +// `-Z packed_bundled_libs` is an unstable rustc flag that makes the compiler +// only require a native library and no supplementary object files to compile. +// #105601 made it possible to have this behaviour without an unstable flag by +// passing +bundle in modifiers, and this test checks that this feature successfully +// compiles and includes only the static libraries, with no object files. +// See https://github.com/rust-lang/rust/pull/105601 + +use run_make_support::{ + build_native_static_lib, fs_wrapper, is_msvc, llvm_ar, regex, rust_lib_name, rustc, + static_lib_name, +}; + +// FIXME only-linux test-various + +fn main() { + build_native_static_lib("native_dep_1"); + build_native_static_lib("native_dep_2"); + build_native_static_lib("native_dep_3"); + build_native_static_lib("native_dep_4"); + // Test cfg with packed bundle. + rustc().input("rust_dep_cfg.rs").crate_type("rlib").run(); + rustc() + .input("main.rs") + .extern_("rust_dep", rust_lib_name("rust_dep_cfg")) + .crate_type("staticlib") + .cfg("should_add") + .run(); + // Only static libraries should appear, no object files at all. + llvm_ar() + .arg("t") + .arg(rust_lib_name("rust_dep_cfg")) + .run() + .assert_stdout_contains(static_lib_name("native_dep_1")); + llvm_ar() + .arg("t") + .arg(rust_lib_name("rust_dep_cfg")) + .run() + .assert_stdout_contains(static_lib_name("native_dep_2")); + llvm_ar() + .arg("t") + .arg(static_lib_name("main")) + .run() + .assert_stdout_contains(object_file_name("native_dep_1")); + llvm_ar() + .arg("t") + .arg(static_lib_name("main")) + .run() + .assert_stdout_not_contains(object_file_name("native_dep_2")); + + // Test bundle with whole archive. + rustc().input("rust_dep.rs").crate_type("rlib").run(); + // Only deps with `+bundle` should appear. + llvm_ar().arg("t").arg(rust_lib_name("rust_dep")).run().assert_stdout_contains("native_dep_1"); + llvm_ar().arg("t").arg(rust_lib_name("rust_dep")).run().assert_stdout_contains("native_dep_3"); + llvm_ar() + .arg("t") + .arg(rust_lib_name("rust_dep")) + .run() + .assert_stdout_not_contains("native_dep_2"); + llvm_ar() + .arg("t") + .arg(rust_lib_name("rust_dep")) + .run() + .assert_stdout_not_contains("native_dep_4"); + + // The compiler shouldn't use files which it doesn't know about. + fs_wrapper::remove_file(static_lib_name("native_dep_1")); + fs_wrapper::remove_file(static_lib_name("native_dep_3")); + + let out = rustc() + .input("main.rs") + .extern_("rust_dep", rust_lib_name("rust_dep")) + .print("link-args") + .run() + .assert_stdout_not_contains("native_dep_3") + .stdout_utf8(); + + let re = regex::Regex::new( +"--whole-archive.*native_dep_1.*--whole-archive.*lnative_dep_2.*no-whole-archive.*lnative_dep_4" + ).unwrap(); + + assert!(re.is_match(&out)); +} + +//FIXME(Oneirical): potential helper fn if this works on msvc too +fn object_file_name(name: &str) -> String { + if is_msvc() { format!("{name}.obj") } else { format!("{name}.o") } +} From 55dda5d86238ab23905eced21eaad2cb1ad40e51 Mon Sep 17 00:00:00 2001 From: Oneirical Date: Mon, 15 Jul 2024 15:17:34 -0400 Subject: [PATCH 3/3] rewrite and rename issue-97463-abi-param-passing to rmake --- .../src/external_deps/llvm.rs | 2 ++ .../tidy/src/allowed_run_make_makefiles.txt | 1 - .../issue-97463-abi-param-passing/Makefile | 15 ---------- .../rmake.rs | 28 ++++++++----------- .../bad.c | 0 .../param_passing.rs | 0 .../zero-extend-abi-param-passing/rmake.rs | 25 +++++++++++++++++ 7 files changed, 39 insertions(+), 32 deletions(-) delete mode 100644 tests/run-make/issue-97463-abi-param-passing/Makefile rename tests/run-make/{issue-97463-abi-param-passing => zero-extend-abi-param-passing}/bad.c (100%) rename tests/run-make/{issue-97463-abi-param-passing => zero-extend-abi-param-passing}/param_passing.rs (100%) create mode 100644 tests/run-make/zero-extend-abi-param-passing/rmake.rs diff --git a/src/tools/run-make-support/src/external_deps/llvm.rs b/src/tools/run-make-support/src/external_deps/llvm.rs index 5e8ad7ed312..b116bd08e3a 100644 --- a/src/tools/run-make-support/src/external_deps/llvm.rs +++ b/src/tools/run-make-support/src/external_deps/llvm.rs @@ -229,6 +229,8 @@ impl LlvmAr { Self { cmd } } + /// Automatically pass the commonly used arguments `rcus`, used for combining one or more + /// input object files into one output static library file. pub fn obj_to_ar(&mut self) -> &mut Self { self.cmd.arg("rcus"); self diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index 63e96566a09..ad28490f1df 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -48,7 +48,6 @@ run-make/issue-47551/Makefile run-make/issue-69368/Makefile run-make/issue-84395-lto-embed-bitcode/Makefile run-make/issue-88756-default-output/Makefile -run-make/issue-97463-abi-param-passing/Makefile run-make/jobserver-error/Makefile run-make/libs-through-symlinks/Makefile run-make/libtest-json/Makefile diff --git a/tests/run-make/issue-97463-abi-param-passing/Makefile b/tests/run-make/issue-97463-abi-param-passing/Makefile deleted file mode 100644 index 7ce7aaeec57..00000000000 --- a/tests/run-make/issue-97463-abi-param-passing/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -# ignore-cross-compile -include ../tools.mk - -# ignore-msvc - -# The issue exercised by this test, rust-lang/rust#97463, explicitly needs `-O` -# flags (like `-O3`) to reproduce. Thus, we call $(CC) instead of nicer -# alternatives provided by tools.mk like using `COMPILE_OBJ` or using a -# `NATIVE_STATICLIB` dependency. - -all: - $(CC) -c -O3 -o $(TMPDIR)/bad.o bad.c - $(AR) rcs $(TMPDIR)/libbad.a $(TMPDIR)/bad.o - $(RUSTC) param_passing.rs -L$(TMPDIR) -lbad -C opt-level=3 - $(call RUN,param_passing) diff --git a/tests/run-make/rlib-format-packed-bundled-libs-3/rmake.rs b/tests/run-make/rlib-format-packed-bundled-libs-3/rmake.rs index ede4d9f031d..d152047600f 100644 --- a/tests/run-make/rlib-format-packed-bundled-libs-3/rmake.rs +++ b/tests/run-make/rlib-format-packed-bundled-libs-3/rmake.rs @@ -6,11 +6,16 @@ // See https://github.com/rust-lang/rust/pull/105601 use run_make_support::{ - build_native_static_lib, fs_wrapper, is_msvc, llvm_ar, regex, rust_lib_name, rustc, - static_lib_name, + build_native_static_lib, is_msvc, llvm_ar, regex, rfs, rust_lib_name, rustc, static_lib_name, }; -// FIXME only-linux test-various +//@ ignore-cross-compile +// Reason: Invalid library format (not ELF) causes compilation failure +// in the final `rustc` call. + +//@ only-linux +// Reason: differences in the native lib compilation process causes differences +// in the --print link-args output fn main() { build_native_static_lib("native_dep_1"); @@ -36,16 +41,12 @@ fn main() { .arg(rust_lib_name("rust_dep_cfg")) .run() .assert_stdout_contains(static_lib_name("native_dep_2")); + llvm_ar().arg("t").arg(static_lib_name("main")).run().assert_stdout_contains("native_dep_1.o"); llvm_ar() .arg("t") .arg(static_lib_name("main")) .run() - .assert_stdout_contains(object_file_name("native_dep_1")); - llvm_ar() - .arg("t") - .arg(static_lib_name("main")) - .run() - .assert_stdout_not_contains(object_file_name("native_dep_2")); + .assert_stdout_not_contains("native_dep_2.o"); // Test bundle with whole archive. rustc().input("rust_dep.rs").crate_type("rlib").run(); @@ -64,8 +65,8 @@ fn main() { .assert_stdout_not_contains("native_dep_4"); // The compiler shouldn't use files which it doesn't know about. - fs_wrapper::remove_file(static_lib_name("native_dep_1")); - fs_wrapper::remove_file(static_lib_name("native_dep_3")); + rfs::remove_file(static_lib_name("native_dep_1")); + rfs::remove_file(static_lib_name("native_dep_3")); let out = rustc() .input("main.rs") @@ -81,8 +82,3 @@ fn main() { assert!(re.is_match(&out)); } - -//FIXME(Oneirical): potential helper fn if this works on msvc too -fn object_file_name(name: &str) -> String { - if is_msvc() { format!("{name}.obj") } else { format!("{name}.o") } -} diff --git a/tests/run-make/issue-97463-abi-param-passing/bad.c b/tests/run-make/zero-extend-abi-param-passing/bad.c similarity index 100% rename from tests/run-make/issue-97463-abi-param-passing/bad.c rename to tests/run-make/zero-extend-abi-param-passing/bad.c diff --git a/tests/run-make/issue-97463-abi-param-passing/param_passing.rs b/tests/run-make/zero-extend-abi-param-passing/param_passing.rs similarity index 100% rename from tests/run-make/issue-97463-abi-param-passing/param_passing.rs rename to tests/run-make/zero-extend-abi-param-passing/param_passing.rs diff --git a/tests/run-make/zero-extend-abi-param-passing/rmake.rs b/tests/run-make/zero-extend-abi-param-passing/rmake.rs new file mode 100644 index 00000000000..aed27f7f5ab --- /dev/null +++ b/tests/run-make/zero-extend-abi-param-passing/rmake.rs @@ -0,0 +1,25 @@ +// This test was created in response to an obscure miscompilation bug, only +// visible with the -O3 flag passed to the cc compiler when trying to obtain +// a native static library for the sake of foreign function interface. This +// flag could cause certain integer types to fail to be zero-extended, resulting +// in type casting errors. After the fix in #97800, this test attempts integer casting +// while simultaneously interfacing with a C library and using the -O3 flag. +// See https://github.com/rust-lang/rust/issues/97463 + +//@ ignore-msvc +// Reason: the rustc compilation fails due to an unresolved external symbol + +//@ ignore-cross-compile +// Reason: The compiled binary is executed. + +use run_make_support::{cc, is_msvc, llvm_ar, run, rustc, static_lib_name}; + +fn main() { + // The issue exercised by this test specifically needs needs `-O` + // flags (like `-O3`) to reproduce. Thus, we call `cc()` instead of + // the nicer `build_native_static_lib`. + cc().arg("-c").arg("-O3").out_exe("bad.o").input("bad.c").run(); + llvm_ar().obj_to_ar().output_input(static_lib_name("bad"), "bad.o").run(); + rustc().input("param_passing.rs").arg("-lbad").opt_level("3").run(); + run("param_passing"); +}