!1 [nix]Add OH support and BUILD.gn for rust nix

Merge pull request !1 from haotuo/master
This commit is contained in:
openharmony_ci 2023-04-13 11:33:31 +00:00 committed by Gitee
commit 700930ac66
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
14 changed files with 124 additions and 43 deletions

70
BUILD.gn Normal file
View File

@ -0,0 +1,70 @@
# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/templates/rust/ohos_cargo_crate.gni")
ohos_cargo_crate("lib") {
crate_name = "nix"
crate_type = "rlib"
crate_root = "src/lib.rs"
sources = ["src/lib.rs"]
edition = "2018"
cargo_pkg_version = "0.26.2"
cargo_pkg_authors = "The nix-rust Project Developers"
cargo_pkg_name = "nix"
cargo_pkg_description = "Rust friendly bindings to *nix APIs"
deps = [
"//third_party/rust/crates/bitflags:lib",
"//third_party/rust/crates/cfg-if:lib",
"//third_party/rust/crates/libc:lib",
"//third_party/rust/crates/memoffset:lib",
"//third_party/rust/crates/pin-utils:lib",
"//third_party/rust/crates/static-assertions-rs:lib",
]
features = [
"acct",
"default",
"dir",
"env",
"event",
"feature",
"fs",
"hostname",
"inotify",
"ioctl",
"kmod",
"memoffset",
"mman",
"mount",
"net",
"personality",
"pin-utils",
"poll",
"process",
"pthread",
"ptrace",
"quota",
"reboot",
"resource",
"sched",
"signal",
"socket",
"term",
"time",
"ucontext",
"uio",
"user",
"zerocopy",
]
}

View File

@ -104,7 +104,7 @@ libc_bitflags!(
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
all(target_os = "linux", not(target_env = "musl")),
all(target_os = "linux", not(any(target_env = "musl", target_env = "ohos"))),
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",

View File

@ -1,8 +1,8 @@
/// The datatype used for the ioctl number
#[cfg(any(target_os = "android", target_env = "musl"))]
#[cfg(any(target_os = "android", target_env = "musl", target_env = "ohos"))]
#[doc(hidden)]
pub type ioctl_num_type = ::libc::c_int;
#[cfg(not(any(target_os = "android", target_env = "musl")))]
#[cfg(not(any(target_os = "android", target_env = "musl", target_env = "ohos")))]
#[doc(hidden)]
pub type ioctl_num_type = ::libc::c_ulong;
/// The datatype used for the 3rd argument

View File

@ -51,6 +51,7 @@ pub fn memfd_create(name: &CStr, flags: MemFdCreateFlag) -> Result<RawFd> {
// If the OS is Linux, gnu and musl expose a memfd_create symbol but not uclibc
target_env = "gnu",
target_env = "musl",
target_env = "ohos",
)))]
{
libc::memfd_create(name.as_ptr(), flags.bits())

View File

@ -1,10 +1,10 @@
//! Memory management declarations.
use crate::errno::Errno;
#[cfg(not(target_os = "android"))]
#[cfg(not(any(target_os = "android", target_env = "ohos")))]
use crate::NixPath;
use crate::Result;
#[cfg(not(target_os = "android"))]
#[cfg(not(any(target_os = "android", target_env = "ohos")))]
#[cfg(feature = "fs")]
use crate::{fcntl::OFlag, sys::stat::Mode};
use libc::{self, c_int, c_void, off_t, size_t};
@ -59,7 +59,7 @@ libc_bitflags! {
/// Put the mapping into the first 2GB of the process address space.
#[cfg(any(all(any(target_os = "android", target_os = "linux"),
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", any(target_env = "musl", target_env = "ohos"), any(target_arch = "x86", target_arch = "x86_64")),
all(target_os = "freebsd", target_pointer_width = "64")))]
#[cfg_attr(docsrs, doc(cfg(all())))]
MAP_32BIT;
@ -555,7 +555,7 @@ pub unsafe fn msync(
Errno::result(libc::msync(addr, length, flags.bits())).map(drop)
}
#[cfg(not(target_os = "android"))]
#[cfg(not(any(target_os = "android", target_env = "ohos")))]
feature! {
#![feature = "fs"]
/// Creates and opens a new, or opens an existing, POSIX shared memory object.
@ -590,7 +590,7 @@ pub fn shm_open<P>(
/// For more information, see [`shm_unlink(3)`].
///
/// [`shm_unlink(3)`]: https://man7.org/linux/man-pages/man3/shm_unlink.3.html
#[cfg(not(target_os = "android"))]
#[cfg(not(any(target_os = "android", target_env = "ohos")))]
pub fn shm_unlink<P: ?Sized + NixPath>(name: &P) -> Result<()> {
let ret =
name.with_nix_path(|cstr| unsafe { libc::shm_unlink(cstr.as_ptr()) })?;

View File

@ -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 = "ohos", target_env = "uclibc")))]
#[cfg_attr(docsrs, doc(cfg(all())))]
FDPIC_FUNCPTRS;
/// Map page 0 as read-only.
@ -42,7 +42,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 = "ohos", target_env = "uclibc")))]
#[cfg_attr(docsrs, doc(cfg(all())))]
UNAME26;
/// No effects.

