Skip to content

Commit 3d4f311

Browse files
authored
feat(ohos): fix some compilation errors and add some features (#2587)
* fix: fixes the build on OpenHarmony * fix: fixes the build on OpenHarmony
1 parent 299feba commit 3d4f311

16 files changed

+133
-37
lines changed

changelog/2587.fixed.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes the build on OpenHarmony

src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,11 @@ feature! {
145145
#![feature = "mount"]
146146
pub mod mount;
147147
}
148-
#[cfg(any(freebsdlike, target_os = "linux", target_os = "netbsd"))]
148+
#[cfg(any(
149+
freebsdlike,
150+
all(target_os = "linux", not(target_env = "ohos")),
151+
target_os = "netbsd"
152+
))]
149153
feature! {
150154
#![feature = "mqueue"]
151155
pub mod mqueue;

src/sys/ioctl/linux.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
use cfg_if::cfg_if;
22

33
/// The datatype used for the ioctl number
4-
#[cfg(any(target_os = "android", target_os = "fuchsia", target_env = "musl"))]
4+
#[cfg(any(
5+
target_os = "android",
6+
target_os = "fuchsia",
7+
target_env = "musl",
8+
target_env = "ohos"
9+
))]
510
#[doc(hidden)]
611
pub type ioctl_num_type = ::libc::c_int;
712
#[cfg(not(any(
813
target_os = "android",
914
target_os = "fuchsia",
10-
target_env = "musl"
15+
target_env = "musl",
16+
target_env = "ohos"
1117
)))]
1218
#[doc(hidden)]
1319
pub type ioctl_num_type = ::libc::c_ulong;

src/sys/memfd.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ libc_bitflags!(
3737
/// [`memfd_create(2)`]: https://man7.org/linux/man-pages/man2/memfd_create.2.html
3838
#[cfg(linux_android)]
3939
MFD_HUGETLB;
40+
/// Shift to get the huge page size.
41+
#[cfg(target_env = "ohos")]
42+
MFD_HUGE_SHIFT;
43+
/// Mask to get the huge page size.
44+
#[cfg(target_env = "ohos")]
45+
MFD_HUGE_MASK;
46+
/// hugetlb size of 64KB.
47+
#[cfg(target_env = "ohos")]
48+
MFD_HUGE_64KB;
49+
/// hugetlb size of 512KB.
50+
#[cfg(target_env = "ohos")]
51+
MFD_HUGE_512KB;
4052
/// Following are to be used with [`MFD_HUGETLB`], indicating the desired hugetlb size.
4153
///
4254
/// See also the hugetlb filesystem in [`memfd_create(2)`].
@@ -99,9 +111,10 @@ pub fn memfd_create<P: NixPath + ?Sized>(
99111
not(target_os = "android"),
100112
any(
101113
target_os = "freebsd",
102-
// If the OS is Linux, gnu and musl expose a memfd_create symbol but not uclibc
114+
// If the OS is Linux, gnu/musl/ohos expose a memfd_create symbol but not uclibc
103115
target_env = "gnu",
104116
target_env = "musl",
117+
target_env = "ohos"
105118
)))]
106119
{
107120
libc::memfd_create(cstr.as_ptr(), flags.bits())

src/sys/mman.rs

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ libc_bitflags! {
6464
#[cfg(any(all(linux_android,
6565
any(target_arch = "x86", target_arch = "x86_64")),
6666
all(target_os = "linux", target_env = "musl", any(target_arch = "x86", target_arch = "x86_64")),
67+
all(target_os = "linux", target_env = "ohos", target_arch = "x86_64"),
6768
all(target_os = "freebsd", target_pointer_width = "64")))]
6869
MAP_32BIT;
6970
/// Used for stacks; indicates to the kernel that the mapping should extend downward in memory.

src/sys/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! Mostly platform-specific functionality
22
#[cfg(any(
33
freebsdlike,
4-
all(target_os = "linux", not(target_env = "uclibc")),
4+
all(
5+
target_os = "linux",
6+
not(any(target_env = "uclibc", target_env = "ohos"))
7+
),
58
apple_targets,
69
target_os = "netbsd"
710
))]

