mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 14:24:45 +00:00
Make a single source of truth for target strings
This commit is contained in:
parent
d5802c1cbe
commit
aeca4b328e
3 changed files with 151 additions and 140 deletions
|
@ -3,7 +3,8 @@
|
|||
// See github.com/roc-lang/roc/issues/800 for discussion of the large_enum_variant check.
|
||||
#![allow(clippy::large_enum_variant)]
|
||||
|
||||
use strum_macros::{EnumCount, EnumIter};
|
||||
use strum_macros::{EnumCount, EnumIter, IntoStaticStr};
|
||||
use target_lexicon::Triple;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub enum OperatingSystem {
|
||||
|
@ -159,56 +160,181 @@ impl From<target_lexicon::Architecture> for Architecture {
|
|||
}
|
||||
}
|
||||
|
||||
pub const WASM_TARGET_STR: &str = "wasm32";
|
||||
pub const LINUX_X86_64_TARGET_STR: &str = "linux-x86_64";
|
||||
pub const LINUX_ARM64_TARGET_STR: &str = "linux-arm64";
|
||||
pub const MACOS_ARM64_TARGET_STR: &str = "macos-arm64";
|
||||
pub const MACOS_X86_64_TARGET_STR: &str = "macos-x86_64";
|
||||
pub const WINDOWS_X86_64_TARGET_STR: &str = "windows-x86_64";
|
||||
pub const WINDOWS_X86_32_TARGET_STR: &str = "windows-x86_32";
|
||||
pub const WIDNOWS_ARM64_TARGET_STR: &str = "windows-arm64";
|
||||
#[derive(Debug, Copy, Clone, EnumIter, IntoStaticStr, PartialEq, Eq, Default)]
|
||||
pub enum Target {
|
||||
#[strum(serialize = "system")]
|
||||
#[default]
|
||||
System,
|
||||
#[strum(serialize = "linux-x86-32")]
|
||||
LinuxX32,
|
||||
#[strum(serialize = "linux-x86-64")]
|
||||
LinuxX64,
|
||||
#[strum(serialize = "linux-arm-64")]
|
||||
LinuxArm64,
|
||||
#[strum(serialize = "macos-x86-64")]
|
||||
MacX64,
|
||||
#[strum(serialize = "macos-arm-64")]
|
||||
MacArm64,
|
||||
#[strum(serialize = "windows-x86-32")]
|
||||
WinX32,
|
||||
#[strum(serialize = "windows-x86-64")]
|
||||
WinX64,
|
||||
#[strum(serialize = "windows-arm-64")]
|
||||
WinArm64,
|
||||
#[strum(serialize = "wasm-32")]
|
||||
Wasm32,
|
||||
}
|
||||
|
||||
const MACOS: target_lexicon::OperatingSystem = target_lexicon::OperatingSystem::MacOSX {
|
||||
major: 12,
|
||||
minor: 0,
|
||||
patch: 0,
|
||||
};
|
||||
|
||||
impl Target {
|
||||
pub fn to_triple(self) -> Triple {
|
||||
use target_lexicon::*;
|
||||
|
||||
match self {
|
||||
Target::System => Triple::host(),
|
||||
Target::LinuxX32 => Triple {
|
||||
architecture: Architecture::X86_32(X86_32Architecture::I386),
|
||||
vendor: Vendor::Unknown,
|
||||
operating_system: OperatingSystem::Linux,
|
||||
environment: Environment::Unknown,
|
||||
binary_format: BinaryFormat::Elf,
|
||||
},
|
||||
Target::LinuxX64 => Triple {
|
||||
architecture: Architecture::X86_64,
|
||||
vendor: Vendor::Unknown,
|
||||
operating_system: OperatingSystem::Linux,
|
||||
environment: Environment::Unknown,
|
||||
binary_format: BinaryFormat::Elf,
|
||||
},
|
||||
Target::LinuxArm64 => Triple {
|
||||
architecture: Architecture::Aarch64(Aarch64Architecture::Aarch64),
|
||||
vendor: Vendor::Unknown,
|
||||
operating_system: OperatingSystem::Linux,
|
||||
environment: Environment::Unknown,
|
||||
binary_format: BinaryFormat::Elf,
|
||||
},
|
||||
Target::WinX32 => Triple {
|
||||
architecture: Architecture::X86_32(X86_32Architecture::I386),
|
||||
vendor: Vendor::Unknown,
|
||||
operating_system: OperatingSystem::Windows,
|
||||
environment: Environment::Gnu,
|
||||
binary_format: BinaryFormat::Coff,
|
||||
},
|
||||
Target::WinX64 => Triple {
|
||||
architecture: Architecture::X86_64,
|
||||
vendor: Vendor::Unknown,
|
||||
operating_system: OperatingSystem::Windows,
|
||||
environment: Environment::Gnu,
|
||||
binary_format: BinaryFormat::Coff,
|
||||
},
|
||||
Target::WinArm64 => Triple {
|
||||
architecture: Architecture::Aarch64(Aarch64Architecture::Aarch64),
|
||||
vendor: Vendor::Unknown,
|
||||
operating_system: OperatingSystem::Windows,
|
||||
environment: Environment::Gnu,
|
||||
binary_format: BinaryFormat::Coff,
|
||||
},
|
||||
Target::MacX64 => Triple {
|
||||
architecture: Architecture::X86_64,
|
||||
vendor: Vendor::Apple,
|
||||
operating_system: MACOS,
|
||||
environment: Environment::Unknown,
|
||||
binary_format: BinaryFormat::Macho,
|
||||
},
|
||||
Target::MacArm64 => Triple {
|
||||
architecture: Architecture::Aarch64(Aarch64Architecture::Aarch64),
|
||||
vendor: Vendor::Apple,
|
||||
operating_system: MACOS,
|
||||
environment: Environment::Unknown,
|
||||
binary_format: BinaryFormat::Macho,
|
||||
},
|
||||
Target::Wasm32 => Triple {
|
||||
architecture: Architecture::Wasm32,
|
||||
vendor: Vendor::Unknown,
|
||||
operating_system: OperatingSystem::Wasi,
|
||||
environment: Environment::Unknown,
|
||||
binary_format: BinaryFormat::Wasm,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Target> for Triple {
|
||||
fn from(target: &Target) -> Self {
|
||||
target.to_triple()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Target {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "{}", Into::<&'static str>::into(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for Target {
|
||||
type Err = String;
|
||||
|
||||
fn from_str(string: &str) -> Result<Self, Self::Err> {
|
||||
match string {
|
||||
"system" => Ok(Target::System),
|
||||
"linux-x86-32" => Ok(Target::LinuxX32),
|
||||
"linux-x86-64" => Ok(Target::LinuxX64),
|
||||
"linux-arm-64" => Ok(Target::LinuxArm64),
|
||||
"macos-x86-64" => Ok(Target::MacX64),
|
||||
"macos-arm-64" => Ok(Target::MacArm64),
|
||||
"windows-x86-64" => Ok(Target::WinX64),
|
||||
"wasm-32" => Ok(Target::Wasm32),
|
||||
_ => Err(format!("Roc does not know how to compile to {}", string)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_target_triple_str(target: &target_lexicon::Triple) -> Option<&'static str> {
|
||||
match target {
|
||||
target_lexicon::Triple {
|
||||
architecture: target_lexicon::Architecture::Wasm32,
|
||||
..
|
||||
} => Some(WASM_TARGET_STR),
|
||||
} => Some(Target::Wasm32.into()),
|
||||
target_lexicon::Triple {
|
||||
operating_system: target_lexicon::OperatingSystem::Linux,
|
||||
architecture: target_lexicon::Architecture::X86_64,
|
||||
..
|
||||
} => Some(LINUX_X86_64_TARGET_STR),
|
||||
} => Some(Target::LinuxX64.into()),
|
||||
target_lexicon::Triple {
|
||||
operating_system: target_lexicon::OperatingSystem::Linux,
|
||||
architecture: target_lexicon::Architecture::Aarch64(_),
|
||||
..
|
||||
} => Some(LINUX_ARM64_TARGET_STR),
|
||||
} => Some(Target::LinuxArm64.into()),
|
||||
target_lexicon::Triple {
|
||||
operating_system: target_lexicon::OperatingSystem::Darwin,
|
||||
architecture: target_lexicon::Architecture::Aarch64(_),
|
||||
..
|
||||
} => Some(MACOS_ARM64_TARGET_STR),
|
||||
} => Some(Target::MacArm64.into()),
|
||||
target_lexicon::Triple {
|
||||
operating_system: target_lexicon::OperatingSystem::Darwin,
|
||||
architecture: target_lexicon::Architecture::X86_64,
|
||||
..
|
||||
} => Some(MACOS_X86_64_TARGET_STR),
|
||||
} => Some(Target::MacX64.into()),
|
||||
target_lexicon::Triple {
|
||||
operating_system: target_lexicon::OperatingSystem::Windows,
|
||||
architecture: target_lexicon::Architecture::X86_64,
|
||||
..
|
||||
} => Some(WINDOWS_X86_64_TARGET_STR),
|
||||
} => Some(Target::WinX64.into()),
|
||||
target_lexicon::Triple {
|
||||
operating_system: target_lexicon::OperatingSystem::Windows,
|
||||
architecture: target_lexicon::Architecture::X86_32(_),
|
||||
..
|
||||
} => Some(WINDOWS_X86_32_TARGET_STR),
|
||||
} => Some(Target::WinX32.into()),
|
||||
target_lexicon::Triple {
|
||||
operating_system: target_lexicon::OperatingSystem::Windows,
|
||||
architecture: target_lexicon::Architecture::Aarch64(_),
|
||||
..
|
||||
} => Some(WIDNOWS_ARM64_TARGET_STR),
|
||||
} => Some(Target::WinArm64.into()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue