Skip to content

Commit da4f8f8

Browse files
committed
Auto merge of #3138 - Amanieu:ohos, r=JohnTitor
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.
2 parents 973c3e1 + 3f5128e commit da4f8f8

File tree

8 files changed

+157
-102
lines changed

8 files changed

+157
-102
lines changed

libc-test/build.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3081,7 +3081,7 @@ fn test_linux(target: &str) {
30813081

30823082
// target_env
30833083
let gnu = target.contains("gnu");
3084-
let musl = target.contains("musl");
3084+
let musl = target.contains("musl") || target.contains("ohos");
30853085
let uclibc = target.contains("uclibc");
30863086

30873087
match (gnu, musl, uclibc) {
@@ -3946,7 +3946,7 @@ fn test_linux(target: &str) {
39463946
// are included (e.g. because including both sets of headers clashes)
39473947
fn test_linux_like_apis(target: &str) {
39483948
let gnu = target.contains("gnu");
3949-
let musl = target.contains("musl");
3949+
let musl = target.contains("musl") || target.contains("ohos");
39503950
let linux = target.contains("linux");
39513951
let emscripten = target.contains("emscripten");
39523952
let android = target.contains("android");

src/unix/linux_like/linux/align.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ macro_rules! expand_align {
2828
size: [u8; ::__SIZEOF_PTHREAD_MUTEXATTR_T],
2929
}
3030

31-
#[cfg_attr(any(target_env = "musl", target_pointer_width = "32"),
31+
#[cfg_attr(any(target_env = "musl", target_env = "ohos", target_pointer_width = "32"),
3232
repr(align(4)))]
3333
#[cfg_attr(all(not(target_env = "musl"),
34+
not(target_env = "ohos"),
3435
target_pointer_width = "64"),
3536
repr(align(8)))]
3637
pub struct pthread_rwlockattr_t {
@@ -63,16 +64,16 @@ macro_rules! expand_align {
6364
}
6465

6566
s_no_extra_traits! {
66-
#[cfg_attr(all(target_env = "musl",
67+
#[cfg_attr(all(any(target_env = "musl", target_env = "ohos"),
6768
target_pointer_width = "32"),
6869
repr(align(4)))]
69-
#[cfg_attr(all(target_env = "musl",
70+
#[cfg_attr(all(any(target_env = "musl", target_env = "ohos"),
7071
target_pointer_width = "64"),
7172
repr(align(8)))]
72-
#[cfg_attr(all(not(target_env = "musl"),
73+
#[cfg_attr(all(not(any(target_env = "musl", target_env = "ohos")),
7374
target_arch = "x86"),
7475
repr(align(4)))]
75-
#[cfg_attr(all(not(target_env = "musl"),
76+
#[cfg_attr(all(not(any(target_env = "musl", target_env = "ohos")),
7677
not(target_arch = "x86")),
7778
repr(align(8)))]
7879
pub struct pthread_cond_t {

src/unix/linux_like/linux/arch/generic/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ cfg_if! {
9595
if #[cfg(all(any(target_arch = "x86",
9696
target_arch = "x86_64",
9797
target_arch = "aarch64"),
98-
not(target_env = "musl")))] {
98+
not(any(target_env = "musl", target_env = "ohos"))))] {
9999
pub const SO_TIMESTAMP_NEW: ::c_int = 63;
100100
pub const SO_TIMESTAMPNS_NEW: ::c_int = 64;
101101
pub const SO_TIMESTAMPING_NEW: ::c_int = 65;
@@ -252,7 +252,7 @@ cfg_if! {
252252
pub const RLIMIT_RTTIME: ::__rlimit_resource_t = 15;
253253
pub const RLIMIT_NLIMITS: ::__rlimit_resource_t = RLIM_NLIMITS;
254254

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

257257
pub const RLIMIT_CPU: ::c_int = 0;
258258
pub const RLIMIT_FSIZE: ::c_int = 1;

src/unix/linux_like/linux/mod.rs

+85-61
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,9 @@ s! {
471471
__pgrp: ::pid_t,
472472
__sd: ::sigset_t,
473473
__ss: ::sigset_t,
474-
#[cfg(target_env = "musl")]
474+
#[cfg(any(target_env = "musl", target_env = "ohos"))]
475475
__prio: ::c_int,
476-
#[cfg(not(target_env = "musl"))]
476+
#[cfg(not(any(target_env = "musl", target_env = "ohos")))]
477477
__sp: ::sched_param,
478478
__policy: ::c_int,
479479
__pad: [::c_int; 16],
@@ -1225,7 +1225,7 @@ cfg_if! {
12251225
}
12261226

12271227
cfg_if! {
1228-
if #[cfg(any(target_env = "gnu", target_env = "musl"))] {
1228+
if #[cfg(any(target_env = "gnu", target_env = "musl", target_env = "ohos"))] {
12291229
pub const ABDAY_1: ::nl_item = 0x20000;
12301230
pub const ABDAY_2: ::nl_item = 0x20001;
12311231
pub const ABDAY_3: ::nl_item = 0x20002;
@@ -3970,7 +3970,7 @@ safe_f! {
39703970
}
39713971

39723972
cfg_if! {
3973-
if #[cfg(not(target_env = "uclibc"))] {
3973+
if #[cfg(all(not(target_env = "uclibc"), not(target_env = "ohos")))] {
39743974
extern "C" {
39753975
pub fn aio_read(aiocbp: *mut aiocb) -> ::c_int;
39763976
pub fn aio_write(aiocbp: *mut aiocb) -> ::c_int;
@@ -3989,6 +3989,13 @@ cfg_if! {
39893989
nitems: ::c_int,
39903990
sevp: *mut ::sigevent,
39913991
) -> ::c_int;
3992+
}
3993+
}
3994+
}
3995+
3996+
cfg_if! {
3997+
if #[cfg(not(target_env = "uclibc"))] {
3998+
extern "C" {
39923999
pub fn pwritev(
39934000
fd: ::c_int,
39944001
iov: *const ::iovec,
@@ -4038,8 +4045,79 @@ cfg_if! {
40384045
}
40394046
}
40404047

4048+
// These functions are not available on OpenHarmony
4049+
cfg_if! {
4050+
if #[cfg(not(target_env = "ohos"))] {
4051+
extern "C" {
4052+
// Only `getspnam_r` is implemented for musl, out of all of the reenterant
4053+
// functions from `shadow.h`.
4054+
// https://git.musl-libc.org/cgit/musl/tree/include/shadow.h
4055+
pub fn getspnam_r(
4056+
name: *const ::c_char,
4057+
spbuf: *mut spwd,
4058+
buf: *mut ::c_char,
4059+
buflen: ::size_t,
4060+
spbufp: *mut *mut spwd,
4061+
) -> ::c_int;
4062+
4063+
pub fn shm_open(name: *const c_char, oflag: ::c_int, mode: mode_t) -> ::c_int;
4064+
pub fn shm_unlink(name: *const ::c_char) -> ::c_int;
4065+
4066+
pub fn mq_open(name: *const ::c_char, oflag: ::c_int, ...) -> ::mqd_t;
4067+
pub fn mq_close(mqd: ::mqd_t) -> ::c_int;
4068+
pub fn mq_unlink(name: *const ::c_char) -> ::c_int;
4069+
pub fn mq_receive(
4070+
mqd: ::mqd_t,
4071+
msg_ptr: *mut ::c_char,
4072+
msg_len: ::size_t,
4073+
msg_prio: *mut ::c_uint,
4074+
) -> ::ssize_t;
4075+
pub fn mq_timedreceive(
4076+
mqd: ::mqd_t,
4077+
msg_ptr: *mut ::c_char,
4078+
msg_len: ::size_t,
4079+
msg_prio: *mut ::c_uint,
4080+
abs_timeout: *const ::timespec,
4081+
) -> ::ssize_t;
4082+
pub fn mq_send(
4083+
mqd: ::mqd_t,
4084+
msg_ptr: *const ::c_char,
4085+
msg_len: ::size_t,
4086+
msg_prio: ::c_uint,
4087+
) -> ::c_int;
4088+
pub fn mq_timedsend(
4089+
mqd: ::mqd_t,
4090+
msg_ptr: *const ::c_char,
4091+
msg_len: ::size_t,
4092+
msg_prio: ::c_uint,
4093+
abs_timeout: *const ::timespec,
4094+
) -> ::c_int;
4095+
pub fn mq_getattr(mqd: ::mqd_t, attr: *mut ::mq_attr) -> ::c_int;
4096+
pub fn mq_setattr(
4097+
mqd: ::mqd_t,
4098+
newattr: *const ::mq_attr,
4099+
oldattr: *mut ::mq_attr
4100+
) -> ::c_int;
4101+
4102+
pub fn pthread_mutex_consistent(mutex: *mut pthread_mutex_t) -> ::c_int;
4103+
pub fn pthread_cancel(thread: ::pthread_t) -> ::c_int;
4104+
pub fn pthread_mutexattr_getrobust(
4105+
attr: *const pthread_mutexattr_t,
4106+
robustness: *mut ::c_int,
4107+
) -> ::c_int;
4108+
pub fn pthread_mutexattr_setrobust(
4109+
attr: *mut pthread_mutexattr_t,
4110+
robustness: ::c_int,
4111+
) -> ::c_int;
4112+
}
4113+
}
4114+
}
4115+
40414116
extern "C" {
4042-
#[cfg_attr(not(target_env = "musl"), link_name = "__xpg_strerror_r")]
4117+
#[cfg_attr(
4118+
not(any(target_env = "musl", target_env = "ohos")),
4119+
link_name = "__xpg_strerror_r"
4120+
)]
40434121
pub fn strerror_r(errnum: ::c_int, buf: *mut c_char, buflen: ::size_t) -> ::c_int;
40444122

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

40724150
pub fn getspnam(name: *const ::c_char) -> *mut spwd;
4073-
// Only `getspnam_r` is implemented for musl, out of all of the reenterant
4074-
// functions from `shadow.h`.
4075-
// https://git.musl-libc.org/cgit/musl/tree/include/shadow.h
4076-
pub fn getspnam_r(
4077-
name: *const ::c_char,
4078-
spbuf: *mut spwd,
4079-
buf: *mut ::c_char,
4080-
buflen: ::size_t,
4081-
spbufp: *mut *mut spwd,
4082-
) -> ::c_int;
4083-
4084-
pub fn shm_open(name: *const c_char, oflag: ::c_int, mode: mode_t) -> ::c_int;
40854151

40864152
// System V IPC
40874153
pub fn shmget(key: ::key_t, size: ::size_t, shmflg: ::c_int) -> ::c_int;
@@ -4187,37 +4253,6 @@ extern "C" {
41874253
id: ::c_int,
41884254
data: *mut ::c_char,
41894255
) -> ::c_int;
4190-
pub fn mq_open(name: *const ::c_char, oflag: ::c_int, ...) -> ::mqd_t;
4191-
pub fn mq_close(mqd: ::mqd_t) -> ::c_int;
4192-
pub fn mq_unlink(name: *const ::c_char) -> ::c_int;
4193-
pub fn mq_receive(
4194-
mqd: ::mqd_t,
4195-
msg_ptr: *mut ::c_char,
4196-
msg_len: ::size_t,
4197-
msg_prio: *mut ::c_uint,
4198-
) -> ::ssize_t;
4199-
pub fn mq_timedreceive(
4200-
mqd: ::mqd_t,
4201-
msg_ptr: *mut ::c_char,
4202-
msg_len: ::size_t,
4203-
msg_prio: *mut ::c_uint,
4204-
abs_timeout: *const ::timespec,
4205-
) -> ::ssize_t;
4206-
pub fn mq_send(
4207-
mqd: ::mqd_t,
4208-
msg_ptr: *const ::c_char,
4209-
msg_len: ::size_t,
4210-
msg_prio: ::c_uint,
4211-
) -> ::c_int;
4212-
pub fn mq_timedsend(
4213-
mqd: ::mqd_t,
4214-
msg_ptr: *const ::c_char,
4215-
msg_len: ::size_t,
4216-
msg_prio: ::c_uint,
4217-
abs_timeout: *const ::timespec,
4218-
) -> ::c_int;
4219-
pub fn mq_getattr(mqd: ::mqd_t, attr: *mut ::mq_attr) -> ::c_int;
4220-
pub fn mq_setattr(mqd: ::mqd_t, newattr: *const ::mq_attr, oldattr: *mut ::mq_attr) -> ::c_int;
42214256
pub fn epoll_pwait(
42224257
epfd: ::c_int,
42234258
events: *mut ::epoll_event,
@@ -4284,8 +4319,6 @@ extern "C" {
42844319

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

4287-
pub fn shm_unlink(name: *const ::c_char) -> ::c_int;
4288-
42894322
pub fn seekdir(dirp: *mut ::DIR, loc: ::c_long);
42904323

42914324
pub fn telldir(dirp: *mut ::DIR) -> ::c_long;
@@ -4389,7 +4422,7 @@ extern "C" {
43894422
attr: *mut pthread_mutexattr_t,
43904423
protocol: ::c_int,
43914424
) -> ::c_int;
4392-
pub fn pthread_mutex_consistent(mutex: *mut pthread_mutex_t) -> ::c_int;
4425+
43934426
pub fn pthread_mutex_timedlock(
43944427
lock: *mut pthread_mutex_t,
43954428
abstime: *const ::timespec,
@@ -4487,7 +4520,6 @@ extern "C" {
44874520
pub fn pthread_sigmask(how: ::c_int, set: *const sigset_t, oldset: *mut sigset_t) -> ::c_int;
44884521
pub fn sem_open(name: *const ::c_char, oflag: ::c_int, ...) -> *mut sem_t;
44894522
pub fn getgrnam(name: *const ::c_char) -> *mut ::group;
4490-
pub fn pthread_cancel(thread: ::pthread_t) -> ::c_int;
44914523
pub fn pthread_kill(thread: ::pthread_t, sig: ::c_int) -> ::c_int;
44924524
pub fn sem_unlink(name: *const ::c_char) -> ::c_int;
44934525
pub fn daemon(nochdir: ::c_int, noclose: ::c_int) -> ::c_int;
@@ -4522,14 +4554,6 @@ extern "C" {
45224554
attr: *const pthread_mutexattr_t,
45234555
pshared: *mut ::c_int,
45244556
) -> ::c_int;
4525-
pub fn pthread_mutexattr_getrobust(
4526-
attr: *const pthread_mutexattr_t,
4527-
robustness: *mut ::c_int,
4528-
) -> ::c_int;
4529-
pub fn pthread_mutexattr_setrobust(
4530-
attr: *mut pthread_mutexattr_t,
4531-
robustness: ::c_int,
4532-
) -> ::c_int;
45334557
pub fn popen(command: *const c_char, mode: *const c_char) -> *mut ::FILE;
45344558
pub fn faccessat(
45354559
dirfd: ::c_int,
@@ -4729,7 +4753,7 @@ cfg_if! {
47294753
if #[cfg(target_env = "uclibc")] {
47304754
mod uclibc;
47314755
pub use self::uclibc::*;
4732-
} else if #[cfg(target_env = "musl")] {
4756+
} else if #[cfg(any(target_env = "musl", target_env = "ohos"))] {
47334757
mod musl;
47344758
pub use self::musl::*;
47354759
} else if #[cfg(target_env = "gnu")] {

src/unix/linux_like/linux/musl/mod.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -287,28 +287,37 @@ s_no_extra_traits! {
287287

288288
// FIXME: musl added paddings and adjusted
289289
// layout in 1.2.0 but our CI is still 1.1.24.
290-
// So, I'm leaving some fields as comments for now.
290+
// So, I'm leaving some fields as cfg for now.
291291
// ref. https://github.com/bminor/musl/commit/
292292
// 1e7f0fcd7ff2096904fd93a2ee6d12a2392be392
293+
//
294+
// OpenHarmony uses the musl 1.2 layout.
293295
pub struct utmpx {
294296
pub ut_type: ::c_short,
295-
//__ut_pad1: ::c_short,
297+
__ut_pad1: ::c_short,
296298
pub ut_pid: ::pid_t,
297299
pub ut_line: [::c_char; 32],
298300
pub ut_id: [::c_char; 4],
299301
pub ut_user: [::c_char; 32],
300302
pub ut_host: [::c_char; 256],
301303
pub ut_exit: __exit_status,
302304

303-
//#[cfg(target_endian = "little")]
305+
#[cfg(target_env = "musl")]
304306
pub ut_session: ::c_long,
305-
//#[cfg(target_endian = "little")]
306-
//__ut_pad2: ::c_long,
307307

308-
//#[cfg(not(target_endian = "little"))]
309-
//__ut_pad2: ::c_int,
310-
//#[cfg(not(target_endian = "little"))]
311-
//pub ut_session: ::c_int,
308+
#[cfg(target_env = "ohos")]
309+
#[cfg(target_endian = "little")]
310+
pub ut_session: ::c_int,
311+
#[cfg(target_env = "ohos")]
312+
#[cfg(target_endian = "little")]
313+
__ut_pad2: ::c_int,
314+
315+
#[cfg(target_env = "ohos")]
316+
#[cfg(not(target_endian = "little"))]
317+
__ut_pad2: ::c_int,
318+
#[cfg(target_env = "ohos")]
319+
#[cfg(not(target_endian = "little"))]
320+
pub ut_session: ::c_int,
312321

313322
pub ut_tv: ::timeval,
314323
pub ut_addr_v6: [::c_uint; 4],

src/unix/linux_like/linux/no_align.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ macro_rules! expand_align {
1111
target_arch = "riscv32",
1212
target_arch = "loongarch64",
1313
all(target_arch = "aarch64",
14-
target_env = "musl")))]
14+
any(target_env = "musl", target_env = "ohos"))))]
1515
__align: [::c_int; 0],
1616
#[cfg(not(any(target_arch = "x86_64",
1717
target_arch = "powerpc64",
@@ -22,15 +22,15 @@ macro_rules! expand_align {
2222
target_arch = "riscv32",
2323
target_arch = "loongarch64",
2424
all(target_arch = "aarch64",
25-
target_env = "musl"))))]
25+
any(target_env = "musl", target_env = "ohos")))))]
2626
__align: [::c_long; 0],
2727
size: [u8; ::__SIZEOF_PTHREAD_MUTEXATTR_T],
2828
}
2929

3030
pub struct pthread_rwlockattr_t {
31-
#[cfg(target_env = "musl")]
31+
#[cfg(any(target_env = "musl", target_env = "ohos"))]
3232
__align: [::c_int; 0],
33-
#[cfg(not(target_env = "musl"))]
33+
#[cfg(not(any(target_env = "musl", target_env = "ohos")))]
3434
__align: [::c_long; 0],
3535
size: [u8; ::__SIZEOF_PTHREAD_RWLOCKATTR_T],
3636
}
@@ -59,9 +59,9 @@ macro_rules! expand_align {
5959

6060
s_no_extra_traits! {
6161
pub struct pthread_cond_t {
62-
#[cfg(target_env = "musl")]
62+
#[cfg(any(target_env = "musl", target_env = "ohos"))]
6363
__align: [*const ::c_void; 0],
64-
#[cfg(not(target_env = "musl"))]
64+
#[cfg(not(any(target_env = "musl", target_env = "ohos")))]
6565
__align: [::c_longlong; 0],
6666
size: [u8; ::__SIZEOF_PTHREAD_COND_T],
6767
}

0 commit comments

Comments
 (0)