|
| 1 | +//! This checks the output of `--print=native-static-libs` |
| 2 | +//! |
| 3 | +//! Specifically, this test makes sure that one and only one |
| 4 | +//! note is emitted with the text "native-static-libs:" as prefix |
| 5 | +//! that the note contains the link args given in the source code |
| 6 | +//! and cli of the current crate and downstream crates. |
| 7 | +//! |
| 8 | +//! It also checks that there aren't any duplicated consecutive |
| 9 | +//! args, as they are useless and suboptimal for debugability. |
| 10 | +//! See https://github.com/rust-lang/rust/issues/113209. |
| 11 | +
|
| 12 | +//@ ignore-cross-compile |
| 13 | +//@ ignore-wasm |
| 14 | + |
| 15 | +extern crate run_make_support; |
| 16 | + |
| 17 | +use std::io::BufRead; |
| 18 | + |
| 19 | +use run_make_support::{rustc, is_msvc}; |
| 20 | + |
| 21 | +fn main() { |
| 22 | + // build supporting crate |
| 23 | + rustc() |
| 24 | + .input("bar.rs") |
| 25 | + .crate_type("rlib") |
| 26 | + .arg("-lbar_cli") |
| 27 | + .run(); |
| 28 | + |
| 29 | + // build main crate as staticlib |
| 30 | + let output = rustc() |
| 31 | + .input("foo.rs") |
| 32 | + .crate_type("staticlib") |
| 33 | + .arg("-lfoo_cli") |
| 34 | + .arg("-lfoo_cli") // 2nd time |
| 35 | + .print("native-static-libs") |
| 36 | + .run(); |
| 37 | + |
| 38 | + let mut found_note = false; |
| 39 | + for l in output.stderr.lines() { |
| 40 | + let l = l.expect("utf-8 string"); |
| 41 | + |
| 42 | + let Some(args) = l.strip_prefix("note: native-static-libs:") else { continue; }; |
| 43 | + assert!(!found_note); |
| 44 | + found_note = true; |
| 45 | + |
| 46 | + let args: Vec<&str> = args.trim().split_ascii_whitespace().collect(); |
| 47 | + |
| 48 | + macro_rules! assert_contains_lib { |
| 49 | + ($lib:literal in $args:ident) => {{ |
| 50 | + let lib = format!( |
| 51 | + "{}{}{}", |
| 52 | + if !is_msvc() { "-l" } else { "" }, |
| 53 | + $lib, |
| 54 | + if !is_msvc() { "" } else { ".lib" }, |
| 55 | + ); |
| 56 | + let found = $args.contains(&&*lib); |
| 57 | + assert!(found, "unable to find lib `{}` in those linker args: {:?}", lib, $args); |
| 58 | + }} |
| 59 | + } |
| 60 | + |
| 61 | + assert_contains_lib!("glib-2.0" in args); // in bar.rs |
| 62 | + assert_contains_lib!("systemd" in args); // in foo.rs |
| 63 | + assert_contains_lib!("bar_cli" in args); |
| 64 | + assert_contains_lib!("foo_cli" in args); |
| 65 | + |
| 66 | + // make sure that no args are consecutively present |
| 67 | + let dedup_args: Vec<&str> = { |
| 68 | + let mut args = args.clone(); |
| 69 | + args.dedup(); |
| 70 | + args |
| 71 | + }; |
| 72 | + assert_eq!(args, dedup_args); |
| 73 | + } |
| 74 | + |
| 75 | + assert!(found_note); |
| 76 | +} |
0 commit comments