|
| 1 | +// `-Z packed_bundled_libs` is an unstable rustc flag that makes the compiler |
| 2 | +// only require a native library and no supplementary object files to compile. |
| 3 | +// #105601 made it possible to have this behaviour without an unstable flag by |
| 4 | +// passing +bundle in modifiers, and this test checks that this feature successfully |
| 5 | +// compiles and includes only the static libraries, with no object files. |
| 6 | +// See https://github.com/rust-lang/rust/pull/105601 |
| 7 | + |
| 8 | +use run_make_support::{ |
| 9 | + build_native_static_lib, is_msvc, llvm_ar, regex, rfs, rust_lib_name, rustc, static_lib_name, |
| 10 | +}; |
| 11 | + |
| 12 | +//@ ignore-cross-compile |
| 13 | +// Reason: Invalid library format (not ELF) causes compilation failure |
| 14 | +// in the final `rustc` call. |
| 15 | + |
| 16 | +//@ only-linux |
| 17 | +// Reason: differences in the native lib compilation process causes differences |
| 18 | +// in the --print link-args output |
| 19 | + |
| 20 | +fn main() { |
| 21 | + build_native_static_lib("native_dep_1"); |
| 22 | + build_native_static_lib("native_dep_2"); |
| 23 | + build_native_static_lib("native_dep_3"); |
| 24 | + build_native_static_lib("native_dep_4"); |
| 25 | + // Test cfg with packed bundle. |
| 26 | + rustc().input("rust_dep_cfg.rs").crate_type("rlib").run(); |
| 27 | + rustc() |
| 28 | + .input("main.rs") |
| 29 | + .extern_("rust_dep", rust_lib_name("rust_dep_cfg")) |
| 30 | + .crate_type("staticlib") |
| 31 | + .cfg("should_add") |
| 32 | + .run(); |
| 33 | + // Only static libraries should appear, no object files at all. |
| 34 | + llvm_ar() |
| 35 | + .arg("t") |
| 36 | + .arg(rust_lib_name("rust_dep_cfg")) |
| 37 | + .run() |
| 38 | + .assert_stdout_contains(static_lib_name("native_dep_1")); |
| 39 | + llvm_ar() |
| 40 | + .arg("t") |
| 41 | + .arg(rust_lib_name("rust_dep_cfg")) |
| 42 | + .run() |
| 43 | + .assert_stdout_contains(static_lib_name("native_dep_2")); |
| 44 | + llvm_ar().arg("t").arg(static_lib_name("main")).run().assert_stdout_contains("native_dep_1.o"); |
| 45 | + llvm_ar() |
| 46 | + .arg("t") |
| 47 | + .arg(static_lib_name("main")) |
| 48 | + .run() |
| 49 | + .assert_stdout_not_contains("native_dep_2.o"); |
| 50 | + |
| 51 | + // Test bundle with whole archive. |
| 52 | + rustc().input("rust_dep.rs").crate_type("rlib").run(); |
| 53 | + // Only deps with `+bundle` should appear. |
| 54 | + llvm_ar().arg("t").arg(rust_lib_name("rust_dep")).run().assert_stdout_contains("native_dep_1"); |
| 55 | + llvm_ar().arg("t").arg(rust_lib_name("rust_dep")).run().assert_stdout_contains("native_dep_3"); |
| 56 | + llvm_ar() |
| 57 | + .arg("t") |
| 58 | + .arg(rust_lib_name("rust_dep")) |
| 59 | + .run() |
| 60 | + .assert_stdout_not_contains("native_dep_2"); |
| 61 | + llvm_ar() |
| 62 | + .arg("t") |
| 63 | + .arg(rust_lib_name("rust_dep")) |
| 64 | + .run() |
| 65 | + .assert_stdout_not_contains("native_dep_4"); |
| 66 | + |
| 67 | + // The compiler shouldn't use files which it doesn't know about. |
| 68 | + rfs::remove_file(static_lib_name("native_dep_1")); |
| 69 | + rfs::remove_file(static_lib_name("native_dep_3")); |
| 70 | + |
| 71 | + let out = rustc() |
| 72 | + .input("main.rs") |
| 73 | + .extern_("rust_dep", rust_lib_name("rust_dep")) |
| 74 | + .print("link-args") |
| 75 | + .run() |
| 76 | + .assert_stdout_not_contains("native_dep_3") |
| 77 | + .stdout_utf8(); |
| 78 | + |
| 79 | + let re = regex::Regex::new( |
| 80 | +"--whole-archive.*native_dep_1.*--whole-archive.*lnative_dep_2.*no-whole-archive.*lnative_dep_4" |
| 81 | + ).unwrap(); |
| 82 | + |
| 83 | + assert!(re.is_match(&out)); |
| 84 | +} |
0 commit comments