Skip to content

Commit a828ffc

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, and often identical, to what other platforms do.
1 parent 8fad3a1 commit a828ffc

File tree

46 files changed

+2296
-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

+2296
-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.15.0"
172+
source = "registry+https://github.com/rust-lang/crates.io-index"
173+
checksum = "058a2807a30527bee4c30df7ababe971cdde94372d4dbd1ff145bb403381436c"
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.15", 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: 20 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,15 @@ 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 = moto_rt::fs::duplicate(self.as_raw_fd()).map_err(crate::sys::map_motor_error)?;
142+
Ok(unsafe { OwnedFd::from_raw_fd(fd) })
143+
}
126144
}
127145

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

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![unstable(feature = "motor_ext", issue = "147456")]
2+
3+
pub mod ffi;
4+
pub mod process;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![unstable(feature = "motor_ext", issue = "147456")]
2+
3+
use crate::sealed::Sealed;
4+
use crate::sys_common::AsInner;
5+
6+
pub trait ChildExt: Sealed {
7+
/// Extracts the main thread raw handle, without taking ownership
8+
fn sys_handle(&self) -> u64;
9+
}
10+
11+
impl ChildExt for crate::process::Child {
12+
fn sys_handle(&self) -> u64 {
13+
self.as_inner().handle()
14+
}
15+
}

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)