Skip to content

Commit 24f622c

Browse files
committed
Initial std library support for NuttX
Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
1 parent f5cd2c5 commit 24f622c

File tree

20 files changed

+225
-22
lines changed

20 files changed

+225
-22
lines changed

library/panic_unwind/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ cfg_if::cfg_if! {
4848
target_os = "psp",
4949
target_os = "xous",
5050
target_os = "solid_asp3",
51-
all(target_family = "unix", not(any(target_os = "espidf", target_os = "rtems"))),
51+
all(target_family = "unix", not(any(target_os = "espidf", target_os = "rtems", target_os = "nuttx"))),
5252
all(target_vendor = "fortanix", target_env = "sgx"),
5353
target_family = "wasm",
5454
))] {

library/std/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ fn main() {
5454
|| target_os == "teeos"
5555
|| target_os == "zkvm"
5656
|| target_os == "rtems"
57+
|| target_os == "nuttx"
5758

5859
// See src/bootstrap/src/core/build_steps/synthetic_targets.rs
5960
|| env::var("RUSTC_BOOTSTRAP_SYNTHETIC_TARGET").is_ok()

library/std/src/os/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ pub mod macos;
139139
pub mod netbsd;
140140
#[cfg(target_os = "nto")]
141141
pub mod nto;
142+
#[cfg(target_os = "nuttx")]
143+
pub mod nuttx;
142144
#[cfg(target_os = "openbsd")]
143145
pub mod openbsd;
144146
#[cfg(target_os = "redox")]

library/std/src/os/nuttx/fs.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#![stable(feature = "metadata_ext", since = "1.1.0")]
2+
3+
use crate::fs::Metadata;
4+
use crate::sys_common::AsInner;
5+
6+
#[stable(feature = "metadata_ext", since = "1.1.0")]
7+
pub trait MetadataExt {
8+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
9+
fn st_dev(&self) -> u64;
10+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
11+
fn st_ino(&self) -> u64;
12+
#[stable(feature = "metadata_ext", since = "1.1.0")]
13+
fn st_mode(&self) -> u32;
14+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
15+
fn st_nlink(&self) -> u64;
16+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
17+
fn st_uid(&self) -> u32;
18+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
19+
fn st_gid(&self) -> u32;
20+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
21+
fn st_rdev(&self) -> u64;
22+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
23+
fn st_size(&self) -> u64;
24+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
25+
fn st_atime(&self) -> i64;
26+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
27+
fn st_atime_nsec(&self) -> i64;
28+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
29+
fn st_mtime(&self) -> i64;
30+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
31+
fn st_mtime_nsec(&self) -> i64;
32+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
33+
fn st_ctime(&self) -> i64;
34+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
35+
fn st_ctime_nsec(&self) -> i64;
36+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
37+
fn st_blksize(&self) -> u64;
38+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
39+
fn st_blocks(&self) -> u64;
40+
}
41+
42+
#[stable(feature = "metadata_ext", since = "1.1.0")]
43+
impl MetadataExt for Metadata {
44+
fn st_dev(&self) -> u64 {
45+
self.as_inner().as_inner().st_dev as u64
46+
}
47+
fn st_ino(&self) -> u64 {
48+
self.as_inner().as_inner().st_ino as u64
49+
}
50+
fn st_mode(&self) -> u32 {
51+
self.as_inner().as_inner().st_mode as u32
52+
}
53+
fn st_nlink(&self) -> u64 {
54+
self.as_inner().as_inner().st_nlink as u64
55+
}
56+
fn st_uid(&self) -> u32 {
57+
self.as_inner().as_inner().st_uid as u32
58+
}
59+
fn st_gid(&self) -> u32 {
60+
self.as_inner().as_inner().st_gid as u32
61+
}
62+
fn st_rdev(&self) -> u64 {
63+
self.as_inner().as_inner().st_rdev as u64
64+
}
65+
fn st_size(&self) -> u64 {
66+
self.as_inner().as_inner().st_size as u64
67+
}
68+
fn st_atime(&self) -> i64 {
69+
self.as_inner().as_inner().st_atim.tv_sec as i64
70+
}
71+
fn st_atime_nsec(&self) -> i64 {
72+
self.as_inner().as_inner().st_atim.tv_nsec as i64
73+
}
74+
fn st_mtime(&self) -> i64 {
75+
self.as_inner().as_inner().st_mtim.tv_sec as i64
76+
}
77+
fn st_mtime_nsec(&self) -> i64 {
78+
self.as_inner().as_inner().st_mtim.tv_nsec as i64
79+
}
80+
fn st_ctime(&self) -> i64 {
81+
self.as_inner().as_inner().st_ctim.tv_sec as i64
82+
}
83+
fn st_ctime_nsec(&self) -> i64 {
84+
self.as_inner().as_inner().st_ctim.tv_nsec as i64
85+
}
86+
fn st_blksize(&self) -> u64 {
87+
self.as_inner().as_inner().st_blksize as u64
88+
}
89+
fn st_blocks(&self) -> u64 {
90+
self.as_inner().as_inner().st_blocks as u64
91+
}
92+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![stable(feature = "raw_ext", since = "1.1.0")]
2+
#![forbid(unsafe_op_in_unsafe_fn)]
3+
pub mod fs;
4+
pub(crate) mod raw;

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//! rtems raw type definitions
2+
3+
#![stable(feature = "raw_ext", since = "1.1.0")]
4+
#![deprecated(
5+
since = "1.8.0",
6+
note = "these type aliases are no longer supported by \
7+
the standard library, the `libc` crate on \
8+
crates.io should be used instead for the correct \
9+
definitions"
10+
)]
11+
#![allow(deprecated)]
12+
13+
#[stable(feature = "pthread_t", since = "1.8.0")]
14+
pub type pthread_t = libc::pthread_t;
15+
16+
#[stable(feature = "raw_ext", since = "1.1.0")]
17+
pub type blkcnt_t = libc::blkcnt_t;
18+
19+
#[stable(feature = "raw_ext", since = "1.1.0")]
20+
pub type blksize_t = libc::blksize_t;
21+
#[stable(feature = "raw_ext", since = "1.1.0")]
22+
pub type dev_t = libc::dev_t;
23+
#[stable(feature = "raw_ext", since = "1.1.0")]
24+
pub type ino_t = libc::ino_t;
25+
#[stable(feature = "raw_ext", since = "1.1.0")]
26+
pub type mode_t = libc::mode_t;
27+
#[stable(feature = "raw_ext", since = "1.1.0")]
28+
pub type nlink_t = libc::nlink_t;
29+
#[stable(feature = "raw_ext", since = "1.1.0")]
30+
pub type off_t = libc::off_t;
31+
32+
#[stable(feature = "raw_ext", since = "1.1.0")]
33+
pub type time_t = libc::time_t;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ mod platform {
6969
pub use crate::os::netbsd::*;
7070
#[cfg(target_os = "nto")]
7171
pub use crate::os::nto::*;
72+
#[cfg(target_os = "nuttx")]
73+
pub use crate::os::nuttx::*;
7274
#[cfg(target_os = "openbsd")]
7375
pub use crate::os::openbsd::*;
7476
#[cfg(target_os = "redox")]

library/std/src/sys/pal/unix/args.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ impl DoubleEndedIterator for Args {
113113
target_os = "nto",
114114
target_os = "hurd",
115115
target_os = "rtems",
116+
target_os = "nuttx",
116117
))]
117118
mod imp {
118119
use crate::ffi::c_char;

library/std/src/sys/pal/unix/env.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,14 @@ pub mod os {
283283
pub const EXE_SUFFIX: &str = "";
284284
pub const EXE_EXTENSION: &str = "";
285285
}
286+
287+
#[cfg(target_os = "nuttx")]
288+
pub mod os {
289+
pub const FAMILY: &str = "unix";
290+
pub const OS: &str = "nuttx";
291+
pub const DLL_PREFIX: &str = "lib";
292+
pub const DLL_SUFFIX: &str = ".so";
293+
pub const DLL_EXTENSION: &str = "so";
294+
pub const EXE_SUFFIX: &str = "";
295+
pub const EXE_EXTENSION: &str = "";
296+
}

library/std/src/sys/pal/unix/fd.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@ impl FileDesc {
9898
Ok(ret as usize)
9999
}
100100

101-
#[cfg(not(any(target_os = "espidf", target_os = "horizon", target_os = "vita")))]
101+
#[cfg(not(any(
102+
target_os = "espidf",
103+
target_os = "horizon",
104+
target_os = "vita",
105+
target_os = "nuttx"
106+
)))]
102107
pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
103108
let ret = cvt(unsafe {
104109
libc::readv(
@@ -110,14 +115,24 @@ impl FileDesc {
110115
Ok(ret as usize)
111116
}
112117

113-
#[cfg(any(target_os = "espidf", target_os = "horizon", target_os = "vita"))]
118+
#[cfg(any(
119+
target_os = "espidf",
120+
target_os = "horizon",
121+
target_os = "vita",
122+
target_os = "nuttx"
123+
))]
114124
pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
115125
io::default_read_vectored(|b| self.read(b), bufs)
116126
}
117127

