Skip to content

feat(ohos): fix some compilation errors and add some features #2587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2587.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes the build on OpenHarmony
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ feature! {
#![feature = "mount"]
pub mod mount;
}
#[cfg(any(freebsdlike, target_os = "linux", target_os = "netbsd"))]
#[cfg(any(
freebsdlike,
all(target_os = "linux", not(target_env = "ohos")),
target_os = "netbsd"
))]
feature! {
#![feature = "mqueue"]
pub mod mqueue;
Expand Down
10 changes: 8 additions & 2 deletions src/sys/ioctl/linux.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
use cfg_if::cfg_if;

/// The datatype used for the ioctl number
#[cfg(any(target_os = "android", target_os = "fuchsia", target_env = "musl"))]
#[cfg(any(
target_os = "android",
target_os = "fuchsia",
target_env = "musl",
target_env = "ohos"
))]
#[doc(hidden)]
pub type ioctl_num_type = ::libc::c_int;
#[cfg(not(any(
target_os = "android",
target_os = "fuchsia",
target_env = "musl"
target_env = "musl",
target_env = "ohos"
)))]
#[doc(hidden)]
pub type ioctl_num_type = ::libc::c_ulong;
Expand Down
15 changes: 14 additions & 1 deletion src/sys/memfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ libc_bitflags!(
/// [`memfd_create(2)`]: https://man7.org/linux/man-pages/man2/memfd_create.2.html
#[cfg(linux_android)]
MFD_HUGETLB;
/// Shift to get the huge page size.
#[cfg(target_env = "ohos")]
MFD_HUGE_SHIFT;
/// Mask to get the huge page size.
#[cfg(target_env = "ohos")]
MFD_HUGE_MASK;
/// hugetlb size of 64KB.
#[cfg(target_env = "ohos")]
MFD_HUGE_64KB;
/// hugetlb size of 512KB.
#[cfg(target_env = "ohos")]
MFD_HUGE_512KB;
/// Following are to be used with [`MFD_HUGETLB`], indicating the desired hugetlb size.
///
/// See also the hugetlb filesystem in [`memfd_create(2)`].
Expand Down Expand Up @@ -99,9 +111,10 @@ pub fn memfd_create<P: NixPath + ?Sized>(
not(target_os = "android"),
any(
target_os = "freebsd",
// If the OS is Linux, gnu and musl expose a memfd_create symbol but not uclibc
// If the OS is Linux, gnu/musl/ohos expose a memfd_create symbol but not uclibc
target_env = "gnu",
target_env = "musl",
target_env = "ohos"
)))]
{
libc::memfd_create(cstr.as_ptr(), flags.bits())
Expand Down
1 change: 1 addition & 0 deletions src/sys/mman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ libc_bitflags! {
#[cfg(any(all(linux_android,
any(target_arch = "x86", target_arch = "x86_64")),
all(target_os = "linux", target_env = "musl", any(target_arch = "x86", target_arch = "x86_64")),
all(target_os = "linux", target_env = "ohos", target_arch = "x86_64"),
all(target_os = "freebsd", target_pointer_width = "64")))]
MAP_32BIT;
/// Used for stacks; indicates to the kernel that the mapping should extend downward in memory.
Expand Down
5 changes: 4 additions & 1 deletion src/sys/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! Mostly platform-specific functionality
#[cfg(any(
freebsdlike,
all(target_os = "linux", not(target_env = "uclibc")),
all(
target_os = "linux",
not(any(target_env = "uclibc", target_env = "ohos"))
),
apple_targets,
target_os = "netbsd"
))]
Expand Down
4 changes: 2 additions & 2 deletions src/sys/personality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ libc_bitflags! {
/// [`mmap(2)`]: https://man7.org/linux/man-pages/man2/mmap.2.html
ADDR_LIMIT_3GB;
/// User-space function pointers to signal handlers point to descriptors.
#[cfg(not(any(target_env = "musl", target_env = "uclibc")))]
#[cfg(not(any(target_env = "musl", target_env = "uclibc", target_env = "ohos")))]
FDPIC_FUNCPTRS;
/// Map page 0 as read-only.
MMAP_PAGE_ZERO;
Expand All @@ -41,7 +41,7 @@ libc_bitflags! {
/// version number.
///
/// [`uname(2)`]: https://man7.org/linux/man-pages/man2/uname.2.html
#[cfg(not(any(target_env = "musl", target_env = "uclibc")))]
#[cfg(not(any(target_env = "musl", target_env = "uclibc", target_env = "ohos")))]
UNAME26;
/// No effects.
WHOLE_SECONDS;
Expand Down
10 changes: 8 additions & 2 deletions src/sys/ptrace/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ cfg_if! {
}

libc_enum! {
#[cfg_attr(not(any(target_env = "musl", target_env = "uclibc", target_os = "android")), repr(u32))]
#[cfg_attr(any(target_env = "musl", target_env = "uclibc", target_os = "android"), repr(i32))]
#[cfg_attr(not(any(target_env = "musl", target_env = "uclibc", target_os = "android", target_env = "ohos")), repr(u32))]
#[cfg_attr(any(target_env = "musl", target_env = "uclibc", target_os = "android", target_env = "ohos"), repr(i32))]
/// Ptrace Request enum defining the action to be taken.
#[non_exhaustive]
pub enum Request {
Expand All @@ -53,6 +53,7 @@ libc_enum! {
PTRACE_SINGLESTEP,
#[cfg(any(all(target_os = "android", target_pointer_width = "32"),
all(target_os = "linux", any(target_env = "musl",
target_env = "ohos",
target_arch = "mips",
target_arch = "mips32r6",
target_arch = "mips64",
Expand All @@ -62,6 +63,7 @@ libc_enum! {
PTRACE_GETREGS,
#[cfg(any(all(target_os = "android", target_pointer_width = "32"),
all(target_os = "linux", any(target_env = "musl",
target_env = "ohos",
target_arch = "mips",
target_arch = "mips32r6",
target_arch = "mips64",
Expand All @@ -71,6 +73,7 @@ libc_enum! {
PTRACE_SETREGS,
#[cfg(any(all(target_os = "android", target_pointer_width = "32"),
all(target_os = "linux", any(target_env = "musl",
target_env = "ohos",
target_arch = "mips",
target_arch = "mips32r6",
target_arch = "mips64",
Expand All @@ -80,6 +83,7 @@ libc_enum! {
PTRACE_GETFPREGS,
#[cfg(any(all(target_os = "android", target_pointer_width = "32"),
all(target_os = "linux", any(target_env = "musl",
target_env = "ohos",
target_arch = "mips",
target_arch = "mips32r6",
target_arch = "mips64",
Expand All @@ -90,6 +94,7 @@ libc_enum! {
PTRACE_ATTACH,
PTRACE_DETACH,
#[cfg(all(target_os = "linux", any(target_env = "musl",
target_env = "ohos",
target_arch = "mips",
target_arch = "mips32r6",
target_arch = "mips64",
Expand All @@ -98,6 +103,7 @@ libc_enum! {
target_arch = "x86_64")))]
PTRACE_GETFPXREGS,
#[cfg(all(target_os = "linux", any(target_env = "musl",
target_env = "ohos",
target_arch = "mips",
target_arch = "mips32r6",
target_arch = "mips64",
Expand Down
28 changes: 21 additions & 7 deletions src/sys/statfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ pub struct Statfs(type_of_statfs);
type fs_type_t = u32;
#[cfg(target_os = "android")]
type fs_type_t = libc::c_ulong;
#[cfg(all(target_os = "linux", target_arch = "s390x", not(target_env = "musl")))]
#[cfg(all(
target_os = "linux",
target_arch = "s390x",
not(target_env = "musl")
))]
type fs_type_t = libc::c_uint;
#[cfg(all(target_os = "linux", target_env = "musl"))]
type fs_type_t = libc::c_ulong;
#[cfg(all(target_os = "linux", target_env = "ohos"))]
#[cfg(all(target_os = "linux", any(target_env = "musl", target_env = "ohos")))]
type fs_type_t = libc::c_ulong;
#[cfg(all(target_os = "linux", target_env = "uclibc"))]
type fs_type_t = libc::c_int;
Expand Down Expand Up @@ -318,7 +320,11 @@ impl Statfs {
}

/// Optimal transfer block size
#[cfg(all(target_os = "linux", target_arch = "s390x", not(target_env = "musl")))]
#[cfg(all(
target_os = "linux",
target_arch = "s390x",
not(target_env = "musl")
))]
pub fn optimal_transfer_size(&self) -> u32 {
self.0.f_bsize
}
Expand Down Expand Up @@ -373,7 +379,11 @@ impl Statfs {

/// Size of a block
// f_bsize on linux: https://github.com/torvalds/linux/blob/master/fs/nfs/super.c#L471
#[cfg(all(target_os = "linux", target_arch = "s390x", not(target_env = "musl")))]
#[cfg(all(
target_os = "linux",
target_arch = "s390x",
not(target_env = "musl")
))]
pub fn block_size(&self) -> u32 {
self.0.f_bsize
}
Expand Down Expand Up @@ -454,7 +464,11 @@ impl Statfs {
}

/// Maximum length of filenames
#[cfg(all(target_os = "linux", target_arch = "s390x", not(target_env = "musl")))]
#[cfg(all(
target_os = "linux",
target_arch = "s390x",
not(target_env = "musl")
))]
pub fn maximum_name_length(&self) -> u32 {
self.0.f_namelen
}
Expand Down
55 changes: 44 additions & 11 deletions src/sys/time.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#[cfg_attr(target_env = "musl", allow(deprecated))]
#[cfg_attr(
any(target_env = "musl", target_env = "ohos"),
allow(deprecated)
)]
// https://github.com/rust-lang/libc/issues/1848
pub use libc::{suseconds_t, time_t};
use libc::{timespec, timeval};
Expand Down Expand Up @@ -253,7 +256,10 @@ impl PartialOrd for TimeSpec {

impl TimeValLike for TimeSpec {
#[inline]
#[cfg_attr(target_env = "musl", allow(deprecated))]
#[cfg_attr(
any(target_env = "musl", target_env = "ohos"),
allow(deprecated)
)]
// https://github.com/rust-lang/libc/issues/1848
fn seconds(seconds: i64) -> TimeSpec {
assert!(
Expand Down Expand Up @@ -286,7 +292,10 @@ impl TimeValLike for TimeSpec {

/// Makes a new `TimeSpec` with given number of nanoseconds.
#[inline]
#[cfg_attr(target_env = "musl", allow(deprecated))]
#[cfg_attr(
any(target_env = "musl", target_env = "ohos"),
allow(deprecated)
)]
// https://github.com/rust-lang/libc/issues/1848
fn nanoseconds(nanoseconds: i64) -> TimeSpec {
let (secs, nanos) = div_mod_floor_64(nanoseconds, NANOS_PER_SEC);
Expand Down Expand Up @@ -340,7 +349,10 @@ impl TimeSpec {
TimeSpec::new(0, libc::UTIME_NOW as timespec_tv_nsec_t);

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

#[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
#[cfg_attr(
any(target_env = "musl", target_env = "ohos"),
allow(deprecated)
)] // https://github.com/rust-lang/libc/issues/1848
pub const fn tv_sec(&self) -> time_t {
self.0.tv_sec
}
Expand All @@ -365,7 +380,10 @@ impl TimeSpec {
self.0.tv_nsec
}

#[cfg_attr(target_env = "musl", allow(deprecated))]
#[cfg_attr(
any(target_env = "musl", target_env = "ohos"),
allow(deprecated)
)]
// https://github.com/rust-lang/libc/issues/1848
pub const fn from_duration(duration: Duration) -> Self {
let mut ts = zero_init_timespec();
Expand Down Expand Up @@ -506,7 +524,10 @@ impl TimeValLike for TimeVal {
(TV_MIN_SECONDS..=TV_MAX_SECONDS).contains(&seconds),
"TimeVal out of bounds; seconds={seconds}"
);
#[cfg_attr(target_env = "musl", allow(deprecated))]
#[cfg_attr(
any(target_env = "musl", target_env = "ohos"),
allow(deprecated)
)]
// https://github.com/rust-lang/libc/issues/1848
TimeVal(timeval {
tv_sec: seconds as time_t,
Expand All @@ -531,7 +552,10 @@ impl TimeValLike for TimeVal {
(TV_MIN_SECONDS..=TV_MAX_SECONDS).contains(&secs),
"TimeVal out of bounds"
);
#[cfg_attr(target_env = "musl", allow(deprecated))]
#[cfg_attr(
any(target_env = "musl", target_env = "ohos"),
allow(deprecated)
)]
// https://github.com/rust-lang/libc/issues/1848
TimeVal(timeval {
tv_sec: secs as time_t,
Expand All @@ -549,7 +573,10 @@ impl TimeValLike for TimeVal {
(TV_MIN_SECONDS..=TV_MAX_SECONDS).contains(&secs),
"TimeVal out of bounds"
);
#[cfg_attr(target_env = "musl", allow(deprecated))]
#[cfg_attr(
any(target_env = "musl", target_env = "ohos"),
allow(deprecated)
)]
// https://github.com/rust-lang/libc/issues/1848
TimeVal(timeval {
tv_sec: secs as time_t,
Expand Down Expand Up @@ -586,7 +613,10 @@ impl TimeValLike for TimeVal {

impl TimeVal {
/// Construct a new `TimeVal` from its components
#[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
#[cfg_attr(
any(target_env = "musl", target_env = "ohos"),
allow(deprecated)
)] // https://github.com/rust-lang/libc/issues/1848
pub const fn new(seconds: time_t, microseconds: suseconds_t) -> Self {
Self(timeval {
tv_sec: seconds,
Expand All @@ -602,7 +632,10 @@ impl TimeVal {
}
}

#[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
#[cfg_attr(
any(target_env = "musl", target_env = "ohos"),
allow(deprecated)
)] // https://github.com/rust-lang/libc/issues/1848
pub const fn tv_sec(&self) -> time_t {
self.0.tv_sec
}
Expand Down
5 changes: 4 additions & 1 deletion src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ impl ClockId {
#[cfg(any(
target_os = "emscripten",
target_os = "fuchsia",
all(target_os = "linux", target_env = "musl")
all(
target_os = "linux",
any(target_env = "musl", target_env = "ohos")
)
))]
pub const CLOCK_SGI_CYCLE: ClockId = ClockId(libc::CLOCK_SGI_CYCLE);
/// International Atomic Time.
Expand Down
10 changes: 5 additions & 5 deletions src/ucontext.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#[cfg(not(target_env = "musl"))]
#[cfg(not(any(target_env = "musl", target_env = "ohos")))]
use crate::errno::Errno;
use crate::sys::signal::SigSet;
#[cfg(not(target_env = "musl"))]
#[cfg(not(any(target_env = "musl", target_env = "ohos")))]
use crate::Result;
#[cfg(not(target_env = "musl"))]
#[cfg(not(any(target_env = "musl", target_env = "ohos")))]
use std::mem;

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

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

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