Skip to content

Commit ca3681b

Browse files
committed
Initial std library support for NuttX
Signed-off-by: Huang Qi <[email protected]>
1 parent 5793a9e commit ca3681b

File tree

20 files changed

+222
-23
lines changed

20 files changed

+222
-23
lines changed

library/panic_unwind/src/lib.rs

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

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

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

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

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

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

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

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

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

+36-6
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))]

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ impl FileAttr {
479479
target_os = "vita",
480480
target_os = "hurd",
481481
target_os = "rtems",
482+
target_os = "nuttx",
482483
)))]
483484
pub fn modified(&self) -> io::Result<SystemTime> {
484485
#[cfg(target_pointer_width = "32")]
@@ -501,7 +502,7 @@ impl FileAttr {
501502
SystemTime::new(self.stat.st_mtime as i64, 0)
502503
}
503504

504-
#[cfg(any(target_os = "horizon", target_os = "hurd"))]
505+
#[cfg(any(target_os = "horizon", target_os = "hurd", target_os = "nuttx"))]
505506
pub fn modified(&self) -> io::Result<SystemTime> {
506507
SystemTime::new(self.stat.st_mtim.tv_sec as i64, self.stat.st_mtim.tv_nsec as i64)
507508
}
@@ -513,6 +514,7 @@ impl FileAttr {
513514
target_os = "vita",
514515
target_os = "hurd",
515516
target_os = "rtems",
517+
target_os = "nuttx",
516518
)))]
517519
pub fn accessed(&self) -> io::Result<SystemTime> {
518520
#[cfg(target_pointer_width = "32")]
@@ -535,7 +537,7 @@ impl FileAttr {
535537
SystemTime::new(self.stat.st_atime as i64, 0)
536538
}
537539

538-
#[cfg(any(target_os = "horizon", target_os = "hurd"))]
540+
#[cfg(any(target_os = "horizon", target_os = "hurd", target_os = "nuttx"))]
539541
pub fn accessed(&self) -> io::Result<SystemTime> {
540542
SystemTime::new(self.stat.st_atim.tv_sec as i64, self.stat.st_atim.tv_nsec as i64)
541543
}
@@ -866,6 +868,7 @@ impl Drop for Dir {
866868
target_os = "horizon",
867869
target_os = "vxworks",
868870
target_os = "rtems",
871+
target_os = "nuttx",
869872
)))]
870873
{
871874
let fd = unsafe { libc::dirfd(self.0) };
@@ -984,6 +987,7 @@ impl DirEntry {
984987
target_os = "nto",
985988
target_os = "hurd",
986989
target_os = "rtems",
990+
target_os = "nuttx",
987991
target_vendor = "apple",
988992
))]
989993
pub fn ino(&self) -> u64 {
@@ -1327,7 +1331,8 @@ impl File {
13271331
target_os = "redox",
13281332
target_os = "espidf",
13291333
target_os = "horizon",
1330-
target_os = "vxworks"
1334+
target_os = "vxworks",
1335+
target_os = "nuttx",
13311336
)))]
13321337
let to_timespec = |time: Option<SystemTime>| match time {
13331338
Some(time) if let Some(ts) = time.t.to_timespec() => Ok(ts),
@@ -1342,7 +1347,7 @@ impl File {
13421347
None => Ok(libc::timespec { tv_sec: 0, tv_nsec: libc::UTIME_OMIT as _ }),
13431348
};
13441349
cfg_if::cfg_if! {
1345-
if #[cfg(any(target_os = "redox", target_os = "espidf", target_os = "horizon", target_os = "vxworks"))] {
1350+
if #[cfg(any(target_os = "redox", target_os = "espidf", target_os = "horizon", target_os = "vxworks", target_os = "nuttx"))] {
13461351
// Redox doesn't appear to support `UTIME_OMIT`.
13471352
// ESP-IDF and HorizonOS do not support `futimens` at all and the behavior for those OS is therefore
13481353
// the same as for Redox.

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ static ON_BROKEN_PIPE_FLAG_USED: crate::sync::atomic::AtomicBool =
224224
target_os = "horizon",
225225
target_os = "vxworks",
226226
target_os = "vita",
227+
target_os = "nuttx",
227228
)))]
228229
pub(crate) fn on_broken_pipe_flag_used() -> bool {
229230
ON_BROKEN_PIPE_FLAG_USED.load(crate::sync::atomic::Ordering::Relaxed)
@@ -426,7 +427,7 @@ cfg_if::cfg_if! {
426427
}
427428
}
428429

429-
#[cfg(any(target_os = "espidf", target_os = "horizon", target_os = "vita"))]
430+
#[cfg(any(target_os = "espidf", target_os = "horizon", target_os = "vita", target_os = "nuttx"))]
430431
mod unsupported {
431432
use crate::io;
432433

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ pub fn cvt_gai(err: c_int) -> io::Result<()> {
3838
// We may need to trigger a glibc workaround. See on_resolver_failure() for details.
3939
on_resolver_failure();
4040

41-
#[cfg(not(target_os = "espidf"))]
41+
#[cfg(not(any(target_os = "espidf", target_os = "nuttx")))]
4242
if err == libc::EAI_SYSTEM {
4343
return Err(io::Error::last_os_error());
4444
}
4545

46-
#[cfg(not(target_os = "espidf"))]
46+
#[cfg(not(any(target_os = "espidf", target_os = "nuttx")))]
4747
let detail = unsafe {
4848
// We can't always expect a UTF-8 environment. When we don't get that luxury,
4949
// it's better to give a low-quality error message than none at all.
5050
CStr::from_ptr(libc::gai_strerror(err)).to_string_lossy()
5151
};
5252

53-
#[cfg(target_os = "espidf")]
53+
#[cfg(any(target_os = "espidf", target_os = "nuttx"))]
5454
let detail = "";
5555

5656
Err(io::Error::new(

0 commit comments

Comments
 (0)