Skip to content

Commit a426856

Browse files
committed
extend archiver coverage
Signed-off-by: onur-ozkan <[email protected]>
1 parent bb33e30 commit a426856

File tree

1 file changed

+49
-28
lines changed

1 file changed

+49
-28
lines changed

tests/archiver.rs

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,65 @@
1+
use std::env;
2+
13
#[test]
24
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") };
514

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");
817

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");
1120

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");
1423

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");
1726

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");
2029

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");
2332

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");
2635

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+
);
3543

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");
3847
}
3948

40-
fn cc_with_target(target: &'static str) -> cc::Build {
49+
fn get_ar_for_target(target: &'static str) -> String {
4150
let mut cfg = cc::Build::new();
4251
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+
}
4465
}

0 commit comments

Comments
 (0)