Skip to content

Commit bb6f269

Browse files
committed
Add support for OpenHarmony
1 parent 973c3e1 commit bb6f269

File tree

8 files changed

+104
-46
lines changed

8 files changed

+104
-46
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

+32-5
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;
@@ -3972,17 +3972,25 @@ safe_f! {
39723972
cfg_if! {
39733973
if #[cfg(not(target_env = "uclibc"))] {
39743974
extern "C" {
3975+
#[cfg(not(target_env = "ohos"))]
39753976
pub fn aio_read(aiocbp: *mut aiocb) -> ::c_int;
3977+
#[cfg(not(target_env = "ohos"))]
39763978
pub fn aio_write(aiocbp: *mut aiocb) -> ::c_int;
3979+
#[cfg(not(target_env = "ohos"))]
39773980
pub fn aio_fsync(op: ::c_int, aiocbp: *mut aiocb) -> ::c_int;
3981+
#[cfg(not(target_env = "ohos"))]
39783982
pub fn aio_error(aiocbp: *const aiocb) -> ::c_int;
3983+
#[cfg(not(target_env = "ohos"))]
39793984
pub fn aio_return(aiocbp: *mut aiocb) -> ::ssize_t;
3985+
#[cfg(not(target_env = "ohos"))]
39803986
pub fn aio_suspend(
39813987
aiocb_list: *const *const aiocb,
39823988
nitems: ::c_int,
39833989
timeout: *const ::timespec,
39843990
) -> ::c_int;
3991+
#[cfg(not(target_env = "ohos"))]
39853992
pub fn aio_cancel(fd: ::c_int, aiocbp: *mut aiocb) -> ::c_int;
3993+
#[cfg(not(target_env = "ohos"))]
39863994
pub fn lio_listio(
39873995
mode: ::c_int,
39883996
aiocb_list: *const *mut aiocb,
@@ -4039,7 +4047,10 @@ cfg_if! {
40394047
}
40404048

40414049
extern "C" {
4042-
#[cfg_attr(not(target_env = "musl"), link_name = "__xpg_strerror_r")]
4050+
#[cfg_attr(
4051+
not(any(target_env = "musl", target_env = "ohos")),
4052+
link_name = "__xpg_strerror_r"
4053+
)]
40434054
pub fn strerror_r(errnum: ::c_int, buf: *mut c_char, buflen: ::size_t) -> ::c_int;
40444055

40454056
pub fn abs(i: ::c_int) -> ::c_int;
@@ -4073,6 +4084,7 @@ extern "C" {
40734084
// Only `getspnam_r` is implemented for musl, out of all of the reenterant
40744085
// functions from `shadow.h`.
40754086
// https://git.musl-libc.org/cgit/musl/tree/include/shadow.h
4087+
#[cfg(not(target_env = "ohos"))]
40764088
pub fn getspnam_r(
40774089
name: *const ::c_char,
40784090
spbuf: *mut spwd,
@@ -4081,6 +4093,7 @@ extern "C" {
40814093
spbufp: *mut *mut spwd,
40824094
) -> ::c_int;
40834095

4096+
#[cfg(not(target_env = "ohos"))]
40844097
pub fn shm_open(name: *const c_char, oflag: ::c_int, mode: mode_t) -> ::c_int;
40854098

40864099
// System V IPC
@@ -4187,36 +4200,45 @@ extern "C" {
41874200
id: ::c_int,
41884201
data: *mut ::c_char,
41894202
) -> ::c_int;
4203+
#[cfg(not(target_env = "ohos"))]
41904204
pub fn mq_open(name: *const ::c_char, oflag: ::c_int, ...) -> ::mqd_t;
4205+
#[cfg(not(target_env = "ohos"))]
41914206
pub fn mq_close(mqd: ::mqd_t) -> ::c_int;
4207+
#[cfg(not(target_env = "ohos"))]
41924208
pub fn mq_unlink(name: *const ::c_char) -> ::c_int;
4209+
#[cfg(not(target_env = "ohos"))]
41934210
pub fn mq_receive(
41944211
mqd: ::mqd_t,
41954212
msg_ptr: *mut ::c_char,
41964213
msg_len: ::size_t,
41974214
msg_prio: *mut ::c_uint,
41984215
) -> ::ssize_t;
4216+
#[cfg(not(target_env = "ohos"))]
41994217
pub fn mq_timedreceive(
42004218
mqd: ::mqd_t,
42014219
msg_ptr: *mut ::c_char,
42024220
msg_len: ::size_t,
42034221
msg_prio: *mut ::c_uint,
42044222
abs_timeout: *const ::timespec,
42054223
) -> ::ssize_t;
4224+
#[cfg(not(target_env = "ohos"))]
42064225
pub fn mq_send(
42074226
mqd: ::mqd_t,
42084227
msg_ptr: *const ::c_char,
42094228
msg_len: ::size_t,
42104229
msg_prio: ::c_uint,
42114230
) -> ::c_int;
4231+
#[cfg(not(target_env = "ohos"))]
42124232
pub fn mq_timedsend(
42134233
mqd: ::mqd_t,
42144234
msg_ptr: *const ::c_char,
42154235
msg_len: ::size_t,
42164236
msg_prio: ::c_uint,
42174237
abs_timeout: *const ::timespec,
42184238
) -> ::c_int;
4239+
#[cfg(not(target_env = "ohos"))]
42194240
pub fn mq_getattr(mqd: ::mqd_t, attr: *mut ::mq_attr) -> ::c_int;
4241+
#[cfg(not(target_env = "ohos"))]
42204242
pub fn mq_setattr(mqd: ::mqd_t, newattr: *const ::mq_attr, oldattr: *mut ::mq_attr) -> ::c_int;
42214243
pub fn epoll_pwait(
42224244
epfd: ::c_int,
@@ -4284,6 +4306,7 @@ extern "C" {
42844306

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

4309+
#[cfg(not(target_env = "ohos"))]
42874310
pub fn shm_unlink(name: *const ::c_char) -> ::c_int;
42884311

42894312
pub fn seekdir(dirp: *mut ::DIR, loc: ::c_long);
@@ -4389,6 +4412,7 @@ extern "C" {
43894412
attr: *mut pthread_mutexattr_t,
43904413
protocol: ::c_int,
43914414
) -> ::c_int;
4415+
#[cfg(not(target_env = "ohos"))]
43924416
pub fn pthread_mutex_consistent(mutex: *mut pthread_mutex_t) -> ::c_int;
43934417
pub fn pthread_mutex_timedlock(
43944418
lock: *mut pthread_mutex_t,
@@ -4487,6 +4511,7 @@ extern "C" {
44874511
pub fn pthread_sigmask(how: ::c_int, set: *const sigset_t, oldset: *mut sigset_t) -> ::c_int;
44884512
pub fn sem_open(name: *const ::c_char, oflag: ::c_int, ...) -> *mut sem_t;
44894513
pub fn getgrnam(name: *const ::c_char) -> *mut ::group;
4514+
#[cfg(not(target_env = "ohos"))]
44904515
pub fn pthread_cancel(thread: ::pthread_t) -> ::c_int;
44914516
pub fn pthread_kill(thread: ::pthread_t, sig: ::c_int) -> ::c_int;
44924517
pub fn sem_unlink(name: *const ::c_char) -> ::c_int;
@@ -4522,10 +4547,12 @@ extern "C" {
45224547
attr: *const pthread_mutexattr_t,
45234548
pshared: *mut ::c_int,
45244549
) -> ::c_int;
4550+
#[cfg(not(target_env = "ohos"))]
45254551
pub fn pthread_mutexattr_getrobust(
45264552
attr: *const pthread_mutexattr_t,
45274553
robustness: *mut ::c_int,
45284554
) -> ::c_int;
4555+
#[cfg(not(target_env = "ohos"))]
45294556
pub fn pthread_mutexattr_setrobust(
45304557
attr: *mut pthread_mutexattr_t,
45314558
robustness: ::c_int,
@@ -4729,7 +4756,7 @@ cfg_if! {
47294756
if #[cfg(target_env = "uclibc")] {
47304757
mod uclibc;
47314758
pub use self::uclibc::*;
4732-
} else if #[cfg(target_env = "musl")] {
4759+
} else if #[cfg(any(target_env = "musl", target_env = "ohos"))] {
47334760
mod musl;
47344761
pub use self::musl::*;
47354762
} 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
}

src/unix/linux_like/mod.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ s! {
108108

109109
pub struct sched_param {
110110
pub sched_priority: ::c_int,
111-
#[cfg(any(target_env = "musl", target_os = "emscripten"))]
111+
#[cfg(any(target_env = "musl", target_os = "emscripten", target_env = "ohos"))]
112112
pub sched_ss_low_priority: ::c_int,
113-
#[cfg(any(target_env = "musl", target_os = "emscripten"))]
113+
#[cfg(any(target_env = "musl", target_os = "emscripten", target_env = "ohos"))]
114114
pub sched_ss_repl_period: ::timespec,
115-
#[cfg(any(target_env = "musl", target_os = "emscripten"))]
115+
#[cfg(any(target_env = "musl", target_os = "emscripten", target_env = "ohos"))]
116116
pub sched_ss_init_budget: ::timespec,
117-
#[cfg(any(target_env = "musl", target_os = "emscripten"))]
117+
#[cfg(any(target_env = "musl", target_os = "emscripten", target_env = "ohos"))]
118118
pub sched_ss_max_repl: ::c_int,
119119
}
120120

@@ -557,7 +557,21 @@ pub const XATTR_CREATE: ::c_int = 0x1;
557557
pub const XATTR_REPLACE: ::c_int = 0x2;
558558

559559
cfg_if! {
560-
if #[cfg(not(target_env = "uclibc"))] {
560+
if #[cfg(target_env = "ohos")] {
561+
pub const LC_CTYPE: ::c_int = 0;
562+
pub const LC_NUMERIC: ::c_int = 1;
563+
pub const LC_TIME: ::c_int = 2;
564+
pub const LC_COLLATE: ::c_int = 3;
565+
pub const LC_MONETARY: ::c_int = 4;
566+
pub const LC_MESSAGES: ::c_int = 5;
567+
pub const LC_PAPER: ::c_int = 6;
568+
pub const LC_NAME: ::c_int = 7;
569+
pub const LC_ADDRESS: ::c_int = 8;
570+
pub const LC_TELEPHONE: ::c_int = 9;
571+
pub const LC_MEASUREMENT: ::c_int = 10;
572+
pub const LC_IDENTIFICATION: ::c_int = 11;
573+
pub const LC_ALL: ::c_int = 12;
574+
} else if #[cfg(not(target_env = "uclibc"))] {
561575
pub const LC_CTYPE: ::c_int = 0;
562576
pub const LC_NUMERIC: ::c_int = 1;
563577
pub const LC_TIME: ::c_int = 2;
@@ -973,7 +987,11 @@ pub const TCP_QUICKACK: ::c_int = 12;
973987
pub const TCP_CONGESTION: ::c_int = 13;
974988
pub const TCP_MD5SIG: ::c_int = 14;
975989
cfg_if! {
976-
if #[cfg(all(target_os = "linux", any(target_env = "gnu", target_env = "musl")))] {
990+
if #[cfg(all(target_os = "linux", any(
991+
target_env = "gnu",
992+
target_env = "musl",
993+
target_env = "ohos"
994+
)))] {
977995
// WARN: deprecated
978996
pub const TCP_COOKIE_TRANSACTIONS: ::c_int = 15;
979997
}

0 commit comments

Comments
 (0)