use std::str::FromStr;
use crate::{Error, ErrorKind};
mod apple;
mod generated;
mod llvm;
mod parser;
pub(crate) use parser::TargetInfoParser;
#[derive(Debug, PartialEq, Clone)]
pub(crate) struct TargetInfo<'a> {
pub full_arch: &'a str,
pub arch: &'a str,
pub vendor: &'a str,
pub os: &'a str,
pub env: &'a str,
pub abi: &'a str,
unversioned_llvm_target: &'a str,
}
impl FromStr for TargetInfo<'_> {
type Err = Error;
fn from_str(target_triple: &str) -> Result<Self, Error> {
if let Ok(index) =
generated::LIST.binary_search_by_key(&target_triple, |(target_triple, _)| target_triple)
{
let (_, info) = &generated::LIST[index];
Ok(info.clone())
} else {
Err(Error::new(
ErrorKind::UnknownTarget,
format!(
"unknown target `{target_triple}`.
NOTE: `cc-rs` only supports a fixed set of targets when not in a build script.
- If adding a new target, you will need to fork of `cc-rs` until the target
has landed on nightly and the auto-generated list has been updated. See also
the `rustc` dev guide on adding a new target:
https://rustc-dev-guide.rust-lang.org/building/new-target.html
- If using a custom target, prefer to upstream it to `rustc` if possible,
otherwise open an issue with `cc-rs`:
https://github.com/rust-lang/cc-rs/issues/new
"
),
))
}
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use super::TargetInfo;
#[test]
fn tier1() {
let targets = [
"aarch64-unknown-linux-gnu",
"aarch64-apple-darwin",
"i686-pc-windows-gnu",
"i686-pc-windows-msvc",
"i686-unknown-linux-gnu",
"x86_64-apple-darwin",
"x86_64-pc-windows-gnu",
"x86_64-pc-windows-msvc",
"x86_64-unknown-linux-gnu",
];
for target in targets {
let _ = TargetInfo::from_str(target).unwrap();
}
}
#[test]
fn cannot_parse_extra() {
let targets = [
"aarch64-unknown-none-gnu",
"aarch64-uwp-windows-gnu",
"arm-frc-linux-gnueabi",
"arm-unknown-netbsd-eabi",
"armv7neon-unknown-linux-gnueabihf",
"armv7neon-unknown-linux-musleabihf",
"thumbv7-unknown-linux-gnueabihf",
"thumbv7-unknown-linux-musleabihf",
"x86_64-rumprun-netbsd",
"x86_64-unknown-linux",
];
for target in targets {
let _ = TargetInfo::from_str(target).unwrap_err();
}
}
}