|
| 1 | +use std::env; |
| 2 | + |
| 3 | +#[test] |
| 4 | +fn main() { |
| 5 | + unsafe { env::set_var("AR_i586_pc_nto_qnx700", "custom-ar") }; |
| 6 | + let ar = get_ar_for_target("i586-pc-nto-qnx700"); |
| 7 | + assert_eq!(ar, "custom-ar"); |
| 8 | + unsafe { env::remove_var("AR_i586_pc_nto_qnx700") }; |
| 9 | + |
| 10 | + unsafe { env::set_var("AR", "custom-ar2") }; |
| 11 | + let ar = get_ar_for_target("x86_64-unknown-linux-gnu"); |
| 12 | + assert_eq!(ar, "custom-ar2"); |
| 13 | + unsafe { env::remove_var("AR") }; |
| 14 | + |
| 15 | + let ar = get_ar_for_target("x86_64-unknown-linux-gnu"); |
| 16 | + assert_eq!(ar, "ar"); |
| 17 | + |
| 18 | + let ar = get_ar_for_target("x86_64-unknown-linux-musl"); |
| 19 | + assert_eq!(ar, "ar"); |
| 20 | + |
| 21 | + let ar = get_ar_for_target("riscv64gc-unknown-openbsd"); |
| 22 | + assert_eq!(ar, "ar"); |
| 23 | + |
| 24 | + let ar = get_ar_for_target("i686-wrs-vxworks"); |
| 25 | + assert_eq!(ar, "wr-ar"); |
| 26 | + |
| 27 | + let ar = get_ar_for_target("i586-pc-nto-qnx700"); |
| 28 | + assert_eq!(ar, "ntox86-ar"); |
| 29 | + |
| 30 | + let ar = get_ar_for_target("aarch64-unknown-nto-qnx700"); |
| 31 | + assert_eq!(ar, "ntoaarch64-ar"); |
| 32 | + |
| 33 | + let ar = get_ar_for_target("x86_64-pc-nto-qnx710"); |
| 34 | + assert_eq!(ar, "ntox86_64-ar"); |
| 35 | + |
| 36 | + let ar = get_ar_for_target("wasm32-wasip1"); |
| 37 | + assert!( |
| 38 | + // `llvm-ar` is usually an absolute path for this target, so we check it with `ends_with`. |
| 39 | + ar.ends_with(&maybe_exe("llvm-ar")) |
| 40 | + // If `llvm-ar` doesn't exist, the logic falls back to `ar` for this target. |
| 41 | + || ar == "ar" |
| 42 | + ); |
| 43 | + |
| 44 | + let ar = get_ar_for_target("riscv64-linux-android"); |
| 45 | + // If `llvm-ar` is not available on the system, this will fall back to `$target-ar` (e.g., `riscv64-linux-android-ar` in this case) |
| 46 | + assert!(ar == "llvm-ar" || ar == "riscv64-linux-android-ar"); |
| 47 | +} |
| 48 | + |
| 49 | +fn get_ar_for_target(target: &'static str) -> String { |
| 50 | + let mut cfg = cc::Build::new(); |
| 51 | + cfg.host("x86_64-unknown-linux-gnu").target(target); |
| 52 | + let ar = cfg.get_archiver(); |
| 53 | + let ar = ar.get_program().to_str().unwrap().to_string(); |
| 54 | + println!("cc::Build::get_archiver -> target: '{target}': resolved archiver: '{ar}'"); |
| 55 | + ar |
| 56 | +} |
| 57 | + |
| 58 | +/// Appends `.exe` to the file name on Windows systems. |
| 59 | +fn maybe_exe(file: &'static str) -> String { |
| 60 | + if cfg!(windows) { |
| 61 | + format!("{file}.exe") |
| 62 | + } else { |
| 63 | + file.to_owned() |
| 64 | + } |
| 65 | +} |
0 commit comments