Skip to content

Commit

Permalink
std: move platform modules into sys::pal
Browse files Browse the repository at this point in the history
  • Loading branch information
joboet committed Oct 27, 2023
1 parent 54e57e6 commit 4c732f9
Show file tree
Hide file tree
Showing 250 changed files with 188 additions and 180 deletions.
18 changes: 9 additions & 9 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,29 +508,29 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
// we need to handle re-exports correctly.
//
// For example, take `std::os::unix::process::CommandExt`, this trait is actually
// defined at `std::sys::unix::ext::process::CommandExt` (at time of writing).
// defined at `std::sys::pal::unix::ext::process::CommandExt` (at time of writing).
//
// `std::os::unix` reexports the contents of `std::sys::unix::ext`. `std::sys` is
// `std::os::unix` reexports the contents of `std::sys::pal::unix::ext`. `std::sys` is
// private so the "true" path to `CommandExt` isn't accessible.
//
// In this case, the `visible_parent_map` will look something like this:
//
// (child) -> (parent)
// `std::sys::unix::ext::process::CommandExt` -> `std::sys::unix::ext::process`
// `std::sys::unix::ext::process` -> `std::sys::unix::ext`
// `std::sys::unix::ext` -> `std::os`
// `std::sys::pal::unix::ext::process::CommandExt` -> `std::sys::pal::unix::ext::process`
// `std::sys::pal::unix::ext::process` -> `std::sys::pal::unix::ext`
// `std::sys::pal::unix::ext` -> `std::os`
//
// This is correct, as the visible parent of `std::sys::unix::ext` is in fact
// This is correct, as the visible parent of `std::sys::pal::unix::ext` is in fact
// `std::os`.
//
// When printing the path to `CommandExt` and looking at the `cur_def_key` that
// corresponds to `std::sys::unix::ext`, we would normally print `ext` and then go
// corresponds to `std::sys::pal::unix::ext`, we would normally print `ext` and then go
// to the parent - resulting in a mangled path like
// `std::os::ext::process::CommandExt`.
//
// Instead, we must detect that there was a re-export and instead print `unix`
// (which is the name `std::sys::unix::ext` was re-exported as in `std::os`). To
// do this, we compare the parent of `std::sys::unix::ext` (`std::sys::unix`) with
// (which is the name `std::sys::pal::unix::ext` was re-exported as in `std::os`). To
// do this, we compare the parent of `std::sys::pal::unix::ext` (`std::sys::pal::unix`) with
// the visible parent (`std::os`). If these do not match, then we iterate over
// the children of the visible parent (as was done when computing
// `visible_parent_map`), looking for the specific child we currently have and then
Expand Down
129 changes: 8 additions & 121 deletions library/std/src/sys/mod.rs
Original file line number Diff line number Diff line change
@@ -1,121 +1,8 @@
//! Platform-dependent platform abstraction.
//!
//! The `std::sys` module is the abstracted interface through which
//! `std` talks to the underlying operating system. It has different
//! implementations for different operating system families, today
//! just Unix and Windows, and initial support for Redox.
//!
//! The centralization of platform-specific code in this module is
//! enforced by the "platform abstraction layer" tidy script in
//! `tools/tidy/src/pal.rs`.
//!
//! This module is closely related to the platform-independent system
//! integration code in `std::sys_common`. See that module's
//! documentation for details.
//!
//! In the future it would be desirable for the independent
//! implementations of this module to be extracted to their own crates
//! that `std` can link to, thus enabling their implementation
//! out-of-tree via crate replacement. Though due to the complex
//! inter-dependencies within `std` that will be a challenging goal to
//! achieve.
#![allow(missing_debug_implementations)]

pub mod common;
mod personality;

cfg_if::cfg_if! {
if #[cfg(unix)] {
mod unix;
pub use self::unix::*;
} else if #[cfg(windows)] {
mod windows;
pub use self::windows::*;
} else if #[cfg(target_os = "solid_asp3")] {
mod solid;
pub use self::solid::*;
} else if #[cfg(target_os = "hermit")] {
mod hermit;
pub use self::hermit::*;
} else if #[cfg(target_os = "wasi")] {
mod wasi;
pub use self::wasi::*;
} else if #[cfg(target_family = "wasm")] {
mod wasm;
pub use self::wasm::*;
} else if #[cfg(target_os = "xous")] {
mod xous;
pub use self::xous::*;
} else if #[cfg(target_os = "uefi")] {
mod uefi;
pub use self::uefi::*;
} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
mod sgx;
pub use self::sgx::*;
} else {
mod unsupported;
pub use self::unsupported::*;
}
}

