Skip to content

Commit f3884e2

Browse files
committed
Add Motor OS std library port
Motor OS was added as a no-std Tier-3 target in #146848 as x86_64-unknown-motor. This patch/PR adds the std library for Motor OS. While the patch may seem large, all it does is proxy std pal calls to moto-rt. When there is some non-trivial code (e.g. thread::spawn), it is quite similar, often identical, to what other platforms do.
1 parent 091d639 commit f3884e2

File tree

46 files changed

+2341
-8
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2341
-8
lines changed

library/Cargo.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ dependencies = [
166166
"rustc-std-workspace-core",
167167
]
168168

169+
[[package]]
170+
name = "moto-rt"
171+
version = "0.14.1"
172+
source = "registry+https://github.com/rust-lang/crates.io-index"
173+
checksum = "89b06cfad394d58f28e4c69ac064b4d0562b5c187cb982d67b44ec53f2acc64e"
174+
dependencies = [
175+
"rustc-std-workspace-alloc",
176+
"rustc-std-workspace-core",
177+
]
178+
169179
[[package]]
170180
name = "object"
171181
version = "0.37.3"
@@ -316,6 +326,7 @@ dependencies = [
316326
"hermit-abi",
317327
"libc",
318328
"miniz_oxide",
329+
"moto-rt",
319330
"object",
320331
"panic_abort",
321332
"panic_unwind",

library/std/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ fortanix-sgx-abi = { version = "0.6.1", features = [
7070
'rustc-dep-of-std',
7171
], public = true }
7272

73+
[target.'cfg(target_os = "motor")'.dependencies]
74+
moto-rt = { version = "0.14", features = ['rustc-dep-of-std'], public = true }
75+
7376
[target.'cfg(target_os = "hermit")'.dependencies]
7477
hermit-abi = { version = "0.5.0", features = [
7578
'rustc-dep-of-std',

library/std/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ fn main() {
3030
|| target_os == "windows"
3131
|| target_os == "fuchsia"
3232
|| (target_vendor == "fortanix" && target_env == "sgx")
33+
|| target_os == "motor"
3334
|| target_os == "hermit"
3435
|| target_os == "trusty"
3536
|| target_os == "l4re"

library/std/src/os/fd/owned.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#![stable(feature = "io_safety", since = "1.63.0")]
44
#![deny(unsafe_op_in_unsafe_fn)]
55

6+
#[cfg(target_os = "motor")]
7+
use moto_rt::libc;
8+
69
use super::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
710
#[cfg(not(target_os = "trusty"))]
811
use crate::fs;
@@ -12,7 +15,8 @@ use crate::mem::ManuallyDrop;
1215
target_arch = "wasm32",
1316
target_env = "sgx",
1417
target_os = "hermit",
15-
target_os = "trusty"
18+
target_os = "trusty",
19+
target_os = "motor"
1620
)))]
1721
use crate::sys::cvt;
1822
#[cfg(not(target_os = "trusty"))]
@@ -95,7 +99,12 @@ impl OwnedFd {
9599
impl BorrowedFd<'_> {
96100
/// Creates a new `OwnedFd` instance that shares the same underlying file
97101
/// description as the existing `BorrowedFd` instance.
98-
#[cfg(not(any(target_arch = "wasm32", target_os = "hermit", target_os = "trusty")))]
102+
#[cfg(not(any(
103+
target_arch = "wasm32",
104+
target_os = "hermit",
105+
target_os = "trusty",
106+
target_os = "motor"
107+
)))]
99108
#[stable(feature = "io_safety", since = "1.63.0")]
100109
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> {
101110
// We want to atomically duplicate this file descriptor and set the
@@ -123,6 +132,16 @@ impl BorrowedFd<'_> {
123132
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> {
124133
Err(crate::io::Error::UNSUPPORTED_PLATFORM)
125134
}
135+
136+
/// Creates a new `OwnedFd` instance that shares the same underlying file
137+
/// description as the existing `BorrowedFd` instance.
138+
#[cfg(target_os = "motor")]
139+
#[stable(feature = "io_safety", since = "1.63.0")]
140+
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> {
141+
let fd =
142+
moto_rt::fs::duplicate(self.as_raw_fd()).map_err(crate::os::motor::map_motor_error)?;
143+
Ok(unsafe { OwnedFd::from_raw_fd(fd) })
144+
}
126145
}
127146

128147
#[stable(feature = "io_safety", since = "1.63.0")]

library/std/src/os/fd/raw.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
#[cfg(target_os = "hermit")]
66
use hermit_abi as libc;
7+
#[cfg(target_os = "motor")]
8+
use moto_rt::libc;
79

10+
#[cfg(target_os = "motor")]
11+
use super::owned::OwnedFd;
812
#[cfg(not(target_os = "trusty"))]
913
use crate::fs;
1014
use crate::io;
1115
#[cfg(target_os = "hermit")]
1216
use crate::os::hermit::io::OwnedFd;
13-
#[cfg(not(target_os = "hermit"))]
17+
#[cfg(all(not(target_os = "hermit"), not(target_os = "motor")))]
1418
use crate::os::raw;
1519
#[cfg(all(doc, not(target_arch = "wasm32")))]
1620
use crate::os::unix::io::AsFd;
@@ -23,10 +27,10 @@ use crate::sys_common::{AsInner, FromInner, IntoInner};
2327