118128
#[inline]
119129
pub fn is_read_vectored(&self) -> bool {
120-
cfg!(not(any(target_os = "espidf", target_os = "horizon", target_os = "vita")))
130+
cfg!(not(any(
131+
target_os = "espidf",
132+
target_os = "horizon",
133+
target_os = "vita",
134+
target_os = "nuttx"
135+
)))
121136
}
122137

123138
pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
@@ -297,7 +312,12 @@ impl FileDesc {
297312
Ok(ret as usize)
298313
}
299314

300-
#[cfg(not(any(target_os = "espidf", target_os = "horizon", target_os = "vita")))]
315+
#[cfg(not(any(
316+
target_os = "espidf",
317+
target_os = "horizon",
318+
target_os = "vita",
319+
target_os = "nuttx"
320+
)))]
301321
pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
302322
let ret = cvt(unsafe {
303323
libc::writev(
@@ -309,14 +329,24 @@ impl FileDesc {
309329
Ok(ret as usize)
310330
}
311331

312-
#[cfg(any(target_os = "espidf", target_os = "horizon", target_os = "vita"))]
332+
#[cfg(any(
333+
target_os = "espidf",
334+
target_os = "horizon",
335+
target_os = "vita",
336+
target_os = "nuttx"
337+
))]
313338
pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
314339
io::default_write_vectored(|b| self.write(b), bufs)
315340
}
316341

317342
#[inline]
318343
pub fn is_write_vectored(&self) -> bool {
319-
cfg!(not(any(target_os = "espidf", target_os = "horizon", target_os = "vita")))
344+
cfg!(not(any(
345+
target_os = "espidf",
346+
target_os = "horizon",
347+
target_os = "vita",
348+
target_os = "nuttx"
349+
)))
320350
}
321351

322352
#[cfg_attr(target_os = "vxworks", allow(unused_unsafe))]

0 commit comments

Comments
 (0)