Skip to content

Commit

Permalink
Auto merge of #36807 - brson:pal, r=alexcrichton
Browse files Browse the repository at this point in the history
Restrict where in the tree platform-specific cfgs may be mentioned

With the ports of Rust never ending, it's important that we keep things tidy. The main thing this PR does is introduce  a new "pal" (platform abstraction layer) tidy check that limits where platform-specific CFGs may appear.

This is intended to maintain existing standards of code organization
in hopes that the standard library will continue to be refactored to
isolate platform-specific bits, making porting easier; where "standard
library" roughly means "all the dependencies of the std and test
crates".

This generally means placing restrictions on where `cfg(unix)`,
`cfg(windows)`, `cfg(target_os)` and `cfg(target_env)` may appear,
the basic objective being to isolate platform-specific code to the
platform-specific `std::sys` modules, and to the allocation,
unwinding, and libc crates.

Following are the basic rules, though there are currently
exceptions:

- core may not have platform-specific code
- liballoc_system may have platform-specific code
- liballoc_jemalloc may have platform-specific code
- libpanic_abort may have platform-specific code
- libpanic_unwind may have platform-specific code
- other crates in the std facade may not
- std may have platform-specific code in the following places
  - sys/unix/
  - sys/windows/
  - os/

There are plenty of exceptions today though, noted in the whitelist.

The end-state, IMO, is for the standard library to be portable by porting only `std::sys` (possibly extracted to its own crate), an allocator crate, an unwinder crate, and possibly a libc crate (if std depends on it); but that outcome is far off and independent of the utility of enforcing where such code lives today.

cc @rust-lang/libs
  • Loading branch information
bors authored Oct 2, 2016
2 parents de90157 + 405c010 commit d97a08e
Show file tree
Hide file tree
Showing 29 changed files with 1,191 additions and 880 deletions.
197 changes: 12 additions & 185 deletions src/libstd/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use ffi::{OsStr, OsString};
use fmt;
use io;
use path::{Path, PathBuf};
use sys;
use sys::os as os_imp;

/// Returns the current working directory as a `PathBuf`.
Expand Down Expand Up @@ -557,7 +558,7 @@ pub struct Args { inner: ArgsOs }
///
/// This structure is created through the `std::env::args_os` method.
#[stable(feature = "env", since = "1.0.0")]
pub struct ArgsOs { inner: os_imp::Args }
pub struct ArgsOs { inner: sys::args::Args }

