xref: /rust-libc-0.2.174/libc-test/build.rs (revision f7f06565)
1 #![deny(warnings)]
2 
3 extern crate cc;
4 extern crate ctest2 as ctest;
5 
6 use std::fs::File;
7 use std::io::{BufRead, BufReader, BufWriter, Write};
8 use std::path::{Path, PathBuf};
9 use std::{env, io};
10 
11 fn do_cc() {
12     let target = env::var("TARGET").unwrap();
13     if cfg!(unix) {
14         let exclude = ["redox", "wasi"];
15         if !exclude.iter().any(|x| target.contains(x)) {
16             let mut cmsg = cc::Build::new();
17 
18             cmsg.file("src/cmsg.c");
19 
20             if target.contains("solaris") || target.contains("illumos") {
21                 cmsg.define("_XOPEN_SOURCE", "700");
22             }
23             cmsg.compile("cmsg");
24         }
25     }
26     if target.contains("android") || target.contains("linux") {
27         cc::Build::new().file("src/errqueue.c").compile("errqueue");
28     }
29     if target.contains("linux")
30         || target.contains("l4re")
31         || target.contains("android")
32         || target.contains("emscripten")
33     {
34         cc::Build::new().file("src/sigrt.c").compile("sigrt");
35     }
36 }
37 
38 fn do_ctest() {
39     match &env::var("TARGET").unwrap() {
40         t if t.contains("android") => return test_android(t),
41         t if t.contains("apple") => return test_apple(t),
42         t if t.contains("dragonfly") => return test_dragonflybsd(t),
43         t if t.contains("emscripten") => return test_emscripten(t),
44         t if t.contains("freebsd") => return test_freebsd(t),
45         t if t.contains("haiku") => return test_haiku(t),
46         t if t.contains("linux") => return test_linux(t),
47         t if t.contains("netbsd") => return test_netbsd(t),
48         t if t.contains("openbsd") => return test_openbsd(t),
49         t if t.contains("redox") => return test_redox(t),
50         t if t.contains("solaris") => return test_solarish(t),
51         t if t.contains("illumos") => return test_solarish(t),
52         t if t.contains("wasi") => return test_wasi(t),
53         t if t.contains("windows") => return test_windows(t),
54         t if t.contains("vxworks") => return test_vxworks(t),
55         t => panic!("unknown target {}", t),
56     }
57 }
58 
59 fn ctest_cfg() -> ctest::TestGenerator {
60     let mut cfg = ctest::TestGenerator::new();
61     let libc_cfgs = [
62         "libc_priv_mod_use",
63         "libc_union",
64         "libc_const_size_of",
65         "libc_align",
66         "libc_core_cvoid",
67         "libc_packedN",
68         "libc_thread_local",
69     ];
70     for f in &libc_cfgs {
71         cfg.cfg(f, None);
72     }
73     cfg
74 }
75 
76 fn do_semver() {
77     let mut out = PathBuf::from(env::var("OUT_DIR").unwrap());
78     out.push("semver.rs");
79     let mut output = BufWriter::new(File::create(&out).unwrap());
80 
81     let family = env::var("CARGO_CFG_TARGET_FAMILY").unwrap();
82     let vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
83     let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
84     let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
85     let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
86 
87     // `libc-test/semver` dir.
88     let mut semver_root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
89     semver_root.push("semver");
90 
91     // NOTE: Windows has the same `family` as `os`, no point in including it
92     // twice.
93     // NOTE: Android doesn't include the unix file (or the Linux file) because
94     // there are some many definitions missing it's actually easier just to
95     // maintain a file for Android.
96     if family != os && os != "android" {
97         process_semver_file(&mut output, &mut semver_root, &family);
98     }
99     process_semver_file(&mut output, &mut semver_root, &vendor);
100     process_semver_file(&mut output, &mut semver_root, &os);
101     let os_arch = format!("{}-{}", os, arch);
102     process_semver_file(&mut output, &mut semver_root, &os_arch);
103     if target_env != "" {
104         let os_env = format!("{}-{}", os, target_env);
105         process_semver_file(&mut output, &mut semver_root, &os_env);
106 
107         let os_env_arch = format!("{}-{}-{}", os, target_env, arch);
108         process_semver_file(&mut output, &mut semver_root, &os_env_arch);
109     }
110 }
111 
112 fn process_semver_file<W: Write, P: AsRef<Path>>(output: &mut W, path: &mut PathBuf, file: P) {
113     // NOTE: `path` is reused between calls, so always remove the file again.
114     path.push(file);
115     path.set_extension("txt");
116 
117     println!("cargo:rerun-if-changed={}", path.display());
118     let input_file = match File::open(&*path) {
119         Ok(file) => file,
120         Err(ref err) if err.kind() == io::ErrorKind::NotFound => {
121             path.pop();
122             return;
123         }
124         Err(err) => panic!("unexpected error opening file: {}", err),
125     };
126     let input = BufReader::new(input_file);
127 
128     write!(output, "// Source: {}.\n", path.display()).unwrap();
129     output.write(b"use libc::{\n").unwrap();
130     for line in input.lines() {
131         let line = line.unwrap().into_bytes();
132         match line.first() {
133             // Ignore comments and empty lines.
134             Some(b'#') | None => continue,
135             _ => {
136                 output.write(b"    ").unwrap();
137                 output.write(&line).unwrap();
138                 output.write(b",\n").unwrap();
139             }
140         }
141     }
142     output.write(b"};\n\n").unwrap();
143     path.pop();
144 }
145 
146 fn main() {
147     do_cc();
148     do_ctest();
149     do_semver();
150 }
151 
152 macro_rules! headers {
153     ($cfg:ident: [$m:expr]: $header:literal) => {
154         if $m {
155             $cfg.header($header);
156         }
157     };
158     ($cfg:ident: $header:literal) => {
159         $cfg.header($header);
160     };
161     ($($cfg:ident: $([$c:expr]:)* $header:literal,)*) => {
162         $(headers!($cfg: $([$c]:)* $header);)*
163     };
164     ($cfg:ident: $( $([$c:expr]:)* $header:literal,)*) => {
165         headers!($($cfg: $([$c]:)* $header,)*);
166     };
167     ($cfg:ident: $( $([$c:expr]:)* $header:literal),*) => {
168         headers!($($cfg: $([$c]:)* $header,)*);
169     };
170 }
171 
172 fn test_apple(target: &str) {
173     assert!(target.contains("apple"));
174     let x86_64 = target.contains("x86_64");
175     let i686 = target.contains("i686");
176 
177     let mut cfg = ctest_cfg();
178     cfg.flag("-Wno-deprecated-declarations");
179     cfg.define("__APPLE_USE_RFC_3542", None);
180 
181     headers! { cfg:
182         "aio.h",
183         "CommonCrypto/CommonCrypto.h",
184         "CommonCrypto/CommonRandom.h",
185         "crt_externs.h",
186         "ctype.h",
187         "dirent.h",
188         "dlfcn.h",
189         "errno.h",
190         "execinfo.h",
191         "fcntl.h",
192         "glob.h",
193         "grp.h",
194         "iconv.h",
195         "ifaddrs.h",
196         "langinfo.h",
197         "libproc.h",
198         "limits.h",
199         "locale.h",
200         "mach-o/dyld.h",
201         "mach/mach_init.h",
202         "mach/mach.h",
203         "mach/mach_time.h",
204         "mach/mach_types.h",
205         "mach/mach_vm.h",
206         "mach/thread_act.h",
207         "mach/thread_policy.h",
208         "malloc/malloc.h",
209         "net/bpf.h",
210         "net/if.h",
211         "net/if_arp.h",
212         "net/if_dl.h",
213         "net/if_utun.h",
214         "net/route.h",
215         "netdb.h",
216         "netinet/if_ether.h",
217         "netinet/in.h",
218         "netinet/ip.h",
219         "netinet/tcp.h",
220         "netinet/udp.h",
221         "poll.h",
222         "pthread.h",
223         "pthread_spis.h",
224         "pthread/introspection.h",
225         "pwd.h",
226         "regex.h",
227         "resolv.h",
228         "sched.h",
229         "semaphore.h",
230         "signal.h",
231         "spawn.h",
232         "stddef.h",
233         "stdint.h",
234         "stdio.h",
235         "stdlib.h",
236         "string.h",
237         "sysdir.h",
238         "sys/attr.h",
239         "sys/clonefile.h",
240         "sys/event.h",
241         "sys/file.h",
242         "sys/ioctl.h",
243         "sys/ipc.h",
244         "sys/kern_control.h",
245         "sys/mman.h",
246         "sys/mount.h",
247         "sys/proc_info.h",
248         "sys/ptrace.h",
249         "sys/quota.h",
250         "sys/resource.h",
251         "sys/sem.h",
252         "sys/shm.h",
253         "sys/socket.h",
254         "sys/stat.h",
255         "sys/statvfs.h",
256         "sys/sys_domain.h",
257         "sys/sysctl.h",
258         "sys/time.h",
259         "sys/times.h",
260         "sys/timex.h",
261         "sys/types.h",
262         "sys/uio.h",
263         "sys/un.h",
264         "sys/utsname.h",
265         "sys/wait.h",
266         "sys/xattr.h",
267         "syslog.h",
268         "termios.h",
269         "time.h",
270         "unistd.h",
271         "util.h",
272         "utime.h",
273         "utmpx.h",
274         "wchar.h",
275         "xlocale.h",
276         [x86_64]: "crt_externs.h",
277     }
278 
279     cfg.skip_struct(move |ty| {
280         match ty {
281             // FIXME: actually a union
282             "sigval" => true,
283 
284             _ => false,
285         }
286     });
287 
288     cfg.skip_type(move |ty| match ty {
289         _ => false,
290     });
291 
292     cfg.skip_const(move |name| {
293         // They're declared via `deprecated_mach` and we don't support it anymore.
294         if name.starts_with("VM_FLAGS_") {
295             return true;
296         }
297         match name {
298             // These OSX constants are removed in Sierra.
299             // https://developer.apple.com/library/content/releasenotes/General/APIDiffsMacOS10_12/Swift/Darwin.html
300             "KERN_KDENABLE_BG_TRACE" | "KERN_KDDISABLE_BG_TRACE" => true,
301             // FIXME: the value has been changed since Catalina (0xffff0000 -> 0x3fff0000).
302             "SF_SETTABLE" => true,
303 
304             // FIXME: XCode 13.1 doesn't have it.
305             "TIOCREMOTE" => true,
306             _ => false,
307         }
308     });
309 
310     cfg.skip_fn(move |name| {
311         // skip those that are manually verified
312         match name {
313             // FIXME: https://github.com/rust-lang/libc/issues/1272
314             "execv" | "execve" | "execvp" => true,
315 
316             // close calls the close_nocancel system call
317             "close" => true,
318 
319             // macOs 12 minimum
320             "backtrace_async" => true,
321 
322             _ => false,
323         }
324     });
325 
326     cfg.skip_field(move |struct_, field| {
327         match (struct_, field) {
328             // FIXME: the array size has been changed since macOS 10.15 ([8] -> [7]).
329             ("statfs", "f_reserved") => true,
330             ("__darwin_arm_neon_state64", "__v") => true,
331             // MAXPATHLEN is too big for auto-derive traits on arrays.
332             ("vnode_info_path", "vip_path") => true,
333             _ => false,
334         }
335     });
336 
337     cfg.skip_field_type(move |struct_, field| {
338         match (struct_, field) {
339             // FIXME: actually a union
340             ("sigevent", "sigev_value") => true,
341             _ => false,
342         }
343     });
344 
345     cfg.volatile_item(|i| {
346         use ctest::VolatileItemKind::*;
347         match i {
348             StructField(ref n, ref f) if n == "aiocb" && f == "aio_buf" => true,
349             _ => false,
350         }
351     });
352 
353     cfg.type_name(move |ty, is_struct, is_union| {
354         match ty {
355             // Just pass all these through, no need for a "struct" prefix
356             "FILE" | "DIR" | "Dl_info" => ty.to_string(),
357 
358             // OSX calls this something else
359             "sighandler_t" => "sig_t".to_string(),
360 
361             t if is_union => format!("union {}", t),
362             t if t.ends_with("_t") => t.to_string(),
363             t if is_struct => format!("struct {}", t),
364             t => t.to_string(),
365         }
366     });
367 
368     cfg.field_name(move |struct_, field| {
369         match field {
370             s if s.ends_with("_nsec") && struct_.starts_with("stat") => {
371                 s.replace("e_nsec", "espec.tv_nsec")
372             }
373             // FIXME: sigaction actually contains a union with two variants:
374             // a sa_sigaction with type: (*)(int, struct __siginfo *, void *)
375             // a sa_handler with type sig_t
376             "sa_sigaction" if struct_ == "sigaction" => "sa_handler".to_string(),
377             s => s.to_string(),
378         }
379     });
380 
381     cfg.skip_roundtrip(move |s| match s {
382         // FIXME: this type has the wrong ABI
383         "max_align_t" if i686 => true,
384         // Can't return an array from a C function.
385         "uuid_t" => true,
386         _ => false,
387     });
388     cfg.generate("../src/lib.rs", "main.rs");
389 }
390 
391 fn test_openbsd(target: &str) {
392     assert!(target.contains("openbsd"));
393 
394     let mut cfg = ctest_cfg();
395     cfg.flag("-Wno-deprecated-declarations");
396 
397     let x86_64 = target.contains("x86_64");
398 
399     headers! { cfg:
400         "elf.h",
401         "errno.h",
402         "execinfo.h",
403         "fcntl.h",
404         "limits.h",
405         "link.h",
406         "locale.h",
407         "stddef.h",
408         "stdint.h",
409         "stdio.h",
410         "stdlib.h",
411         "sys/stat.h",
412         "sys/types.h",
413         "time.h",
414         "wchar.h",
415         "ctype.h",
416         "dirent.h",
417         "sys/socket.h",
418         [x86_64]:"machine/fpu.h",
419         "net/if.h",
420         "net/route.h",
421         "net/if_arp.h",
422         "netdb.h",
423         "netinet/in.h",
424         "netinet/ip.h",
425         "netinet/tcp.h",
426         "netinet/udp.h",
427         "net/bpf.h",
428         "regex.h",
429         "resolv.h",
430         "pthread.h",
431         "dlfcn.h",
432         "signal.h",
433         "string.h",
434         "sys/file.h",
435         "sys/ioctl.h",
436         "sys/ipc.h",
437         "sys/mman.h",
438         "sys/param.h",
439         "sys/resource.h",
440         "sys/shm.h",
441         "sys/socket.h",
442         "sys/time.h",
443         "sys/uio.h",
444         "sys/ktrace.h",
445         "sys/un.h",
446         "sys/wait.h",
447         "unistd.h",
448         "utime.h",
449         "pwd.h",
450         "grp.h",
451         "sys/utsname.h",
452         "sys/ptrace.h",
453         "sys/mount.h",
454         "sys/uio.h",
455         "sched.h",
456         "termios.h",
457         "poll.h",
458         "syslog.h",
459         "semaphore.h",
460         "sys/statvfs.h",
461         "sys/times.h",
462         "glob.h",
463         "ifaddrs.h",
464         "langinfo.h",
465         "sys/sysctl.h",
466         "utmp.h",
467         "sys/event.h",
468         "net/if_dl.h",
469         "util.h",
470         "ufs/ufs/quota.h",
471         "pthread_np.h",
472         "sys/syscall.h",
473         "sys/shm.h",
474         "sys/param.h",
475     }
476 
477     cfg.skip_struct(move |ty| {
478         match ty {
479             // FIXME: actually a union
480             "sigval" => true,
481 
482             _ => false,
483         }
484     });
485 
486     cfg.skip_const(move |name| {
487         match name {
488             // Removed in OpenBSD 6.0
489             "KERN_USERMOUNT" | "KERN_ARND" => true,
490             // Good chance it's going to be wrong depending on the host release
491             "KERN_MAXID" | "NET_RT_MAXID" => true,
492             "EV_SYSFLAGS" => true,
493             _ => false,
494         }
495     });
496 
497     cfg.skip_fn(move |name| {
498         match name {
499             // FIXME: https://github.com/rust-lang/libc/issues/1272
500             "execv" | "execve" | "execvp" | "execvpe" => true,
501 
502             // Removed in OpenBSD 6.5
503             // https://marc.info/?l=openbsd-cvs&m=154723400730318
504             "mincore" => true,
505 
506             _ => false,
507         }
508     });
509 
510     cfg.type_name(move |ty, is_struct, is_union| {
511         match ty {
512             // Just pass all these through, no need for a "struct" prefix
513             "FILE" | "DIR" | "Dl_info" | "Elf32_Phdr" | "Elf64_Phdr" => ty.to_string(),
514 
515             // OSX calls this something else
516             "sighandler_t" => "sig_t".to_string(),
517 
518             t if is_union => format!("union {}", t),
519             t if t.ends_with("_t") => t.to_string(),
520             t if is_struct => format!("struct {}", t),
521             t => t.to_string(),
522         }
523     });
524 
525     cfg.field_name(move |struct_, field| match field {
526         "st_birthtime" if struct_.starts_with("stat") => "__st_birthtime".to_string(),
527         "st_birthtime_nsec" if struct_.starts_with("stat") => "__st_birthtimensec".to_string(),
528         s if s.ends_with("_nsec") && struct_.starts_with("stat") => s.replace("e_nsec", ".tv_nsec"),
529         "sa_sigaction" if struct_ == "sigaction" => "sa_handler".to_string(),
530         s => s.to_string(),
531     });
532 
533     cfg.skip_field_type(move |struct_, field| {
534         // type siginfo_t.si_addr changed from OpenBSD 6.0 to 6.1
535         struct_ == "siginfo_t" && field == "si_addr"
536     });
537 
538     cfg.skip_field(|struct_, field| {
539         match (struct_, field) {
540             // conflicting with `p_type` macro from <resolve.h>.
541             ("Elf32_Phdr", "p_type") => true,
542             ("Elf64_Phdr", "p_type") => true,
543             _ => false,
544         }
545     });
546 
547     cfg.generate("../src/lib.rs", "main.rs");
548 }
549 
550 fn test_windows(target: &str) {
551     assert!(target.contains("windows"));
552     let gnu = target.contains("gnu");
553 
554     let mut cfg = ctest_cfg();
555     if target.contains("msvc") {
556         cfg.flag("/wd4324");
557     }
558     cfg.define("_WIN32_WINNT", Some("0x8000"));
559 
560     headers! { cfg:
561         "direct.h",
562         "errno.h",
563         "fcntl.h",
564         "io.h",
565         "limits.h",
566         "locale.h",
567         "process.h",
568         "signal.h",
569         "stddef.h",
570         "stdint.h",
571         "stdio.h",
572         "stdlib.h",
573         "sys/stat.h",
574         "sys/types.h",
575         "sys/utime.h",
576         "time.h",
577         "wchar.h",
578         [gnu]: "ws2tcpip.h",
579         [!gnu]: "Winsock2.h",
580     }
581 
582     cfg.type_name(move |ty, is_struct, is_union| {
583         match ty {
584             // Just pass all these through, no need for a "struct" prefix
585             "FILE" | "DIR" | "Dl_info" => ty.to_string(),
586 
587             // FIXME: these don't exist:
588             "time64_t" => "__time64_t".to_string(),
589             "ssize_t" => "SSIZE_T".to_string(),
590 
591             "sighandler_t" if !gnu => "_crt_signal_t".to_string(),
592             "sighandler_t" if gnu => "__p_sig_fn_t".to_string(),
593 
594             t if is_union => format!("union {}", t),
595             t if t.ends_with("_t") => t.to_string(),
596 
597             // Windows uppercase structs don't have `struct` in front:
598             t if is_struct => {
599                 if ty.clone().chars().next().unwrap().is_uppercase() {
600                     t.to_string()
601                 } else if t == "stat" {
602                     "struct __stat64".to_string()
603                 } else if t == "utimbuf" {
604                     "struct __utimbuf64".to_string()
605                 } else {
606                     // put `struct` in front of all structs:
607                     format!("struct {}", t)
608                 }
609             }
610             t => t.to_string(),
611         }
612     });
613 
614     cfg.fn_cname(move |name, cname| cname.unwrap_or(name).to_string());
615 
616     cfg.skip_type(move |name| match name {
617         "SSIZE_T" if !gnu => true,
618         "ssize_t" if !gnu => true,
619         _ => false,
620     });
621 
622     cfg.skip_struct(move |ty| {
623         if ty.starts_with("__c_anonymous_") {
624             return true;
625         }
626         return false;
627     });
628 
629     cfg.skip_const(move |name| {
630         match name {
631             // FIXME: API error:
632             // SIG_ERR type is "void (*)(int)", not "int"
633             "SIG_ERR" |
634             // Similar for SIG_DFL/IGN/GET/SGE/ACK
635             "SIG_DFL" | "SIG_IGN" | "SIG_GET" | "SIG_SGE" | "SIG_ACK" => true,
636             // FIXME: newer windows-gnu environment on CI?
637             "_O_OBTAIN_DIR" if gnu => true,
638             _ => false,
639         }
640     });
641 
642     cfg.skip_field(move |s, field| match s {
643         "CONTEXT" if field == "Fp" => true,
644         _ => false,
645     });
646     // FIXME: All functions point to the wrong addresses?
647     cfg.skip_fn_ptrcheck(|_| true);
648 
649     cfg.skip_signededness(move |c| {
650         match c {
651             // windows-isms
652             n if n.starts_with("P") => true,
653             n if n.starts_with("H") => true,
654             n if n.starts_with("LP") => true,
655             "sighandler_t" if gnu => true,
656             _ => false,
657         }
658     });
659 
660     cfg.skip_fn(move |name| {
661         match name {
662             // FIXME: https://github.com/rust-lang/libc/issues/1272
663             "execv" | "execve" | "execvp" | "execvpe" => true,
664 
665             _ => false,
666         }
667     });
668 
669     cfg.generate("../src/lib.rs", "main.rs");
670 }
671 
672 fn test_redox(target: &str) {
673     assert!(target.contains("redox"));
674 
675     let mut cfg = ctest_cfg();
676     cfg.flag("-Wno-deprecated-declarations");
677 
678     headers! {
679         cfg:
680         "ctype.h",
681         "dirent.h",
682         "dlfcn.h",
683         "errno.h",
684         "fcntl.h",
685         "grp.h",
686         "limits.h",
687         "locale.h",
688         "netdb.h",
689         "netinet/in.h",
690         "netinet/ip.h",
691         "netinet/tcp.h",
692         "poll.h",
693         "pwd.h",
694         "semaphore.h",
695         "string.h",
696         "strings.h",
697         "sys/file.h",
698         "sys/ioctl.h",
699         "sys/mman.h",
700         "sys/ptrace.h",
701         "sys/resource.h",
702         "sys/socket.h",
703         "sys/stat.h",
704         "sys/statvfs.h",
705         "sys/time.h",
706         "sys/types.h",
707         "sys/uio.h",
708         "sys/un.h",
709         "sys/utsname.h",
710         "sys/wait.h",
711         "termios.h",
712         "time.h",
713         "unistd.h",
714         "utime.h",
715         "wchar.h",
716     }
717 
718     cfg.generate("../src/lib.rs", "main.rs");
719 }
720 
721 fn test_solarish(target: &str) {
722     let is_solaris = target.contains("solaris");
723     let is_illumos = target.contains("illumos");
724     assert!(is_solaris || is_illumos);
725 
726     // ctest generates arguments supported only by clang, so make sure to run with CC=clang.
727     // While debugging, "CFLAGS=-ferror-limit=<large num>" is useful to get more error output.
728     let mut cfg = ctest_cfg();
729     cfg.flag("-Wno-deprecated-declarations");
730 
731     cfg.define("_XOPEN_SOURCE", Some("700"));
732     cfg.define("__EXTENSIONS__", None);
733     cfg.define("_LCONV_C99", None);
734 
735     headers! {
736         cfg:
737         "ctype.h",
738         "dirent.h",
739         "dlfcn.h",
740         "door.h",
741         "errno.h",
742         "execinfo.h",
743         "fcntl.h",
744         "glob.h",
745         "grp.h",
746         "ifaddrs.h",
747         "langinfo.h",
748         "limits.h",
749         "locale.h",
750         "mqueue.h",
751         "net/if.h",
752         "net/if_arp.h",
753         "net/route.h",
754         "netdb.h",
755         "netinet/in.h",
756         "netinet/ip.h",
757         "netinet/tcp.h",
758         "netinet/udp.h",
759         "poll.h",
760         "port.h",
761         "pthread.h",
762         "pwd.h",
763         "resolv.h",
764         "sched.h",
765         "semaphore.h",
766         "signal.h",
767         "stddef.h",
768         "stdint.h",
769         "stdio.h",
770         "stdlib.h",
771         "string.h",
772         "sys/epoll.h",
773         "sys/eventfd.h",
774         "sys/file.h",
775         "sys/filio.h",
776         "sys/ioctl.h",
777         "sys/loadavg.h",
778         "sys/mman.h",
779         "sys/mount.h",
780         "sys/priv.h",
781         "sys/pset.h",
782         "sys/resource.h",
783         "sys/socket.h",
784         "sys/stat.h",
785         "sys/statvfs.h",
786         "sys/stropts.h",
787         "sys/shm.h",
788         "sys/time.h",
789         "sys/times.h",
790         "sys/timex.h",
791         "sys/types.h",
792         "sys/uio.h",
793         "sys/un.h",
794         "sys/utsname.h",
795         "sys/wait.h",
796         "syslog.h",
797         "termios.h",
798         "thread.h",
799         "time.h",
800         "ucontext.h",
801         "unistd.h",
802         "utime.h",
803         "utmpx.h",
804         "wchar.h",
805     }
806 
807     cfg.skip_type(move |ty| {
808         match ty {
809             // sighandler_t is not present here
810             "sighandler_t" => true,
811             _ => false,
812         }
813     });
814 
815     cfg.type_name(move |ty, is_struct, is_union| match ty {
816         "FILE" => "__FILE".to_string(),
817         "DIR" | "Dl_info" => ty.to_string(),
818         t if t.ends_with("_t") => t.to_string(),
819         t if is_struct => format!("struct {}", t),
820         t if is_union => format!("union {}", t),
821         t => t.to_string(),
822     });
823 
824     cfg.field_name(move |struct_, field| {
825         match struct_ {
826             // rust struct uses raw u64, rather than union
827             "epoll_event" if field == "u64" => "data.u64".to_string(),
828             // rust struct was committed with typo for Solaris
829             "door_arg_t" if field == "dec_num" => "desc_num".to_string(),
830             "stat" if field.ends_with("_nsec") => {
831                 // expose stat.Xtim.tv_nsec fields
832                 field.trim_end_matches("e_nsec").to_string() + ".tv_nsec"
833             }
834             _ => field.to_string(),
835         }
836     });
837 
838     cfg.skip_const(move |name| match name {
839         "DT_FIFO" | "DT_CHR" | "DT_DIR" | "DT_BLK" | "DT_REG" | "DT_LNK" | "DT_SOCK"
840         | "USRQUOTA" | "GRPQUOTA" | "PRIO_MIN" | "PRIO_MAX" => true,
841 
842         // skip sighandler_t assignments
843         "SIG_DFL" | "SIG_ERR" | "SIG_IGN" => true,
844 
845         "DT_UNKNOWN" => true,
846 
847         "_UTX_LINESIZE" | "_UTX_USERSIZE" | "_UTX_PADSIZE" | "_UTX_IDSIZE" | "_UTX_HOSTSIZE" => {
848             true
849         }
850 
851         "EADI" | "EXTPROC" | "IPC_SEAT" => true,
852 
853         // This evaluates to a sysconf() call rather than a constant
854         "PTHREAD_STACK_MIN" => true,
855 
856         // EPOLLEXCLUSIVE is a relatively recent addition to the epoll interface and may not be
857         // defined on older systems.  It is, however, safe to use on systems which do not
858         // explicitly support it. (A no-op is an acceptable implementation of EPOLLEXCLUSIVE.)
859         "EPOLLEXCLUSIVE" => true,
860 
861         _ => false,
862     });
863 
864     cfg.skip_struct(move |ty| {
865         if ty.starts_with("__c_anonymous_") {
866             return true;
867         }
868         // the union handling is a mess
869         if ty.contains("door_desc_t_") {
870             return true;
871         }
872         match ty {
873             // union, not a struct
874             "sigval" => true,
875             // a bunch of solaris-only fields
876             "utmpx" if is_illumos => true,
877             _ => false,
878         }
879     });
880 
881     cfg.skip_field(move |s, field| {
882         match s {
883             // C99 sizing on this is tough
884             "dirent" if field == "d_name" => true,
885             // the union/macro makes this rough
886             "sigaction" if field == "sa_sigaction" => true,
887             // Missing in illumos
888             "sigevent" if field == "ss_sp" => true,
889             // Avoid sigval union issues
890             "sigevent" if field == "sigev_value" => true,
891             // const issues
892             "sigevent" if field == "sigev_notify_attributes" => true,
893 
894             // Avoid const and union issues
895             "door_arg" if field == "desc_ptr" => true,
896             "door_desc_t" if field == "d_data" => true,
897             "door_arg_t" if field.ends_with("_ptr") => true,
898             "door_arg_t" if field.ends_with("rbuf") => true,
899 
900             _ => false,
901         }
902     });
903 
904     cfg.skip_fn(move |name| {
905         // skip those that are manually verified
906         match name {
907             // const-ness only added recently
908             "dladdr" => true,
909 
910             // Definition of those functions as changed since unified headers
911             // from NDK r14b These changes imply some API breaking changes but
912             // are still ABI compatible. We can wait for the next major release
913             // to be compliant with the new API.
914             //
915             // FIXME: unskip these for next major release
916             "setpriority" | "personality" => true,
917 
918             // signal is defined in terms of sighandler_t, so ignore
919             "signal" => true,
920 
921             // Currently missing
922             "cfmakeraw" | "cfsetspeed" => true,
923 
924             // const-ness issues
925             "execv" | "execve" | "execvp" | "settimeofday" | "sethostname" => true,
926 
927             // Solaris-different
928             "getpwent_r" | "getgrent_r" | "updwtmpx" if is_illumos => true,
929             "madvise" | "mprotect" if is_illumos => true,
930             "door_call" | "door_return" | "door_create" if is_illumos => true,
931 
932             // These functions may return int or void depending on the exact
933             // configuration of the compilation environment, but the return
934             // value is not useful (always 0) so we can ignore it:
935             "setservent" | "endservent" if is_illumos => true,
936 
937             _ => false,
938         }
939     });
940 
941     cfg.generate("../src/lib.rs", "main.rs");
942 }
943 
944 fn test_netbsd(target: &str) {
945     assert!(target.contains("netbsd"));
946     let rumprun = target.contains("rumprun");
947     let mut cfg = ctest_cfg();
948 
949     cfg.flag("-Wno-deprecated-declarations");
950     cfg.define("_NETBSD_SOURCE", Some("1"));
951 
952     headers! {
953         cfg:
954         "elf.h",
955         "errno.h",
956         "fcntl.h",
957         "limits.h",
958         "link.h",
959         "locale.h",
960         "stddef.h",
961         "stdint.h",
962         "stdio.h",
963         "stdlib.h",
964         "sys/stat.h",
965         "sys/types.h",
966         "time.h",
967         "wchar.h",
968         "aio.h",
969         "ctype.h",
970         "dirent.h",
971         "dlfcn.h",
972         "glob.h",
973         "grp.h",
974         "ifaddrs.h",
975         "langinfo.h",
976         "net/bpf.h",
977         "net/if.h",
978         "net/if_arp.h",
979         "net/if_dl.h",
980         "net/route.h",
981         "netdb.h",
982         "netinet/in.h",
983         "netinet/ip.h",
984         "netinet/tcp.h",
985         "netinet/udp.h",
986         "poll.h",
987         "pthread.h",
988         "pwd.h",
989         "regex.h",
990         "resolv.h",
991         "sched.h",
992         "semaphore.h",
993         "signal.h",
994         "string.h",
995         "sys/endian.h",
996         "sys/exec_elf.h",
997         "sys/extattr.h",
998         "sys/file.h",
999         "sys/ioctl.h",
1000         "sys/ioctl_compat.h",
1001         "sys/ipc.h",
1002         "sys/ktrace.h",
1003         "sys/mman.h",
1004         "sys/mount.h",
1005         "sys/ptrace.h",
1006         "sys/resource.h",
1007         "sys/shm.h",
1008         "sys/socket.h",
1009         "sys/statvfs.h",
1010         "sys/sysctl.h",
1011         "sys/time.h",
1012         "sys/times.h",
1013         "sys/timex.h",
1014         "sys/ucontext.h",
1015         "sys/ucred.h",
1016         "sys/uio.h",
1017         "sys/un.h",
1018         "sys/utsname.h",
1019         "sys/wait.h",
1020         "syslog.h",
1021         "termios.h",
1022         "ufs/ufs/quota.h",
1023         "ufs/ufs/quota1.h",
1024         "unistd.h",
1025         "util.h",
1026         "utime.h",
1027         "mqueue.h",
1028         "netinet/dccp.h",
1029         "sys/event.h",
1030         "sys/quota.h",
1031         "sys/shm.h",
1032         "iconv.h",
1033     }
1034 
1035     cfg.type_name(move |ty, is_struct, is_union| {
1036         match ty {
1037             // Just pass all these through, no need for a "struct" prefix
1038             "FILE" | "fd_set" | "Dl_info" | "DIR" | "Elf32_Phdr" | "Elf64_Phdr" | "Elf32_Shdr"
1039             | "Elf64_Shdr" | "Elf32_Sym" | "Elf64_Sym" | "Elf32_Ehdr" | "Elf64_Ehdr"
1040             | "Elf32_Chdr" | "Elf64_Chdr" => ty.to_string(),
1041 
1042             // OSX calls this something else
1043             "sighandler_t" => "sig_t".to_string(),
1044 
1045             t if is_union => format!("union {}", t),
1046 
1047             t if t.ends_with("_t") => t.to_string(),
1048 
1049             // put `struct` in front of all structs:.
1050             t if is_struct => format!("struct {}", t),
1051 
1052             t => t.to_string(),
1053         }
1054     });
1055 
1056     cfg.field_name(move |struct_, field| {
1057         match field {
1058             // Our stat *_nsec fields normally don't actually exist but are part
1059             // of a timeval struct
1060             s if s.ends_with("_nsec") && struct_.starts_with("stat") => {
1061                 s.replace("e_nsec", ".tv_nsec")
1062             }
1063             "u64" if struct_ == "epoll_event" => "data.u64".to_string(),
1064             s => s.to_string(),
1065         }
1066     });
1067 
1068     cfg.skip_type(move |ty| {
1069         if ty.starts_with("__c_anonymous_") {
1070             return true;
1071         }
1072         match ty {
1073             // FIXME: sighandler_t is crazy across platforms
1074             "sighandler_t" => true,
1075             _ => false,
1076         }
1077     });
1078 
1079     cfg.skip_struct(move |ty| {
1080         match ty {
1081             // This is actually a union, not a struct
1082             "sigval" => true,
1083             // These are tested as part of the linux_fcntl tests since there are
1084             // header conflicts when including them with all the other structs.
1085             "termios2" => true,
1086             _ => false,
1087         }
1088     });
1089 
1090     cfg.skip_signededness(move |c| {
1091         match c {
1092             "LARGE_INTEGER" | "float" | "double" => true,
1093             n if n.starts_with("pthread") => true,
1094             // sem_t is a struct or pointer
1095             "sem_t" => true,
1096             _ => false,
1097         }
1098     });
1099 
1100     cfg.skip_const(move |name| {
1101         match name {
1102             "SIG_DFL" | "SIG_ERR" | "SIG_IGN" => true, // sighandler_t weirdness
1103             "SIGUNUSED" => true,                       // removed in glibc 2.26
1104 
1105             // weird signed extension or something like that?
1106             "MS_NOUSER" => true,
1107             "MS_RMT_MASK" => true, // updated in glibc 2.22 and musl 1.1.13
1108             "BOTHER" => true,
1109 
1110             _ => false,
1111         }
1112     });
1113 
1114     cfg.skip_fn(move |name| {
1115         match name {
1116             // FIXME: https://github.com/rust-lang/libc/issues/1272
1117             "execv" | "execve" | "execvp" => true,
1118 
1119             "getrlimit" | "getrlimit64" |    // non-int in 1st arg
1120             "setrlimit" | "setrlimit64" |    // non-int in 1st arg
1121             "prlimit" | "prlimit64" |        // non-int in 2nd arg
1122 
1123             // These functions presumably exist on netbsd but don't look like
1124             // they're implemented on rumprun yet, just let them slide for now.
1125             // Some of them look like they have headers but then don't have
1126             // corresponding actual definitions either...
1127             "shm_open" |
1128             "shm_unlink" |
1129             "syscall" |
1130             "mq_open" |
1131             "mq_close" |
1132             "mq_getattr" |
1133             "mq_notify" |
1134             "mq_receive" |
1135             "mq_send" |
1136             "mq_setattr" |
1137             "mq_timedreceive" |
1138             "mq_timedsend" |
1139             "mq_unlink" |
1140             "ptrace" |
1141             "sigaltstack" if rumprun => true,
1142 
1143             _ => false,
1144         }
1145     });
1146 
1147     cfg.skip_field_type(move |struct_, field| {
1148         // This is a weird union, don't check the type.
1149         (struct_ == "ifaddrs" && field == "ifa_ifu") ||
1150         // sighandler_t type is super weird
1151         (struct_ == "sigaction" && field == "sa_sigaction") ||
1152         // sigval is actually a union, but we pretend it's a struct
1153         (struct_ == "sigevent" && field == "sigev_value") ||
1154         // aio_buf is "volatile void*" and Rust doesn't understand volatile
1155         (struct_ == "aiocb" && field == "aio_buf")
1156     });
1157 
1158     cfg.skip_field(|struct_, field| {
1159         match (struct_, field) {
1160             // conflicting with `p_type` macro from <resolve.h>.
1161             ("Elf32_Phdr", "p_type") => true,
1162             ("Elf64_Phdr", "p_type") => true,
1163             // pthread_spin_t is a volatile uchar
1164             ("pthread_spinlock_t", "pts_spin") => true,
1165             _ => false,
1166         }
1167     });
1168 
1169     cfg.generate("../src/lib.rs", "main.rs");
1170 }
1171 
1172 fn test_dragonflybsd(target: &str) {
1173     assert!(target.contains("dragonfly"));
1174     let mut cfg = ctest_cfg();
1175     cfg.flag("-Wno-deprecated-declarations");
1176 
1177     headers! {
1178         cfg:
1179         "aio.h",
1180         "ctype.h",
1181         "dirent.h",
1182         "dlfcn.h",
1183         "errno.h",
1184         "execinfo.h",
1185         "fcntl.h",
1186         "glob.h",
1187         "grp.h",
1188         "ifaddrs.h",
1189         "langinfo.h",
1190         "limits.h",
1191         "link.h",
1192         "locale.h",
1193         "mqueue.h",
1194         "net/bpf.h",
1195         "net/if.h",
1196         "net/if_arp.h",
1197         "net/if_dl.h",
1198         "net/route.h",
1199         "netdb.h",
1200         "netinet/in.h",
1201         "netinet/ip.h",
1202         "netinet/tcp.h",
1203         "netinet/udp.h",
1204         "poll.h",
1205         "pthread.h",
1206         "pthread_np.h",
1207         "pwd.h",
1208         "regex.h",
1209         "resolv.h",
1210         "sched.h",
1211         "semaphore.h",
1212         "signal.h",
1213         "stddef.h",
1214         "stdint.h",
1215         "stdio.h",
1216         "stdlib.h",
1217         "string.h",
1218         "sys/event.h",
1219         "sys/file.h",
1220         "sys/ioctl.h",
1221         "sys/cpuctl.h",
1222         "sys/ipc.h",
1223         "sys/kinfo.h",
1224         "sys/ktrace.h",
1225         "sys/malloc.h",
1226         "sys/mman.h",
1227         "sys/mount.h",
1228         "sys/procctl.h",
1229         "sys/ptrace.h",
1230         "sys/resource.h",
1231         "sys/rtprio.h",
1232         "sys/sched.h",
1233         "sys/shm.h",
1234         "sys/socket.h",
1235         "sys/stat.h",
1236         "sys/statvfs.h",
1237         "sys/sysctl.h",
1238         "sys/time.h",
1239         "sys/times.h",
1240         "sys/timex.h",
1241         "sys/types.h",
1242         "sys/checkpoint.h",
1243         "sys/uio.h",
1244         "sys/un.h",
1245         "sys/utsname.h",
1246         "sys/wait.h",
1247         "syslog.h",
1248         "termios.h",
1249         "time.h",
1250         "ucontext.h",
1251         "unistd.h",
1252         "util.h",
1253         "utime.h",
1254         "utmpx.h",
1255         "vfs/ufs/quota.h",
1256         "wchar.h",
1257         "iconv.h",
1258     }
1259 
1260     cfg.type_name(move |ty, is_struct, is_union| {
1261         match ty {
1262             // Just pass all these through, no need for a "struct" prefix
1263             "FILE" | "fd_set" | "Dl_info" | "DIR" | "Elf32_Phdr" | "Elf64_Phdr" | "Elf32_Shdr"
1264             | "Elf64_Shdr" | "Elf32_Sym" | "Elf64_Sym" | "Elf32_Ehdr" | "Elf64_Ehdr"
1265             | "Elf32_Chdr" | "Elf64_Chdr" => ty.to_string(),
1266 
1267             // FIXME: OSX calls this something else
1268             "sighandler_t" => "sig_t".to_string(),
1269 
1270             t if is_union => format!("union {}", t),
1271 
1272             t if t.ends_with("_t") => t.to_string(),
1273 
1274             // sigval is a struct in Rust, but a union in C:
1275             "sigval" => format!("union sigval"),
1276 
1277             // put `struct` in front of all structs:.
1278             t if is_struct => format!("struct {}", t),
1279 
1280             t => t.to_string(),
1281         }
1282     });
1283 
1284     cfg.field_name(move |struct_, field| {
1285         match field {
1286             // Our stat *_nsec fields normally don't actually exist but are part
1287             // of a timeval struct
1288             s if s.ends_with("_nsec") && struct_.starts_with("stat") => {
1289                 s.replace("e_nsec", ".tv_nsec")
1290             }
1291             "u64" if struct_ == "epoll_event" => "data.u64".to_string(),
1292             // Field is named `type` in C but that is a Rust keyword,
1293             // so these fields are translated to `type_` in the bindings.
1294             "type_" if struct_ == "rtprio" => "type".to_string(),
1295             s => s.to_string(),
1296         }
1297     });
1298 
1299     cfg.skip_type(move |ty| {
1300         match ty {
1301             // sighandler_t is crazy across platforms
1302             "sighandler_t" => true,
1303 
1304             _ => false,
1305         }
1306     });
1307 
1308     cfg.skip_struct(move |ty| {
1309         match ty {
1310             // FIXME: These are tested as part of the linux_fcntl tests since
1311             // there are header conflicts when including them with all the other
1312             // structs.
1313             "termios2" => true,
1314 
1315             _ => false,
1316         }
1317     });
1318 
1319     cfg.skip_signededness(move |c| {
1320         match c {
1321             "LARGE_INTEGER" | "float" | "double" => true,
1322             // uuid_t is a struct, not an integer.
1323             "uuid_t" => true,
1324             n if n.starts_with("pthread") => true,
1325             // sem_t is a struct or pointer
1326             "sem_t" => true,
1327             // mqd_t is a pointer on DragonFly
1328             "mqd_t" => true,
1329 
1330             _ => false,
1331         }
1332     });
1333 
1334     cfg.skip_const(move |name| {
1335         match name {
1336             "SIG_DFL" | "SIG_ERR" | "SIG_IGN" => true, // sighandler_t weirdness
1337 
1338             // weird signed extension or something like that?
1339             "MS_NOUSER" => true,
1340             "MS_RMT_MASK" => true, // updated in glibc 2.22 and musl 1.1.13
1341 
1342             // These are defined for Solaris 11, but the crate is tested on
1343             // illumos, where they are currently not defined
1344             "EADI" | "PORT_SOURCE_POSTWAIT" | "PORT_SOURCE_SIGNAL" | "PTHREAD_STACK_MIN" => true,
1345 
1346             _ => false,
1347         }
1348     });
1349 
1350     cfg.skip_fn(move |name| {
1351         // skip those that are manually verified
1352         match name {
1353             // FIXME: https://github.com/rust-lang/libc/issues/1272
1354             "execv" | "execve" | "execvp" | "fexecve" => true,
1355 
1356             "getrlimit" | "getrlimit64" |    // non-int in 1st arg
1357             "setrlimit" | "setrlimit64" |    // non-int in 1st arg
1358             "prlimit" | "prlimit64"        // non-int in 2nd arg
1359              => true,
1360 
1361             _ => false,
1362         }
1363     });
1364 
1365     cfg.skip_field_type(move |struct_, field| {
1366         // This is a weird union, don't check the type.
1367         (struct_ == "ifaddrs" && field == "ifa_ifu") ||
1368         // sighandler_t type is super weird
1369         (struct_ == "sigaction" && field == "sa_sigaction") ||
1370         // sigval is actually a union, but we pretend it's a struct
1371         (struct_ == "sigevent" && field == "sigev_value") ||
1372         // aio_buf is "volatile void*" and Rust doesn't understand volatile
1373         (struct_ == "aiocb" && field == "aio_buf")
1374     });
1375 
1376     cfg.skip_field(move |struct_, field| {
1377         // this is actually a union on linux, so we can't represent it well and
1378         // just insert some padding.
1379         (struct_ == "siginfo_t" && field == "_pad") ||
1380         // sigev_notify_thread_id is actually part of a sigev_un union
1381         (struct_ == "sigevent" && field == "sigev_notify_thread_id")
1382     });
1383 
1384     cfg.generate("../src/lib.rs", "main.rs");
1385 }
1386 
1387 fn test_wasi(target: &str) {
1388     assert!(target.contains("wasi"));
1389 
1390     let mut cfg = ctest_cfg();
1391     cfg.define("_GNU_SOURCE", None);
1392 
1393     headers! { cfg:
1394         "ctype.h",
1395         "dirent.h",
1396         "errno.h",
1397         "fcntl.h",
1398         "limits.h",
1399         "locale.h",
1400         "malloc.h",
1401         "poll.h",
1402         "sched.h",
1403         "stdbool.h",
1404         "stddef.h",
1405         "stdint.h",
1406         "stdio.h",
1407         "stdlib.h",
1408         "string.h",
1409         "sys/resource.h",
1410         "sys/select.h",
1411         "sys/socket.h",
1412         "sys/stat.h",
1413         "sys/times.h",
1414         "sys/types.h",
1415         "sys/uio.h",
1416         "sys/utsname.h",
1417         "sys/ioctl.h",
1418         "time.h",
1419         "unistd.h",
1420         "wasi/api.h",
1421         "wasi/libc.h",
1422         "wasi/libc-find-relpath.h",
1423         "wasi/libc-nocwd.h",
1424         "wchar.h",
1425     }
1426 
1427     cfg.type_name(move |ty, is_struct, is_union| match ty {
1428         "FILE" | "fd_set" | "DIR" => ty.to_string(),
1429         t if is_union => format!("union {}", t),
1430         t if t.starts_with("__wasi") && t.ends_with("_u") => format!("union {}", t),
1431         t if t.starts_with("__wasi") && is_struct => format!("struct {}", t),
1432         t if t.ends_with("_t") => t.to_string(),
1433         t if is_struct => format!("struct {}", t),
1434         t => t.to_string(),
1435     });
1436 
1437     cfg.field_name(move |_struct, field| {
1438         match field {
1439             // deal with fields as rust keywords
1440             "type_" => "type".to_string(),
1441             s => s.to_string(),
1442         }
1443     });
1444 
1445     // Looks like LLD doesn't merge duplicate imports, so if the Rust
1446     // code imports from a module and the C code also imports from a
1447     // module we end up with two imports of function pointers which
1448     // import the same thing but have different function pointers
1449     cfg.skip_fn_ptrcheck(|f| f.starts_with("__wasi"));
1450 
1451     // d_name is declared as a flexible array in WASI libc, so it
1452     // doesn't support sizeof.
1453     cfg.skip_field(|s, field| s == "dirent" && field == "d_name");
1454 
1455     // Currently Rust/clang disagree on function argument ABI, so skip these
1456     // tests. For more info see WebAssembly/tool-conventions#88
1457     cfg.skip_roundtrip(|_| true);
1458 
1459     cfg.generate("../src/lib.rs", "main.rs");
1460 }
1461 
1462 fn test_android(target: &str) {
1463     assert!(target.contains("android"));
1464     let target_pointer_width = match target {
1465         t if t.contains("aarch64") || t.contains("x86_64") => 64,
1466         t if t.contains("i686") || t.contains("arm") => 32,
1467         t => panic!("unsupported target: {}", t),
1468     };
1469     let x86 = target.contains("i686") || target.contains("x86_64");
1470 
1471     let mut cfg = ctest_cfg();
1472     cfg.define("_GNU_SOURCE", None);
1473 
1474     headers! { cfg:
1475                "arpa/inet.h",
1476                "ctype.h",
1477                "dirent.h",
1478                "dlfcn.h",
1479                "elf.h",
1480                "errno.h",
1481                "fcntl.h",
1482                "grp.h",
1483                "ifaddrs.h",
1484                "limits.h",
1485                "link.h",
1486                "locale.h",
1487                "malloc.h",
1488                "net/ethernet.h",
1489                "net/if.h",
1490                "net/if_arp.h",
1491                "net/route.h",
1492                "netdb.h",
1493                "netinet/in.h",
1494                "netinet/ip.h",
1495                "netinet/tcp.h",
1496                "netinet/udp.h",
1497                "netpacket/packet.h",
1498                "poll.h",
1499                "pthread.h",
1500                "pty.h",
1501                "pwd.h",
1502                "regex.h",
1503                "resolv.h",
1504                "sched.h",
1505                "semaphore.h",
1506                "signal.h",
1507                "stddef.h",
1508                "stdint.h",
1509                "stdio.h",
1510                "stdlib.h",
1511                "string.h",
1512                "sys/auxv.h",
1513                "sys/epoll.h",
1514                "sys/eventfd.h",
1515                "sys/file.h",
1516                "sys/fsuid.h",
1517                "sys/inotify.h",
1518                "sys/ioctl.h",
1519                "sys/mman.h",
1520                "sys/mount.h",
1521                "sys/personality.h",
1522                "sys/prctl.h",
1523                "sys/ptrace.h",
1524                "sys/random.h",
1525                "sys/reboot.h",
1526                "sys/resource.h",
1527                "sys/sendfile.h",
1528                "sys/signalfd.h",
1529                "sys/socket.h",
1530                "sys/stat.h",
1531                "sys/statvfs.h",
1532                "sys/swap.h",
1533                "sys/syscall.h",
1534                "sys/sysinfo.h",
1535                "sys/system_properties.h",
1536                "sys/time.h",
1537                "sys/timerfd.h",
1538                "sys/times.h",
1539                "sys/types.h",
1540                "sys/ucontext.h",
1541                "sys/uio.h",
1542                "sys/un.h",
1543                "sys/user.h",
1544                "sys/utsname.h",
1545                "sys/vfs.h",
1546                "sys/xattr.h",
1547                "sys/wait.h",
1548                "syslog.h",
1549                "termios.h",
1550                "time.h",
1551                "unistd.h",
1552                "utime.h",
1553                "utmp.h",
1554                "wchar.h",
1555                "xlocale.h",
1556                // time64_t is not defined for 64-bit targets If included it will
1557                // generate the error 'Your time_t is already 64-bit'
1558                [target_pointer_width == 32]: "time64.h",
1559                [x86]: "sys/reg.h",
1560     }
1561 
1562     // Include linux headers at the end:
1563     headers! { cfg:
1564                 "asm/mman.h",
1565                 "linux/auxvec.h",
1566                 "linux/dccp.h",
1567                 "linux/elf.h",
1568                 "linux/errqueue.h",
1569                 "linux/falloc.h",
1570                 "linux/filter.h",
1571                 "linux/futex.h",
1572                 "linux/fs.h",
1573                 "linux/genetlink.h",
1574                 "linux/if_alg.h",
1575                 "linux/if_ether.h",
1576                 "linux/if_tun.h",
1577                 "linux/magic.h",
1578                 "linux/memfd.h",
1579                 "linux/module.h",
1580                 "linux/net_tstamp.h",
1581                 "linux/netfilter/nfnetlink.h",
1582                 "linux/netfilter/nfnetlink_log.h",
1583                 "linux/netfilter/nfnetlink_queue.h",
1584                 "linux/netfilter/nf_tables.h",
1585                 "linux/netfilter_ipv4.h",
1586                 "linux/netfilter_ipv6.h",
1587                 "linux/netfilter_ipv6/ip6_tables.h",
1588                 "linux/netlink.h",
1589                 "linux/quota.h",
1590                 "linux/reboot.h",
1591                 "linux/seccomp.h",
1592                 "linux/sched.h",
1593                 "linux/sockios.h",
1594                 "linux/vm_sockets.h",
1595                 "linux/wait.h",
1596 
1597     }
1598 
1599     // Include Android-specific headers:
1600     headers! { cfg:
1601                 "android/set_abort_message.h"
1602     }
1603 
1604     cfg.type_name(move |ty, is_struct, is_union| {
1605         match ty {
1606             // Just pass all these through, no need for a "struct" prefix
1607             "FILE" | "fd_set" | "Dl_info" | "Elf32_Phdr" | "Elf64_Phdr" => ty.to_string(),
1608 
1609             t if is_union => format!("union {}", t),
1610 
1611             t if t.ends_with("_t") => t.to_string(),
1612 
1613             // sigval is a struct in Rust, but a union in C:
1614             "sigval" => format!("union sigval"),
1615 
1616             // put `struct` in front of all structs:.
1617             t if is_struct => format!("struct {}", t),
1618 
1619             t => t.to_string(),
1620         }
1621     });
1622 
1623     cfg.field_name(move |struct_, field| {
1624         match field {
1625             // Our stat *_nsec fields normally don't actually exist but are part
1626             // of a timeval struct
1627             s if s.ends_with("_nsec") && struct_.starts_with("stat") => s.to_string(),
1628             // FIXME: appears that `epoll_event.data` is an union
1629             "u64" if struct_ == "epoll_event" => "data.u64".to_string(),
1630             s => s.to_string(),
1631         }
1632     });
1633 
1634     cfg.skip_type(move |ty| {
1635         match ty {
1636             // FIXME: `sighandler_t` type is incorrect, see:
1637             // https://github.com/rust-lang/libc/issues/1359
1638             "sighandler_t" => true,
1639             _ => false,
1640         }
1641     });
1642 
1643     cfg.skip_struct(move |ty| {
1644         if ty.starts_with("__c_anonymous_") {
1645             return true;
1646         }
1647         match ty {
1648             // These are tested as part of the linux_fcntl tests since there are
1649             // header conflicts when including them with all the other structs.
1650             "termios2" => true,
1651             // uc_sigmask and uc_sigmask64 of ucontext_t are an anonymous union
1652             "ucontext_t" => true,
1653             // 'private' type
1654             "prop_info" => true,
1655 
1656             _ => false,
1657         }
1658     });
1659 
1660     cfg.skip_const(move |name| {
1661         match name {
1662             // FIXME: deprecated: not available in any header
1663             // See: https://github.com/rust-lang/libc/issues/1356
1664             "ENOATTR" => true,
1665 
1666             // FIXME: still necessary?
1667             "SIG_DFL" | "SIG_ERR" | "SIG_IGN" => true, // sighandler_t weirdness
1668             // FIXME: deprecated - removed in glibc 2.26
1669             "SIGUNUSED" => true,
1670 
1671             // Needs a newer Android SDK for the definition
1672             "P_PIDFD" => true,
1673 
1674             // Requires Linux kernel 5.6
1675             "VMADDR_CID_LOCAL" => true,
1676 
1677             // FIXME: conflicts with standard C headers and is tested in
1678             // `linux_termios.rs` below:
1679             "IBSHIFT" => true,
1680             "TCGETS2" | "TCSETS2" | "TCSETSW2" | "TCSETSF2" => true,
1681 
1682             _ => false,
1683         }
1684     });
1685 
1686     cfg.skip_fn(move |name| {
1687         // skip those that are manually verified
1688         match name {
1689             // FIXME: https://github.com/rust-lang/libc/issues/1272
1690             "execv" | "execve" | "execvp" | "execvpe" | "fexecve" => true,
1691 
1692             // There are two versions of the sterror_r function, see
1693             //
1694             // https://linux.die.net/man/3/strerror_r
1695             //
1696             // An XSI-compliant version provided if:
1697             //
1698             // (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE
1699             //
1700             // and a GNU specific version provided if _GNU_SOURCE is defined.
1701             //
1702             // libc provides bindings for the XSI-compliant version, which is
1703             // preferred for portable applications.
1704             //
1705             // We skip the test here since here _GNU_SOURCE is defined, and
1706             // test the XSI version below.
1707             "strerror_r" => true,
1708             "reallocarray" => true,
1709             "__system_property_wait" => true,
1710 
1711             // Added in API level 30, but tests use level 28.
1712             "mlock2" => true,
1713 
1714             _ => false,
1715         }
1716     });
1717 
1718     cfg.skip_field_type(move |struct_, field| {
1719         // This is a weird union, don't check the type.
1720         (struct_ == "ifaddrs" && field == "ifa_ifu") ||
1721         // sigval is actually a union, but we pretend it's a struct
1722         (struct_ == "sigevent" && field == "sigev_value") ||
1723         // FIXME: `sa_sigaction` has type `sighandler_t` but that type is
1724         // incorrect, see: https://github.com/rust-lang/libc/issues/1359
1725         (struct_ == "sigaction" && field == "sa_sigaction") ||
1726         // signalfd had SIGSYS fields added in Android 4.19, but CI does not have that version yet.
1727         (struct_ == "signalfd_siginfo" && field == "ssi_call_addr")
1728     });
1729 
1730     cfg.skip_field(move |struct_, field| {
1731         // this is actually a union on linux, so we can't represent it well and
1732         // just insert some padding.
1733         (struct_ == "siginfo_t" && field == "_pad") ||
1734         // FIXME: `sa_sigaction` has type `sighandler_t` but that type is
1735         // incorrect, see: https://github.com/rust-lang/libc/issues/1359
1736         (struct_ == "sigaction" && field == "sa_sigaction") ||
1737         // sigev_notify_thread_id is actually part of a sigev_un union
1738         (struct_ == "sigevent" && field == "sigev_notify_thread_id") ||
1739         // signalfd had SIGSYS fields added in Android 4.19, but CI does not have that version yet.
1740         (struct_ == "signalfd_siginfo" && (field == "ssi_syscall" ||
1741                                            field == "ssi_call_addr" ||
1742                                            field == "ssi_arch"))
1743     });
1744 
1745     cfg.skip_field(|struct_, field| {
1746         match (struct_, field) {
1747             // conflicting with `p_type` macro from <resolve.h>.
1748             ("Elf32_Phdr", "p_type") => true,
1749             ("Elf64_Phdr", "p_type") => true,
1750 
1751             // this is actually a union on linux, so we can't represent it well and
1752             // just insert some padding.
1753             ("siginfo_t", "_pad") => true,
1754 
1755             _ => false,
1756         }
1757     });
1758 
1759     cfg.generate("../src/lib.rs", "main.rs");
1760 
1761     test_linux_like_apis(target);
1762 }
1763 
1764 fn test_freebsd(target: &str) {
1765     assert!(target.contains("freebsd"));
1766     let mut cfg = ctest_cfg();
1767 
1768     let freebsd_ver = which_freebsd();
1769 
1770     match freebsd_ver {
1771         Some(10) => cfg.cfg("freebsd10", None),
1772         Some(11) => cfg.cfg("freebsd11", None),
1773         Some(12) => cfg.cfg("freebsd12", None),
1774         Some(13) => cfg.cfg("freebsd13", None),
1775         Some(14) => cfg.cfg("freebsd14", None),
1776         _ => &mut cfg,
1777     };
1778 
1779     // For sched linux compat fn
1780     cfg.define("_WITH_CPU_SET_T", None);
1781     // Required for `getline`:
1782     cfg.define("_WITH_GETLINE", None);
1783     // Required for making freebsd11_stat available in the headers
1784     match freebsd_ver {
1785         Some(10) => &mut cfg,
1786         _ => cfg.define("_WANT_FREEBSD11_STAT", None),
1787     };
1788 
1789     let freebsd12 = match freebsd_ver {
1790         Some(n) if n >= 12 => true,
1791         _ => false,
1792     };
1793 
1794     let freebsd13 = match freebsd_ver {
1795         Some(n) if n >= 13 => true,
1796         _ => false,
1797     };
1798 
1799     headers! { cfg:
1800                 "aio.h",
1801                 "arpa/inet.h",
1802                 "ctype.h",
1803                 "dirent.h",
1804                 "dlfcn.h",
1805                 "elf.h",
1806                 "errno.h",
1807                 "execinfo.h",
1808                 "fcntl.h",
1809                 "glob.h",
1810                 "grp.h",
1811                 "iconv.h",
1812                 "ifaddrs.h",
1813                 "langinfo.h",
1814                 "libutil.h",
1815                 "limits.h",
1816                 "link.h",
1817                 "locale.h",
1818                 "machine/elf.h",
1819                 "machine/reg.h",
1820                 "malloc_np.h",
1821                 "mqueue.h",
1822                 "net/bpf.h",
1823                 "net/if.h",
1824                 "net/if_arp.h",
1825                 "net/if_dl.h",
1826                 "net/if_mib.h",
1827                 "net/route.h",
1828                 "netdb.h",
1829                 "netinet/ip.h",
1830                 "netinet/in.h",
1831                 "netinet/tcp.h",
1832                 "netinet/udp.h",
1833                 "poll.h",
1834                 "pthread.h",
1835                 "pthread_np.h",
1836                 "pwd.h",
1837                 "regex.h",
1838                 "resolv.h",
1839                 "sched.h",
1840                 "semaphore.h",
1841                 "signal.h",
1842                 "spawn.h",
1843                 "stddef.h",
1844                 "stdint.h",
1845                 "stdio.h",
1846                 "stdlib.h",
1847                 "string.h",
1848                 "sys/capsicum.h",
1849                 [freebsd12]:"sys/auxv.h",
1850                 "sys/cpuset.h",
1851                 [freebsd12]:"sys/domainset.h",
1852                 "sys/event.h",
1853                 [freebsd13]:"sys/eventfd.h",
1854                 "sys/extattr.h",
1855                 "sys/file.h",
1856                 "sys/ioctl.h",
1857                 "sys/ipc.h",
1858                 "sys/jail.h",
1859                 "sys/mman.h",
1860                 "sys/mount.h",
1861                 "sys/msg.h",
1862                 "sys/procctl.h",
1863                 "sys/procdesc.h",
1864                 "sys/ptrace.h",
1865                 "sys/queue.h",
1866                 "sys/random.h",
1867                 "sys/resource.h",
1868                 "sys/rtprio.h",
1869                 "sys/sem.h",
1870                 "sys/shm.h",
1871                 "sys/socket.h",
1872                 "sys/stat.h",
1873                 "sys/statvfs.h",
1874                 "sys/sysctl.h",
1875                 "sys/thr.h",
1876                 "sys/time.h",
1877                 "sys/times.h",
1878                 "sys/timex.h",
1879                 "sys/types.h",
1880                 "sys/proc.h",
1881                 "kvm.h", // must be after "sys/types.h"
1882                 "sys/ucontext.h",
1883                 "sys/uio.h",
1884                 "sys/ktrace.h",
1885                 "sys/un.h",
1886                 "sys/user.h",
1887                 "sys/utsname.h",
1888                 "sys/uuid.h",
1889                 "sys/vmmeter.h",
1890                 "sys/wait.h",
1891                 "libprocstat.h",
1892                 "devstat.h",
1893                 "syslog.h",
1894                 "termios.h",
1895                 "time.h",
1896                 "ufs/ufs/quota.h",
1897                 "unistd.h",
1898                 "utime.h",
1899                 "utmpx.h",
1900                 "wchar.h",
1901     }
1902 
1903     cfg.type_name(move |ty, is_struct, is_union| {
1904         match ty {
1905             // Just pass all these through, no need for a "struct" prefix
1906             "FILE"
1907             | "fd_set"
1908             | "Dl_info"
1909             | "DIR"
1910             | "Elf32_Phdr"
1911             | "Elf64_Phdr"
1912             | "Elf32_Auxinfo"
1913             | "Elf64_Auxinfo"
1914             | "devstat_select_mode"
1915             | "devstat_support_flags"
1916             | "devstat_type_flags"
1917             | "devstat_match_flags"
1918             | "devstat_priority" => ty.to_string(),
1919 
1920             // FIXME: https://github.com/rust-lang/libc/issues/1273
1921             "sighandler_t" => "sig_t".to_string(),
1922 
1923             t if is_union => format!("union {}", t),
1924 
1925             t if t.ends_with("_t") => t.to_string(),
1926 
1927             // sigval is a struct in Rust, but a union in C:
1928             "sigval" => format!("union sigval"),
1929 
1930             // put `struct` in front of all structs:.
1931             t if is_struct => format!("struct {}", t),
1932 
1933             t => t.to_string(),
1934         }
1935     });
1936 
1937     cfg.field_name(move |struct_, field| {
1938         match field {
1939             // Our stat *_nsec fields normally don't actually exist but are part
1940             // of a timeval struct
1941             s if s.ends_with("_nsec") && struct_.starts_with("stat") => {
1942                 s.replace("e_nsec", ".tv_nsec")
1943             }
1944             // Field is named `type` in C but that is a Rust keyword,
1945             // so these fields are translated to `type_` in the bindings.
1946             "type_" if struct_ == "rtprio" => "type".to_string(),
1947             "type_" if struct_ == "sockstat" => "type".to_string(),
1948             "type_" if struct_ == "devstat_match_table" => "type".to_string(),
1949             s => s.to_string(),
1950         }
1951     });
1952 
1953     cfg.skip_const(move |name| {
1954         match name {
1955             // These constants were introduced in FreeBSD 13:
1956             "F_ADD_SEALS" | "F_GET_SEALS" | "F_SEAL_SEAL" | "F_SEAL_SHRINK" | "F_SEAL_GROW"
1957             | "F_SEAL_WRITE"
1958                 if Some(13) > freebsd_ver =>
1959             {
1960                 true
1961             }
1962 
1963             // These constants were introduced in FreeBSD 13:
1964             "EFD_CLOEXEC" | "EFD_NONBLOCK" | "EFD_SEMAPHORE" if Some(13) > freebsd_ver => true,
1965 
1966             // These constants were introduced in FreeBSD 12:
1967             "SF_USER_READAHEAD"
1968             | "EVFILT_EMPTY"
1969             | "SO_REUSEPORT_LB"
1970             | "IP_ORIGDSTADDR"
1971             | "IP_RECVORIGDSTADDR"
1972             | "IPV6_ORIGDSTADDR"
1973             | "IPV6_RECVORIGDSTADDR"
1974             | "NI_NUMERICSCOPE"
1975             | "SO_DOMAIN"
1976                 if Some(11) == freebsd_ver =>
1977             {
1978                 true
1979             }
1980 
1981             // These constants were introduced in FreeBSD 11:
1982             "SF_USER_READAHEAD"
1983             | "SF_NOCACHE"
1984             | "RLIMIT_KQUEUES"
1985             | "RLIMIT_UMTXP"
1986             | "EVFILT_PROCDESC"
1987             | "EVFILT_SENDFILE"
1988             | "EVFILT_EMPTY"
1989             | "SO_REUSEPORT_LB"
1990             | "TCP_CCALGOOPT"
1991             | "TCP_PCAP_OUT"
1992             | "TCP_PCAP_IN"
1993             | "IP_BINDMULTI"
1994             | "IP_ORIGDSTADDR"
1995             | "IP_RECVORIGDSTADDR"
1996             | "IPV6_ORIGDSTADDR"
1997             | "IPV6_RECVORIGDSTADDR"
1998             | "PD_CLOEXEC"
1999             | "PD_ALLOWED_AT_FORK"
2000             | "IP_RSS_LISTEN_BUCKET"
2001             | "NI_NUMERICSCOPE"
2002                 if Some(10) == freebsd_ver =>
2003             {
2004                 true
2005             }
2006 
2007             // FIXME: This constant has a different value in FreeBSD 10:
2008             "RLIM_NLIMITS" if Some(10) == freebsd_ver => true,
2009 
2010             // FIXME: There are deprecated - remove in a couple of releases.
2011             // These constants were removed in FreeBSD 11 (svn r273250) but will
2012             // still be accepted and ignored at runtime.
2013             "MAP_RENAME" | "MAP_NORESERVE" if Some(10) != freebsd_ver => true,
2014 
2015             // FIXME: There are deprecated - remove in a couple of releases.
2016             // These constants were removed in FreeBSD 11 (svn r262489),
2017             // and they've never had any legitimate use outside of the
2018             // base system anyway.
2019             "CTL_MAXID" | "KERN_MAXID" | "HW_MAXID" | "USER_MAXID" => true,
2020 
2021             // This constant was removed in FreeBSD 13 (svn r363622), and never
2022             // had any legitimate use outside of the base system anyway.
2023             "CTL_P1003_1B_MAXID" => true,
2024 
2025             // This was renamed in FreeBSD 12.2 and 13 (r352486).
2026             "CTL_UNSPEC" | "CTL_SYSCTL" => true,
2027 
2028             // These were added in FreeBSD 12.2 and 13 (r352486),
2029             // but they are just names for magic numbers that existed for ages.
2030             "CTL_SYSCTL_DEBUG"
2031             | "CTL_SYSCTL_NAME"
2032             | "CTL_SYSCTL_NEXT"
2033             | "CTL_SYSCTL_NAME2OID"
2034             | "CTL_SYSCTL_OIDFMT"
2035             | "CTL_SYSCTL_OIDDESCR"
2036             | "CTL_SYSCTL_OIDLABEL" => true,
2037 
2038             // This was renamed in FreeBSD 12.2 and 13 (r350749).
2039             "IPPROTO_SEP" | "IPPROTO_DCCP" => true,
2040 
2041             // This was changed to 96(0x60) in FreeBSD 13:
2042             // https://github.com/freebsd/freebsd/
2043             // commit/06b00ceaa914a3907e4e27bad924f44612bae1d7
2044             "MINCORE_SUPER" if Some(13) == freebsd_ver => true,
2045 
2046             // Added in FreeBSD 12.0
2047             "EINTEGRITY" if Some(11) == freebsd_ver => true,
2048 
2049             // This was increased to 97 in FreeBSD 12.2 and 13.
2050             // https://github.com/freebsd/freebsd/
2051             // commit/72a21ba0f62da5e86a1c0b462aeb3f5ff849a1b7
2052             "ELAST" if Some(12) == freebsd_ver => true,
2053 
2054             // Added in FreeBSD 12.0 (r331279)
2055             "GRND_NONBLOCK" | "GRND_RANDOM" if Some(11) == freebsd_ver => true,
2056             // Added in FreeBSD 13.0 (r356667)
2057             "GRND_INSECURE" if Some(13) > freebsd_ver => true,
2058 
2059             // Added in FreeBSD 12.1 (r343964)
2060             "PROC_ASLR_CTL"
2061             | "PROC_ASLR_STATUS"
2062             | "PROC_ASLR_FORCE_ENABLE"
2063             | "PROC_ASLR_FORCE_DISABLE"
2064             | "PROC_ASLR_NOFORCE"
2065             | "PROC_ASLR_ACTIVE"
2066                 if Some(11) == freebsd_ver =>
2067             {
2068                 true
2069             }
2070 
2071             // Added in FreeBSD 12.1 (r345228)
2072             "PROC_PROCCTL_MD_MIN" if Some(11) == freebsd_ver => true,
2073 
2074             // Added in FreeBSD 13.0 (r349609)
2075             "PROC_PROTMAX_CTL"
2076             | "PROC_PROTMAX_STATUS"
2077             | "PROC_PROTMAX_FORCE_ENABLE"
2078             | "PROC_PROTMAX_FORCE_DISABLE"
2079             | "PROC_PROTMAX_NOFORCE"
2080             | "PROC_PROTMAX_ACTIVE"
2081                 if Some(13) > freebsd_ver =>
2082             {
2083                 true
2084             }
2085 
2086             // Added in FreeBSD 12.1
2087             "PT_GET_SC_RET" | "PT_GET_SC_ARGS" if Some(11) == freebsd_ver => true,
2088 
2089             // Added in in FreeBSD 13.0 (r367776 and r367287)
2090             "SCM_CREDS2" | "LOCAL_CREDS_PERSISTENT" if Some(13) > freebsd_ver => true,
2091 
2092             "VM_TOTAL" if Some(11) == freebsd_ver => true,
2093 
2094             // Added in FreeBSD 14.
2095             "KERN_STACKTOP" if Some(14) > freebsd_ver => true,
2096             // Added in FreeBSD 13.
2097             "KERN_PROC_SIGFASTBLK"
2098             | "USER_LOCALBASE"
2099             | "TDP_SIGFASTBLOCK"
2100             | "TDP_UIOHELD"
2101             | "TDP_SIGFASTPENDING"
2102             | "TDP2_COMPAT32RB"
2103             | "P2_PROTMAX_ENABLE"
2104             | "P2_PROTMAX_DISABLE"
2105             | "CTLFLAG_NEEDGIANT"
2106             | "CTL_SYSCTL_NEXTNOSKIP"
2107                 if Some(13) > freebsd_ver =>
2108             {
2109                 true
2110             }
2111             // Added in FreeBSD 12.
2112             "KERN_MAXPHYS"
2113             | "KVME_FLAG_USER_WIRED"
2114             | "TDP2_SBPAGES"
2115             | "P2_ASLR_ENABLE"
2116             | "P2_ASLR_DISABLE"
2117             | "P2_ASLR_IGNSTART"
2118             | "P_TREE_GRPEXITED"
2119                 if Some(12) > freebsd_ver =>
2120             {
2121                 true
2122             }
2123 
2124             // Added in freebsd 14.
2125             "IFCAP_MEXTPG" if Some(14) > freebsd_ver => true,
2126             // Added in freebsd 13.
2127             "IFF_KNOWSEPOCH" | "IFCAP_TXTLS4" | "IFCAP_TXTLS6" | "IFCAP_VXLAN_HWCSUM"
2128             | "IFCAP_VXLAN_HWTSO" | "IFCAP_TXTLS_RTLMT" | "IFCAP_TXTLS"
2129                 if Some(13) > freebsd_ver =>
2130             {
2131                 true
2132             }
2133             // Added in freebsd 12.
2134             "IFF_NOGROUP" | "IFCAP_TXRTLMT" | "IFCAP_HWRXTSTMP" if Some(12) > freebsd_ver => true,
2135             // Added in FreeBSD 13.
2136             "PS_FST_TYPE_EVENTFD" if Some(13) > freebsd_ver => true,
2137 
2138             // Added in FreeBSD 14.
2139             "MNT_RECURSE"
2140             | "MNT_DEFERRED"
2141             | "MNTK_RECURSE"
2142             | "MNTK_UPPER_WAITER"
2143             | "MNTK_TASKQUEUE_WAITER"
2144                 if Some(14) > freebsd_ver =>
2145             {
2146                 true
2147             }
2148 
2149             // Added in FreeBSD 13.
2150             "MNT_EXTLS" | "MNT_EXTLSCERT" | "MNT_EXTLSCERTUSER" | "MNT_NOCOVER"
2151             | "MNT_EMPTYDIR" | "MNTK_NOMSYNC" | "MNTK_UNIONFS" | "MNTK_FPLOOKUP"
2152             | "MNTK_SUSPEND_ALL"
2153                 if Some(13) > freebsd_ver =>
2154             {
2155                 true
2156             }
2157 
2158             // Added in FreeBSD 12.
2159             "MNT_UNTRUSTED" | "MNT_VERIFIED" | "MNTK_TEXT_REFS" | "MNTK_VMSETSIZE_BUG"
2160                 if Some(12) > freebsd_ver =>
2161             {
2162                 true
2163             }
2164 
2165             // Added in FreeBSD 14.
2166             "PT_COREDUMP" | "PC_ALL" | "PC_COMPRESS" if Some(14) > freebsd_ver => true,
2167 
2168             // Added in FreeBSD 14.
2169             "F_KINFO" => true, // FIXME: depends how frequent freebsd 14 is updated on CI, this addition went this week only.
2170             "SHM_RENAME_NOREPLACE"
2171             | "SHM_RENAME_EXCHANGE"
2172             | "MFD_CLOEXEC"
2173             | "MFD_ALLOW_SEALING"
2174             | "MFD_HUGETLB"
2175                 if Some(13) > freebsd_ver =>
2176             {
2177                 true
2178             }
2179 
2180             _ => false,
2181         }
2182     });
2183 
2184     cfg.skip_type(move |ty| {
2185         match ty {
2186             // the struct "__kvm" is quite tricky to bind so since we only use a pointer to it
2187             // for now, it doesn't matter too much...
2188             "kvm_t" => true,
2189 
2190             _ => false,
2191         }
2192     });
2193 
2194     cfg.skip_struct(move |ty| {
2195         if ty.starts_with("__c_anonymous_") {
2196             return true;
2197         }
2198         match ty {
2199             // `mmsghdr` is not available in FreeBSD 10
2200             "mmsghdr" if Some(10) == freebsd_ver => true,
2201 
2202             // `max_align_t` is not available in FreeBSD 10
2203             "max_align_t" if Some(10) == freebsd_ver => true,
2204 
2205             // `procstat` is a private struct
2206             "procstat" => true,
2207 
2208             // `ptrace_sc_ret` is not available in FreeBSD 11
2209             "ptrace_sc_ret" if Some(11) == freebsd_ver => true,
2210 
2211             // obsolete version
2212             "vmtotal" if Some(11) == freebsd_ver => true,
2213 
2214             // `ptrace_coredump` introduced in FreeBSD 14.
2215             "ptrace_coredump" if Some(14) > freebsd_ver => true,
2216 
2217             // `sockcred2` is not available in FreeBSD 12.
2218             "sockcred2" if Some(13) > freebsd_ver => true,
2219 
2220             _ => false,
2221         }
2222     });
2223 
2224     cfg.skip_fn(move |name| {
2225         // skip those that are manually verified
2226         match name {
2227             // FIXME: https://github.com/rust-lang/libc/issues/1272
2228             "execv" | "execve" | "execvp" | "execvpe" | "fexecve" => true,
2229 
2230             // These functions were added in FreeBSD 11:
2231             "fdatasync" | "mq_getfd_np" | "sendmmsg" | "recvmmsg" if Some(10) == freebsd_ver => {
2232                 true
2233             }
2234 
2235             // This function changed its return type from `int` in FreeBSD10 to
2236             // `ssize_t` in FreeBSD11:
2237             "aio_waitcomplete" if Some(10) == freebsd_ver => true,
2238 
2239             // The `uname` function in the `utsname.h` FreeBSD header is a C
2240             // inline function (has no symbol) that calls the `__xuname` symbol.
2241             // Therefore the function pointer comparison does not make sense for it.
2242             "uname" => true,
2243 
2244             // FIXME: Our API is unsound. The Rust API allows aliasing
2245             // pointers, but the C API requires pointers not to alias.
2246             // We should probably be at least using `&`/`&mut` here, see:
2247             // https://github.com/gnzlbg/ctest/issues/68
2248             "lio_listio" => true,
2249 
2250             // Those are introduced in FreeBSD 14.
2251             "sched_getaffinity" | "sched_setaffinity" | "sched_getcpu"
2252                 if Some(14) > freebsd_ver =>
2253             {
2254                 true
2255             }
2256 
2257             // This is not available in FreeBSD 12.
2258             "SOCKCRED2SIZE" if Some(13) > freebsd_ver => true,
2259 
2260             // Those are not available in FreeBSD 12.
2261             "memfd_create" | "shm_rename" if Some(13) > freebsd_ver => true,
2262 
2263             _ => false,
2264         }
2265     });
2266 
2267     cfg.skip_signededness(move |c| {
2268         match c {
2269             // FIXME: has a different sign in FreeBSD10
2270             "blksize_t" if Some(10) == freebsd_ver => true,
2271             _ => false,
2272         }
2273     });
2274 
2275     cfg.volatile_item(|i| {
2276         use ctest::VolatileItemKind::*;
2277         match i {
2278             // aio_buf is a volatile void** but since we cannot express that in
2279             // Rust types, we have to explicitly tell the checker about it here:
2280             StructField(ref n, ref f) if n == "aiocb" && f == "aio_buf" => true,
2281             _ => false,
2282         }
2283     });
2284 
2285     cfg.skip_field(move |struct_, field| {
2286         match (struct_, field) {
2287             // FIXME: `sa_sigaction` has type `sighandler_t` but that type is
2288             // incorrect, see: https://github.com/rust-lang/libc/issues/1359
2289             ("sigaction", "sa_sigaction") => true,
2290 
2291             // FIXME: in FreeBSD10 this field has type `char*` instead of
2292             // `void*`:
2293             ("stack_t", "ss_sp") if Some(10) == freebsd_ver => true,
2294 
2295             // conflicting with `p_type` macro from <resolve.h>.
2296             ("Elf32_Phdr", "p_type") => true,
2297             ("Elf64_Phdr", "p_type") => true,
2298 
2299             // not available until FreeBSD 12, and is an anonymous union there.
2300             ("xucred", "cr_pid__c_anonymous_union") => true,
2301 
2302             // m_owner field is a volatile __lwpid_t
2303             ("umutex", "m_owner") => true,
2304             // c_has_waiters field is a volatile int32_t
2305             ("ucond", "c_has_waiters") => true,
2306             // is PATH_MAX long but tests can't accept multi array as equivalent.
2307             ("kinfo_vmentry", "kve_path") => true,
2308 
2309             // a_un field is a union
2310             ("Elf32_Auxinfo", "a_un") => true,
2311             ("Elf64_Auxinfo", "a_un") => true,
2312 
2313             // union fields
2314             ("if_data", "__ifi_epoch") => true,
2315             ("if_data", "__ifi_lastchange") => true,
2316             ("ifreq", "ifr_ifru") => true,
2317 
2318             // anonymous struct
2319             ("devstat", "dev_links") => true,
2320 
2321             // FIXME: structs too complicated to bind for now...
2322             ("kinfo_proc", "ki_paddr") => true,
2323             ("kinfo_proc", "ki_addr") => true,
2324             ("kinfo_proc", "ki_tracep") => true,
2325             ("kinfo_proc", "ki_textvp") => true,
2326             ("kinfo_proc", "ki_fd") => true,
2327             ("kinfo_proc", "ki_vmspace") => true,
2328             ("kinfo_proc", "ki_pcb") => true,
2329             ("kinfo_proc", "ki_tdaddr") => true,
2330             ("kinfo_proc", "ki_pd") => true,
2331 
2332             // Anonymous type.
2333             ("filestat", "next") => true,
2334 
2335             // We ignore this field because we needed to use a hack in order to make rust 1.19
2336             // happy...
2337             ("kinfo_proc", "ki_sparestrings") => true,
2338 
2339             // `__sem_base` is a private struct field
2340             ("semid_ds", "__sem_base") => true,
2341 
2342             // `snap_time` is a `long double`, but it's a nightmare to bind correctly in rust
2343             // for the moment, so it's a best effort thing...
2344             ("statinfo", "snap_time") => true,
2345 
2346             _ => false,
2347         }
2348     });
2349 
2350     cfg.generate("../src/lib.rs", "main.rs");
2351 }
2352 
2353 fn test_emscripten(target: &str) {
2354     assert!(target.contains("emscripten"));
2355 
2356     let mut cfg = ctest_cfg();
2357     cfg.define("_GNU_SOURCE", None); // FIXME: ??
2358 
2359     headers! { cfg:
2360                "aio.h",
2361                "ctype.h",
2362                "dirent.h",
2363                "dlfcn.h",
2364                "errno.h",
2365                "fcntl.h",
2366                "glob.h",
2367                "grp.h",
2368                "ifaddrs.h",
2369                "langinfo.h",
2370                "limits.h",
2371                "locale.h",
2372                "malloc.h",
2373                "mntent.h",
2374                "mqueue.h",
2375                "net/ethernet.h",
2376                "net/if.h",
2377                "net/if_arp.h",
2378                "net/route.h",
2379                "netdb.h",
2380                "netinet/in.h",
2381                "netinet/ip.h",
2382                "netinet/tcp.h",
2383                "netinet/udp.h",
2384                "netpacket/packet.h",
2385                "poll.h",
2386                "pthread.h",
2387                "pty.h",
2388                "pwd.h",
2389                "resolv.h",
2390                "sched.h",
2391                "sched.h",
2392                "semaphore.h",
2393                "shadow.h",
2394                "signal.h",
2395                "stddef.h",
2396                "stdint.h",
2397                "stdio.h",
2398                "stdlib.h",
2399                "string.h",
2400                "sys/epoll.h",
2401                "sys/eventfd.h",
2402                "sys/file.h",
2403                "sys/ioctl.h",
2404                "sys/ipc.h",
2405                "sys/mman.h",
2406                "sys/mount.h",
2407                "sys/msg.h",
2408                "sys/personality.h",
2409                "sys/prctl.h",
2410                "sys/ptrace.h",
2411                "sys/quota.h",
2412                "sys/reboot.h",
2413                "sys/resource.h",
2414                "sys/sem.h",
2415                "sys/sendfile.h",
2416                "sys/shm.h",
2417                "sys/signalfd.h",
2418                "sys/socket.h",
2419                "sys/stat.h",
2420                "sys/statvfs.h",
2421                "sys/swap.h",
2422                "sys/syscall.h",
2423                "sys/sysctl.h",
2424                "sys/sysinfo.h",
2425                "sys/time.h",
2426                "sys/timerfd.h",
2427                "sys/times.h",
2428                "sys/types.h",
2429                "sys/uio.h",
2430                "sys/un.h",
2431                "sys/user.h",
2432                "sys/utsname.h",
2433                "sys/vfs.h",
2434                "sys/wait.h",
2435                "sys/xattr.h",
2436                "syslog.h",
2437                "termios.h",
2438                "time.h",
2439                "ucontext.h",
2440                "unistd.h",
2441                "utime.h",
2442                "utmp.h",
2443                "utmpx.h",
2444                "wchar.h",
2445     }
2446 
2447     cfg.type_name(move |ty, is_struct, is_union| {
2448         match ty {
2449             // Just pass all these through, no need for a "struct" prefix
2450             "FILE" | "fd_set" | "Dl_info" | "DIR" => ty.to_string(),
2451 
2452             t if is_union => format!("union {}", t),
2453 
2454             t if t.ends_with("_t") => t.to_string(),
2455 
2456             // put `struct` in front of all structs:.
2457             t if is_struct => format!("struct {}", t),
2458 
2459             t => t.to_string(),
2460         }
2461     });
2462 
2463     cfg.field_name(move |struct_, field| {
2464         match field {
2465             // Our stat *_nsec fields normally don't actually exist but are part
2466             // of a timeval struct
2467             s if s.ends_with("_nsec") && struct_.starts_with("stat") => {
2468                 s.replace("e_nsec", ".tv_nsec")
2469             }
2470             // FIXME: appears that `epoll_event.data` is an union
2471             "u64" if struct_ == "epoll_event" => "data.u64".to_string(),
2472             s => s.to_string(),
2473         }
2474     });
2475 
2476     cfg.skip_type(move |ty| {
2477         match ty {
2478             // sighandler_t is crazy across platforms
2479             // FIXME: is this necessary?
2480             "sighandler_t" => true,
2481 
2482             _ => false,
2483         }
2484     });
2485 
2486     cfg.skip_struct(move |ty| {
2487         match ty {
2488             // This is actually a union, not a struct
2489             // FIXME: is this necessary?
2490             "sigval" => true,
2491 
2492             // FIXME: It was removed in
2493             // emscripten-core/emscripten@953e414
2494             "pthread_mutexattr_t" => true,
2495 
2496             // FIXME: Investigate why the test fails.
2497             // Skip for now to unblock CI.
2498             "pthread_condattr_t" => true,
2499 
2500             _ => false,
2501         }
2502     });
2503 
2504     cfg.skip_fn(move |name| {
2505         match name {
2506             // FIXME: https://github.com/rust-lang/libc/issues/1272
2507             "execv" | "execve" | "execvp" | "execvpe" | "fexecve" => true,
2508 
2509             // FIXME: Investigate why CI is missing it.
2510             "clearenv" => true,
2511 
2512             _ => false,
2513         }
2514     });
2515 
2516     cfg.skip_const(move |name| {
2517         match name {
2518             // FIXME: deprecated - SIGNUNUSED was removed in glibc 2.26
2519             // users should use SIGSYS instead
2520             "SIGUNUSED" => true,
2521 
2522             // FIXME: emscripten uses different constants to constructs these
2523             n if n.contains("__SIZEOF_PTHREAD") => true,
2524 
2525             // FIXME: `SYS_gettid` was removed in
2526             // emscripten-core/emscripten@6d6474e
2527             "SYS_gettid" => true,
2528 
2529             _ => false,
2530         }
2531     });
2532 
2533     cfg.skip_field_type(move |struct_, field| {
2534         // This is a weird union, don't check the type.
2535         // FIXME: is this necessary?
2536         (struct_ == "ifaddrs" && field == "ifa_ifu") ||
2537         // sighandler_t type is super weird
2538         // FIXME: is this necessary?
2539         (struct_ == "sigaction" && field == "sa_sigaction") ||
2540         // sigval is actually a union, but we pretend it's a struct
2541         // FIXME: is this necessary?
2542         (struct_ == "sigevent" && field == "sigev_value") ||
2543         // aio_buf is "volatile void*" and Rust doesn't understand volatile
2544         // FIXME: is this necessary?
2545         (struct_ == "aiocb" && field == "aio_buf")
2546     });
2547 
2548     cfg.skip_field(move |struct_, field| {
2549         // this is actually a union on linux, so we can't represent it well and
2550         // just insert some padding.
2551         // FIXME: is this necessary?
2552         (struct_ == "siginfo_t" && field == "_pad") ||
2553         // musl names this __dummy1 but it's still there
2554         // FIXME: is this necessary?
2555         (struct_ == "glob_t" && field == "gl_flags") ||
2556         // musl seems to define this as an *anonymous* bitfield
2557         // FIXME: is this necessary?
2558         (struct_ == "statvfs" && field == "__f_unused") ||
2559         // sigev_notify_thread_id is actually part of a sigev_un union
2560         (struct_ == "sigevent" && field == "sigev_notify_thread_id") ||
2561         // signalfd had SIGSYS fields added in Linux 4.18, but no libc release has them yet.
2562         (struct_ == "signalfd_siginfo" && (field == "ssi_addr_lsb" ||
2563                                            field == "_pad2" ||
2564                                            field == "ssi_syscall" ||
2565                                            field == "ssi_call_addr" ||
2566                                            field == "ssi_arch"))
2567     });
2568 
2569     // FIXME: test linux like
2570     cfg.generate("../src/lib.rs", "main.rs");
2571 }
2572 
2573 fn test_vxworks(target: &str) {
2574     assert!(target.contains("vxworks"));
2575 
2576     let mut cfg = ctest::TestGenerator::new();
2577     headers! { cfg:
2578                "vxWorks.h",
2579                "yvals.h",
2580                "nfs/nfsCommon.h",
2581                "rtpLibCommon.h",
2582                "randomNumGen.h",
2583                "taskLib.h",
2584                "sysLib.h",
2585                "ioLib.h",
2586                "inetLib.h",
2587                "socket.h",
2588                "errnoLib.h",
2589                "ctype.h",
2590                "dirent.h",
2591                "dlfcn.h",
2592                "elf.h",
2593                "fcntl.h",
2594                "grp.h",
2595                "sys/poll.h",
2596                "ifaddrs.h",
2597                "langinfo.h",
2598                "limits.h",
2599                "link.h",
2600                "locale.h",
2601                "sys/stat.h",
2602                "netdb.h",
2603                "pthread.h",
2604                "pwd.h",
2605                "sched.h",
2606                "semaphore.h",
2607                "signal.h",
2608                "stddef.h",
2609                "stdint.h",
2610                "stdio.h",
2611                "stdlib.h",
2612                "string.h",
2613                "sys/file.h",
2614                "sys/ioctl.h",
2615                "sys/socket.h",
2616                "sys/time.h",
2617                "sys/times.h",
2618                "sys/types.h",
2619                "sys/uio.h",
2620                "sys/un.h",
2621                "sys/utsname.h",
2622                "sys/wait.h",
2623                "netinet/tcp.h",
2624                "syslog.h",
2625                "termios.h",
2626                "time.h",
2627                "ucontext.h",
2628                "unistd.h",
2629                "utime.h",
2630                "wchar.h",
2631                "errno.h",
2632                "sys/mman.h",
2633                "pathLib.h",
2634                "mqueue.h",
2635     }
2636     // FIXME
2637     cfg.skip_const(move |name| match name {
2638         // sighandler_t weirdness
2639         "SIG_DFL" | "SIG_ERR" | "SIG_IGN"
2640         // This is not defined in vxWorks
2641         | "RTLD_DEFAULT"   => true,
2642         _ => false,
2643     });
2644     // FIXME
2645     cfg.skip_type(move |ty| match ty {
2646         "stat64" | "sighandler_t" | "off64_t" => true,
2647         _ => false,
2648     });
2649 
2650     cfg.skip_field_type(move |struct_, field| match (struct_, field) {
2651         ("siginfo_t", "si_value") | ("stat", "st_size") | ("sigaction", "sa_u") => true,
2652         _ => false,
2653     });
2654 
2655     cfg.skip_roundtrip(move |s| match s {
2656         _ => false,
2657     });
2658 
2659     cfg.type_name(move |ty, is_struct, is_union| match ty {
2660         "DIR" | "FILE" | "Dl_info" | "RTP_DESC" => ty.to_string(),
2661         t if is_union => format!("union {}", t),
2662         t if t.ends_with("_t") => t.to_string(),
2663         t if is_struct => format!("struct {}", t),
2664         t => t.to_string(),
2665     });
2666 
2667     // FIXME
2668     cfg.skip_fn(move |name| match name {
2669         // sigval
2670         "sigqueue" | "_sigqueue"
2671         // sighandler_t
2672         | "signal"
2673         // not used in static linking by default
2674         | "dlerror" => true,
2675         _ => false,
2676     });
2677 
2678     cfg.generate("../src/lib.rs", "main.rs");
2679 }
2680 
2681 fn test_linux(target: &str) {
2682     assert!(target.contains("linux"));
2683 
2684     // target_env
2685     let gnu = target.contains("gnu");
2686     let musl = target.contains("musl");
2687     let uclibc = target.contains("uclibc");
2688 
2689     match (gnu, musl, uclibc) {
2690         (true, false, false) => (),
2691         (false, true, false) => (),
2692         (false, false, true) => (),
2693         (_, _, _) => panic!(
2694             "linux target lib is gnu: {}, musl: {}, uclibc: {}",
2695             gnu, musl, uclibc
2696         ),
2697     }
2698 
2699     let arm = target.contains("arm");
2700     let i686 = target.contains("i686");
2701     let mips = target.contains("mips");
2702     let mips32 = mips && !target.contains("64");
2703     let mips64 = mips && target.contains("64");
2704     let ppc64 = target.contains("powerpc64");
2705     let s390x = target.contains("s390x");
2706     let sparc64 = target.contains("sparc64");
2707     let x32 = target.contains("x32");
2708     let x86_32 = target.contains("i686");
2709     let x86_64 = target.contains("x86_64");
2710     let aarch64_musl = target.contains("aarch64") && musl;
2711     let gnuabihf = target.contains("gnueabihf");
2712     let x86_64_gnux32 = target.contains("gnux32") && x86_64;
2713     let riscv64 = target.contains("riscv64");
2714     let uclibc = target.contains("uclibc");
2715 
2716     let mut cfg = ctest_cfg();
2717     cfg.define("_GNU_SOURCE", None);
2718     // This macro re-deifnes fscanf,scanf,sscanf to link to the symbols that are
2719     // deprecated since glibc >= 2.29. This allows Rust binaries to link against
2720     // glibc versions older than 2.29.
2721     cfg.define("__GLIBC_USE_DEPRECATED_SCANF", None);
2722 
2723     headers! { cfg:
2724                "ctype.h",
2725                "dirent.h",
2726                "dlfcn.h",
2727                "elf.h",
2728                "fcntl.h",
2729                "glob.h",
2730                "grp.h",
2731                "iconv.h",
2732                "ifaddrs.h",
2733                "langinfo.h",
2734                "limits.h",
2735                "link.h",
2736                "locale.h",
2737                "malloc.h",
2738                "mntent.h",
2739                "mqueue.h",
2740                "net/ethernet.h",
2741                "net/if.h",
2742                "net/if_arp.h",
2743                "net/route.h",
2744                "netdb.h",
2745                "netinet/in.h",
2746                "netinet/ip.h",
2747                "netinet/tcp.h",
2748                "netinet/udp.h",
2749                "netpacket/packet.h",
2750                "poll.h",
2751                "pthread.h",
2752                "pty.h",
2753                "pwd.h",
2754                "regex.h",
2755                "resolv.h",
2756                "sched.h",
2757                "semaphore.h",
2758                "shadow.h",
2759                "signal.h",
2760                "spawn.h",
2761                "stddef.h",
2762                "stdint.h",
2763                "stdio.h",
2764                "stdlib.h",
2765                "string.h",
2766                "sys/epoll.h",
2767                "sys/eventfd.h",
2768                "sys/file.h",
2769                "sys/fsuid.h",
2770                "sys/inotify.h",
2771                "sys/ioctl.h",
2772                "sys/ipc.h",
2773                "sys/mman.h",
2774                "sys/mount.h",
2775                "sys/msg.h",
2776                "sys/personality.h",
2777                "sys/prctl.h",
2778                "sys/ptrace.h",
2779                "sys/quota.h",
2780                "sys/random.h",
2781                "sys/reboot.h",
2782                "sys/resource.h",
2783                "sys/sem.h",
2784                "sys/sendfile.h",
2785                "sys/shm.h",
2786                "sys/signalfd.h",
2787                "sys/socket.h",
2788                "sys/stat.h",
2789                "sys/statvfs.h",
2790                "sys/swap.h",
2791                "sys/syscall.h",
2792                "sys/time.h",
2793                "sys/timerfd.h",
2794                "sys/times.h",
2795                "sys/timex.h",
2796                "sys/types.h",
2797                "sys/uio.h",
2798                "sys/un.h",
2799                "sys/user.h",
2800                "sys/utsname.h",
2801                "sys/vfs.h",
2802                "sys/wait.h",
2803                "syslog.h",
2804                "termios.h",
2805                "time.h",
2806                "ucontext.h",
2807                "unistd.h",
2808                "utime.h",
2809                "utmp.h",
2810                "utmpx.h",
2811                "wchar.h",
2812                "errno.h",
2813                // `sys/io.h` is only available on x86*, Alpha, IA64, and 32-bit
2814                // ARM: https://bugzilla.redhat.com/show_bug.cgi?id=1116162
2815                // Also unavailable on gnuabihf with glibc 2.30.
2816                // https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=6b33f373c7b9199e00ba5fbafd94ac9bfb4337b1
2817                [(x86_64 || x86_32 || arm) && !gnuabihf]: "sys/io.h",
2818                // `sys/reg.h` is only available on x86 and x86_64
2819                [x86_64 || x86_32]: "sys/reg.h",
2820                // sysctl system call is deprecated and not available on musl
2821                // It is also unsupported in x32, deprecated since glibc 2.30:
2822                [!(x32 || musl || gnu)]: "sys/sysctl.h",
2823                // <execinfo.h> is not supported by musl:
2824                // https://www.openwall.com/lists/musl/2015/04/09/3
2825                // <execinfo.h> is not present on uclibc.
2826                [!(musl || uclibc)]: "execinfo.h",
2827     }
2828 
2829     // Include linux headers at the end:
2830     headers! {
2831         cfg:
2832         "asm/mman.h",
2833         "linux/can.h",
2834         "linux/can/raw.h",
2835         "linux/dccp.h",
2836         "linux/errqueue.h",
2837         "linux/falloc.h",
2838         "linux/filter.h",
2839         "linux/fs.h",
2840         "linux/futex.h",
2841         "linux/genetlink.h",
2842         "linux/if.h",
2843         "linux/if_addr.h",
2844         "linux/if_alg.h",
2845         "linux/if_ether.h",
2846         "linux/if_tun.h",
2847         "linux/input.h",
2848         "linux/keyctl.h",
2849         "linux/magic.h",
2850         "linux/memfd.h",
2851         "linux/mman.h",
2852         "linux/module.h",
2853         "linux/net_tstamp.h",
2854         "linux/netfilter/nfnetlink.h",
2855         "linux/netfilter/nfnetlink_log.h",
2856         "linux/netfilter/nfnetlink_queue.h",
2857         "linux/netfilter/nf_tables.h",
2858         "linux/netfilter_ipv4.h",
2859         "linux/netfilter_ipv6.h",
2860         "linux/netfilter_ipv6/ip6_tables.h",
2861         "linux/netlink.h",
2862         // FIXME: requires more recent kernel headers:
2863         // "linux/openat2.h",
2864         [!musl]: "linux/ptrace.h",
2865         "linux/quota.h",
2866         "linux/random.h",
2867         "linux/reboot.h",
2868         "linux/rtnetlink.h",
2869         "linux/sched.h",
2870         "linux/seccomp.h",
2871         "linux/sockios.h",
2872         "linux/uinput.h",
2873         "linux/vm_sockets.h",
2874         "linux/wait.h",
2875         "sys/fanotify.h",
2876         // <sys/auxv.h> is not present on uclibc
2877         [!uclibc]: "sys/auxv.h",
2878     }
2879 
2880     // note: aio.h must be included before sys/mount.h
2881     headers! {
2882         cfg:
2883         "sys/xattr.h",
2884         "sys/sysinfo.h",
2885         // AIO is not supported by uclibc:
2886         [!uclibc]: "aio.h",
2887     }
2888 
2889     cfg.type_name(move |ty, is_struct, is_union| {
2890         match ty {
2891             // Just pass all these through, no need for a "struct" prefix
2892             "FILE" | "fd_set" | "Dl_info" | "DIR" | "Elf32_Phdr" | "Elf64_Phdr" | "Elf32_Shdr"
2893             | "Elf64_Shdr" | "Elf32_Sym" | "Elf64_Sym" | "Elf32_Ehdr" | "Elf64_Ehdr"
2894             | "Elf32_Chdr" | "Elf64_Chdr" => ty.to_string(),
2895 
2896             "Ioctl" if gnu => "unsigned long".to_string(),
2897             "Ioctl" => "int".to_string(),
2898 
2899             t if is_union => format!("union {}", t),
2900 
2901             t if t.ends_with("_t") => t.to_string(),
2902 
2903             // In MUSL `flock64` is a typedef to `flock`.
2904             "flock64" if musl => format!("struct {}", ty),
2905 
2906             // put `struct` in front of all structs:.
2907             t if is_struct => format!("struct {}", t),
2908 
2909             t => t.to_string(),
2910         }
2911     });
2912 
2913     cfg.field_name(move |struct_, field| {
2914         match field {
2915             // Our stat *_nsec fields normally don't actually exist but are part
2916             // of a timeval struct
2917             s if s.ends_with("_nsec") && struct_.starts_with("stat") => {
2918                 s.replace("e_nsec", ".tv_nsec")
2919             }
2920             // FIXME: epoll_event.data is actually a union in C, but in Rust
2921             // it is only a u64 because we only expose one field
2922             // http://man7.org/linux/man-pages/man2/epoll_wait.2.html
2923             "u64" if struct_ == "epoll_event" => "data.u64".to_string(),
2924             // The following structs have a field called `type` in C,
2925             // but `type` is a Rust keyword, so these fields are translated
2926             // to `type_` in Rust.
2927             "type_"
2928                 if struct_ == "input_event"
2929                     || struct_ == "input_mask"
2930                     || struct_ == "ff_effect" =>
2931             {
2932                 "type".to_string()
2933             }
2934 
2935             s => s.to_string(),
2936         }
2937     });
2938 
2939     cfg.skip_type(move |ty| {
2940         match ty {
2941             // FIXME: `sighandler_t` type is incorrect, see:
2942             // https://github.com/rust-lang/libc/issues/1359
2943             "sighandler_t" => true,
2944 
2945             // These cannot be tested when "resolv.h" is included and are tested
2946             // in the `linux_elf.rs` file.
2947             "Elf64_Phdr" | "Elf32_Phdr" => true,
2948 
2949             // This type is private on Linux. It is implemented as a C `enum`
2950             // (`c_uint`) and this clashes with the type of the `rlimit` APIs
2951             // which expect a `c_int` even though both are ABI compatible.
2952             "__rlimit_resource_t" => true,
2953             // on Linux, this is a volatile int
2954             "pthread_spinlock_t" => true,
2955 
2956             // For internal use only, to define architecture specific ioctl constants with a libc specific type.
2957             "Ioctl" => true,
2958 
2959             _ => false,
2960         }
2961     });
2962 
2963     cfg.skip_struct(move |ty| {
2964         if ty.starts_with("__c_anonymous_") {
2965             return true;
2966         }
2967         // FIXME: musl CI has old headers
2968         if (musl || sparc64) && ty.starts_with("uinput_") {
2969             return true;
2970         }
2971         match ty {
2972             // These cannot be tested when "resolv.h" is included and are tested
2973             // in the `linux_elf.rs` file.
2974             "Elf64_Phdr" | "Elf32_Phdr" => true,
2975 
2976             // On Linux, the type of `ut_tv` field of `struct utmpx`
2977             // can be an anonymous struct, so an extra struct,
2978             // which is absent in glibc, has to be defined.
2979             "__timeval" => true,
2980 
2981             // FIXME: This is actually a union, not a struct
2982             "sigval" => true,
2983 
2984             // This type is tested in the `linux_termios.rs` file since there
2985             // are header conflicts when including them with all the other
2986             // structs.
2987             "termios2" => true,
2988 
2989             // FIXME: remove once we set minimum supported glibc version.
2990             // ucontext_t added a new field as of glibc 2.28; our struct definition is
2991             // conservative and omits the field, but that means the size doesn't match for newer
2992             // glibcs (see https://github.com/rust-lang/libc/issues/1410)
2993             "ucontext_t" if gnu => true,
2994 
2995             // FIXME: Somehow we cannot include headers correctly in glibc 2.30.
2996             // So let's ignore for now and re-visit later.
2997             // Probably related: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91085
2998             "statx" => true,
2999             "statx_timestamp" => true,
3000 
3001             // On Linux, the type of `ut_exit` field of struct `utmpx`
3002             // can be an anonymous struct, so an extra struct,
3003             // which is absent in musl, has to be defined.
3004             "__exit_status" if musl => true,
3005 
3006             // FIXME: CI's kernel header version is old.
3007             "sockaddr_can" => true,
3008 
3009             // Requires glibc 2.33 or newer.
3010             "mallinfo2" => true,
3011 
3012             // Might differ between kernel versions
3013             "open_how" => true,
3014 
3015             _ => false,
3016         }
3017     });
3018 
3019     cfg.skip_const(move |name| {
3020         if !gnu {
3021             // Skip definitions from the kernel on non-glibc Linux targets.
3022             // They're libc-independent, so we only need to check them on one
3023             // libc. We don't want to break CI if musl or another libc doesn't
3024             // have the definitions yet. (We do still want to check them on
3025             // every glibc target, though, as some of them can vary by
3026             // architecture.)
3027             //
3028             // This is not an exhaustive list of kernel constants, just a list
3029             // of prefixes of all those that have appeared here or that get
3030             // updated regularly and seem likely to cause breakage.
3031             if name.starts_with("AF_")
3032                 || name.starts_with("ARPHRD_")
3033                 || name.starts_with("EPOLL")
3034                 || name.starts_with("F_")
3035                 || name.starts_with("FALLOC_FL_")
3036                 || name.starts_with("IFLA_")
3037                 || name.starts_with("MS_")
3038                 || name.starts_with("MSG_")
3039                 || name.starts_with("P_")
3040                 || name.starts_with("PF_")
3041                 || name.starts_with("RLIMIT_")
3042                 || name.starts_with("SOL_")
3043                 || name.starts_with("STATX_")
3044                 || name.starts_with("SW_")
3045                 || name.starts_with("SYS_")
3046                 || name.starts_with("TCP_")
3047                 || name.starts_with("UINPUT_")
3048                 || name.starts_with("VMADDR_")
3049             {
3050                 return true;
3051             }
3052         }
3053         match name {
3054             // These constants are not available if gnu headers have been included
3055             // and can therefore not be tested here
3056             //
3057             // The IPV6 constants are tested in the `linux_ipv6.rs` tests:
3058             | "IPV6_FLOWINFO"
3059             | "IPV6_FLOWLABEL_MGR"
3060             | "IPV6_FLOWINFO_SEND"
3061             | "IPV6_FLOWINFO_FLOWLABEL"
3062             | "IPV6_FLOWINFO_PRIORITY"
3063             // The F_ fnctl constants are tested in the `linux_fnctl.rs` tests:
3064             | "F_CANCELLK"
3065             | "F_ADD_SEALS"
3066             | "F_GET_SEALS"
3067             | "F_SEAL_SEAL"
3068             | "F_SEAL_SHRINK"
3069             | "F_SEAL_GROW"
3070             | "F_SEAL_WRITE" => true,
3071             // The `ARPHRD_CAN` is tested in the `linux_if_arp.rs` tests
3072             // because including `linux/if_arp.h` causes some conflicts:
3073             "ARPHRD_CAN" => true,
3074 
3075             // Require Linux kernel 5.1:
3076             "F_SEAL_FUTURE_WRITE" => true,
3077 
3078             // FIXME: deprecated: not available in any header
3079             // See: https://github.com/rust-lang/libc/issues/1356
3080             "ENOATTR" => true,
3081 
3082             // FIXME: SIGUNUSED was removed in glibc 2.26
3083             // Users should use SIGSYS instead.
3084             "SIGUNUSED" => true,
3085 
3086             // FIXME: conflicts with glibc headers and is tested in
3087             // `linux_termios.rs` below:
3088             | "BOTHER"
3089             | "IBSHIFT"
3090             | "TCGETS2"
3091             | "TCSETS2"
3092             | "TCSETSW2"
3093             | "TCSETSF2" => true,
3094 
3095             // FIXME: on musl the pthread types are defined a little differently
3096             // - these constants are used by the glibc implementation.
3097             n if musl && n.contains("__SIZEOF_PTHREAD") => true,
3098 
3099             // FIXME: It was extended to 4096 since glibc 2.31 (Linux 5.4).
3100             // We should do so after a while.
3101             "SOMAXCONN" if gnu => true,
3102 
3103             // deprecated: not available from Linux kernel 5.6:
3104             "VMADDR_CID_RESERVED" => true,
3105 
3106             // Require Linux kernel 5.6:
3107             "VMADDR_CID_LOCAL" => true,
3108 
3109             // Requires Linux kernel 5.7:
3110             "MREMAP_DONTUNMAP" => true,
3111 
3112             // IPPROTO_MAX was increased in 5.6 for IPPROTO_MPTCP:
3113             | "IPPROTO_MAX"
3114             | "IPPROTO_MPTCP" => true,
3115 
3116             // FIXME: Not currently available in headers
3117             "P_PIDFD" if mips => true,
3118             "SYS_pidfd_open" if mips => true,
3119 
3120             // FIXME: Not currently available in headers on MIPS
3121             // Not yet implemented on sparc64
3122             "SYS_clone3" if mips | sparc64 => true,
3123 
3124             // FIXME: these syscalls were added in Linux 5.9 or later
3125             // and are currently not included in the glibc headers.
3126             | "SYS_close_range"
3127             | "SYS_openat2"
3128             | "SYS_pidfd_getfd"
3129             | "SYS_faccessat2"
3130             | "SYS_process_madvise"
3131             | "SYS_epoll_pwait2"
3132             | "SYS_mount_setattr" => true,
3133 
3134             // Requires more recent kernel headers:
3135             | "IFLA_PROP_LIST"
3136             | "IFLA_ALT_IFNAME"
3137             | "IFLA_PERM_ADDRESS"
3138             | "IFLA_PROTO_DOWN_REASON" => true,
3139 
3140             // FIXME: They require recent kernel header:
3141             | "CAN_J1939"
3142             | "CAN_RAW_FILTER_MAX"
3143             | "CAN_NPROTO" => true,
3144 
3145             // FIXME: Requires recent kernel headers (5.8):
3146             "STATX_MNT_ID" => true,
3147 
3148             // FIXME: requires more recent kernel headers on CI
3149             | "UINPUT_VERSION"
3150             | "SW_MAX"
3151             | "SW_CNT"
3152                 if mips || ppc64 || riscv64 || sparc64 => true,
3153 
3154             // FIXME: Requires more recent kernel headers (5.9 / 5.11):
3155             | "CLOSE_RANGE_UNSHARE"
3156             | "CLOSE_RANGE_CLOEXEC" => true,
3157 
3158             // FIXME: requires more recent kernel headers:
3159             | "RESOLVE_BENEATH"
3160             | "RESOLVE_CACHED"
3161             | "RESOLVE_IN_ROOT"
3162             | "RESOLVE_NO_MAGICLINKS"
3163             | "RESOLVE_NO_SYMLINKS"
3164             | "RESOLVE_NO_XDEV" => true,
3165 
3166             // FIXME: Not currently available in headers on ARM, MIPS and musl.
3167             "NETLINK_GET_STRICT_CHK" if arm || mips || musl => true,
3168 
3169             // kernel constants not available in uclibc 1.0.34
3170             | "EXTPROC"
3171             | "FAN_MARK_FILESYSTEM"
3172             | "FAN_MARK_INODE"
3173             | "IPPROTO_BEETPH"
3174             | "IPPROTO_MPLS"
3175             | "IPV6_HDRINCL"
3176             | "IPV6_MULTICAST_ALL"
3177             | "IPV6_PMTUDISC_INTERFACE"
3178             | "IPV6_PMTUDISC_OMIT"
3179             | "IPV6_ROUTER_ALERT_ISOLATE"
3180             | "PACKET_MR_UNICAST"
3181             | "RUSAGE_THREAD"
3182             | "SHM_EXEC"
3183             | "UDP_GRO"
3184             | "UDP_SEGMENT"
3185                 if uclibc => true,
3186 
3187             // headers conflicts with linux/pidfd.h
3188             "PIDFD_NONBLOCK" => true,
3189 
3190             // is a private value for kernel usage normally
3191             "FUSE_SUPER_MAGIC" => true,
3192 
3193             _ => false,
3194         }
3195     });
3196 
3197     cfg.skip_fn(move |name| {
3198         // skip those that are manually verified
3199         match name {
3200             // FIXME: https://github.com/rust-lang/libc/issues/1272
3201             "execv" | "execve" | "execvp" | "execvpe" | "fexecve" => true,
3202 
3203             // There are two versions of the sterror_r function, see
3204             //
3205             // https://linux.die.net/man/3/strerror_r
3206             //
3207             // An XSI-compliant version provided if:
3208             //
3209             // (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600)
3210             //  && ! _GNU_SOURCE
3211             //
3212             // and a GNU specific version provided if _GNU_SOURCE is defined.
3213             //
3214             // libc provides bindings for the XSI-compliant version, which is
3215             // preferred for portable applications.
3216             //
3217             // We skip the test here since here _GNU_SOURCE is defined, and
3218             // test the XSI version below.
3219             "strerror_r" => true,
3220 
3221             // FIXME: Our API is unsound. The Rust API allows aliasing
3222             // pointers, but the C API requires pointers not to alias.
3223             // We should probably be at least using `&`/`&mut` here, see:
3224             // https://github.com/gnzlbg/ctest/issues/68
3225             "lio_listio" if musl => true,
3226 
3227             // FIXME: the glibc version used by the Sparc64 build jobs
3228             // which use Debian 10.0 is too old.
3229             "statx" if sparc64 => true,
3230 
3231             // FIXME: Deprecated since glibc 2.30. Remove fn once upstream does.
3232             "sysctl" if gnu => true,
3233 
3234             // FIXME: It now takes c_void instead of timezone since glibc 2.31.
3235             "gettimeofday" if gnu => true,
3236 
3237             // These are all implemented as static inline functions in uclibc, so
3238             // they cannot be linked against.
3239             // If implementations are required, they might need to be implemented
3240             // in this crate.
3241             "posix_spawnattr_init" if uclibc => true,
3242             "posix_spawnattr_destroy" if uclibc => true,
3243             "posix_spawnattr_getsigdefault" if uclibc => true,
3244             "posix_spawnattr_setsigdefault" if uclibc => true,
3245             "posix_spawnattr_getsigmask" if uclibc => true,
3246             "posix_spawnattr_setsigmask" if uclibc => true,
3247             "posix_spawnattr_getflags" if uclibc => true,
3248             "posix_spawnattr_setflags" if uclibc => true,
3249             "posix_spawnattr_getpgroup" if uclibc => true,
3250             "posix_spawnattr_setpgroup" if uclibc => true,
3251             "posix_spawnattr_getschedpolicy" if uclibc => true,
3252             "posix_spawnattr_setschedpolicy" if uclibc => true,
3253             "posix_spawnattr_getschedparam" if uclibc => true,
3254             "posix_spawnattr_setschedparam" if uclibc => true,
3255             "posix_spawn_file_actions_init" if uclibc => true,
3256             "posix_spawn_file_actions_destroy" if uclibc => true,
3257 
3258             // uclibc defines the flags type as a uint, but dependent crates
3259             // assume it's a int instead.
3260             "getnameinfo" if uclibc => true,
3261 
3262             // FIXME: This needs musl 1.2.2 or later.
3263             "gettid" if musl => true,
3264 
3265             // Needs glibc 2.33 or later.
3266             "mallinfo2" => true,
3267 
3268             "reallocarray" if musl => true,
3269 
3270             // Not defined in uclibc as of 1.0.34
3271             "gettid" if uclibc => true,
3272 
3273             _ => false,
3274         }
3275     });
3276 
3277     cfg.skip_field_type(move |struct_, field| {
3278         // This is a weird union, don't check the type.
3279         (struct_ == "ifaddrs" && field == "ifa_ifu") ||
3280         // sighandler_t type is super weird
3281         (struct_ == "sigaction" && field == "sa_sigaction") ||
3282         // __timeval type is a patch which doesn't exist in glibc
3283         (struct_ == "utmpx" && field == "ut_tv") ||
3284         // sigval is actually a union, but we pretend it's a struct
3285         (struct_ == "sigevent" && field == "sigev_value") ||
3286         // this one is an anonymous union
3287         (struct_ == "ff_effect" && field == "u") ||
3288         // `__exit_status` type is a patch which is absent in musl
3289         (struct_ == "utmpx" && field == "ut_exit" && musl) ||
3290         // `can_addr` is an anonymous union
3291         (struct_ == "sockaddr_can" && field == "can_addr")
3292     });
3293 
3294     cfg.volatile_item(|i| {
3295         use ctest::VolatileItemKind::*;
3296         match i {
3297             // aio_buf is a volatile void** but since we cannot express that in
3298             // Rust types, we have to explicitly tell the checker about it here:
3299             StructField(ref n, ref f) if n == "aiocb" && f == "aio_buf" => true,
3300             _ => false,
3301         }
3302     });
3303 
3304     cfg.skip_field(move |struct_, field| {
3305         // this is actually a union on linux, so we can't represent it well and
3306         // just insert some padding.
3307         (struct_ == "siginfo_t" && field == "_pad") ||
3308         // musl names this __dummy1 but it's still there
3309         (musl && struct_ == "glob_t" && field == "gl_flags") ||
3310         // musl seems to define this as an *anonymous* bitfield
3311         (musl && struct_ == "statvfs" && field == "__f_unused") ||
3312         // sigev_notify_thread_id is actually part of a sigev_un union
3313         (struct_ == "sigevent" && field == "sigev_notify_thread_id") ||
3314         // signalfd had SIGSYS fields added in Linux 4.18, but no libc release
3315         // has them yet.
3316         (struct_ == "signalfd_siginfo" && (field == "ssi_addr_lsb" ||
3317                                            field == "_pad2" ||
3318                                            field == "ssi_syscall" ||
3319                                            field == "ssi_call_addr" ||
3320                                            field == "ssi_arch")) ||
3321         // FIXME: After musl 1.1.24, it have only one field `sched_priority`,
3322         // while other fields become reserved.
3323         (struct_ == "sched_param" && [
3324             "sched_ss_low_priority",
3325             "sched_ss_repl_period",
3326             "sched_ss_init_budget",
3327             "sched_ss_max_repl",
3328         ].contains(&field) && musl) ||
3329         // FIXME: After musl 1.1.24, the type becomes `int` instead of `unsigned short`.
3330         (struct_ == "ipc_perm" && field == "__seq" && aarch64_musl) ||
3331         // glibc uses unnamed fields here and Rust doesn't support that yet
3332         (struct_ == "timex" && field.starts_with("__unused")) ||
3333         // FIXME: It now takes mode_t since glibc 2.31 on some targets.
3334         (struct_ == "ipc_perm" && field == "mode"
3335             && ((x86_64 || i686 || arm || riscv64) && gnu || x86_64_gnux32)
3336         ) ||
3337         // the `u` field is in fact an anonymous union
3338         (gnu && struct_ == "ptrace_syscall_info" && (field == "u" || field == "pad"))
3339     });
3340 
3341     cfg.skip_roundtrip(move |s| match s {
3342         // FIXME:
3343         "utsname" if mips32 || mips64 => true,
3344         // FIXME:
3345         "mcontext_t" if s390x => true,
3346         // FIXME: This is actually a union.
3347         "fpreg_t" if s390x => true,
3348 
3349         "sockaddr_un" | "sembuf" | "ff_constant_effect" if mips32 && (gnu || musl) => true,
3350         "ipv6_mreq"
3351         | "ip_mreq_source"
3352         | "sockaddr_in6"
3353         | "sockaddr_ll"
3354         | "in_pktinfo"
3355         | "arpreq"
3356         | "arpreq_old"
3357         | "sockaddr_un"
3358         | "ff_constant_effect"
3359         | "ff_ramp_effect"
3360         | "ff_condition_effect"
3361         | "Elf32_Ehdr"
3362         | "Elf32_Chdr"
3363         | "ucred"
3364         | "in6_pktinfo"
3365         | "sockaddr_nl"
3366         | "termios"
3367         | "nlmsgerr"
3368             if (mips64 || sparc64) && gnu =>
3369         {
3370             true
3371         }
3372 
3373         // FIXME: the call ABI of max_align_t is incorrect on these platforms:
3374         "max_align_t" if i686 || mips64 || ppc64 => true,
3375 
3376         _ => false,
3377     });
3378 
3379     cfg.generate("../src/lib.rs", "main.rs");
3380 
3381     test_linux_like_apis(target);
3382 }
3383 
3384 // This function tests APIs that are incompatible to test when other APIs
3385 // are included (e.g. because including both sets of headers clashes)
3386 fn test_linux_like_apis(target: &str) {
3387     let gnu = target.contains("gnu");
3388     let musl = target.contains("musl");
3389     let linux = target.contains("linux");
3390     let emscripten = target.contains("emscripten");
3391     let android = target.contains("android");
3392     assert!(linux || android || emscripten);
3393 
3394     if linux || android || emscripten {
3395         // test strerror_r from the `string.h` header
3396         let mut cfg = ctest_cfg();
3397         cfg.skip_type(|_| true).skip_static(|_| true);
3398 
3399         headers! { cfg: "string.h" }
3400         cfg.skip_fn(|f| match f {
3401             "strerror_r" => false,
3402             _ => true,
3403         })
3404         .skip_const(|_| true)
3405         .skip_struct(|_| true);
3406         cfg.generate("../src/lib.rs", "linux_strerror_r.rs");
3407     }
3408 
3409     if linux || android || emscripten {
3410         // test fcntl - see:
3411         // http://man7.org/linux/man-pages/man2/fcntl.2.html
3412         let mut cfg = ctest_cfg();
3413 
3414         if musl {
3415             cfg.header("fcntl.h");
3416         } else {
3417             cfg.header("linux/fcntl.h");
3418         }
3419 
3420         cfg.skip_type(|_| true)
3421             .skip_static(|_| true)
3422             .skip_struct(|_| true)
3423             .skip_fn(|_| true)
3424             .skip_const(move |name| match name {
3425                 // test fcntl constants:
3426                 "F_CANCELLK" | "F_ADD_SEALS" | "F_GET_SEALS" | "F_SEAL_SEAL" | "F_SEAL_SHRINK"
3427                 | "F_SEAL_GROW" | "F_SEAL_WRITE" => false,
3428                 _ => true,
3429             })
3430             .type_name(move |ty, is_struct, is_union| match ty {
3431                 t if is_struct => format!("struct {}", t),
3432                 t if is_union => format!("union {}", t),
3433                 t => t.to_string(),
3434             });
3435 
3436         cfg.generate("../src/lib.rs", "linux_fcntl.rs");
3437     }
3438 
3439     if linux || android {
3440         // test termios
3441         let mut cfg = ctest_cfg();
3442         cfg.header("asm/termbits.h");
3443         cfg.header("linux/termios.h");
3444         cfg.skip_type(|_| true)
3445             .skip_static(|_| true)
3446             .skip_fn(|_| true)
3447             .skip_const(|c| match c {
3448                 "BOTHER" | "IBSHIFT" => false,
3449                 "TCGETS2" | "TCSETS2" | "TCSETSW2" | "TCSETSF2" => false,
3450                 _ => true,
3451             })
3452             .skip_struct(|s| s != "termios2")
3453             .type_name(move |ty, is_struct, is_union| match ty {
3454                 "Ioctl" if gnu => "unsigned long".to_string(),
3455                 "Ioctl" => "int".to_string(),
3456                 t if is_struct => format!("struct {}", t),
3457                 t if is_union => format!("union {}", t),
3458                 t => t.to_string(),
3459             });
3460         cfg.generate("../src/lib.rs", "linux_termios.rs");
3461     }
3462 
3463     if linux || android {
3464         // test IPV6_ constants:
3465         let mut cfg = ctest_cfg();
3466         headers! {
3467             cfg:
3468             "linux/in6.h"
3469         }
3470         cfg.skip_type(|_| true)
3471             .skip_static(|_| true)
3472             .skip_fn(|_| true)
3473             .skip_const(|_| true)
3474             .skip_struct(|_| true)
3475             .skip_const(move |name| match name {
3476                 "IPV6_FLOWINFO"
3477                 | "IPV6_FLOWLABEL_MGR"
3478                 | "IPV6_FLOWINFO_SEND"
3479                 | "IPV6_FLOWINFO_FLOWLABEL"
3480                 | "IPV6_FLOWINFO_PRIORITY" => false,
3481                 _ => true,
3482             })
3483             .type_name(move |ty, is_struct, is_union| match ty {
3484                 t if is_struct => format!("struct {}", t),
3485                 t if is_union => format!("union {}", t),
3486                 t => t.to_string(),
3487             });
3488         cfg.generate("../src/lib.rs", "linux_ipv6.rs");
3489     }
3490 
3491     if linux || android {
3492         // Test Elf64_Phdr and Elf32_Phdr
3493         // These types have a field called `p_type`, but including
3494         // "resolve.h" defines a `p_type` macro that expands to `__p_type`
3495         // making the tests for these fails when both are included.
3496         let mut cfg = ctest_cfg();
3497         cfg.header("elf.h");
3498         cfg.skip_fn(|_| true)
3499             .skip_static(|_| true)
3500             .skip_const(|_| true)
3501             .type_name(move |ty, _is_struct, _is_union| ty.to_string())
3502             .skip_struct(move |ty| match ty {
3503                 "Elf64_Phdr" | "Elf32_Phdr" => false,
3504                 _ => true,
3505             })
3506             .skip_type(move |ty| match ty {
3507                 "Elf64_Phdr" | "Elf32_Phdr" => false,
3508                 _ => true,
3509             });
3510         cfg.generate("../src/lib.rs", "linux_elf.rs");
3511     }
3512 
3513     if linux || android {
3514         // Test `ARPHRD_CAN`.
3515         let mut cfg = ctest_cfg();
3516         cfg.header("linux/if_arp.h");
3517         cfg.skip_fn(|_| true)
3518             .skip_static(|_| true)
3519             .skip_const(move |name| match name {
3520                 "ARPHRD_CAN" => false,
3521                 _ => true,
3522             })
3523             .skip_struct(|_| true)
3524             .skip_type(|_| true);
3525         cfg.generate("../src/lib.rs", "linux_if_arp.rs");
3526     }
3527 }
3528 
3529 fn which_freebsd() -> Option<i32> {
3530     let output = std::process::Command::new("freebsd-version")
3531         .output()
3532         .ok()?;
3533     if !output.status.success() {
3534         return None;
3535     }
3536 
3537     let stdout = String::from_utf8(output.stdout).ok()?;
3538 
3539     match &stdout {
3540         s if s.starts_with("10") => Some(10),
3541         s if s.starts_with("11") => Some(11),
3542         s if s.starts_with("12") => Some(12),
3543         s if s.starts_with("13") => Some(13),
3544         s if s.starts_with("14") => Some(14),
3545         _ => None,
3546     }
3547 }
3548 
3549 fn test_haiku(target: &str) {
3550     assert!(target.contains("haiku"));
3551 
3552     let mut cfg = ctest_cfg();
3553     cfg.flag("-Wno-deprecated-declarations");
3554     cfg.define("__USE_GNU", Some("1"));
3555 
3556     // POSIX API
3557     headers! { cfg:
3558                "alloca.h",
3559                "arpa/inet.h",
3560                "arpa/nameser.h",
3561                "arpa/nameser_compat.h",
3562                "assert.h",
3563                "bsd_mem.h",
3564                "complex.h",
3565                "ctype.h",
3566                "dirent.h",
3567                "div_t.h",
3568                "dlfcn.h",
3569                "endian.h",
3570                "errno.h",
3571                "fcntl.h",
3572                "fenv.h",
3573                "fnmatch.h",
3574                "fts.h",
3575                "ftw.h",
3576                "getopt.h",
3577                "glob.h",
3578                "grp.h",
3579                "inttypes.h",
3580                "iovec.h",
3581                "langinfo.h",
3582                "libgen.h",
3583                "libio.h",
3584                "limits.h",
3585                "locale.h",
3586                "malloc.h",
3587                "malloc_debug.h",
3588                "math.h",
3589                "memory.h",
3590                "monetary.h",
3591                "net/if.h",
3592                "net/if_dl.h",
3593                "net/if_media.h",
3594                "net/if_tun.h",
3595                "net/if_types.h",
3596                "net/route.h",
3597                "netdb.h",
3598                "netinet/icmp6.h",
3599                "netinet/in.h",
3600                "netinet/ip.h",
3601                "netinet/ip6.h",
3602                "netinet/ip_icmp.h",
3603                "netinet/ip_var.h",
3604                "netinet/tcp.h",
3605                "netinet/udp.h",
3606                "netinet6/in6.h",
3607                "nl_types.h",
3608                "null.h",
3609                "poll.h",
3610                "pthread.h",
3611                "pwd.h",
3612                "regex.h",
3613                "resolv.h",
3614                "sched.h",
3615                "search.h",
3616                "semaphore.h",
3617                "setjmp.h",
3618                "shadow.h",
3619                "signal.h",
3620                "size_t.h",
3621                "spawn.h",
3622                "stdint.h",
3623                "stdio.h",
3624                "stdlib.h",
3625                "string.h",
3626                "strings.h",
3627                "sys/cdefs.h",
3628                "sys/file.h",
3629                "sys/ioctl.h",
3630                "sys/ipc.h",
3631                "sys/mman.h",
3632                "sys/msg.h",
3633                "sys/param.h",
3634                "sys/poll.h",
3635                "sys/resource.h",
3636                "sys/select.h",
3637                "sys/sem.h",
3638                "sys/socket.h",
3639                "sys/sockio.h",
3640                "sys/stat.h",
3641                "sys/statvfs.h",
3642                "sys/time.h",
3643                "sys/timeb.h",
3644                "sys/times.h",
3645                "sys/types.h",
3646                "sys/uio.h",
3647                "sys/un.h",
3648                "sys/utsname.h",
3649                "sys/wait.h",
3650                "syslog.h",
3651                "tar.h",
3652                "termios.h",
3653                "time.h",
3654                "uchar.h",
3655                "unistd.h",
3656                "utime.h",
3657                "wchar.h",
3658                "wchar_t.h",
3659                "wctype.h"
3660     }
3661 
3662     // BSD Extensions
3663     headers! { cfg:
3664                "pty.h",
3665     }
3666 
3667     // Native API
3668     headers! { cfg:
3669                "kernel/OS.h",
3670                "kernel/fs_attr.h",
3671                "kernel/fs_index.h",
3672                "kernel/fs_info.h",
3673                "kernel/fs_query.h",
3674                "kernel/fs_volume.h",
3675                "kernel/image.h",
3676                "kernel/scheduler.h",
3677                "storage/FindDirectory.h",
3678                "storage/StorageDefs.h",
3679                "support/Errors.h",
3680                "support/SupportDefs.h",
3681                "support/TypeConstants.h"
3682     }
3683 
3684     cfg.skip_struct(move |ty| {
3685         if ty.starts_with("__c_anonymous_") {
3686             return true;
3687         }
3688         match ty {
3689             // FIXME: actually a union
3690             "sigval" => true,
3691             // FIXME: locale_t does not exist on Haiku
3692             "locale_t" => true,
3693             // FIXME: rusage has a different layout on Haiku
3694             "rusage" => true,
3695             // FIXME?: complains that rust aligns on 4 byte boundary, but
3696             //         Haiku does not align it at all.
3697             "in6_addr" => true,
3698             // The d_name attribute is an array of 1 on Haiku, with the
3699             // intention that the developer allocates a larger or smaller
3700             // piece of memory depending on the expected/actual size of the name.
3701             // Other platforms have sensible defaults. In Rust, the d_name field
3702             // is sized as the _POSIX_MAX_PATH, so that path names will fit in
3703             // newly allocated dirent objects. This breaks the automated tests.
3704             "dirent" => true,
3705             // The following structs contain function pointers, which cannot be initialized
3706             // with mem::zeroed(), so skip the automated test
3707             "image_info" | "thread_info" => true,
3708 
3709             _ => false,
3710         }
3711     });
3712 
3713     cfg.skip_type(move |ty| {
3714         match ty {
3715             // FIXME: locale_t does not exist on Haiku
3716             "locale_t" => true,
3717             // These cause errors, to be reviewed in the future
3718             "sighandler_t" => true,
3719             "pthread_t" => true,
3720             "pthread_condattr_t" => true,
3721             "pthread_mutexattr_t" => true,
3722             "pthread_rwlockattr_t" => true,
3723             _ => false,
3724         }
3725     });
3726 
3727     cfg.skip_fn(move |name| {
3728         // skip those that are manually verified
3729         match name {
3730             // FIXME: https://github.com/rust-lang/libc/issues/1272
3731             "execv" | "execve" | "execvp" | "execvpe" => true,
3732             // FIXME: does not exist on haiku
3733             "open_wmemstream" => true,
3734             "mlockall" | "munlockall" => true,
3735             "tcgetsid" => true,
3736             "cfsetspeed" => true,
3737             // ignore for now, will be part of Haiku R1 beta 3
3738             "mlock" | "munlock" => true,
3739             // returns const char * on Haiku
3740             "strsignal" => true,
3741             // uses an enum as a parameter argument, which is incorrectly
3742             // translated into a struct argument
3743             "find_path" => true,
3744 
3745             _ => false,
3746         }
3747     });
3748 
3749     cfg.skip_const(move |name| {
3750         match name {
3751             // FIXME: these constants do not exist on Haiku
3752             "DT_UNKNOWN" | "DT_FIFO" | "DT_CHR" | "DT_DIR" | "DT_BLK" | "DT_REG" | "DT_LNK"
3753             | "DT_SOCK" => true,
3754             "USRQUOTA" | "GRPQUOTA" => true,
3755             "SIGIOT" => true,
3756             "ARPOP_REQUEST" | "ARPOP_REPLY" | "ATF_COM" | "ATF_PERM" | "ATF_PUBL"
3757             | "ATF_USETRAILERS" => true,
3758             // Haiku does not have MAP_FILE, but rustc requires it
3759             "MAP_FILE" => true,
3760             // The following does not exist on Haiku but is required by
3761             // several crates
3762             "FIOCLEX" => true,
3763             // just skip this one, it is not defined on Haiku beta 2 but
3764             // since it is meant as a mask and not a parameter it can exist
3765             // here
3766             "LOG_PRIMASK" => true,
3767             // not defined on Haiku, but [get|set]priority is, so they are
3768             // useful
3769             "PRIO_MIN" | "PRIO_MAX" => true,
3770             //
3771             _ => false,
3772         }
3773     });
3774 
3775     cfg.skip_field(move |struct_, field| {
3776         match (struct_, field) {
3777             // FIXME: the stat struct actually has timespec members, whereas
3778             //        the current representation has these unpacked.
3779             ("stat", "st_atime") => true,
3780             ("stat", "st_atime_nsec") => true,
3781             ("stat", "st_mtime") => true,
3782             ("stat", "st_mtime_nsec") => true,
3783             ("stat", "st_ctime") => true,
3784             ("stat", "st_ctime_nsec") => true,
3785             ("stat", "st_crtime") => true,
3786             ("stat", "st_crtime_nsec") => true,
3787 
3788             // these are actually unions, but we cannot represent it well
3789             ("siginfo_t", "sigval") => true,
3790             ("sem_t", "named_sem_id") => true,
3791             ("sigaction", "sa_sigaction") => true,
3792             ("sigevent", "sigev_value") => true,
3793             ("fpu_state", "_fpreg") => true,
3794             // these fields have a simplified data definition in libc
3795             ("fpu_state", "_xmm") => true,
3796             ("savefpu", "_fp_ymm") => true,
3797 
3798             // skip these enum-type fields
3799             ("thread_info", "state") => true,
3800             ("image_info", "image_type") => true,
3801             _ => false,
3802         }
3803     });
3804 
3805     cfg.skip_roundtrip(move |s| match s {
3806         // FIXME: for some reason the roundtrip check fails for cpu_info
3807         "cpu_info" => true,
3808         _ => false,
3809     });
3810 
3811     cfg.type_name(move |ty, is_struct, is_union| {
3812         match ty {
3813             // Just pass all these through, no need for a "struct" prefix
3814             "area_info" | "port_info" | "port_message_info" | "team_info" | "sem_info"
3815             | "team_usage_info" | "thread_info" | "cpu_info" | "system_info"
3816             | "object_wait_info" | "image_info" | "attr_info" | "index_info" | "fs_info"
3817             | "FILE" | "DIR" | "Dl_info" => ty.to_string(),
3818 
3819             // is actually a union
3820             "sigval" => format!("union sigval"),
3821             t if is_union => format!("union {}", t),
3822             t if t.ends_with("_t") => t.to_string(),
3823             t if is_struct => format!("struct {}", t),
3824             t => t.to_string(),
3825         }
3826     });
3827 
3828     cfg.field_name(move |struct_, field| {
3829         match field {
3830             // Field is named `type` in C but that is a Rust keyword,
3831             // so these fields are translated to `type_` in the bindings.
3832             "type_" if struct_ == "object_wait_info" => "type".to_string(),
3833             "type_" if struct_ == "sem_t" => "type".to_string(),
3834             "type_" if struct_ == "attr_info" => "type".to_string(),
3835             "type_" if struct_ == "index_info" => "type".to_string(),
3836             "image_type" if struct_ == "image_info" => "type".to_string(),
3837             s => s.to_string(),
3838         }
3839     });
3840     cfg.generate("../src/lib.rs", "main.rs");
3841 }
3842