mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-03 19:58:18 +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
|
@ -20,6 +20,7 @@ use roc_load::{ExpectMetadata, Threading};
|
|||
use roc_mono::ir::OptLevel;
|
||||
use roc_packaging::cache::RocCacheDir;
|
||||
use roc_packaging::tarball::Compression;
|
||||
use roc_target::Target;
|
||||
use std::env;
|
||||
use std::ffi::{CString, OsStr, OsString};
|
||||
use std::io;
|
||||
|
@ -28,11 +29,8 @@ use std::os::raw::{c_char, c_int};
|
|||
use std::path::{Path, PathBuf};
|
||||
use std::process;
|
||||
use std::time::Instant;
|
||||
use strum::{EnumIter, IntoEnumIterator, IntoStaticStr};
|
||||
use target_lexicon::{Aarch64Architecture, BinaryFormat};
|
||||
use target_lexicon::{
|
||||
Architecture, Environment, OperatingSystem, Triple, Vendor, X86_32Architecture,
|
||||
};
|
||||
use strum::IntoEnumIterator;
|
||||
use target_lexicon::{Architecture, Triple};
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
use tempfile::TempDir;
|
||||
|
||||
|
@ -1275,117 +1273,3 @@ fn run_wasm<I: Iterator<Item = S>, S: AsRef<[u8]>>(wasm_path: &std::path::Path,
|
|||
fn run_wasm<I: Iterator<Item = S>, S: AsRef<[u8]>>(_wasm_path: &std::path::Path, _args: I) {
|
||||
println!("Running wasm files is not supported on this target.");
|
||||
}
|
||||
|
||||
#[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-64")]
|
||||
WinX64,
|
||||
#[strum(serialize = "wasm-32")]
|
||||
Wasm32,
|
||||
}
|
||||
|
||||
const MACOS: OperatingSystem = OperatingSystem::MacOSX {
|
||||
major: 12,
|
||||
minor: 0,
|
||||
patch: 0,
|
||||
};
|
||||
|
||||
impl Target {
|
||||
pub fn to_triple(self) -> Triple {
|
||||
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::WinX64 => Triple {
|
||||
architecture: Architecture::X86_64,
|
||||
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)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,10 +2,10 @@
|
|||
use roc_build::link::LinkType;
|
||||
use roc_build::program::{check_file, CodeGenBackend};
|
||||
use roc_cli::{
|
||||
build_app, format, test, BuildConfig, FormatMode, Target, CMD_BUILD, CMD_CHECK, CMD_DEV,
|
||||
CMD_DOCS, CMD_EDIT, CMD_FORMAT, CMD_GEN_STUB_LIB, CMD_GLUE, CMD_REPL, CMD_RUN, CMD_TEST,
|
||||
CMD_VERSION, DIRECTORY_OR_FILES, FLAG_CHECK, FLAG_DEV, FLAG_LIB, FLAG_NO_LINK, FLAG_TARGET,
|
||||
FLAG_TIME, GLUE_DIR, GLUE_SPEC, ROC_FILE,
|
||||
build_app, format, test, BuildConfig, FormatMode, CMD_BUILD, CMD_CHECK, CMD_DEV, CMD_DOCS,
|
||||
CMD_EDIT, CMD_FORMAT, CMD_GEN_STUB_LIB, CMD_GLUE, CMD_REPL, CMD_RUN, CMD_TEST, CMD_VERSION,
|
||||
DIRECTORY_OR_FILES, FLAG_CHECK, FLAG_DEV, FLAG_LIB, FLAG_NO_LINK, FLAG_TARGET, FLAG_TIME,
|
||||
GLUE_DIR, GLUE_SPEC, ROC_FILE,
|
||||
};
|
||||
use roc_docs::generate_docs_html;
|
||||
use roc_error_macros::user_error;
|
||||
|
@ -13,6 +13,7 @@ use roc_gen_dev::AssemblyBackendMode;
|
|||
use roc_gen_llvm::llvm::build::LlvmBackendMode;
|
||||
use roc_load::{LoadingProblem, Threading};
|
||||
use roc_packaging::cache::{self, RocCacheDir};
|
||||
use roc_target::Target;
|
||||
use std::fs::{self, FileType};
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
|
|
@ -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