cfg_if::cfg_if! {
// Fuchsia components default to full backtrace.
if #[cfg(target_os = "fuchsia")] {
pub const FULL_BACKTRACE_DEFAULT: bool = true;
} else {
pub const FULL_BACKTRACE_DEFAULT: bool = false;
}
}

#[cfg(not(test))]
cfg_if::cfg_if! {
if #[cfg(target_os = "android")] {
pub use self::android::log2f32;
pub use self::android::log2f64;
} else {
#[inline]
pub fn log2f32(n: f32) -> f32 {
unsafe { crate::intrinsics::log2f32(n) }
}

#[inline]
pub fn log2f64(n: f64) -> f64 {
unsafe { crate::intrinsics::log2f64(n) }
}
}
}

// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
// because of their non-standard behavior (e.g., log(-n) returns -Inf instead
// of expected NaN).
#[cfg(not(test))]
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
#[inline]
pub fn log_wrapper<F: Fn(f64) -> f64>(n: f64, log_fn: F) -> f64 {
if n.is_finite() {
if n > 0.0 {
log_fn(n)
} else if n == 0.0 {
f64::NEG_INFINITY // log(0) = -Inf
} else {
f64::NAN // log(-n) = NaN
}
} else if n.is_nan() {
n // log(NaN) = NaN
} else if n > 0.0 {
n // log(Inf) = Inf
} else {
f64::NAN // log(-Inf) = NaN
}
}

#[cfg(not(test))]
#[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
#[inline]
pub fn log_wrapper<F: Fn(f64) -> f64>(n: f64, log_fn: F) -> f64 {
log_fn(n)
}

#[cfg(not(target_os = "uefi"))]
pub type RawOsError = i32;
/// The PAL (platform abstraction layer) contains platform-specific abstractions
/// for implementing the features in the other submodules module, e.g. UNIX file
/// descriptors.
mod pal;

// FIXME(117276): remove this, move feature implementations into individual
// submodules.
pub use pal::*;
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::alloc::{GlobalAlloc, Layout, System};
use crate::ptr;
use crate::sys::hermit::abi;
use crate::sys::pal::hermit::abi;

#[stable(feature = "alloc_system_type", since = "1.28.0")]
unsafe impl GlobalAlloc for System {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use crate::io::{self, Read};
use crate::os::hermit::io::{FromRawFd, OwnedFd, RawFd};
use crate::sys::cvt;
use crate::sys::hermit::abi;
use crate::sys::unsupported;
use crate::sys::pal::hermit::abi;
use crate::sys::pal::unsupported;
use crate::sys_common::{AsInner, FromInner, IntoInner};

use crate::os::hermit::io::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use crate::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, Raw
use crate::path::{Path, PathBuf};
use crate::sys::common::small_c_string::run_path_with_cstr;
use crate::sys::cvt;
use crate::sys::hermit::abi::{
use crate::sys::pal::hermit::abi::{
self, O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY,
};
use crate::sys::hermit::fd::FileDesc;
use crate::sys::pal::hermit::fd::FileDesc;
use crate::sys::pal::unsupported;
use crate::sys::time::SystemTime;
use crate::sys::unsupported;
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};

pub use crate::sys_common::fs::{copy, try_exists};
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub unsafe extern "C" fn runtime_entry(
argv: *const *const c_char,
env: *const *const c_char,
) -> ! {
use crate::sys::hermit::thread_local_dtor::run_dtors;
use crate::sys::pal::hermit::thread_local_dtor::run_dtors;
extern "C" {
fn main(argc: isize, argv: *const *const c_char) -> i32;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::io::{self, BorrowedBuf, BorrowedCursor, IoSlice, IoSliceMut};
use crate::mem;
use crate::net::{Shutdown, SocketAddr};
use crate::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, RawFd};
use crate::sys::hermit::fd::FileDesc;
use crate::sys::pal::hermit::fd::FileDesc;
use crate::sys::time::Instant;
use crate::sys_common::net::{getsockopt, setsockopt, sockaddr_to_addr};
use crate::sys_common::{AsInner, FromInner, IntoInner};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use crate::os::hermit::ffi::OsStringExt;
use crate::path::{self, PathBuf};
use crate::str;
use crate::sync::Mutex;
use crate::sys::hermit::abi;
use crate::sys::memchr;
use crate::sys::unsupported;
use crate::sys::pal::hermit::abi;
use crate::sys::pal::unsupported;
use crate::vec;

pub fn errno() -> i32 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::io;
use crate::io::{IoSlice, IoSliceMut};
use crate::sys::hermit::abi;
use crate::sys::pal::hermit::abi;

pub struct Stdin;
pub struct Stdout;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::io;
use crate::mem;
use crate::num::NonZeroUsize;
use crate::ptr;
use crate::sys::hermit::abi;
use crate::sys::hermit::thread_local_dtor::run_dtors;
use crate::sys::pal::hermit::abi;
use crate::sys::pal::hermit::thread_local_dtor::run_dtors;
use crate::time::Duration;

pub type Tid = abi::Tid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

use crate::cmp::Ordering;
use crate::ops::{Add, AddAssign, Sub, SubAssign};
use crate::sys::hermit::abi;
use crate::sys::hermit::abi::timespec;
use crate::sys::hermit::abi::{CLOCK_MONOTONIC, CLOCK_REALTIME, NSEC_PER_SEC};
use crate::sys::pal::hermit::abi;
use crate::sys::pal::hermit::abi::timespec;
use crate::sys::pal::hermit::abi::{CLOCK_MONOTONIC, CLOCK_REALTIME, NSEC_PER_SEC};
use crate::time::Duration;
use core::hash::{Hash, Hasher};

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
121 changes: 121 additions & 0 deletions library/std/src/sys/pal/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//! Platform-dependent platform abstraction.
//!
//! The `std::sys` module is the abstracted interface through which
//! `std` talks to the underlying operating system. It has different
//! implementations for different operating system families, today
//! just Unix and Windows, and initial support for Redox.
//!
//! The centralization of platform-specific code in this module is
//! enforced by the "platform abstraction layer" tidy script in
//! `tools/tidy/src/pal.rs`.
//!
//! This module is closely related to the platform-independent system
//! integration code in `std::sys_common`. See that module's
//! documentation for details.
//!
//! In the future it would be desirable for the independent
//! implementations of this module to be extracted to their own crates
//! that `std` can link to, thus enabling their implementation
//! out-of-tree via crate replacement. Though due to the complex
//! inter-dependencies within `std` that will be a challenging goal to
//! achieve.
#![allow(missing_debug_implementations)]

pub mod common;
mod personality;

cfg_if::cfg_if! {
if #[cfg(unix)] {
mod unix;
pub use self::unix::*;
} else if #[cfg(windows)] {
mod windows;
pub use self::windows::*;
} else if #[cfg(target_os = "solid_asp3")] {
mod solid;
pub use self::solid::*;
} else if #[cfg(target_os = "hermit")] {
mod hermit;
pub use self::hermit::*;
} else if #[cfg(target_os = "wasi")] {
mod wasi;
pub use self::wasi::*;
} else if #[cfg(target_family = "wasm")] {
mod wasm;
pub use self::wasm::*;
} else if #[cfg(target_os = "xous")] {
mod xous;
pub use self::xous::*;
} else if #[cfg(target_os = "uefi")] {
mod uefi;
pub use self::uefi::*;
} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
mod sgx;
pub use self::sgx::*;
} else {
mod unsupported;
pub use self::unsupported::*;
}
}

