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