2428
/// Raw file descriptors.
2529
#[stable(feature = "rust1", since = "1.0.0")]
26-
#[cfg(not(target_os = "hermit"))]
30+
#[cfg(all(not(target_os = "hermit"), not(target_os = "motor")))]
2731
pub type RawFd = raw::c_int;
2832
#[stable(feature = "rust1", since = "1.0.0")]
29-
#[cfg(target_os = "hermit")]
33+
#[cfg(any(target_os = "hermit", target_os = "motor"))]
3034
pub type RawFd = i32;
3135

3236
/// A trait to extract the raw file descriptor from an underlying object.

library/std/src/os/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ pub mod ios;
155155
pub mod l4re;
156156
#[cfg(target_os = "macos")]
157157
pub mod macos;
158+
#[cfg(target_os = "motor")]
159+
pub mod motor;
158160
#[cfg(target_os = "netbsd")]
159161
pub mod netbsd;
160162
#[cfg(target_os = "nto")]
@@ -182,7 +184,14 @@ pub mod vxworks;
182184
#[cfg(target_os = "xous")]
183185
pub mod xous;
184186

185-
#[cfg(any(unix, target_os = "hermit", target_os = "trusty", target_os = "wasi", doc))]
187+
#[cfg(any(
188+
unix,
189+
target_os = "hermit",
190+
target_os = "trusty",
191+
target_os = "wasi",
192+
target_os = "motor",
193+
doc
194+
))]
186195
pub mod fd;
187196

188197
#[cfg(any(target_os = "linux", target_os = "android", target_os = "cygwin", doc))]

library/std/src/os/motor/ffi.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//! Motor OS-specific extensions to primitives in the [`std::ffi`] module.
2+
3+
#![stable(feature = "rust1", since = "1.0.0")]
4+
5+
use crate::ffi::{OsStr, OsString};
6+
use crate::sealed::Sealed;
7+
8+
/// Motor OS-specific extensions to [`OsString`].
9+
///
10+
/// This trait is sealed: it cannot be implemented outside the standard library.
11+
/// This is so that future additional methods are not breaking changes.
12+
#[stable(feature = "rust1", since = "1.0.0")]
13+
pub trait OsStringExt: Sealed {
14+
/// Motor OS strings are utf-8, and thus just strings.
15+
#[stable(feature = "rust1", since = "1.0.0")]
16+
fn as_str(&self) -> &str;
17+
}
18+
19+
#[stable(feature = "rust1", since = "1.0.0")]
20+
impl OsStringExt for OsString {
21+
#[inline]
22+
fn as_str(&self) -> &str {
23+
self.to_str().unwrap()
24+
}
25+
}
26+
27+
/// Motor OS-specific extensions to [`OsString`].
28+
///
29+
/// This trait is sealed: it cannot be implemented outside the standard library.
30+
/// This is so that future additional methods are not breaking changes.
31+
#[stable(feature = "rust1", since = "1.0.0")]
32+
pub trait OsStrExt: Sealed {
33+
/// Motor OS strings are utf-8, and thus just strings.
34+
#[stable(feature = "rust1", since = "1.0.0")]
35+
fn as_str(&self) -> &str;
36+
}
37+
38+
#[stable(feature = "rust1", since = "1.0.0")]
39+
impl OsStrExt for OsStr {
40+
#[inline]
41+
fn as_str(&self) -> &str {
42+
self.to_str().unwrap()
43+
}
44+
}

library/std/src/os/motor/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![unstable(feature = "motor_ext", issue = "none")]
2+
3+
#[unstable(feature = "motor_ext", issue = "none")]
4+
pub fn map_motor_error(err: moto_rt::ErrorCode) -> crate::io::Error {
5+
use moto_rt::error::*;
6+
7+
use crate::io::ErrorKind;
8+
9+
let kind: ErrorKind = match err {
10+
E_ALREADY_IN_USE => ErrorKind::AlreadyExists,
11+
E_INVALID_FILENAME => ErrorKind::InvalidFilename,
12+
E_NOT_FOUND => ErrorKind::NotFound,
13+
E_TIMED_OUT => ErrorKind::TimedOut,
14+
E_NOT_IMPLEMENTED => ErrorKind::Unsupported,
15+
E_FILE_TOO_LARGE => ErrorKind::FileTooLarge,
16+
E_UNEXPECTED_EOF => ErrorKind::UnexpectedEof,
17+
E_INVALID_ARGUMENT => ErrorKind::InvalidInput,
18+
E_NOT_READY => ErrorKind::WouldBlock,
19+
E_NOT_CONNECTED => ErrorKind::NotConnected,
20+
_ => ErrorKind::Other,
21+
};
22+
23+
crate::io::Error::from(kind)
24+
}
25+
26+
pub mod ffi;
27+
pub mod process;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use crate::sealed::Sealed;
2+
use crate::sys_common::AsInner;
3+
4+
#[unstable(feature = "motor_ext", issue = "none")]
5+
pub trait ChildExt: Sealed {
6+
/// Extracts the main thread raw handle, without taking ownership
7+
#[unstable(feature = "motor_ext", issue = "none")]
8+
fn sys_handle(&self) -> u64;
9+
}
10+
11+
#[unstable(feature = "motor_ext", issue = "none")]
12+
impl ChildExt for crate::process::Child {
13+
fn sys_handle(&self) -> u64 {
14+
self.as_inner().handle()
15+
}
16+
}

library/std/src/sys/alloc/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ cfg_select! {
8383
target_os = "hermit" => {
8484
mod hermit;
8585
}
86+
target_os = "motor" => {
87+
mod motor;
88+
}
8689
all(target_vendor = "fortanix", target_env = "sgx") => {
8790
mod sgx;
8891
}

0 commit comments

Comments
 (0)