Rollup merge of #129018 - Oneirical:nmemonic-artifice, r=jieyouxu

Migrate `rlib-format-packed-bundled-libs` and `native-link-modifier-bundle` `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: test-various (ATTEMPTED: IGNORE RESTORED)
try-job: x86_64-msvc
try-job: aarch64-apple
try-job: aarch64-gnu
try-job: x86_64-mingw
try-job: i686-mingw
This commit is contained in:
Jubilee 2024-08-15 18:44:17 -07:00 committed by GitHub
commit 759f93f421
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 180 additions and 79 deletions

View File

@ -297,6 +297,12 @@ impl LlvmAr {
self
}
/// Print the table of contents.
pub fn table_of_contents(&mut self) -> &mut Self {
self.cmd.arg("t");
self
}
/// Provide an output, then an input file. Bundled in one function, as llvm-ar has
/// no "--output"-style flag.
pub fn output_input(&mut self, out: impl AsRef<Path>, input: impl AsRef<Path>) -> &mut Self {

View File

@ -10,9 +10,7 @@ run-make/libtest-json/Makefile
run-make/libtest-junit/Makefile
run-make/libtest-thread-limit/Makefile
run-make/macos-deployment-target/Makefile
run-make/native-link-modifier-bundle/Makefile
run-make/reproducible-build/Makefile
run-make/rlib-format-packed-bundled-libs/Makefile
run-make/split-debuginfo/Makefile
run-make/symbol-mangling-hashed/Makefile
run-make/translation/Makefile

View File

@ -1,38 +0,0 @@
# ignore-cross-compile
# ignore-windows-msvc
include ../tools.mk
# We're using the llvm-nm instead of the system nm to ensure it is compatible
# with the LLVM bitcode generated by rustc.
# Except on Windows where piping/IO redirection under MSYS2 is wonky with llvm-nm.
ifndef IS_WINDOWS
NM = "$(LLVM_BIN_DIR)"/llvm-nm
else
NM = nm
endif
all: $(call NATIVE_STATICLIB,native-staticlib)
# Build a staticlib and a rlib, the `native_func` symbol will be bundled into them
$(RUSTC) bundled.rs --crate-type=staticlib --crate-type=rlib
$(NM) $(TMPDIR)/libbundled.a | $(CGREP) -e "T _*native_func"
$(NM) $(TMPDIR)/libbundled.a | $(CGREP) -e "U _*native_func"
$(NM) $(TMPDIR)/libbundled.rlib | $(CGREP) -e "T _*native_func"
$(NM) $(TMPDIR)/libbundled.rlib | $(CGREP) -e "U _*native_func"
# Build a staticlib and a rlib, the `native_func` symbol will not be bundled into it
$(RUSTC) non-bundled.rs --crate-type=staticlib --crate-type=rlib
$(NM) $(TMPDIR)/libnon_bundled.a | $(CGREP) -ve "T _*native_func"
$(NM) $(TMPDIR)/libnon_bundled.a | $(CGREP) -e "U _*native_func"
$(NM) $(TMPDIR)/libnon_bundled.rlib | $(CGREP) -ve "T _*native_func"
$(NM) $(TMPDIR)/libnon_bundled.rlib | $(CGREP) -e "U _*native_func"
# Build a cdylib, `native-staticlib` will not appear on the linker line because it was bundled previously
# The cdylib will contain the `native_func` symbol in the end
$(RUSTC) cdylib-bundled.rs --crate-type=cdylib --print link-args | $(CGREP) -ve '-l[" ]*native-staticlib'
$(NM) $(call DYLIB,cdylib_bundled) | $(CGREP) -e "[Tt] _*native_func"
# Build a cdylib, `native-staticlib` will appear on the linker line because it was not bundled previously
# The cdylib will contain the `native_func` symbol in the end
$(RUSTC) cdylib-non-bundled.rs --crate-type=cdylib --print link-args | $(CGREP) -e '-l[" ]*native-staticlib'
$(NM) $(call DYLIB,cdylib_non_bundled) | $(CGREP) -e "[Tt] _*native_func"

View File

@ -0,0 +1,90 @@
// This test exercises the `bundle` link argument, which can be turned on or off.
// When building a rlib or staticlib, +bundle means that all object files from the native static
// library will be added to the rlib or staticlib archive, and then used from it during linking of
// the final binary.
// When building a rlib -bundle means that the native static library is registered as a dependency
// of that rlib "by name", and object files from it are included only during linking of the final
// binary, the file search by that name is also performed during final linking.
// When building a staticlib -bundle means that the native static library is simply not included
// into the archive and some higher level build system will need to add it later during linking of
// the final binary.
// This modifier has no effect when building other targets like executables or dynamic libraries.
// The default for this modifier is +bundle.
// See https://github.com/rust-lang/rust/pull/95818
//@ ignore-cross-compile
// Reason: cross-compilation fails to export native symbols
use run_make_support::{
build_native_static_lib, dynamic_lib_name, is_msvc, llvm_nm, rust_lib_name, rustc,
static_lib_name,
};
fn main() {
build_native_static_lib("native-staticlib");
// Build a staticlib and a rlib, the `native_func` symbol will be bundled into them
rustc().input("bundled.rs").crate_type("staticlib").crate_type("rlib").run();
llvm_nm()
.input(static_lib_name("bundled"))
.run()
.assert_stdout_contains_regex("T _*native_func");
llvm_nm()
.input(static_lib_name("bundled"))
.run()
.assert_stdout_contains_regex("U _*native_func");
llvm_nm().input(rust_lib_name("bundled")).run().assert_stdout_contains_regex("T _*native_func");
llvm_nm().input(rust_lib_name("bundled")).run().assert_stdout_contains_regex("U _*native_func");
// Build a staticlib and a rlib, the `native_func` symbol will not be bundled into it
build_native_static_lib("native-staticlib");
rustc().input("non-bundled.rs").crate_type("staticlib").crate_type("rlib").run();
llvm_nm()
.input(static_lib_name("non_bundled"))
.run()
.assert_stdout_not_contains_regex("T _*native_func");
llvm_nm()
.input(static_lib_name("non_bundled"))
.run()
.assert_stdout_contains_regex("U _*native_func");
llvm_nm()
.input(rust_lib_name("non_bundled"))
.run()
.assert_stdout_not_contains_regex("T _*native_func");
llvm_nm()
.input(rust_lib_name("non_bundled"))
.run()
.assert_stdout_contains_regex("U _*native_func");
// This part of the test does not function on Windows MSVC - no symbols are printed.
if !is_msvc() {
// Build a cdylib, `native-staticlib` will not appear on the linker line because it was
// bundled previously. The cdylib will contain the `native_func` symbol in the end.
rustc()
.input("cdylib-bundled.rs")
.crate_type("cdylib")
.print("link-args")
.run()
.assert_stdout_not_contains(r#"-l[" ]*native-staticlib"#);
llvm_nm()
.input(dynamic_lib_name("cdylib_bundled"))
.run()
.assert_stdout_contains_regex("[Tt] _*native_func");
// Build a cdylib, `native-staticlib` will appear on the linker line because it was not
// bundled previously. The cdylib will contain the `native_func` symbol in the end
rustc()
.input("cdylib-non-bundled.rs")
.crate_type("cdylib")
.print("link-args")
.run()
.assert_stdout_contains_regex(r#"-l[" ]*native-staticlib"#);
llvm_nm()
.input(dynamic_lib_name("cdylib_non_bundled"))
.run()
.assert_stdout_contains_regex("[Tt] _*native_func");
}
}

View File

@ -1,39 +0,0 @@
include ../tools.mk
# ignore-cross-compile
# Make sure rlib format with -Zpacked_bundled_libs is correct.
# We're using the llvm-nm instead of the system nm to ensure it is compatible
# with the LLVM bitcode generated by rustc.
# Except on Windows where piping/IO redirection under MSYS2 is wonky with llvm-nm.
ifndef IS_WINDOWS
NM = "$(LLVM_BIN_DIR)"/llvm-nm
else
NM = nm
endif
all: $(call NATIVE_STATICLIB,native_dep_1) $(call NATIVE_STATICLIB,native_dep_2) $(call NATIVE_STATICLIB,native_dep_3)
$(RUSTC) rust_dep_up.rs --crate-type=rlib -Zpacked_bundled_libs
$(NM) $(TMPDIR)/librust_dep_up.rlib | $(CGREP) -e "U.*native_f2"
$(NM) $(TMPDIR)/librust_dep_up.rlib | $(CGREP) -e "U.*native_f3"
$(NM) $(TMPDIR)/librust_dep_up.rlib | $(CGREP) -e "T.*rust_dep_up"
$(AR) t $(TMPDIR)/librust_dep_up.rlib | $(CGREP) "native_dep_2"
$(AR) t $(TMPDIR)/librust_dep_up.rlib | $(CGREP) "native_dep_3"
$(RUSTC) rust_dep_local.rs --extern rlib=$(TMPDIR)/librust_dep_up.rlib -Zpacked_bundled_libs --crate-type=rlib
$(NM) $(TMPDIR)/librust_dep_local.rlib | $(CGREP) -e "U.*native_f1"
$(NM) $(TMPDIR)/librust_dep_local.rlib | $(CGREP) -e "T.*rust_dep_local"
$(AR) t $(TMPDIR)/librust_dep_local.rlib | $(CGREP) "native_dep_1"
# Make sure compiler doesn't use files, that it shouldn't know about.
rm $(TMPDIR)/*native_dep_*
$(RUSTC) main.rs --extern lib=$(TMPDIR)/librust_dep_local.rlib -o $(TMPDIR)/main.exe -Zpacked_bundled_libs --print link-args | $(CGREP) -e "native_dep_1.*native_dep_2.*native_dep_3"
ifndef IS_MSVC
$(NM) $(TMPDIR)/main.exe | $(CGREP) -e "T.*native_f1"
$(NM) $(TMPDIR)/main.exe | $(CGREP) -e "T.*native_f2"
$(NM) $(TMPDIR)/main.exe | $(CGREP) -e "T.*native_f3"
$(NM) $(TMPDIR)/main.exe | $(CGREP) -e "T.*rust_dep_local"
$(NM) $(TMPDIR)/main.exe | $(CGREP) -e "T.*rust_dep_up"
endif

View File

@ -0,0 +1,84 @@
// `-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.
// Output files compiled with this flag should still contain all expected symbols -
// that is what this test checks.
// See https://github.com/rust-lang/rust/pull/100101
//@ ignore-cross-compile
// Reason: cross-compilation fails to export native symbols
use run_make_support::{
bin_name, build_native_static_lib, cwd, filename_contains, is_msvc, llvm_ar, llvm_nm, rfs,
rust_lib_name, rustc, shallow_find_files,
};
fn main() {
build_native_static_lib("native_dep_1");
build_native_static_lib("native_dep_2");
build_native_static_lib("native_dep_3");
rustc().input("rust_dep_up.rs").crate_type("rlib").arg("-Zpacked_bundled_libs").run();
llvm_nm()
.input(rust_lib_name("rust_dep_up"))
.run()
.assert_stdout_contains_regex("U.*native_f2");
llvm_nm()
.input(rust_lib_name("rust_dep_up"))
.run()
.assert_stdout_contains_regex("U.*native_f3");
llvm_nm()
.input(rust_lib_name("rust_dep_up"))
.run()
.assert_stdout_contains_regex("T.*rust_dep_up");
llvm_ar()
.table_of_contents()
.arg(rust_lib_name("rust_dep_up"))
.run()
.assert_stdout_contains("native_dep_2");
llvm_ar()
.table_of_contents()
.arg(rust_lib_name("rust_dep_up"))
.run()
.assert_stdout_contains("native_dep_3");
rustc()
.input("rust_dep_local.rs")
.extern_("rlib", rust_lib_name("rust_dep_up"))
.arg("-Zpacked_bundled_libs")
.crate_type("rlib")
.run();
llvm_nm()
.input(rust_lib_name("rust_dep_local"))
.run()
.assert_stdout_contains_regex("U.*native_f1");
llvm_nm()
.input(rust_lib_name("rust_dep_local"))
.run()
.assert_stdout_contains_regex("T.*rust_dep_local");
llvm_ar()
.table_of_contents()
.arg(rust_lib_name("rust_dep_local"))
.run()
.assert_stdout_contains("native_dep_1");
// Ensure the compiler will not use files it should not know about.
for file in shallow_find_files(cwd(), |path| filename_contains(path, "native_dep_")) {
rfs::remove_file(file);
}
rustc()
.input("main.rs")
.extern_("lib", rust_lib_name("rust_dep_local"))
.output(bin_name("main"))
.arg("-Zpacked_bundled_libs")
.print("link-args")
.run()
.assert_stdout_contains_regex("native_dep_1.*native_dep_2.*native_dep_3");
// The binary "main" will not contain any symbols on MSVC.
if !is_msvc() {
llvm_nm().input(bin_name("main")).run().assert_stdout_contains_regex("T.*native_f1");
llvm_nm().input(bin_name("main")).run().assert_stdout_contains_regex("T.*native_f2");
llvm_nm().input(bin_name("main")).run().assert_stdout_contains_regex("T.*native_f3");
llvm_nm().input(bin_name("main")).run().assert_stdout_contains_regex("T.*rust_dep_local");
llvm_nm().input(bin_name("main")).run().assert_stdout_contains_regex("T.*rust_dep_up");
}
}