src/sys/personality.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ libc_bitflags! {
2020
/// [`mmap(2)`]: https://man7.org/linux/man-pages/man2/mmap.2.html
2121
ADDR_LIMIT_3GB;
2222
/// User-space function pointers to signal handlers point to descriptors.
23-
#[cfg(not(any(target_env = "musl", target_env = "uclibc")))]
23+
#[cfg(not(any(target_env = "musl", target_env = "uclibc", target_env = "ohos")))]
2424
FDPIC_FUNCPTRS;
2525
/// Map page 0 as read-only.
2626
MMAP_PAGE_ZERO;
@@ -41,7 +41,7 @@ libc_bitflags! {
4141
/// version number.
4242
///
4343
/// [`uname(2)`]: https://man7.org/linux/man-pages/man2/uname.2.html
44-
#[cfg(not(any(target_env = "musl", target_env = "uclibc")))]
44+
#[cfg(not(any(target_env = "musl", target_env = "uclibc", target_env = "ohos")))]
4545
UNAME26;
4646
/// No effects.
4747
WHOLE_SECONDS;

src/sys/ptrace/linux.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ cfg_if! {
3636
}
3737

3838
libc_enum! {
39-
#[cfg_attr(not(any(target_env = "musl", target_env = "uclibc", target_os = "android")), repr(u32))]
40-
#[cfg_attr(any(target_env = "musl", target_env = "uclibc", target_os = "android"), repr(i32))]
39+
#[cfg_attr(not(any(target_env = "musl", target_env = "uclibc", target_os = "android", target_env = "ohos")), repr(u32))]
40+
#[cfg_attr(any(target_env = "musl", target_env = "uclibc", target_os = "android", target_env = "ohos"), repr(i32))]
4141
/// Ptrace Request enum defining the action to be taken.
4242
#[non_exhaustive]
4343
pub enum Request {
@@ -53,6 +53,7 @@ libc_enum! {
5353
PTRACE_SINGLESTEP,
5454
#[cfg(any(all(target_os = "android", target_pointer_width = "32"),
5555
all(target_os = "linux", any(target_env = "musl",
56+
target_env = "ohos",
5657
target_arch = "mips",
5758
target_arch = "mips32r6",
5859
target_arch = "mips64",
@@ -62,6 +63,7 @@ libc_enum! {
6263
PTRACE_GETREGS,
6364
#[cfg(any(all(target_os = "android", target_pointer_width = "32"),
6465
all(target_os = "linux", any(target_env = "musl",
66+
target_env = "ohos",
6567
target_arch = "mips",
6668
target_arch = "mips32r6",
6769
target_arch = "mips64",
@@ -71,6 +73,7 @@ libc_enum! {
7173
PTRACE_SETREGS,
7274
#[cfg(any(all(target_os = "android", target_pointer_width = "32"),
7375
all(target_os = "linux", any(target_env = "musl",
76+
target_env = "ohos",
7477
target_arch = "mips",
7578
target_arch = "mips32r6",
7679
target_arch = "mips64",
@@ -80,6 +83,7 @@ libc_enum! {
8083
PTRACE_GETFPREGS,
8184
#[cfg(any(all(target_os = "android", target_pointer_width = "32"),
8285
all(target_os = "linux", any(target_env = "musl",
86+
target_env = "ohos",
8387
target_arch = "mips",
8488
target_arch = "mips32r6",
8589
target_arch = "mips64",
@@ -90,6 +94,7 @@ libc_enum! {
9094
PTRACE_ATTACH,
9195
PTRACE_DETACH,
9296
#[cfg(all(target_os = "linux", any(target_env = "musl",
97+
target_env = "ohos",
9398
target_arch = "mips",
9499
target_arch = "mips32r6",
95100
target_arch = "mips64",
@@ -98,6 +103,7 @@ libc_enum! {
98103
target_arch = "x86_64")))]
99104
PTRACE_GETFPXREGS,
100105
#[cfg(all(target_os = "linux", any(target_env = "musl",
106+
target_env = "ohos",
101107
target_arch = "mips",
102108
target_arch = "mips32r6",
103109
target_arch = "mips64",

src/sys/statfs.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ pub struct Statfs(type_of_statfs);
5151
type fs_type_t = u32;
5252
#[cfg(target_os = "android")]
5353
type fs_type_t = libc::c_ulong;
54-
#[cfg(all(target_os = "linux", target_arch = "s390x", not(target_env = "musl")))]
54+
#[cfg(all(
55+
target_os = "linux",
56+
target_arch = "s390x",
57+
not(target_env = "musl")
58+
))]
5559
type fs_type_t = libc::c_uint;
56-
#[cfg(all(target_os = "linux", target_env = "musl"))]
57-
type fs_type_t = libc::c_ulong;
58-
#[cfg(all(target_os = "linux", target_env = "ohos"))]
60+
#[cfg(all(target_os = "linux", any(target_env = "musl", target_env = "ohos")))]
5961
type fs_type_t = libc::c_ulong;
6062
#[cfg(all(target_os = "linux", target_env = "uclibc"))]
6163
type fs_type_t = libc::c_int;
@@ -318,7 +320,11 @@ impl Statfs {
318320
}
319321

320322
/// Optimal transfer block size
321-
#[cfg(all(target_os = "linux", target_arch = "s390x", not(target_env = "musl")))]
323+
#[cfg(all(
324+
target_os = "linux",
325+
target_arch = "s390x",
326+
not(target_env = "musl")
327+
))]
322328
pub fn optimal_transfer_size(&self) -> u32 {
323329
self.0.f_bsize
324330
}
@@ -373,7 +379,11 @@ impl Statfs {
373379

374380
/// Size of a block
375381
// f_bsize on linux: https://github.com/torvalds/linux/blob/master/fs/nfs/super.c#L471
376-
#[cfg(all(target_os = "linux", target_arch = "s390x", not(target_env = "musl")))]
382+
#[cfg(all(
383+
target_os = "linux",
384+
target_arch = "s390x",
385+
not(target_env = "musl")
386+
))]
377387
pub fn block_size(&self) -> u32 {
378388
self.0.f_bsize
379389
}
@@ -454,7 +464,11 @@ impl Statfs {
454464
}
455465

456466
/// Maximum length of filenames
457-
#[cfg(all(target_os = "linux", target_arch = "s390x", not(target_env = "musl")))]
467+
#[cfg(all(
468+
target_os = "linux",
469+
target_arch = "s390x",
470+
not(target_env = "musl")
471+
))]
458472
pub fn maximum_name_length(&self) -> u32 {
459473
self.0.f_namelen
460474
}

src/sys/time.rs

+44-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
#[cfg_attr(target_env = "musl", allow(deprecated))]
1+
#[cfg_attr(
2+
any(target_env = "musl", target_env = "ohos"),
3+
allow(deprecated)
4+
)]
25
// https://github.com/rust-lang/libc/issues/1848
36
pub use libc::{suseconds_t, time_t};
47
use libc::{timespec, timeval};
@@ -253,7 +256,10 @@ impl PartialOrd for TimeSpec {
253256

254257
impl TimeValLike for TimeSpec {
255258
#[inline]
256-
#[cfg_attr(target_env = "musl", allow(deprecated))]
259+
#[cfg_attr(
260+
any(target_env = "musl", target_env = "ohos"),
261+
allow(deprecated)
262+
)]
257263
// https://github.com/rust-lang/libc/issues/1848
258264
fn seconds(seconds: i64) -> TimeSpec {
259265
assert!(
@@ -286,7 +292,10 @@ impl TimeValLike for TimeSpec {
286292

287293
/// Makes a new `TimeSpec` with given number of nanoseconds.
288294
#[inline]
289-
#[cfg_attr(target_env = "musl", allow(deprecated))]
295+
#[cfg_attr(
296+
any(target_env = "musl", target_env = "ohos"),
297+
allow(deprecated)
298+
)]
290299
// https://github.com/rust-lang/libc/issues/1848
291300
fn nanoseconds(nanoseconds: i64) -> TimeSpec {
292301
let (secs, nanos) = div_mod_floor_64(nanoseconds, NANOS_PER_SEC);
@@ -340,7 +349,10 @@ impl TimeSpec {
340349
TimeSpec::new(0, libc::UTIME_NOW as timespec_tv_nsec_t);
341350

342351
/// Construct a new `TimeSpec` from its components
343-
#[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
352+
#[cfg_attr(
353+
any(target_env = "musl", target_env = "ohos"),
354+
allow(deprecated)
355+
)] // https://github.com/rust-lang/libc/issues/1848
344356
pub const fn new(seconds: time_t, nanoseconds: timespec_tv_nsec_t) -> Self {
345357
let mut ts = zero_init_timespec();
346358
ts.tv_sec = seconds;
@@ -356,7 +368,10 @@ impl TimeSpec {
356368
}
357369
}
358370

359-
#[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
371+
#[cfg_attr(
372+
any(target_env = "musl", target_env = "ohos"),
373+
allow(deprecated)
374+
)] // https://github.com/rust-lang/libc/issues/1848
360375
pub const fn tv_sec(&self) -> time_t {
361376
self.0.tv_sec
362377
}
@@ -365,7 +380,10 @@ impl TimeSpec {
365380
self.0.tv_nsec
366381
}
367382

