Skip to content

Add basic support for libstd without a runtime. #8454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libstd/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ macro_rules! compress (
)


#[cfg(not(no_rt))]
impl Writer for SipState {
// Methods for io::writer
#[inline]
Expand Down
26 changes: 16 additions & 10 deletions src/libstd/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
use c_str::ToCStr;
use clone::Clone;
use container::Container;
#[cfg(not(no_rt))]
use io;
use iterator::{IteratorUtil, range};
use libc;
use libc::{c_char, c_void, c_int, size_t};
use libc::FILE;
#[cfg(not(no_rt))]
use local_data;
use option::{Some, None};
use os;
Expand Down Expand Up @@ -338,16 +340,16 @@ pub fn fdopen(fd: c_int) -> *FILE {

// fsync related

#[cfg(windows)]
#[cfg(windows, not(no_rt))]
pub fn fsync_fd(fd: c_int, _level: io::fsync::Level) -> c_int {
unsafe {
use libc::funcs::extra::msvcrt::*;
return commit(fd);
}
}

#[cfg(target_os = "linux")]
#[cfg(target_os = "android")]
#[cfg(target_os = "linux", not(no_rt))]
#[cfg(target_os = "android", not(no_rt))]
pub fn fsync_fd(fd: c_int, level: io::fsync::Level) -> c_int {
unsafe {
use libc::funcs::posix01::unistd::*;
Expand All @@ -359,7 +361,7 @@ pub fn fsync_fd(fd: c_int, level: io::fsync::Level) -> c_int {
}
}

#[cfg(target_os = "macos")]
#[cfg(target_os = "macos", not(no_rt))]
pub fn fsync_fd(fd: c_int, level: io::fsync::Level) -> c_int {
unsafe {
use libc::consts::os::extra::*;
Expand All @@ -379,7 +381,7 @@ pub fn fsync_fd(fd: c_int, level: io::fsync::Level) -> c_int {
}
}

#[cfg(target_os = "freebsd")]
#[cfg(target_os = "freebsd", not(no_rt))]
pub fn fsync_fd(fd: c_int, _l: io::fsync::Level) -> c_int {
unsafe {
use libc::funcs::posix01::unistd::*;
Expand Down Expand Up @@ -1109,6 +1111,7 @@ pub fn last_os_error() -> ~str {
* and is supervised by the scheduler then any user-specified exit status is
* ignored and the process exits with the default failure status
*/
#[cfg(not(no_rt))]
pub fn set_exit_status(code: int) {
use rt;
rt::util::set_exit_status(code);
Expand All @@ -1127,7 +1130,7 @@ unsafe fn load_argc_and_argv(argc: c_int, argv: **c_char) -> ~[~str] {
*
* Returns a list of the command line arguments.
*/
#[cfg(target_os = "macos")]
#[cfg(target_os = "macos", not(no_rt))]
pub fn real_args() -> ~[~str] {
unsafe {
let (argc, argv) = (*_NSGetArgc() as c_int,
Expand All @@ -1136,9 +1139,9 @@ pub fn real_args() -> ~[~str] {
}
}

#[cfg(target_os = "linux")]
#[cfg(target_os = "android")]
#[cfg(target_os = "freebsd")]
#[cfg(target_os = "linux", not(no_rt))]
#[cfg(target_os = "android", not(no_rt))]
#[cfg(target_os = "freebsd", not(no_rt))]
pub fn real_args() -> ~[~str] {
use rt;

Expand All @@ -1148,7 +1151,7 @@ pub fn real_args() -> ~[~str] {
}
}

#[cfg(windows)]
#[cfg(windows, not(no_rt))]
pub fn real_args() -> ~[~str] {
let mut nArgs: c_int = 0;
let lpArgCount: *mut c_int = &mut nArgs;
Expand Down Expand Up @@ -1197,13 +1200,15 @@ struct OverriddenArgs {
val: ~[~str]
}

#[cfg(not(no_rt))]
static overridden_arg_key: local_data::Key<@OverriddenArgs> = &local_data::Key;

/// Returns the arguments which this program was started with (normally passed
/// via the command line).
///
/// The return value of the function can be changed by invoking the
/// `os::set_args` function.
#[cfg(not(no_rt))]
pub fn args() -> ~[~str] {
match local_data::get(overridden_arg_key, |k| k.map(|&k| *k)) {
None => real_args(),
Expand All @@ -1214,6 +1219,7 @@ pub fn args() -> ~[~str] {
/// For the current task, overrides the task-local cache of the arguments this
/// program had when it started. These new arguments are only available to the
/// current task via the `os::args` method.
#[cfg(not(no_rt))]
pub fn set_args(new_args: ~[~str]) {
let overridden_args = @OverriddenArgs {
val: new_args.clone()
Expand Down
5 changes: 5 additions & 0 deletions src/libstd/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub use option::{Option, Some, None};
pub use result::{Result, Ok, Err};

// Reexported functions
#[cfg(not(no_rt))]
pub use io::{print, println};
pub use iterator::range;

Expand All @@ -48,6 +49,7 @@ pub use clone::{Clone, DeepClone};
pub use cmp::{Eq, ApproxEq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater, Equiv};
pub use char::Char;
pub use container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
#[cfg(not(no_rt))]
pub use hash::Hash;
pub use iter::Times;
pub use iterator::Extendable;
Expand Down Expand Up @@ -79,8 +81,11 @@ pub use tuple::{ImmutableTuple10, ImmutableTuple11, ImmutableTuple12};
pub use vec::{Vector, VectorVector, CopyableVector, ImmutableVector};
pub use vec::{ImmutableEqVector, ImmutableTotalOrdVector, ImmutableCopyableVector};
pub use vec::{OwnedVector, OwnedCopyableVector,OwnedEqVector, MutableVector};
#[cfg(not(no_rt))]
pub use io::{Reader, ReaderUtil, Writer, WriterUtil};

// Reexported runtime types
#[cfg(not(no_rt))]
pub use comm::{stream, Port, Chan, GenericChan, GenericSmartChan, GenericPort, Peekable};
#[cfg(not(no_rt))]
pub use task::spawn;
12 changes: 12 additions & 0 deletions src/libstd/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use cmp;
use container::Container;
use int;
use iterator::{Iterator, range};
#[cfg(not(no_rt))]
use local_data;
use num;
use prelude::*;
Expand Down Expand Up @@ -875,6 +876,7 @@ pub fn seed() -> ~[u8] {
}

// used to make space in TLS for a random number generator
#[cfg(not(no_rt))]
static tls_rng_state: local_data::Key<@@mut IsaacRng> = &local_data::Key;

/**
Expand All @@ -883,6 +885,7 @@ static tls_rng_state: local_data::Key<@@mut IsaacRng> = &local_data::Key;
* `task_rng().gen::<int>()`.
*/
#[inline]
#[cfg(not(no_rt))]
pub fn task_rng() -> @mut IsaacRng {
let r = local_data::get(tls_rng_state, |k| k.map(|&k| *k));
match r {
Expand All @@ -908,10 +911,19 @@ impl<R: Rng> Rng for @mut R {
* generator.
*/
#[inline]
#[cfg(not(no_rt))]
pub fn random<T: Rand>() -> T {
task_rng().gen()
}

#[cfg(no_rt)]
/// Returns a random value of the `Rand` type, using a XorShiftRng seeded with
/// a constant.
pub fn random<T: Rand>() -> T {
// XXX: always seeded with a constant! bad!
XorShiftRng::new().gen()
}

#[cfg(test)]
mod test {
use option::{Option, Some};
Expand Down
Loading