/// Returns the arguments which this program was started with (normally passed
/// via the command line).
Expand Down Expand Up @@ -606,7 +607,7 @@ pub fn args() -> Args {
/// ```
#[stable(feature = "env", since = "1.0.0")]
pub fn args_os() -> ArgsOs {
ArgsOs { inner: os_imp::args() }
ArgsOs { inner: sys::args::args() }
}

#[stable(feature = "env", since = "1.0.0")]
Expand Down Expand Up @@ -649,6 +650,8 @@ impl DoubleEndedIterator for ArgsOs {
/// Constants associated with the current target
#[stable(feature = "env", since = "1.0.0")]
pub mod consts {
use sys::env::os;

/// A string describing the architecture of the CPU that is currently
/// in use.
///
Expand All @@ -673,7 +676,7 @@ pub mod consts {
/// - unix
/// - windows
#[stable(feature = "env", since = "1.0.0")]
pub const FAMILY: &'static str = super::os::FAMILY;
pub const FAMILY: &'static str = os::FAMILY;

/// A string describing the specific operating system in use.
/// Example value is `linux`.
Expand All @@ -692,7 +695,7 @@ pub mod consts {
/// - android
/// - windows
#[stable(feature = "env", since = "1.0.0")]
pub const OS: &'static str = super::os::OS;
pub const OS: &'static str = os::OS;

/// Specifies the filename prefix used for shared libraries on this
/// platform. Example value is `lib`.
Expand All @@ -702,7 +705,7 @@ pub mod consts {
/// - lib
/// - `""` (an empty string)
#[stable(feature = "env", since = "1.0.0")]
pub const DLL_PREFIX: &'static str = super::os::DLL_PREFIX;
pub const DLL_PREFIX: &'static str = os::DLL_PREFIX;

/// Specifies the filename suffix used for shared libraries on this
/// platform. Example value is `.so`.
Expand All @@ -713,7 +716,7 @@ pub mod consts {
/// - .dylib
/// - .dll
#[stable(feature = "env", since = "1.0.0")]
pub const DLL_SUFFIX: &'static str = super::os::DLL_SUFFIX;
pub const DLL_SUFFIX: &'static str = os::DLL_SUFFIX;

/// Specifies the file extension used for shared libraries on this
/// platform that goes after the dot. Example value is `so`.
Expand All @@ -724,7 +727,7 @@ pub mod consts {
/// - dylib
/// - dll
#[stable(feature = "env", since = "1.0.0")]
pub const DLL_EXTENSION: &'static str = super::os::DLL_EXTENSION;
pub const DLL_EXTENSION: &'static str = os::DLL_EXTENSION;

/// Specifies the filename suffix used for executable binaries on this
/// platform. Example value is `.exe`.
Expand All @@ -736,7 +739,7 @@ pub mod consts {
/// - .pexe
/// - `""` (an empty string)
#[stable(feature = "env", since = "1.0.0")]
pub const EXE_SUFFIX: &'static str = super::os::EXE_SUFFIX;
pub const EXE_SUFFIX: &'static str = os::EXE_SUFFIX;

/// Specifies the file extension, if any, used for executable binaries
/// on this platform. Example value is `exe`.
Expand All @@ -746,183 +749,7 @@ pub mod consts {
/// - exe
/// - `""` (an empty string)
#[stable(feature = "env", since = "1.0.0")]
pub const EXE_EXTENSION: &'static str = super::os::EXE_EXTENSION;

}

#[cfg(target_os = "linux")]
mod os {
pub const FAMILY: &'static str = "unix";
pub const OS: &'static str = "linux";
pub const DLL_PREFIX: &'static str = "lib";
pub const DLL_SUFFIX: &'static str = ".so";
pub const DLL_EXTENSION: &'static str = "so";
pub const EXE_SUFFIX: &'static str = "";
pub const EXE_EXTENSION: &'static str = "";
}

#[cfg(target_os = "macos")]
mod os {
pub const FAMILY: &'static str = "unix";
pub const OS: &'static str = "macos";
pub const DLL_PREFIX: &'static str = "lib";
pub const DLL_SUFFIX: &'static str = ".dylib";
pub const DLL_EXTENSION: &'static str = "dylib";
pub const EXE_SUFFIX: &'static str = "";
pub const EXE_EXTENSION: &'static str = "";
}

#[cfg(target_os = "ios")]
mod os {
pub const FAMILY: &'static str = "unix";
pub const OS: &'static str = "ios";
pub const DLL_PREFIX: &'static str = "lib";
pub const DLL_SUFFIX: &'static str = ".dylib";
pub const DLL_EXTENSION: &'static str = "dylib";
pub const EXE_SUFFIX: &'static str = "";
pub const EXE_EXTENSION: &'static str = "";
}

#[cfg(target_os = "freebsd")]
mod os {
pub const FAMILY: &'static str = "unix";
pub const OS: &'static str = "freebsd";
pub const DLL_PREFIX: &'static str = "lib";
pub const DLL_SUFFIX: &'static str = ".so";
pub const DLL_EXTENSION: &'static str = "so";
pub const EXE_SUFFIX: &'static str = "";
pub const EXE_EXTENSION: &'static str = "";
}

#[cfg(target_os = "dragonfly")]
mod os {
pub const FAMILY: &'static str = "unix";
pub const OS: &'static str = "dragonfly";
pub const DLL_PREFIX: &'static str = "lib";
pub const DLL_SUFFIX: &'static str = ".so";
pub const DLL_EXTENSION: &'static str = "so";
pub const EXE_SUFFIX: &'static str = "";
pub const EXE_EXTENSION: &'static str = "";
}

#[cfg(target_os = "bitrig")]
mod os {
pub const FAMILY: &'static str = "unix";
pub const OS: &'static str = "bitrig";
pub const DLL_PREFIX: &'static str = "lib";
pub const DLL_SUFFIX: &'static str = ".so";
pub const DLL_EXTENSION: &'static str = "so";
pub const EXE_SUFFIX: &'static str = "";
pub const EXE_EXTENSION: &'static str = "";
}

#[cfg(target_os = "netbsd")]
mod os {
pub const FAMILY: &'static str = "unix";
pub const OS: &'static str = "netbsd";
pub const DLL_PREFIX: &'static str = "lib";
pub const DLL_SUFFIX: &'static str = ".so";
pub const DLL_EXTENSION: &'static str = "so";
pub const EXE_SUFFIX: &'static str = "";
pub const EXE_EXTENSION: &'static str = "";
}

#[cfg(target_os = "openbsd")]
mod os {
pub const FAMILY: &'static str = "unix";
pub const OS: &'static str = "openbsd";
pub const DLL_PREFIX: &'static str = "lib";
pub const DLL_SUFFIX: &'static str = ".so";
pub const DLL_EXTENSION: &'static str = "so";
pub const EXE_SUFFIX: &'static str = "";
pub const EXE_EXTENSION: &'static str = "";
}

#[cfg(target_os = "android")]
mod os {
pub const FAMILY: &'static str = "unix";
pub const OS: &'static str = "android";
pub const DLL_PREFIX: &'static str = "lib";
pub const DLL_SUFFIX: &'static str = ".so";
pub const DLL_EXTENSION: &'static str = "so";
pub const EXE_SUFFIX: &'static str = "";
pub const EXE_EXTENSION: &'static str = "";
}

#[cfg(target_os = "solaris")]
mod os {
pub const FAMILY: &'static str = "unix";
pub const OS: &'static str = "solaris";
pub const DLL_PREFIX: &'static str = "lib";
pub const DLL_SUFFIX: &'static str = ".so";
pub const DLL_EXTENSION: &'static str = "so";
pub const EXE_SUFFIX: &'static str = "";
pub const EXE_EXTENSION: &'static str = "";
}

#[cfg(target_os = "windows")]
mod os {
pub const FAMILY: &'static str = "windows";
pub const OS: &'static str = "windows";
pub const DLL_PREFIX: &'static str = "";
pub const DLL_SUFFIX: &'static str = ".dll";
pub const DLL_EXTENSION: &'static str = "dll";
pub const EXE_SUFFIX: &'static str = ".exe";
pub const EXE_EXTENSION: &'static str = "exe";
}

#[cfg(all(target_os = "nacl", not(target_arch = "le32")))]
mod os {
pub const FAMILY: &'static str = "unix";
pub const OS: &'static str = "nacl";
pub const DLL_PREFIX: &'static str = "lib";
pub const DLL_SUFFIX: &'static str = ".so";
pub const DLL_EXTENSION: &'static str = "so";
pub const EXE_SUFFIX: &'static str = ".nexe";
pub const EXE_EXTENSION: &'static str = "nexe";
}
#[cfg(all(target_os = "nacl", target_arch = "le32"))]
mod os {
pub const FAMILY: &'static str = "unix";
pub const OS: &'static str = "pnacl";
pub const DLL_PREFIX: &'static str = "lib";
pub const DLL_SUFFIX: &'static str = ".pso";
pub const DLL_EXTENSION: &'static str = "pso";
pub const EXE_SUFFIX: &'static str = ".pexe";
pub const EXE_EXTENSION: &'static str = "pexe";
}

#[cfg(all(target_os = "emscripten", target_arch = "asmjs"))]
mod os {
pub const FAMILY: &'static str = "unix";
pub const OS: &'static str = "emscripten";
pub const DLL_PREFIX: &'static str = "lib";
pub const DLL_SUFFIX: &'static str = ".so";
pub const DLL_EXTENSION: &'static str = "so";
pub const EXE_SUFFIX: &'static str = ".js";
pub const EXE_EXTENSION: &'static str = "js";
}

#[cfg(all(target_os = "emscripten", target_arch = "wasm32"))]
mod os {
pub const FAMILY: &'static str = "unix";
pub const OS: &'static str = "emscripten";
pub const DLL_PREFIX: &'static str = "lib";
pub const DLL_SUFFIX: &'static str = ".so";
pub const DLL_EXTENSION: &'static str = "so";
pub const EXE_SUFFIX: &'static str = ".js";
pub const EXE_EXTENSION: &'static str = "js";
}

#[cfg(target_os = "haiku")]
mod os {
pub const FAMILY: &'static str = "unix";
pub const OS: &'static str = "haiku";
pub const DLL_PREFIX: &'static str = "lib";
pub const DLL_SUFFIX: &'static str = ".so";
pub const DLL_EXTENSION: &'static str = "so";
pub const EXE_SUFFIX: &'static str = "";
pub const EXE_EXTENSION: &'static str = "";
pub const EXE_EXTENSION: &'static str = os::EXE_EXTENSION;
}

#[cfg(target_arch = "x86")]
Expand Down
11 changes: 0 additions & 11 deletions src/libstd/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,6 @@ impl OsString {
OsString { inner: Buf::from_string(String::new()) }
}

#[cfg(unix)]
fn _from_bytes(vec: Vec<u8>) -> Option<OsString> {
use os::unix::ffi::OsStringExt;
Some(OsString::from_vec(vec))
}

#[cfg(windows)]
fn _from_bytes(vec: Vec<u8>) -> Option<OsString> {
String::from_utf8(vec).ok().map(OsString::from)
}

/// Converts to an `OsStr` slice.
#[stable(feature = "rust1", since = "1.0.0")]
pub fn as_os_str(&self) -> &OsStr {
Expand Down
7 changes: 2 additions & 5 deletions src/libstd/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,10 @@ impl<R: io::Read> io::Read for Maybe<R> {
}

fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
#[cfg(windows)]
const ERR: i32 = ::sys::c::ERROR_INVALID_HANDLE as i32;
#[cfg(not(windows))]
const ERR: i32 = ::libc::EBADF as i32;
use sys::stdio::EBADF_ERR;

match r {
Err(ref e) if e.raw_os_error() == Some(ERR) => Ok(default),
Err(ref e) if e.raw_os_error() == Some(EBADF_ERR) => Ok(default),
r => r
}
}
Expand Down
Loading

0 comments on commit d97a08e

Please sign in to comment.