368-
#[cfg_attr(target_env = "musl", allow(deprecated))]
383+
#[cfg_attr(
384+
any(target_env = "musl", target_env = "ohos"),
385+
allow(deprecated)
386+
)]
369387
// https://github.com/rust-lang/libc/issues/1848
370388
pub const fn from_duration(duration: Duration) -> Self {
371389
let mut ts = zero_init_timespec();
@@ -506,7 +524,10 @@ impl TimeValLike for TimeVal {
506524
(TV_MIN_SECONDS..=TV_MAX_SECONDS).contains(&seconds),
507525
"TimeVal out of bounds; seconds={seconds}"
508526
);
509-
#[cfg_attr(target_env = "musl", allow(deprecated))]
527+
#[cfg_attr(
528+
any(target_env = "musl", target_env = "ohos"),
529+
allow(deprecated)
530+
)]
510531
// https://github.com/rust-lang/libc/issues/1848
511532
TimeVal(timeval {
512533
tv_sec: seconds as time_t,
@@ -531,7 +552,10 @@ impl TimeValLike for TimeVal {
531552
(TV_MIN_SECONDS..=TV_MAX_SECONDS).contains(&secs),
532553
"TimeVal out of bounds"
533554
);
534-
#[cfg_attr(target_env = "musl", allow(deprecated))]
555+
#[cfg_attr(
556+
any(target_env = "musl", target_env = "ohos"),
557+
allow(deprecated)
558+
)]
535559
// https://github.com/rust-lang/libc/issues/1848
536560
TimeVal(timeval {
537561
tv_sec: secs as time_t,
@@ -549,7 +573,10 @@ impl TimeValLike for TimeVal {
549573
(TV_MIN_SECONDS..=TV_MAX_SECONDS).contains(&secs),
550574
"TimeVal out of bounds"
551575
);
552-
#[cfg_attr(target_env = "musl", allow(deprecated))]
576+
#[cfg_attr(
577+
any(target_env = "musl", target_env = "ohos"),
578+
allow(deprecated)
579+
)]
553580
// https://github.com/rust-lang/libc/issues/1848
554581
TimeVal(timeval {
555582
tv_sec: secs as time_t,
@@ -586,7 +613,10 @@ impl TimeValLike for TimeVal {
586613

587614
impl TimeVal {
588615
/// Construct a new `TimeVal` from its components
589-
#[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
616+
#[cfg_attr(
617+
any(target_env = "musl", target_env = "ohos"),
618+
allow(deprecated)
619+
)] // https://github.com/rust-lang/libc/issues/1848
590620
pub const fn new(seconds: time_t, microseconds: suseconds_t) -> Self {
591621
Self(timeval {
592622
tv_sec: seconds,
@@ -602,7 +632,10 @@ impl TimeVal {
602632
}
603633
}
604634

605-
#[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
635+
#[cfg_attr(
636+
any(target_env = "musl", target_env = "ohos"),
637+
allow(deprecated)
638+
)] // https://github.com/rust-lang/libc/issues/1848
606639
pub const fn tv_sec(&self) -> time_t {
607640
self.0.tv_sec
608641
}

src/time.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ impl ClockId {
122122
#[cfg(any(
123123
target_os = "emscripten",
124124
target_os = "fuchsia",
125-
all(target_os = "linux", target_env = "musl")
125+
all(
126+
target_os = "linux",
127+
any(target_env = "musl", target_env = "ohos")
128+
)
126129
))]
127130
pub const CLOCK_SGI_CYCLE: ClockId = ClockId(libc::CLOCK_SGI_CYCLE);
128131
/// International Atomic Time.

