Skip to content

Commit 9872144

Browse files
committed
libc: port apple to use ctest-next
1 parent 627a530 commit 9872144

File tree

4 files changed

+269
-6
lines changed

4 files changed

+269
-6
lines changed

ctest-next/src/template.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ impl TestTemplate {
400400
.as_ref()
401401
.is_some_and(|skip| skip(ident))
402402
};
403-
for func in helper.ffi_items.foreign_functions() {
403+
for func in helper.filtered_ffi_items.foreign_functions() {
404404
if should_skip_fn_test(func.ident()) {
405405
continue;
406406
}
@@ -423,7 +423,7 @@ impl TestTemplate {
423423
&mut self,
424424
helper: &TranslateHelper,
425425
) -> Result<(), TranslationError> {
426-
for static_ in helper.ffi_items.foreign_statics() {
426+
for static_ in helper.filtered_ffi_items.foreign_statics() {
427427
let rust_ty = static_.ty.to_token_stream().to_string().into_boxed_str();
428428

429429
let item = TestForeignStatic {

libc-test/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ name = "ctest_next"
3939
path = "test/ctest_next.rs"
4040
harness = false
4141

42+
[[test]]
43+
name = "main_next"
44+
path = "test/main_next.rs"
45+
harness = false
46+
4247
[[test]]
4348
name = "linux-fcntl"
4449
path = "test/linux_fcntl.rs"

libc-test/build.rs

Lines changed: 259 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn do_cc() {
5252
fn do_ctest() {
5353
match &env::var("TARGET").unwrap() {
5454
t if t.contains("android") => test_android(t),
55-
t if t.contains("apple") => test_apple(t),
55+
t if t.contains("apple") => test_apple_next(t),
5656
t if t.contains("dragonfly") => test_dragonflybsd(t),
5757
t if t.contains("emscripten") => test_emscripten(t),
5858
t if t.contains("freebsd") => test_freebsd(t),
@@ -77,7 +77,6 @@ fn ctest_cfg() -> ctest::TestGenerator {
7777
ctest::TestGenerator::new()
7878
}
7979

80-
#[expect(unused)]
8180
fn ctest_next_cfg() -> ctest_next::TestGenerator {
8281
ctest_next::TestGenerator::new()
8382
}
@@ -221,6 +220,7 @@ macro_rules! headers {
221220
};
222221
}
223222

223+
#[expect(unused)]
224224
fn test_apple(target: &str) {
225225
assert!(target.contains("apple"));
226226
let x86_64 = target.contains("x86_64");
@@ -485,6 +485,263 @@ fn test_apple(target: &str) {
485485
cfg.generate(src_hotfix_dir().join("lib.rs"), "ctest_output.rs");
486486
}
487487

488+
fn test_apple_next(target: &str) {
489+
assert!(target.contains("apple"));
490+
let x86_64 = target.contains("x86_64");
491+
let i686 = target.contains("i686");
492+
493+
let mut cfg = ctest_next_cfg();
494+
cfg.skip_private(true);
495+
cfg.flag("-Wno-deprecated-declarations");
496+
cfg.define("__APPLE_USE_RFC_3542", None);
497+
498+
headers! { cfg:
499+
"aio.h",
500+
"CommonCrypto/CommonCrypto.h",
501+
"CommonCrypto/CommonRandom.h",
502+
"copyfile.h",
503+
"crt_externs.h",
504+
"ctype.h",
505+
"dirent.h",
506+
"dlfcn.h",
507+
"errno.h",
508+
"execinfo.h",
509+
"fcntl.h",
510+
"fnmatch.h",
511+
"getopt.h",
512+
"glob.h",
513+
"grp.h",
514+
"iconv.h",
515+
"ifaddrs.h",
516+
"langinfo.h",
517+
"libgen.h",
518+
"libproc.h",
519+
"limits.h",
520+
"locale.h",
521+
"malloc/malloc.h",
522+
"net/bpf.h",
523+
"net/dlil.h",
524+
"net/if.h",
525+
"net/if_arp.h",
526+
"net/if_dl.h",
527+
"net/if_mib.h",
528+
"net/if_utun.h",
529+
"net/if_var.h",
530+
"net/ndrv.h",
531+
"net/route.h",
532+
"netdb.h",
533+
"netinet/if_ether.h",
534+
"netinet/in.h",
535+
"netinet/ip.h",
536+
"netinet/tcp.h",
537+
"netinet/udp.h",
538+
"netinet6/in6_var.h",
539+
"os/clock.h",
540+
"os/lock.h",
541+
"os/signpost.h",
542+
// FIXME(macos): Requires the macOS 14.4 SDK.
543+
//"os/os_sync_wait_on_address.h",
544+
"poll.h",
545+
"pthread.h",
546+
"pthread_spis.h",
547+
"pthread/introspection.h",
548+
"pthread/spawn.h",
549+
"pthread/stack_np.h",
550+
"pwd.h",
551+
"regex.h",
552+
"resolv.h",
553+
"sched.h",
554+
"semaphore.h",
555+
"signal.h",
556+
"spawn.h",
557+
"stddef.h",
558+
"stdint.h",
559+
"stdio.h",
560+
"stdlib.h",
561+
"string.h",
562+
"sysdir.h",
563+
"sys/appleapiopts.h",
564+
"sys/attr.h",
565+
"sys/clonefile.h",
566+
"sys/event.h",
567+
"sys/file.h",
568+
"sys/ioctl.h",
569+
"sys/ipc.h",
570+
"sys/kern_control.h",
571+
"sys/mman.h",
572+
"sys/mount.h",
573+
"sys/proc_info.h",
574+
"sys/ptrace.h",
575+
"sys/quota.h",
576+
"sys/random.h",
577+
"sys/resource.h",
578+
"sys/sem.h",
579+
"sys/shm.h",
580+
"sys/socket.h",
581+
"sys/stat.h",
582+
"sys/statvfs.h",
583+
"sys/sys_domain.h",
584+
"sys/sysctl.h",
585+
"sys/time.h",
586+
"sys/times.h",
587+
"sys/timex.h",
588+
"sys/types.h",
589+
"sys/uio.h",
590+
"sys/un.h",
591+
"sys/utsname.h",
592+
"sys/vsock.h",
593+
"sys/wait.h",
594+
"sys/xattr.h",
595+
"syslog.h",
596+
"termios.h",
597+
"time.h",
598+
"unistd.h",
599+
"util.h",
600+
"utime.h",
601+
"utmpx.h",
602+
"wchar.h",
603+
"xlocale.h",
604+
[x86_64]: "crt_externs.h",
605+
}
606+
607+
// Skip anonymous unions/structs.
608+
cfg.skip_union(|u| u.ident().starts_with("__c_anonymous_"));
609+
cfg.skip_struct(|s| s.ident().starts_with("__c_anonymous_"));
610+
611+
cfg.skip_struct(|s| {
612+
match s.ident() {
613+
// FIXME(union): actually a union
614+
"sigval" => true,
615+
// FIXME(macos): The size is changed in recent macOSes.
616+
"malloc_zone_t" => true,
617+
// it is a moving target, changing through versions
618+
// also contains bitfields members
619+
"tcp_connection_info" => true,
620+
// FIXME(macos): The size is changed in recent macOSes.
621+
"malloc_introspection_t" => true,
622+
// sonoma changes the padding `rmx_filler` field.
623+
"rt_metrics" => true,
624+
_ => false,
625+
}
626+
});
627+
628+
cfg.skip_alias(|ty| ty.ident().starts_with("__c_anonymous_"));
629+
cfg.skip_alias(|ty| {
630+
match ty.ident() {
631+
// FIXME(macos): Requires the macOS 14.4 SDK.
632+
"os_sync_wake_by_address_flags_t" | "os_sync_wait_on_address_flags_t" => true,
633+
634+
// FIXME(macos): "'__uint128' undeclared" in C
635+
"__uint128" => true,
636+
637+
_ => false,
638+
}
639+
});
640+
641+
cfg.skip_const(move |constant| {
642+
match constant.ident() {
643+
// These OSX constants are removed in Sierra.
644+
// https://developer.apple.com/library/content/releasenotes/General/APIDiffsMacOS10_12/Swift/Darwin.html
645+
"KERN_KDENABLE_BG_TRACE" | "KERN_KDDISABLE_BG_TRACE" => true,
646+
// FIXME(macos): the value has been changed since Catalina (0xffff0000 -> 0x3fff0000).
647+
"SF_SETTABLE" => true,
648+
649+
// FIXME(macos): XCode 13.1 doesn't have it.
650+
"TIOCREMOTE" => true,
651+
652+
// FIXME(macos): Requires the macOS 14.4 SDK.
653+
"OS_SYNC_WAKE_BY_ADDRESS_NONE"
654+
| "OS_SYNC_WAKE_BY_ADDRESS_SHARED"
655+
| "OS_SYNC_WAIT_ON_ADDRESS_NONE"
656+
| "OS_SYNC_WAIT_ON_ADDRESS_SHARED" => true,
657+
658+
_ => false,
659+
}
660+
});
661+
662+
cfg.skip_fn(move |func| {
663+
// skip those that are manually verified
664+
match func.ident() {
665+
// close calls the close_nocancel system call
666+
"close" => true,
667+
668+
// FIXME(1.0): std removed libresolv support: https://github.com/rust-lang/rust/pull/102766
669+
"res_init" => true,
670+
671+
// FIXME(macos): remove once the target in CI is updated
672+
"pthread_jit_write_freeze_callbacks_np" => true,
673+
674+
// FIXME(macos): ABI has been changed on recent macOSes.
675+
"os_unfair_lock_assert_owner" | "os_unfair_lock_assert_not_owner" => true,
676+
677+
// FIXME(macos): Once the SDK get updated to Ventura's level
678+
"freadlink" | "mknodat" | "mkfifoat" => true,
679+
680+
// FIXME(macos): Requires the macOS 14.4 SDK.
681+
"os_sync_wake_by_address_any"
682+
| "os_sync_wake_by_address_all"
683+
| "os_sync_wake_by_address_flags_t"
684+
| "os_sync_wait_on_address"
685+
| "os_sync_wait_on_address_flags_t"
686+
| "os_sync_wait_on_address_with_deadline"
687+
| "os_sync_wait_on_address_with_timeout" => true,
688+
689+
_ => false,
690+
}
691+
});
692+
693+
cfg.skip_struct_field(move |struct_, field| {
694+
match (struct_.ident(), field.ident()) {
695+
// FIXME(union): actually a union
696+
("sigevent", "sigev_value") => true,
697+
// FIXME(macos): the array size has been changed since macOS 10.15 ([8] -> [7]).
698+
("statfs", "f_reserved") => true,
699+
("__darwin_arm_neon_state64", "__v") => true,
700+
701+
("ifreq", "ifr_ifru") => true,
702+
("in6_ifreq", "ifr_ifru") => true,
703+
("ifkpi", "ifk_data") => true,
704+
("ifconf", "ifc_ifcu") => true,
705+
_ => false,
706+
}
707+
});
708+
709+
cfg.volatile_struct_field(|s, f| s.ident() == "aiocb" && f.ident() == "aio_buf");
710+
711+
cfg.rename_struct_ty(move |ty| {
712+
// Just pass all these through, no need for a "struct" prefix
713+
["FILE", "DIR", "Dl_info"]
714+
.contains(&ty)
715+
.then_some(ty.to_string())
716+
});
717+
cfg.rename_type(|ty| (ty == "sighandler_t").then_some("sig_t".to_string()));
718+
cfg.rename_struct_ty(|ty| ty.ends_with("_t").then_some(ty.to_string()));
719+
cfg.rename_union_ty(|ty| ty.ends_with("_t").then_some(ty.to_string()));
720+
721+
cfg.rename_struct_field(|s, f| {
722+
match f.ident() {
723+
n if n.ends_with("_nsec") && s.ident().starts_with("stat") => {
724+
n.replace("e_nsec", "espec.tv_nsec").into()
725+
}
726+
// FIXME(macos): sigaction actually contains a union with two variants:
727+
// a sa_sigaction with type: (*)(int, struct __siginfo *, void *)
728+
// a sa_handler with type sig_t
729+
"sa_sigaction" if s.ident() == "sigaction" => "sa_handler".to_string().into(),
730+
_ => None,
731+
}
732+
});
733+
734+
cfg.skip_roundtrip(move |s| match s {
735+
// FIXME(macos): this type has the wrong ABI
736+
"max_align_t" if i686 => true,
737+
// Can't return an array from a C function.
738+
"uuid_t" | "vol_capabilities_set_t" => true,
739+
_ => false,
740+
});
741+
742+
ctest_next::generate_test(&mut cfg, "../src/lib.rs", "ctest_next.rs").unwrap();
743+
}
744+
488745
fn test_openbsd(target: &str) {
489746
assert!(target.contains("openbsd"));
490747

libc-test/test/ctest_next.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#[allow(deprecated)]
1+
#![allow(deprecated)]
2+
23
#[allow(unused_imports)]
34
use libc::*;
45

5-
include!(concat!(env!("OUT_DIR"), "/main_next.rs"));
6+
include!(concat!(env!("OUT_DIR"), "/ctest_next.rs"));

0 commit comments

Comments
 (0)