Skip to content

Commit e2e23a1

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 63d94fc commit e2e23a1

File tree

46 files changed

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

+2310
-7
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.0"
172+
source = "registry+https://github.com/rust-lang/crates.io-index"
173+
checksum = "14df6749ee6ccf4acd47bfc930932efbca78e49cc5d432b9b355628789a2f81e"
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/panic_abort/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ libc = { version = "0.2", default-features = false }
1919

2020
[target.'cfg(any(target_os = "android", target_os = "zkvm"))'.dependencies]
2121
alloc = { path = "../alloc" }
22+
23+
[lints.rust.unexpected_cfgs]
24+
level = "warn"
25+
check-cfg = [
26+
'cfg(target_os, values("motor"))',
27+
]

library/std/Cargo.toml

Lines changed: 4 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',
@@ -153,6 +156,7 @@ test = true
153156
[lints.rust.unexpected_cfgs]
154157
level = "warn"
155158
check-cfg = [
159+
'cfg(target_os, values("motor"))',
156160
# std use #[path] imports to portable-simd `std_float` crate
157161
# and to the `backtrace` crate which messes-up with Cargo list
158162
# of declared features, we therefor expect any feature cfg

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: 9 additions & 2 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,12 +27,15 @@ 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")]
2933
#[cfg(target_os = "hermit")]
3034
pub type RawFd = i32;
3135

36+
#[stable(feature = "rust1", since = "1.0.0")]
37+
#[cfg(target_os = "motor")]
38+
pub type RawFd = i32;
3239
/// A trait to extract the raw file descriptor from an underlying object.
3340
///
3441
/// This is only available on unix and WASI platforms and must be imported in

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/mod.rs

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

library/std/src/panicking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ pub fn panicking() -> bool {
626626
}
627627

628628
/// Entry point of panics from the core crate (`panic_impl` lang item).
629-
#[cfg(not(any(test, doctest)))]
629+
#[cfg(all(not(any(test, doctest)), not(target_os = "motor")))]
630630
#[panic_handler]
631631
pub fn panic_handler(info: &core::panic::PanicInfo<'_>) -> ! {
632632
struct FormatStringPayload<'a> {

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)