|
| 1 | +use std::env; |
| 2 | + |
1 | 3 | #[test]
|
2 | 4 | fn main() {
|
3 |
| - let cfg = cc_with_target("i586-pc-nto-qnx700"); |
4 |
| - assert_eq!(cfg.get_archiver().get_program(), "ntox86-ar"); |
| 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") }; |
5 | 14 |
|
6 |
| - let cfg = cc_with_target("x86_64-unknown-linux-gnu"); |
7 |
| - assert_eq!(cfg.get_archiver().get_program(), "ar"); |
| 15 | + let ar = get_ar_for_target("x86_64-unknown-linux-gnu"); |
| 16 | + assert_eq!(ar, "ar"); |
8 | 17 |
|
9 |
| - let cfg = cc_with_target("x86_64-unknown-linux-musl"); |
10 |
| - assert_eq!(cfg.get_archiver().get_program(), "ar"); |
| 18 | + let ar = get_ar_for_target("x86_64-unknown-linux-musl"); |
| 19 | + assert_eq!(ar, "ar"); |
11 | 20 |
|
12 |
| - let cfg = cc_with_target("riscv64gc-unknown-openbsd"); |
13 |
| - assert_eq!(cfg.get_archiver().get_program(), "ar"); |
| 21 | + let ar = get_ar_for_target("riscv64gc-unknown-openbsd"); |
| 22 | + assert_eq!(ar, "ar"); |
14 | 23 |
|
15 |
| - let cfg = cc_with_target("i686-wrs-vxworks"); |
16 |
| - assert_eq!(cfg.get_archiver().get_program(), "wr-ar"); |
| 24 | + let ar = get_ar_for_target("i686-wrs-vxworks"); |
| 25 | + assert_eq!(ar, "wr-ar"); |
17 | 26 |
|
18 |
| - let cfg = cc_with_target("i586-pc-nto-qnx700"); |
19 |
| - assert_eq!(cfg.get_archiver().get_program(), "ntox86-ar"); |
| 27 | + let ar = get_ar_for_target("i586-pc-nto-qnx700"); |
| 28 | + assert_eq!(ar, "ntox86-ar"); |
20 | 29 |
|
21 |
| - let cfg = cc_with_target("aarch64-unknown-nto-qnx700"); |
22 |
| - assert_eq!(cfg.get_archiver().get_program(), "ntoaarch64-ar"); |
| 30 | + let ar = get_ar_for_target("aarch64-unknown-nto-qnx700"); |
| 31 | + assert_eq!(ar, "ntoaarch64-ar"); |
23 | 32 |
|
24 |
| - let cfg = cc_with_target("x86_64-pc-nto-qnx710"); |
25 |
| - assert_eq!(cfg.get_archiver().get_program(), "ntox86_64-ar"); |
| 33 | + let ar = get_ar_for_target("x86_64-pc-nto-qnx710"); |
| 34 | + assert_eq!(ar, "ntox86_64-ar"); |
26 | 35 |
|
27 |
| - let cfg = cc_with_target("wasm32-wasip1"); |
28 |
| - // This usually returns an absolute path, so using `assert_eq` might make the test flaky. |
29 |
| - assert!(cfg |
30 |
| - .get_archiver() |
31 |
| - .get_program() |
32 |
| - .to_str() |
33 |
| - .unwrap() |
34 |
| - .ends_with("llvm-ar")); |
| 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 == maybe_exe("ar").as_str() |
| 42 | + ); |
35 | 43 |
|
36 |
| - let cfg = cc_with_target("riscv64-linux-android"); |
37 |
| - assert_eq!(cfg.get_archiver().get_program(), "llvm-ar"); |
| 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"); |
38 | 47 | }
|
39 | 48 |
|
40 |
| -fn cc_with_target(target: &'static str) -> cc::Build { |
| 49 | +fn get_ar_for_target(target: &'static str) -> String { |
41 | 50 | let mut cfg = cc::Build::new();
|
42 | 51 | cfg.host("x86_64-unknown-linux-gnu").target(target);
|
43 |
| - cfg |
| 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!(target_env = "msvc") { |
| 61 | + format!("{file}.exe") |
| 62 | + } else { |
| 63 | + file.to_owned() |
| 64 | + } |
44 | 65 | }
|
0 commit comments