|
| 1 | +// When using the flag -C linker-plugin-lto, static libraries could lose their upstream object |
| 2 | +// files during compilation. This bug was fixed in #53031, and this test compiles a staticlib |
| 3 | +// dependent on upstream, checking that the upstream object file still exists after no LTO and |
| 4 | +// thin LTO. |
| 5 | +// See https://github.com/rust-lang/rust/pull/53031 |
| 6 | + |
| 7 | +// ignore windows due to libLLVM being present in PATH and the PATH and library path being the same |
| 8 | +// (so fixing it is harder). See #57765 for context |
| 9 | +//FIXME(Oneirical): ignore-windows |
| 10 | + |
| 11 | +use run_make_support::{ |
| 12 | + cwd, has_extension, has_prefix, has_suffix, llvm_ar, rfs, rustc, shallow_find_files, |
| 13 | + static_lib_name, |
| 14 | +}; |
| 15 | + |
| 16 | +fn main() { |
| 17 | + // The test starts with no LTO enabled. |
| 18 | + rustc().input("upstream.rs").arg("-Clinker-plugin-lto").codegen_units(1).run(); |
| 19 | + rustc() |
| 20 | + .input("staticlib.rs") |
| 21 | + .arg("-Clinker-plugin-lto") |
| 22 | + .codegen_units(1) |
| 23 | + .output(static_lib_name("staticlib")) |
| 24 | + .run(); |
| 25 | + llvm_ar().arg("x").arg(static_lib_name("staticlib")).run(); |
| 26 | + // Ensure the upstream object file was included. |
| 27 | + assert_eq!( |
| 28 | + shallow_find_files(cwd(), |path| { |
| 29 | + has_prefix(path, "upstream.") && has_suffix(path, ".rcgu.o") |
| 30 | + }) |
| 31 | + .len(), |
| 32 | + 1 |
| 33 | + ); |
| 34 | + // Remove all output files that are not source Rust code for cleanup. |
| 35 | + for file in shallow_find_files(cwd(), |path| !has_extension(path, "rs")) { |
| 36 | + rfs::remove_file(file) |
| 37 | + } |
| 38 | + |
| 39 | + // Check it again, with Thin LTO. |
| 40 | + rustc() |
| 41 | + .input("upstream.rs") |
| 42 | + .arg("-Clinker-plugin-lto") |
| 43 | + .codegen_units(1) |
| 44 | + .arg("-Clto=thin") |
| 45 | + .run(); |
| 46 | + rustc() |
| 47 | + .input("staticlib.rs") |
| 48 | + .arg("-Clinker-plugin-lto") |
| 49 | + .codegen_units(1) |
| 50 | + .arg("-Clto=thin") |
| 51 | + .output(static_lib_name("staticlib")) |
| 52 | + .run(); |
| 53 | + llvm_ar().arg("x").arg(static_lib_name("staticlib")).run(); |
| 54 | + assert_eq!( |
| 55 | + shallow_find_files(cwd(), |path| { |
| 56 | + has_prefix(path, "upstream.") && has_suffix(path, ".rcgu.o") |
| 57 | + }) |
| 58 | + .len(), |
| 59 | + 1 |
| 60 | + ); |
| 61 | +} |
0 commit comments