| 
 | 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, fs_wrapper, is_msvc, llvm_ar, regex, rust_lib_name, rustc,  | 
 | 10 | +    static_lib_name,  | 
 | 11 | +};  | 
 | 12 | + | 
 | 13 | +// FIXME only-linux test-various  | 
 | 14 | + | 
 | 15 | +fn main() {  | 
 | 16 | +    build_native_static_lib("native_dep_1");  | 
 | 17 | +    build_native_static_lib("native_dep_2");  | 
 | 18 | +    build_native_static_lib("native_dep_3");  | 
 | 19 | +    build_native_static_lib("native_dep_4");  | 
 | 20 | +    // Test cfg with packed bundle.  | 
 | 21 | +    rustc().input("rust_dep_cfg.rs").crate_type("rlib").run();  | 
 | 22 | +    rustc()  | 
 | 23 | +        .input("main.rs")  | 
 | 24 | +        .extern_("rust_dep", rust_lib_name("rust_dep_cfg"))  | 
 | 25 | +        .crate_type("staticlib")  | 
 | 26 | +        .cfg("should_add")  | 
 | 27 | +        .run();  | 
 | 28 | +    // Only static libraries should appear, no object files at all.  | 
 | 29 | +    llvm_ar()  | 
 | 30 | +        .arg("t")  | 
 | 31 | +        .arg(rust_lib_name("rust_dep_cfg"))  | 
 | 32 | +        .run()  | 
 | 33 | +        .assert_stdout_contains(static_lib_name("native_dep_1"));  | 
 | 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_2"));  | 
 | 39 | +    llvm_ar()  | 
 | 40 | +        .arg("t")  | 
 | 41 | +        .arg(static_lib_name("main"))  | 
 | 42 | +        .run()  | 
 | 43 | +        .assert_stdout_contains(object_file_name("native_dep_1"));  | 
 | 44 | +    llvm_ar()  | 
 | 45 | +        .arg("t")  | 
 | 46 | +        .arg(static_lib_name("main"))  | 
 | 47 | +        .run()  | 
 | 48 | +        .assert_stdout_not_contains(object_file_name("native_dep_2"));  | 
 | 49 | + | 
 | 50 | +    // Test bundle with whole archive.  | 
 | 51 | +    rustc().input("rust_dep.rs").crate_type("rlib").run();  | 
 | 52 | +    // Only deps with `+bundle` should appear.  | 
 | 53 | +    llvm_ar().arg("t").arg(rust_lib_name("rust_dep")).run().assert_stdout_contains("native_dep_1");  | 
 | 54 | +    llvm_ar().arg("t").arg(rust_lib_name("rust_dep")).run().assert_stdout_contains("native_dep_3");  | 
 | 55 | +    llvm_ar()  | 
 | 56 | +        .arg("t")  | 
 | 57 | +        .arg(rust_lib_name("rust_dep"))  | 
 | 58 | +        .run()  | 
 | 59 | +        .assert_stdout_not_contains("native_dep_2");  | 
 | 60 | +    llvm_ar()  | 
 | 61 | +        .arg("t")  | 
 | 62 | +        .arg(rust_lib_name("rust_dep"))  | 
 | 63 | +        .run()  | 
 | 64 | +        .assert_stdout_not_contains("native_dep_4");  | 
 | 65 | + | 
 | 66 | +    // The compiler shouldn't use files which it doesn't know about.  | 
 | 67 | +    fs_wrapper::remove_file(static_lib_name("native_dep_1"));  | 
 | 68 | +    fs_wrapper::remove_file(static_lib_name("native_dep_3"));  | 
 | 69 | + | 
 | 70 | +    let out = rustc()  | 
 | 71 | +        .input("main.rs")  | 
 | 72 | +        .extern_("rust_dep", rust_lib_name("rust_dep"))  | 
 | 73 | +        .print("link-args")  | 
 | 74 | +        .run()  | 
 | 75 | +        .assert_stdout_not_contains("native_dep_3")  | 
 | 76 | +        .stdout_utf8();  | 
 | 77 | + | 
 | 78 | +    let re = regex::Regex::new(  | 
 | 79 | +"--whole-archive.*native_dep_1.*--whole-archive.*lnative_dep_2.*no-whole-archive.*lnative_dep_4"  | 
 | 80 | +    ).unwrap();  | 
 | 81 | + | 
 | 82 | +    assert!(re.is_match(&out));  | 
 | 83 | +}  | 
 | 84 | + | 
 | 85 | +//FIXME(Oneirical): potential helper fn if this works on msvc too  | 
 | 86 | +fn object_file_name(name: &str) -> String {  | 
 | 87 | +    if is_msvc() { format!("{name}.obj") } else { format!("{name}.o") }  | 
 | 88 | +}  | 
0 commit comments