View File

@ -15,7 +15,7 @@ pub type AddressType = *mut ::libc::c_void;
any(
all(
target_arch = "x86_64",
any(target_env = "gnu", target_env = "musl")
any(target_env = "gnu", target_env = "musl", target_env = "ohos")
),
all(target_arch = "x86", target_env = "gnu")
)
@ -35,8 +35,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 = "ohos", target_env = "uclibc", target_os = "android")), repr(u32))]
#[cfg_attr(any(target_env = "musl", target_env = "ohos", target_env = "uclibc", target_os = "android"), repr(i32))]
/// Ptrace Request enum defining the action to be taken.
#[non_exhaustive]
pub enum Request {
@ -52,6 +52,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 = "mips64",
target_arch = "x86_64",
@ -59,6 +60,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 = "mips64",
target_arch = "x86_64",
@ -66,6 +68,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 = "mips64",
target_arch = "x86_64",
@ -73,6 +76,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 = "mips64",
target_arch = "x86_64",
@ -81,12 +85,14 @@ libc_enum! {
PTRACE_ATTACH,
PTRACE_DETACH,
#[cfg(all(target_os = "linux", any(target_env = "musl",
target_env = "ohos",
target_arch = "mips",
target_arch = "mips64",
target_arch = "x86",
target_arch = "x86_64")))]
PTRACE_GETFPXREGS,
#[cfg(all(target_os = "linux", any(target_env = "musl",
target_env = "ohos",
target_arch = "mips",
target_arch = "mips64",
target_arch = "x86",
@ -204,7 +210,7 @@ fn ptrace_peek(
any(
all(
target_arch = "x86_64",
any(target_env = "gnu", target_env = "musl")
any(target_env = "gnu", target_env = "musl", target_env = "ohos")
),
all(target_arch = "x86", target_env = "gnu")
)
@ -219,7 +225,7 @@ pub fn getregs(pid: Pid) -> Result<user_regs_struct> {
any(
all(
target_arch = "x86_64",
any(target_env = "gnu", target_env = "musl")
any(target_env = "gnu", target_env = "musl", target_env = "ohos")
),
all(target_arch = "x86", target_env = "gnu")
)

View File

@ -1069,7 +1069,7 @@ mod sigevent {
SigevNotify::SigevThreadId{..} => libc::SIGEV_THREAD_ID,
#[cfg(all(target_os = "linux", target_env = "uclibc"))]
SigevNotify::SigevThreadId{..} => libc::SIGEV_THREAD_ID,
#[cfg(any(all(target_os = "linux", target_env = "musl"), target_arch = "mips"))]
#[cfg(any(all(target_os = "linux", any(target_env = "musl", target_env = "ohos")), target_arch = "mips"))]
SigevNotify::SigevThreadId{..} => 4 // No SIGEV_THREAD_ID defined
};
sev.sigev_signo = match sigev_notify {

View File

@ -1184,13 +1184,13 @@ impl<'a> ControlMessage<'a> {
/// The value of CMSG_LEN on this message.
/// Safe because CMSG_LEN is always safe
#[cfg(any(target_os = "android",
all(target_os = "linux", not(target_env = "musl"))))]
all(target_os = "linux", not(any(target_env = "musl", target_env = "ohos")))))]
fn cmsg_len(&self) -> usize {
unsafe{CMSG_LEN(self.len() as libc::c_uint) as usize}
}
#[cfg(not(any(target_os = "android",
all(target_os = "linux", not(target_env = "musl")))))]
all(target_os = "linux", not(any(target_env = "musl", target_env = "ohos"))))))]
fn cmsg_len(&self) -> libc::c_uint {
unsafe{CMSG_LEN(self.len() as libc::c_uint)}
}

View File

