Skip to content

Commit b777e6a

Browse files
HackerFooemilio
authored andcommitted
more sophisticated handling of the triple in rust_to_clang_target
1 parent e5480dd commit b777e6a

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

bindgen/lib.rs

+39-20
Original file line numberDiff line numberDiff line change
@@ -684,33 +684,44 @@ pub(crate) const HOST_TARGET: &str =
684684
fn rust_to_clang_target(rust_target: &str) -> Box<str> {
685685
const TRIPLE_HYPHENS_MESSAGE: &str = "Target triple should contain hyphens";
686686

687-
let mut clang_target = rust_target.to_owned();
687+
let mut triple: Vec<&str> = rust_target.split_terminator('-').collect();
688688

689-
if clang_target.starts_with("riscv32") {
690-
let idx = clang_target.find('-').expect(TRIPLE_HYPHENS_MESSAGE);
689+
assert!(!triple.is_empty(), "{}", TRIPLE_HYPHENS_MESSAGE);
690+
triple.resize(4, "");
691691

692-
clang_target.replace_range(..idx, "riscv32");
693-
} else if clang_target.starts_with("riscv64") {
694-
let idx = clang_target.find('-').expect(TRIPLE_HYPHENS_MESSAGE);
695-
696-
clang_target.replace_range(..idx, "riscv64");
697-
} else if clang_target.starts_with("aarch64-apple-") {
698-
let idx = clang_target.find('-').expect(TRIPLE_HYPHENS_MESSAGE);
699-
700-
clang_target.replace_range(..idx, "arm64");
692+
// RISC-V
693+
if triple[0].starts_with("riscv32") {
694+
triple[0] = "riscv32";
695+
} else if triple[0].starts_with("riscv64") {
696+
triple[0] = "riscv64";
701697
}
702698

703-
if clang_target.ends_with("-espidf") {
704-
let idx = clang_target.rfind('-').expect(TRIPLE_HYPHENS_MESSAGE);
705-
706-
clang_target.replace_range((idx + 1).., "elf");
707-
} else if clang_target.ends_with("apple-ios-sim") {
708-
let idx = clang_target.rfind('-').expect(TRIPLE_HYPHENS_MESSAGE);
699+
// Apple
700+
if triple[1] == "apple" {
701+
if triple[0] == "aarch64" {
702+
triple[0] = "arm64";
703+
}
704+
if triple[3] == "sim" {
705+
triple[3] = "simulator";
706+
}
707+
}
709708

710-
clang_target.replace_range((idx + 1).., "simulator");
709+
// ESP-IDF
710+
if triple[2] == "espidf" {
711+
triple[2] = "elf";
711712
}
712713

713-
clang_target.into()
714+
triple
715+
.iter()
716+
.skip(1)
717+
.fold(triple[0].to_string(), |triple, part| {
718+
if part.is_empty() {
719+
triple
720+
} else {
721+
triple + "-" + part
722+
}
723+
})
724+
.into()
714725
}
715726

716727
/// Returns the effective target, and whether it was explicitly specified on the
@@ -1400,4 +1411,12 @@ fn test_rust_to_clang_target_simulator() {
14001411
rust_to_clang_target("aarch64-apple-ios-sim").as_ref(),
14011412
"arm64-apple-ios-simulator"
14021413
);
1414+
assert_eq!(
1415+
rust_to_clang_target("aarch64-apple-tvos-sim").as_ref(),
1416+
"arm64-apple-tvos-simulator"
1417+
);
1418+
assert_eq!(
1419+
rust_to_clang_target("aarch64-apple-watchos-sim").as_ref(),
1420+
"arm64-apple-watchos-simulator"
1421+
);
14031422
}

0 commit comments

Comments
 (0)