src/ucontext.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
#[cfg(not(target_env = "musl"))]
1+
#[cfg(not(any(target_env = "musl", target_env = "ohos")))]
22
use crate::errno::Errno;
33
use crate::sys::signal::SigSet;
4-
#[cfg(not(target_env = "musl"))]
4+
#[cfg(not(any(target_env = "musl", target_env = "ohos")))]
55
use crate::Result;
6-
#[cfg(not(target_env = "musl"))]
6+
#[cfg(not(any(target_env = "musl", target_env = "ohos")))]
77
use std::mem;
88

99
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
@@ -12,7 +12,7 @@ pub struct UContext {
1212
}
1313

1414
impl UContext {
15-
#[cfg(not(target_env = "musl"))]
15+
#[cfg(not(any(target_env = "musl", target_env = "ohos")))]
1616
pub fn get() -> Result<UContext> {
1717
let mut context = mem::MaybeUninit::<libc::ucontext_t>::uninit();
1818
let res = unsafe { libc::getcontext(context.as_mut_ptr()) };
@@ -23,7 +23,7 @@ impl UContext {
2323
})
2424
}
2525

26-
#[cfg(not(target_env = "musl"))]
26+
#[cfg(not(any(target_env = "musl", target_env = "ohos")))]
2727
pub fn set(&self) -> Result<()> {
2828
let res = unsafe {
2929
libc::setcontext(&self.context as *const libc::ucontext_t)

0 commit comments

Comments
 (0)