Skip to content

Commit

Permalink
Auto merge of #3138 - Amanieu:ohos, r=JohnTitor
Browse files Browse the repository at this point in the history
Add support for OpenHarmony

This PR adds support for [OpenHarmony](https://gitee.com/openharmony/docs/) targets:
- `aarch64-linux-ohos`
- `arm-linux-ohos`

Compiler team MCP: rust-lang/compiler-team#568

OpenHarmony uses a fork of musl with minor modifications, so most of the code is shared with other musl targets. Additionally, although OpenHarmony uses musl 1.2, it is still ABI-compatible with musl 1.1 so the existing bindings should continue to work until #3068 is merged.

A PR to add the targets to rustc will follow after this is merged.
  • Loading branch information
bors committed Mar 4, 2023
2 parents 973c3e1 + 3f5128e commit da4f8f8
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 102 deletions.
4 changes: 2 additions & 2 deletions libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3081,7 +3081,7 @@ fn test_linux(target: &str) {

// target_env
let gnu = target.contains("gnu");
let musl = target.contains("musl");
let musl = target.contains("musl") || target.contains("ohos");
let uclibc = target.contains("uclibc");

match (gnu, musl, uclibc) {
Expand Down Expand Up @@ -3946,7 +3946,7 @@ fn test_linux(target: &str) {
// are included (e.g. because including both sets of headers clashes)
fn test_linux_like_apis(target: &str) {
let gnu = target.contains("gnu");
let musl = target.contains("musl");
let musl = target.contains("musl") || target.contains("ohos");
let linux = target.contains("linux");
let emscripten = target.contains("emscripten");
let android = target.contains("android");
Expand Down
11 changes: 6 additions & 5 deletions src/unix/linux_like/linux/align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ macro_rules! expand_align {
size: [u8; ::__SIZEOF_PTHREAD_MUTEXATTR_T],
}

#[cfg_attr(any(target_env = "musl", target_pointer_width = "32"),
#[cfg_attr(any(target_env = "musl", target_env = "ohos", target_pointer_width = "32"),
repr(align(4)))]
#[cfg_attr(all(not(target_env = "musl"),
not(target_env = "ohos"),
target_pointer_width = "64"),
repr(align(8)))]
pub struct pthread_rwlockattr_t {
Expand Down Expand Up @@ -63,16 +64,16 @@ macro_rules! expand_align {
}

s_no_extra_traits! {
#[cfg_attr(all(target_env = "musl",
#[cfg_attr(all(any(target_env = "musl", target_env = "ohos"),
target_pointer_width = "32"),
repr(align(4)))]
#[cfg_attr(all(target_env = "musl",
#[cfg_attr(all(any(target_env = "musl", target_env = "ohos"),
target_pointer_width = "64"),
repr(align(8)))]
#[cfg_attr(all(not(target_env = "musl"),
#[cfg_attr(all(not(any(target_env = "musl", target_env = "ohos")),
target_arch = "x86"),
repr(align(4)))]
#[cfg_attr(all(not(target_env = "musl"),
#[cfg_attr(all(not(any(target_env = "musl", target_env = "ohos")),
not(target_arch = "x86")),
repr(align(8)))]
pub struct pthread_cond_t {
Expand Down
4 changes: 2 additions & 2 deletions src/unix/linux_like/linux/arch/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ cfg_if! {
if #[cfg(all(any(target_arch = "x86",
target_arch = "x86_64",
target_arch = "aarch64"),
not(target_env = "musl")))] {
not(any(target_env = "musl", target_env = "ohos"))))] {
pub const SO_TIMESTAMP_NEW: ::c_int = 63;
pub const SO_TIMESTAMPNS_NEW: ::c_int = 64;
pub const SO_TIMESTAMPING_NEW: ::c_int = 65;
Expand Down Expand Up @@ -252,7 +252,7 @@ cfg_if! {
pub const RLIMIT_RTTIME: ::__rlimit_resource_t = 15;
pub const RLIMIT_NLIMITS: ::__rlimit_resource_t = RLIM_NLIMITS;

} else if #[cfg(target_env = "musl")] {
} else if #[cfg(any(target_env = "musl", target_env = "ohos"))] {

