feat: Add host_triple mod

This commit is contained in:
Sergio Gasquez 2022-10-07 19:32:04 +02:00
parent 1eeebc1ca5
commit cb52c6800e
6 changed files with 93 additions and 103 deletions

56
src/host_triple.rs Normal file
View File

@ -0,0 +1,56 @@
use crate::emoji;
use anyhow::Result;
use guess_host_triple::guess_host_triple;
use std::str::FromStr;
use strum::Display;
#[derive(Display, Debug, Clone)]
pub enum HostTriple {
/// 64-bit Linux
#[strum(serialize = "x86_64-unknown-linux-gnu")]
X86_64UnknownLinuxGnu = 0,
/// ARM64 Linux
#[strum(serialize = "aarch64-unknown-linux-gnu")]
Aarch64UnknownLinuxGnu,
/// 64-bit MSVC
#[strum(serialize = "x86_64-pc-windows-msvc")]
X86_64PcWindowsMsvc,
/// 64-bit MinGW
#[strum(serialize = "x86_64-pc-windows-gnu")]
X86_64PcWindowsGnu,
/// 64-bit macOS
#[strum(serialize = "x86_64-apple-darwin")]
X86_64AppleDarwin,
/// ARM64 macOS
#[strum(serialize = "aarch64-apple-darwin")]
Aarch64AppleDarwin,
}
impl FromStr for HostTriple {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"x86_64-unknown-linux-gnu" => Ok(HostTriple::X86_64UnknownLinuxGnu),
"aarch64-unknown-linux-gnu" => Ok(HostTriple::Aarch64UnknownLinuxGnu),
"x86_64-pc-windows-msvc" => Ok(HostTriple::X86_64PcWindowsMsvc),
"x86_64-pc-windows-gnu" => Ok(HostTriple::X86_64PcWindowsGnu),
"x86_64-apple-darwin" => Ok(HostTriple::X86_64AppleDarwin),
"aarch64-apple-darwin" => Ok(HostTriple::Aarch64AppleDarwin),
_ => Err(format!(
"{} Host triple '{}' is not supported.",
emoji::ERROR,
s
)),
}
}
}
/// Parse the host triple if specified, otherwise guess it.
pub fn get_host_triple(host_triple_arg: Option<String>) -> Result<HostTriple> {
let host_triple = match host_triple_arg {
Some(host_triple) => HostTriple::from_str(&host_triple).unwrap(),
None => HostTriple::from_str(guess_host_triple().unwrap()).unwrap(),
};
Ok(host_triple)
}

View File

