Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 47 additions & 16 deletions bindgen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,29 +684,44 @@ pub(crate) const HOST_TARGET: &str =
fn rust_to_clang_target(rust_target: &str) -> Box<str> {
const TRIPLE_HYPHENS_MESSAGE: &str = "Target triple should contain hyphens";

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

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

clang_target.replace_range(..idx, "riscv32");
} else if clang_target.starts_with("riscv64") {
let idx = clang_target.find('-').expect(TRIPLE_HYPHENS_MESSAGE);

clang_target.replace_range(..idx, "riscv64");
} else if clang_target.starts_with("aarch64-apple-") {
let idx = clang_target.find('-').expect(TRIPLE_HYPHENS_MESSAGE);

clang_target.replace_range(..idx, "arm64");
// RISC-V
if triple[0].starts_with("riscv32") {
triple[0] = "riscv32";
} else if triple[0].starts_with("riscv64") {
triple[0] = "riscv64";
}

if clang_target.ends_with("-espidf") {
let idx = clang_target.rfind('-').expect(TRIPLE_HYPHENS_MESSAGE);
// Apple
if triple[1] == "apple" {
if triple[0] == "aarch64" {
triple[0] = "arm64";
}
if triple[3] == "sim" {
triple[3] = "simulator";
}
}

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

clang_target.into()
triple
.iter()
.skip(1)
.fold(triple[0].to_string(), |triple, part| {
if part.is_empty() {
triple
} else {
triple + "-" + part
}
})
.into()
}

/// Returns the effective target, and whether it was explicitly specified on the
Expand Down Expand Up @@ -1389,3 +1404,19 @@ fn test_rust_to_clang_target_espidf() {
"xtensa-esp32-elf"
);
}

#[test]
fn test_rust_to_clang_target_simulator() {
assert_eq!(
rust_to_clang_target("aarch64-apple-ios-sim").as_ref(),
"arm64-apple-ios-simulator"
);
assert_eq!(
rust_to_clang_target("aarch64-apple-tvos-sim").as_ref(),
"arm64-apple-tvos-simulator"
);
assert_eq!(
rust_to_clang_target("aarch64-apple-watchos-sim").as_ref(),
"arm64-apple-watchos-simulator"
);
}
Loading