pub const RLIMIT_CPU: ::c_int = 0;
pub const RLIMIT_FSIZE: ::c_int = 1;
Expand Down
146 changes: 85 additions & 61 deletions src/unix/linux_like/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,9 @@ s! {
__pgrp: ::pid_t,
__sd: ::sigset_t,
__ss: ::sigset_t,
#[cfg(target_env = "musl")]
#[cfg(any(target_env = "musl", target_env = "ohos"))]
__prio: ::c_int,
#[cfg(not(target_env = "musl"))]
#[cfg(not(any(target_env = "musl", target_env = "ohos")))]
__sp: ::sched_param,
__policy: ::c_int,
__pad: [::c_int; 16],
Expand Down Expand Up @@ -1225,7 +1225,7 @@ cfg_if! {
}

cfg_if! {
if #[cfg(any(target_env = "gnu", target_env = "musl"))] {
if #[cfg(any(target_env = "gnu", target_env = "musl", target_env = "ohos"))] {
pub const ABDAY_1: ::nl_item = 0x20000;
pub const ABDAY_2: ::nl_item = 0x20001;
pub const ABDAY_3: ::nl_item = 0x20002;
Expand Down Expand Up @@ -3970,7 +3970,7 @@ safe_f! {
}

cfg_if! {
if #[cfg(not(target_env = "uclibc"))] {
if #[cfg(all(not(target_env = "uclibc"), not(target_env = "ohos")))] {
extern "C" {
pub fn aio_read(aiocbp: *mut aiocb) -> ::c_int;
pub fn aio_write(aiocbp: *mut aiocb) -> ::c_int;
Expand All @@ -3989,6 +3989,13 @@ cfg_if! {
nitems: ::c_int,
sevp: *mut ::sigevent,
) -> ::c_int;
}
}
}

cfg_if! {
if #[cfg(not(target_env = "uclibc"))] {
extern "C" {
pub fn pwritev(
fd: ::c_int,
iov: *const ::iovec,
Expand Down Expand Up @@ -4038,8 +4045,79 @@ cfg_if! {
}
}

// These functions are not available on OpenHarmony
cfg_if! {
if #[cfg(not(target_env = "ohos"))] {
extern "C" {
// Only `getspnam_r` is implemented for musl, out of all of the reenterant
// functions from `shadow.h`.
// https://git.musl-libc.org/cgit/musl/tree/include/shadow.h
pub fn getspnam_r(
name: *const ::c_char,
spbuf: *mut spwd,
buf: *mut ::c_char,
buflen: ::size_t,
spbufp: *mut *mut spwd,
) -> ::c_int;

pub fn shm_open(name: *const c_char, oflag: ::c_int, mode: mode_t) -> ::c_int;
pub fn shm_unlink(name: *const ::c_char) -> ::c_int;

pub fn mq_open(name: *const ::c_char, oflag: ::c_int, ...) -> ::mqd_t;
pub fn mq_close(mqd: ::mqd_t) -> ::c_int;
pub fn mq_unlink(name: *const ::c_char) -> ::c_int;
pub fn mq_receive(
mqd: ::mqd_t,
msg_ptr: *mut ::c_char,
msg_len: ::size_t,
msg_prio: *mut ::c_uint,
) -> ::ssize_t;
pub fn mq_timedreceive(
mqd: ::mqd_t,
msg_ptr: *mut ::c_char,
msg_len: ::size_t,
msg_prio: *mut ::c_uint,
abs_timeout: *const ::timespec,
) -> ::ssize_t;
pub fn mq_send(
mqd: ::mqd_t,
msg_ptr: *const ::c_char,
msg_len: ::size_t,
msg_prio: ::c_uint,
) -> ::c_int;
pub fn mq_timedsend(
mqd: ::mqd_t,
msg_ptr: *const ::c_char,
msg_len: ::size_t,
msg_prio: ::c_uint,
abs_timeout: *const ::timespec,
) -> ::c_int;
pub fn mq_getattr(mqd: ::mqd_t, attr: *mut ::mq_attr) -> ::c_int;
pub fn mq_setattr(
mqd: ::mqd_t,
newattr: *const ::mq_attr,
oldattr: *mut ::mq_attr
) -> ::c_int;

pub fn pthread_mutex_consistent(mutex: *mut pthread_mutex_t) -> ::c_int;
pub fn pthread_cancel(thread: ::pthread_t) -> ::c_int;
pub fn pthread_mutexattr_getrobust(
attr: *const pthread_mutexattr_t,
robustness: *mut ::c_int,
) -> ::c_int;
pub fn pthread_mutexattr_setrobust(
attr: *mut pthread_mutexattr_t,
robustness: ::c_int,
) -> ::c_int;
}
}
}

extern "C" {
#[cfg_attr(not(target_env = "musl"), link_name = "__xpg_strerror_r")]
#[cfg_attr(
not(any(target_env = "musl", target_env = "ohos")),
link_name = "__xpg_strerror_r"
)]
pub fn strerror_r(errnum: ::c_int, buf: *mut c_char, buflen: ::size_t) -> ::c_int;

pub fn abs(i: ::c_int) -> ::c_int;
Expand Down Expand Up @@ -4070,18 +4148,6 @@ extern "C" {
pub fn getspent() -> *mut spwd;

pub fn getspnam(name: *const ::c_char) -> *mut spwd;
// Only `getspnam_r` is implemented for musl, out of all of the reenterant
// functions from `shadow.h`.
// https://git.musl-libc.org/cgit/musl/tree/include/shadow.h
pub fn getspnam_r(
name: *const ::c_char,
spbuf: *mut spwd,
buf: *mut ::c_char,
buflen: ::size_t,
spbufp: *mut *mut spwd,
) -> ::c_int;

pub fn shm_open(name: *const c_char, oflag: ::c_int, mode: mode_t) -> ::c_int;

// System V IPC
pub fn shmget(key: ::key_t, size: ::size_t, shmflg: ::c_int) -> ::c_int;
Expand Down Expand Up @@ -4187,37 +4253,6 @@ extern "C" {
id: ::c_int,
data: *mut ::c_char,
) -> ::c_int;
pub fn mq_open(name: *const ::c_char, oflag: ::c_int, ...) -> ::mqd_t;
pub fn mq_close(mqd: ::mqd_t) -> ::c_int;
pub fn mq_unlink(name: *const ::c_char) -> ::c_int;
pub fn mq_receive(
mqd: ::mqd_t,
msg_ptr: *mut ::c_char,
msg_len: ::size_t,
msg_prio: *mut ::c_uint,
) -> ::ssize_t;
pub fn mq_timedreceive(
mqd: ::mqd_t,
msg_ptr: *mut ::c_char,
msg_len: ::size_t,
msg_prio: *mut ::c_uint,
abs_timeout: *const ::timespec,
) -> ::ssize_t;
pub fn mq_send(
mqd: ::mqd_t,
msg_ptr: *const ::c_char,
msg_len: ::size_t,
msg_prio: ::c_uint,
) -> ::c_int;
pub fn mq_timedsend(
mqd: ::mqd_t,
msg_ptr: *const ::c_char,
msg_len: ::size_t,
msg_prio: ::c_uint,
abs_timeout: *const ::timespec,
) -> ::c_int;
pub fn mq_getattr(mqd: ::mqd_t, attr: *mut ::mq_attr) -> ::c_int;
pub fn mq_setattr(mqd: ::mqd_t, newattr: *const ::mq_attr, oldattr: *mut ::mq_attr) -> ::c_int;
pub fn epoll_pwait(
epfd: ::c_int,
events: *mut ::epoll_event,
Expand Down Expand Up @@ -4284,8 +4319,6 @@ extern "C" {

pub fn posix_madvise(addr: *mut ::c_void, len: ::size_t, advice: ::c_int) -> ::c_int;

pub fn shm_unlink(name: *const ::c_char) -> ::c_int;

pub fn seekdir(dirp: *mut ::DIR, loc: ::c_long);

pub fn telldir(dirp: *mut ::DIR) -> ::c_long;
Expand Down Expand Up @@ -4389,7 +4422,7 @@ extern "C" {
attr: *mut pthread_mutexattr_t,
protocol: ::c_int,
) -> ::c_int;
pub fn pthread_mutex_consistent(mutex: *mut pthread_mutex_t) -> ::c_int;

pub fn pthread_mutex_timedlock(
lock: *mut pthread_mutex_t,
abstime: *const ::timespec,
Expand Down Expand Up @@ -4487,7 +4520,6 @@ extern "C" {
pub fn pthread_sigmask(how: ::c_int, set: *const sigset_t, oldset: *mut sigset_t) -> ::c_int;
pub fn sem_open(name: *const ::c_char, oflag: ::c_int, ...) -> *mut sem_t;
pub fn getgrnam(name: *const ::c_char) -> *mut ::group;
pub fn pthread_cancel(thread: ::pthread_t) -> ::c_int;
pub fn pthread_kill(thread: ::pthread_t, sig: ::c_int) -> ::c_int;
pub fn sem_unlink(name: *const ::c_char) -> ::c_int;
pub fn daemon(nochdir: ::c_int, noclose: ::c_int) -> ::c_int;
Expand Down Expand Up @@ -4522,14 +4554,6 @@ extern "C" {
attr: *const pthread_mutexattr_t,
pshared: *mut ::c_int,
) -> ::c_int;
pub fn pthread_mutexattr_getrobust(
attr: *const pthread_mutexattr_t,
robustness: *mut ::c_int,
) -> ::c_int;
pub fn pthread_mutexattr_setrobust(
attr: *mut pthread_mutexattr_t,
robustness: ::c_int,
) -> ::c_int;
pub fn popen(command: *const c_char, mode: *const c_char) -> *mut ::FILE;
pub fn faccessat(
dirfd: ::c_int,
Expand Down Expand Up @@ -4729,7 +4753,7 @@ cfg_if! {
if #[cfg(target_env = "uclibc")] {
mod uclibc;
pub use self::uclibc::*;
} else if #[cfg(target_env = "musl")] {
} else if #[cfg(any(target_env = "musl", target_env = "ohos"))] {
mod musl;
pub use self::musl::*;
} else if #[cfg(target_env = "gnu")] {
Expand Down
27 changes: 18 additions & 9 deletions src/unix/linux_like/linux/musl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,28 +287,37 @@ s_no_extra_traits! {

// FIXME: musl added paddings and adjusted
// layout in 1.2.0 but our CI is still 1.1.24.
// So, I'm leaving some fields as comments for now.
// So, I'm leaving some fields as cfg for now.
// ref. https://github.com/bminor/musl/commit/
// 1e7f0fcd7ff2096904fd93a2ee6d12a2392be392
//
// OpenHarmony uses the musl 1.2 layout.
pub struct utmpx {
pub ut_type: ::c_short,
//__ut_pad1: ::c_short,
__ut_pad1: ::c_short,
pub ut_pid: ::pid_t,
pub ut_line: [::c_char; 32],
pub ut_id: [::c_char; 4],
pub ut_user: [::c_char; 32],
pub ut_host: [::c_char; 256],
pub ut_exit: __exit_status,

//#[cfg(target_endian = "little")]
#[cfg(target_env = "musl")]
pub ut_session: ::c_long,
//#[cfg(target_endian = "little")]
//__ut_pad2: ::c_long,

//#[cfg(not(target_endian = "little"))]
//__ut_pad2: ::c_int,
//#[cfg(not(target_endian = "little"))]
//pub ut_session: ::c_int,
#[cfg(target_env = "ohos")]
#[cfg(target_endian = "little")]
pub ut_session: ::c_int,
#[cfg(target_env = "ohos")]
#[cfg(target_endian = "little")]
__ut_pad2: ::c_int,

#[cfg(target_env = "ohos")]
#[cfg(not(target_endian = "little"))]
__ut_pad2: ::c_int,
#[cfg(target_env = "ohos")]
#[cfg(not(target_endian = "little"))]
pub ut_session: ::c_int,

pub ut_tv: ::timeval,
pub ut_addr_v6: [::c_uint; 4],
Expand Down
12 changes: 6 additions & 6 deletions src/unix/linux_like/linux/no_align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ macro_rules! expand_align {
target_arch = "riscv32",
target_arch = "loongarch64",
all(target_arch = "aarch64",
target_env = "musl")))]
any(target_env = "musl", target_env = "ohos"))))]
__align: [::c_int; 0],
#[cfg(not(any(target_arch = "x86_64",
target_arch = "powerpc64",
Expand All @@ -22,15 +22,15 @@ macro_rules! expand_align {
target_arch = "riscv32",
target_arch = "loongarch64",
all(target_arch = "aarch64",
target_env = "musl"))))]
any(target_env = "musl", target_env = "ohos")))))]
__align: [::c_long; 0],
size: [u8; ::__SIZEOF_PTHREAD_MUTEXATTR_T],
}

pub struct pthread_rwlockattr_t {
#[cfg(target_env = "musl")]
#[cfg(any(target_env = "musl", target_env = "ohos"))]
__align: [::c_int; 0],
#[cfg(not(target_env = "musl"))]
#[cfg(not(any(target_env = "musl", target_env = "ohos")))]
__align: [::c_long; 0],
size: [u8; ::__SIZEOF_PTHREAD_RWLOCKATTR_T],
}
Expand Down Expand Up @@ -59,9 +59,9 @@ macro_rules! expand_align {

s_no_extra_traits! {
pub struct pthread_cond_t {
#[cfg(target_env = "musl")]
#[cfg(any(target_env = "musl", target_env = "ohos"))]
__align: [*const ::c_void; 0],
#[cfg(not(target_env = "musl"))]
#[cfg(not(any(target_env = "musl", target_env = "ohos")))]
__align: [::c_longlong; 0],
size: [u8; ::__SIZEOF_PTHREAD_COND_T],
}
Expand Down
Loading

0 comments on commit da4f8f8

Please sign in to comment.