@ -64,7 +64,7 @@ type fs_type_t = u32;
type fs_type_t = libc::c_ulong;
#[cfg(all(target_os = "linux", target_arch = "s390x"))]
type fs_type_t = libc::c_uint;
#[cfg(all(target_os = "linux", target_env = "musl"))]
#[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;
@ -73,6 +73,7 @@ type fs_type_t = libc::c_int;
not(any(
target_arch = "s390x",
target_env = "musl",
target_env = "ohos",
target_env = "uclibc"
))
))]
@ -83,10 +84,10 @@ type fs_type_t = libc::__fsword_t;
target_os = "freebsd",
target_os = "android",
all(target_os = "linux", target_arch = "s390x"),
all(target_os = "linux", target_env = "musl"),
all(target_os = "linux", any(target_env = "musl", target_env = "ohos")),
all(
target_os = "linux",
not(any(target_arch = "s390x", target_env = "musl"))
not(any(target_arch = "s390x", target_env = "musl", target_env = "ohos"))
),
))]
#[derive(Eq, Copy, Clone, PartialEq, Debug)]
@ -292,7 +293,7 @@ pub const XENFS_SUPER_MAGIC: FsType =
pub const NSFS_MAGIC: FsType = FsType(libc::NSFS_MAGIC as fs_type_t);
#[cfg(all(
any(target_os = "linux", target_os = "android"),
not(target_env = "musl")
not(any(target_env = "musl", target_env = "ohos"))
))]
#[allow(missing_docs)]
pub const XFS_SUPER_MAGIC: FsType = FsType(libc::XFS_SUPER_MAGIC as fs_type_t);
@ -342,7 +343,7 @@ impl Statfs {
/// Optimal transfer block size
#[cfg(any(
target_os = "android",
all(target_os = "linux", target_env = "musl")
all(target_os = "linux", any(target_env = "musl", target_env = "ohos"))
))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn optimal_transfer_size(&self) -> libc::c_ulong {
@ -355,6 +356,7 @@ impl Statfs {
not(any(
target_arch = "s390x",
target_env = "musl",
target_env = "ohos",
target_env = "uclibc"
))
))]
@ -401,7 +403,7 @@ 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_env = "musl"))]
#[cfg(all(target_os = "linux", any(target_env = "musl", target_env = "ohos")))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn block_size(&self) -> libc::c_ulong {
self.0.f_bsize
@ -422,6 +424,7 @@ impl Statfs {
not(any(
target_arch = "s390x",
target_env = "musl",
target_env = "ohos",
target_env = "uclibc"
))
))]
@ -492,7 +495,7 @@ impl Statfs {
}
/// Maximum length of filenames
#[cfg(all(target_os = "linux", target_env = "musl"))]
#[cfg(all(target_os = "linux", any(target_env = "musl", target_env = "ohos")))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn maximum_name_length(&self) -> libc::c_ulong {
self.0.f_namelen
@ -511,6 +514,7 @@ impl Statfs {
not(any(
target_arch = "s390x",
target_env = "musl",
target_env = "ohos",
target_env = "uclibc"
))
))]

View File

@ -58,7 +58,7 @@ libc_bitflags!(
#[cfg_attr(docsrs, doc(cfg(all())))]
ST_NODIRATIME;
/// Update access time relative to modify/change time
#[cfg(any(target_os = "android", all(target_os = "linux", not(target_env = "musl"))))]
#[cfg(any(target_os = "android", all(target_os = "linux", not(any(target_env = "musl", target_env = "ohos")))))]
#[cfg_attr(docsrs, doc(cfg(all())))]
ST_RELATIME;
}

View File

@ -1,4 +1,4 @@
#[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};
@ -256,7 +256,7 @@ 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!(
@ -290,7 +290,7 @@ 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);
@ -333,7 +333,7 @@ impl TimeValLike for TimeSpec {
impl TimeSpec {
/// 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;
@ -349,7 +349,7 @@ 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
}
@ -358,7 +358,7 @@ 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();
@ -500,7 +500,7 @@ impl TimeValLike for TimeVal {
"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,
@ -525,7 +525,7 @@ 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,
@ -543,7 +543,7 @@ 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,
@ -580,7 +580,7 @@ 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,
@ -596,7 +596,7 @@ 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
}

View File

@ -162,7 +162,7 @@ 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"))
))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub const CLOCK_SGI_CYCLE: ClockId = ClockId(libc::CLOCK_SGI_CYCLE);

View File

@ -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)]
@ -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()) };
@ -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)