@ -1,4 +1,5 @@
pub mod emoji;
pub mod host_triple;
pub mod targets;
pub mod toolchain;
pub mod logging {

View File

@ -5,6 +5,7 @@ use clap::Parser;
use embuild::espidf::{parse_esp_idf_git_ref, EspIdfRemote};
use espup::{
emoji,
host_triple::get_host_triple,
logging::initialize_logger,
targets::{parse_targets, Target},
toolchain::{
@ -19,7 +20,6 @@ use espup::{
},
},
};
use guess_host_triple::guess_host_triple;
use log::{debug, info};
use std::{
collections::HashSet,
@ -139,7 +139,7 @@ fn install(args: InstallOpts) -> Result<()> {
info!("{} Installing esp-rs", emoji::DISC);
let targets: HashSet<Target> = parse_targets(&args.targets).unwrap();
let host_triple = get_host_triple(args.default_host).unwrap();
let host_triple = get_host_triple(args.default_host)?;
let mut extra_crates: HashSet<RustCrate> =
args.extra_crates.split(',').map(RustCrate::new).collect();
let mut exports: Vec<String> = Vec::new();
@ -258,7 +258,7 @@ fn uninstall(args: UninstallOpts) -> Result<()> {
fn update(args: UpdateOpts) -> Result<()> {
initialize_logger(&args.log_level);
info!("{} Updating Xtensa Rust toolchain", emoji::DISC);
let host_triple = get_host_triple(args.default_host).unwrap();
let host_triple = get_host_triple(args.default_host)?;
debug!(
"{} Arguments:
@ -338,72 +338,3 @@ pub fn check_arguments(targets: &HashSet<Target>, espidf_version: &Option<String
Ok(())
}
pub fn get_host_triple(host_triple_arg: Option<String>) -> Result<String, String> {
match host_triple_arg {
Some(host_triple) => {
if !matches!(
host_triple.as_str(),
"x86_64-unknown-linux-gnu"
| "aarch64-unknown-linux-gnu"
| "x86_64-pc-windows-msvc"
| "x86_64-pc-windows-gnu"
| "x86_64-apple-darwin"
| "aarch64-apple-darwin"
) {
return Err(format!(
"{} Host triple '{}' is not supported.",
emoji::ERROR,
host_triple
));
}
Ok(host_triple)
}
None => Ok(guess_host_triple().unwrap().to_string()),
}
}
#[cfg(test)]
mod tests {
use crate::emoji;
use crate::get_host_triple;
#[test]
fn test_get_host_triple() {
assert_eq!(
get_host_triple(Some("x86_64-unknown-linux-gnu".to_string())),
Ok("x86_64-unknown-linux-gnu".to_string())
);
assert_eq!(
get_host_triple(Some("aarch64-unknown-linux-gnu".to_string())),
Ok("aarch64-unknown-linux-gnu".to_string())
);
assert_eq!(
get_host_triple(Some("x86_64-pc-windows-msvc".to_string())),
Ok("x86_64-pc-windows-msvc".to_string())
);
assert_eq!(
get_host_triple(Some("x86_64-pc-windows-gnu".to_string())),
Ok("x86_64-pc-windows-gnu".to_string())
);
assert_eq!(
get_host_triple(Some("x86_64-apple-darwin".to_string())),
Ok("x86_64-apple-darwin".to_string())
);
assert_eq!(
get_host_triple(Some("aarch64-apple-darwin".to_string())),
Ok("aarch64-apple-darwin".to_string())
);
assert_eq!(
get_host_triple(Some("non-existing-triple".to_string())),
Err(format!(
"{} Host triple 'non-existing-triple' is not supported.",
emoji::ERROR
))
);
assert_eq!(
get_host_triple(Some("".to_string())),
Err(format!("{} Host triple '' is not supported.", emoji::ERROR))
);
}
}

View File

@ -2,6 +2,7 @@
use crate::{
emoji,
host_triple::HostTriple,
targets::Target,
toolchain::{download_file, espidf::get_tool_path},
};
@ -17,7 +18,7 @@ const DEFAULT_GCC_VERSION: &str = "8_4_0";
#[derive(Debug)]
pub struct GccToolchain {
/// Host triple.
pub host_triple: String,
pub host_triple: HostTriple,
/// Repository release version to use.
pub release: String,
/// The repository containing GCC sources.
@ -64,9 +65,9 @@ impl GccToolchain {
}
/// Create a new instance with default values and proper toolchain name.
pub fn new(target: Target, host_triple: &str) -> Self {
pub fn new(target: Target, host_triple: &HostTriple) -> Self {
Self {
host_triple: host_triple.to_string(),
host_triple: host_triple.clone(),
release: DEFAULT_GCC_RELEASE.to_string(),
repository_url: DEFAULT_GCC_REPOSITORY.to_string(),
toolchain_name: get_toolchain_name(target),
@ -76,24 +77,20 @@ impl GccToolchain {
}
/// Gets the name of the GCC arch based on the host triple.
fn get_arch(host_triple: &str) -> Result<&str, String> {
fn get_arch(host_triple: &HostTriple) -> Result<&str> {
match host_triple {
"aarch64-apple-darwin" | "x86_64-apple-darwin" => Ok("macos"),
"aarch64-unknown-linux-gnu" => Ok("linux-arm64"),
"x86_64-unknown-linux-gnu" => Ok("linux-amd64"),
"x86_64-pc-windows-msvc" | "x86_64-pc-windows-gnu" => Ok("win64"),
_ => Err(format!(
"No GCC arch found for the host triple: {}",
host_triple
)),
HostTriple::Aarch64AppleDarwin | HostTriple::X86_64AppleDarwin => Ok("macos"),
HostTriple::X86_64UnknownLinuxGnu => Ok("linux-amd64"),
HostTriple::Aarch64UnknownLinuxGnu => Ok("linux-arm64"),
HostTriple::X86_64PcWindowsMsvc | HostTriple::X86_64PcWindowsGnu => Ok("win64"),
}
}
/// Gets the artifact extension based on the host architecture.
fn get_artifact_extension(host_triple: &str) -> &str {
/// Gets the artifact extension based on the host triple.
fn get_artifact_extension(host_triple: &HostTriple) -> &str {
match host_triple {
"x86_64-pc-windows-msvc" | "x86_64-pc-windows-gnu" => "zip",
_ => "tar.gz",
HostTriple::X86_64PcWindowsMsvc | HostTriple::X86_64PcWindowsGnu => "zip",
_ => "tar.xz",
}
}
@ -130,7 +127,10 @@ pub fn get_ulp_toolchain_name(target: Target, version: Option<&EspIdfVersion>) -
}
/// Installs GCC toolchain the selected targets.
pub fn install_gcc_targets(targets: HashSet<Target>, host_triple: &str) -> Result<Vec<String>> {
pub fn install_gcc_targets(
targets: HashSet<Target>,
host_triple: &HostTriple,
) -> Result<Vec<String>> {
info!("{} Installing gcc for build targets", emoji::WRENCH);
let mut exports: Vec<String> = Vec::new();
for target in targets {

View File

@ -2,6 +2,7 @@
use crate::{
emoji,
host_triple::HostTriple,
toolchain::{download_file, espidf::get_tool_path},
};
use anyhow::{bail, Ok, Result};
@ -19,7 +20,7 @@ pub struct LlvmToolchain {
/// LLVM Toolchain file name.
pub file_name: String,
/// Host triple.
pub host_triple: String,
pub host_triple: HostTriple,
/// LLVM Toolchain path.
pub path: PathBuf,
/// The repository containing LVVM sources.
@ -30,23 +31,23 @@ pub struct LlvmToolchain {
impl LlvmToolchain {
/// Gets the name of the LLVM arch based on the host triple.
fn get_arch(host_triple: &str) -> Result<String> {
fn get_arch(host_triple: &HostTriple) -> Result<&str> {
match host_triple {
"aarch64-apple-darwin" | "x86_64-apple-darwin" => Ok("macos".to_string()),
"x86_64-unknown-linux-gnu" => Ok("linux-amd64".to_string()),
"x86_64-pc-windows-msvc" | "x86_64-pc-windows-gnu" => Ok("win64".to_string()),
HostTriple::Aarch64AppleDarwin | HostTriple::X86_64AppleDarwin => Ok("macos"),
HostTriple::X86_64UnknownLinuxGnu => Ok("linux-amd64"),
HostTriple::X86_64PcWindowsMsvc | HostTriple::X86_64PcWindowsGnu => Ok("win64"),
_ => bail!(
"{} No LLVM arch found for the host triple: {}",
"{} No LLVM arch found for the host triple: '{}'",
emoji::ERROR,
host_triple
),
}
}
/// Gets the artifact extension based on the host architecture.
fn get_artifact_extension(host_triple: &str) -> &str {
/// Gets the artifact extension based on the host triple.
fn get_artifact_extension(host_triple: &HostTriple) -> &str {
match host_triple {
"x86_64-pc-windows-msvc" | "x86_64-pc-windows-gnu" => "zip",
HostTriple::X86_64PcWindowsMsvc | HostTriple::X86_64PcWindowsGnu => "zip",
_ => "tar.xz",
}
}
@ -97,7 +98,7 @@ impl LlvmToolchain {
}
/// Create a new instance with default values and proper toolchain version.
pub fn new(minified: bool, host_triple: &str) -> Self {
pub fn new(minified: bool, host_triple: &HostTriple) -> Self {
let file_name: String;
let version = DEFAULT_LLVM_VERSION.to_string();
let repository_url: String;
@ -132,7 +133,7 @@ impl LlvmToolchain {
.into();
Self {
file_name,
host_triple: host_triple.to_string(),
host_triple: host_triple.clone(),
path,
repository_url,
version,

View File

@ -2,6 +2,7 @@
use crate::{
emoji,
host_triple::HostTriple,
toolchain::{download_file, espidf::get_dist_path, get_home_dir},
};
use anyhow::{bail, Result};
@ -105,7 +106,7 @@ impl RustToolchain {
}
/// Create a new instance.
pub fn new(toolchain_version: &str, host_triple: &str) -> Self {
pub fn new(toolchain_version: &str, host_triple: &HostTriple) -> Self {
let artifact_extension = get_artifact_extension(host_triple);
let version = toolchain_version.to_string();
let dist = format!("rust-{}-{}", version, host_triple);
@ -176,9 +177,9 @@ impl RustCrate {
}
/// Gets the artifact extension based on the host architecture.
fn get_artifact_extension(host_triple: &str) -> &str {
fn get_artifact_extension(host_triple: &HostTriple) -> &str {
match host_triple {
"x86_64-pc-windows-msvc" | "x86_64-pc-windows-gnu" => "zip",
HostTriple::X86_64PcWindowsMsvc | HostTriple::X86_64PcWindowsGnu => "zip",
_ => "tar.xz",
}
}