From 7df31619dd645f66bc42412c230d20d486f74c40 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Mon, 1 Aug 2022 23:53:17 +0200 Subject: [PATCH] add the concept of an OS to target info --- crates/cli/src/build.rs | 12 ++++----- crates/compiler/build/src/link.rs | 11 ++++---- crates/compiler/builtins/src/bitcode.rs | 5 ++-- crates/compiler/roc_target/src/lib.rs | 36 ++++++++++++++++++------- crates/glue/src/load.rs | 4 +-- crates/glue/src/rust_glue.rs | 5 ++-- 6 files changed, 45 insertions(+), 28 deletions(-) diff --git a/crates/cli/src/build.rs b/crates/cli/src/build.rs index a50ec84261..ddae57fb32 100644 --- a/crates/cli/src/build.rs +++ b/crates/cli/src/build.rs @@ -75,23 +75,23 @@ pub fn build_file<'a>( // // and zig does not currently emit `.a` webassembly static libraries let (host_extension, app_extension, extension) = { - use roc_target::Architecture::*; + use roc_target::OperatingSystem::*; - match roc_target::Architecture::from(target.architecture) { - Wasm32 => { + match roc_target::OperatingSystem::from(target.operating_system) { + Wasi => { if matches!(opt_level, OptLevel::Development) { ("wasm", "wasm", Some("wasm")) } else { ("zig", "bc", Some("wasm")) } } - Aarch32 | Aarch64 | Wasm32 | X86_32 | X86_64 => ("o", "o", None), - Windows64 => ("obj", "obj", Some("exe")), + Unix => ("o", "o", None), + Windows => ("obj", "obj", Some("exe")), } }; let cwd = app_module_path.parent().unwrap(); - let mut binary_path = cwd.join(&*loaded.output_path); // TODO should join ".exe" on Windows + let mut binary_path = cwd.join(&*loaded.output_path); if let Some(extension) = extension { binary_path.set_extension(extension); diff --git a/crates/compiler/build/src/link.rs b/crates/compiler/build/src/link.rs index 6a244f9831..318cb999ef 100644 --- a/crates/compiler/build/src/link.rs +++ b/crates/compiler/build/src/link.rs @@ -136,7 +136,7 @@ pub fn build_zig_host_native( zig_str_path, "--pkg-end", // include the zig runtime - "-fcompiler-rt", + // "-fcompiler-rt", // include libc "--library", "c", @@ -158,6 +158,7 @@ pub fn build_zig_host_native( } else if matches!(opt_level, OptLevel::Size) { command.args(&["-O", "ReleaseSmall"]); } + command.output().unwrap() } @@ -423,9 +424,10 @@ pub fn rebuild_host( host_input_path.with_file_name(if shared_lib_path.is_some() { "dynhost" } else { - match roc_target::Architecture::from(target.architecture) { - roc_target::Architecture::Windows64 => "host.obj", - _ => "host.o", + match roc_target::OperatingSystem::from(target.operating_system) { + roc_target::OperatingSystem::Windows => "host.obj", + roc_target::OperatingSystem::Unix => "host.o", + roc_target::OperatingSystem::Wasi => "host.o", } }) }; @@ -1101,7 +1103,6 @@ fn link_windows( _link_type: LinkType, ) -> io::Result<(Child, PathBuf)> { let zig_str_path = find_zig_str_path(); - let wasi_libc_path = find_wasi_libc_path(); let child = Command::new(&zig_executable()) .args(&["build-exe"]) diff --git a/crates/compiler/builtins/src/bitcode.rs b/crates/compiler/builtins/src/bitcode.rs index dbbfc5ccf5..be7ac8dbdd 100644 --- a/crates/compiler/builtins/src/bitcode.rs +++ b/crates/compiler/builtins/src/bitcode.rs @@ -60,7 +60,7 @@ impl FloatWidth { match self { F32 => 4, F64 | F128 => match target_info.architecture { - X86_64 | Aarch64 | Wasm32 | Windows64 => 8, + X86_64 | Aarch64 | Wasm32 => 8, X86_32 | Aarch32 => 4, }, } @@ -127,8 +127,7 @@ impl IntWidth { Architecture::X86_64 | Architecture::Aarch64 | Architecture::Aarch32 - | Architecture::Wasm32 - | Architecture::Windows64 => 8, + | Architecture::Wasm32 => 8, Architecture::X86_32 => 4, }, U128 | I128 => { diff --git a/crates/compiler/roc_target/src/lib.rs b/crates/compiler/roc_target/src/lib.rs index d514d03052..9c953c45d4 100644 --- a/crates/compiler/roc_target/src/lib.rs +++ b/crates/compiler/roc_target/src/lib.rs @@ -4,9 +4,30 @@ use strum_macros::{EnumCount, EnumIter}; +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum OperatingSystem { + Windows, + Unix, + Wasi, +} + +impl From for OperatingSystem { + fn from(target: target_lexicon::OperatingSystem) -> Self { + match target { + target_lexicon::OperatingSystem::Windows => OperatingSystem::Windows, + target_lexicon::OperatingSystem::Wasi => OperatingSystem::Wasi, + target_lexicon::OperatingSystem::Linux => OperatingSystem::Unix, + target_lexicon::OperatingSystem::MacOSX {..} => OperatingSystem::Unix, + other => unreachable!("unsupported operating system {:?}", other), + } + } +} + + #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct TargetInfo { pub architecture: Architecture, + pub operating_system: OperatingSystem, } impl TargetInfo { @@ -28,18 +49,21 @@ impl TargetInfo { pub const fn default_aarch64() -> Self { TargetInfo { architecture: Architecture::Aarch64, + operating_system: OperatingSystem::Unix, } } pub const fn default_x86_64() -> Self { TargetInfo { architecture: Architecture::X86_64, + operating_system: OperatingSystem::Unix, } } pub const fn default_wasm32() -> Self { TargetInfo { architecture: Architecture::Wasm32, + operating_system: OperatingSystem::Wasi, } } } @@ -47,14 +71,9 @@ impl TargetInfo { impl From<&target_lexicon::Triple> for TargetInfo { fn from(triple: &target_lexicon::Triple) -> Self { let architecture = Architecture::from(triple.architecture); + let operating_system = OperatingSystem::from(triple.operating_system); - Self { architecture } - } -} - -impl From for TargetInfo { - fn from(architecture: Architecture) -> Self { - Self { architecture } + Self { architecture, operating_system } } } @@ -74,7 +93,6 @@ pub enum Architecture { Wasm32, X86_32, X86_64, - Windows64, } impl Architecture { @@ -82,7 +100,7 @@ impl Architecture { use Architecture::*; match self { - X86_64 | Aarch64 | Windows64 => PtrWidth::Bytes8, + X86_64 | Aarch64 => PtrWidth::Bytes8, X86_32 | Aarch32 | Wasm32 => PtrWidth::Bytes4, } } diff --git a/crates/glue/src/load.rs b/crates/glue/src/load.rs index 0b942325b2..1e4a612cee 100644 --- a/crates/glue/src/load.rs +++ b/crates/glue/src/load.rs @@ -3,7 +3,7 @@ use crate::types::{Env, Types}; use bumpalo::Bump; use roc_load::{LoadedModule, LoadingProblem, Threading}; use roc_reporting::report::RenderTarget; -use roc_target::{Architecture, TargetInfo}; +use roc_target::{Architecture, TargetInfo,OperatingSystem}; use std::fs::File; use std::io::{self, ErrorKind, Write}; use std::path::{Path, PathBuf}; @@ -135,7 +135,7 @@ pub fn load_types( let types_and_targets = Architecture::iter() .map(|arch| { - let target_info = arch.into(); + let target_info = TargetInfo { architecture: arch.into(), operating_system: OperatingSystem::Unix}; let mut env = Env::new(arena, subs, &mut interns, target_info); (env.vars_to_types(variables.clone()), target_info) diff --git a/crates/glue/src/rust_glue.rs b/crates/glue/src/rust_glue.rs index 127c1f43ab..f40896b929 100644 --- a/crates/glue/src/rust_glue.rs +++ b/crates/glue/src/rust_glue.rs @@ -1711,7 +1711,6 @@ fn arch_to_str(architecture: Architecture) -> &'static str { Architecture::Aarch64 => "aarch64", Architecture::Aarch32 => "arm", Architecture::Wasm32 => "wasm32", - Architecture::Windows64 => todo!(), } } @@ -1724,7 +1723,7 @@ fn write_indents(indentations: usize, buf: &mut String) { fn max_pointer_tagged_variants(architecture: Architecture) -> usize { match architecture { // On a 64-bit system, pointers have 3 bits that are unused, so return 2^3 = 8 - Architecture::X86_64 | Architecture::Aarch64 | Architecture::Windows64 => 8, + Architecture::X86_64 | Architecture::Aarch64 => 8, // On a 32-bit system, pointers have 2 bits that are unused, so return 2^4 = 4 Architecture::X86_32 | Architecture::Aarch32 | Architecture::Wasm32 => 4, } @@ -1734,7 +1733,7 @@ fn max_pointer_tagged_variants(architecture: Architecture) -> usize { fn tagged_pointer_bitmask(architecture: Architecture) -> u8 { match architecture { // On a 64-bit system, pointers have 3 bits that are unused - Architecture::X86_64 | Architecture::Aarch64 | Architecture::Windows64 => 0b0000_0111, + Architecture::X86_64 | Architecture::Aarch64 => 0b0000_0111, // On a 32-bit system, pointers have 2 bits that are unused Architecture::X86_32 | Architecture::Aarch32 | Architecture::Wasm32 => 0b0000_0011, }