cfg_if::cfg_if! {
// Fuchsia components default to full backtrace.
if #[cfg(target_os = "fuchsia")] {
pub const FULL_BACKTRACE_DEFAULT: bool = true;
} else {
pub const FULL_BACKTRACE_DEFAULT: bool = false;
}
}

#[cfg(not(test))]
cfg_if::cfg_if! {
if #[cfg(target_os = "android")] {
pub use self::android::log2f32;
pub use self::android::log2f64;
} else {
#[inline]
pub fn log2f32(n: f32) -> f32 {
unsafe { crate::intrinsics::log2f32(n) }
}

#[inline]
pub fn log2f64(n: f64) -> f64 {
unsafe { crate::intrinsics::log2f64(n) }
}
}
}

// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
// because of their non-standard behavior (e.g., log(-n) returns -Inf instead
// of expected NaN).
#[cfg(not(test))]
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
#[inline]
pub fn log_wrapper<F: Fn(f64) -> f64>(n: f64, log_fn: F) -> f64 {
if n.is_finite() {
if n > 0.0 {
log_fn(n)
} else if n == 0.0 {
f64::NEG_INFINITY // log(0) = -Inf
} else {
f64::NAN // log(-n) = NaN
}
} else if n.is_nan() {
n // log(NaN) = NaN
} else if n > 0.0 {
n // log(Inf) = Inf
} else {
f64::NAN // log(-Inf) = NaN
}
}

#[cfg(not(test))]
#[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
#[inline]
pub fn log_wrapper<F: Fn(f64) -> f64>(n: f64, log_fn: F) -> f64 {
log_fn(n)
}

#[cfg(not(target_os = "uefi"))]
pub type RawOsError = i32;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::alloc::{GlobalAlloc, Layout, System};
use crate::ptr;
use crate::sys::sgx::abi::mem as sgx_mem;
use crate::sys::pal::sgx::abi::mem as sgx_mem;
use core::sync::atomic::{AtomicBool, Ordering};

use super::waitqueue::SpinMutex;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 4c732f9

Please sign in to comment.