summaryrefslogtreecommitdiff
path: root/libc/libc-test/build.rs
blob: ffab4523017ee26cdb49c282bd07c5490fd7f93b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
#![deny(warnings)]

extern crate ctest;

use std::env;

fn main() {
    let target = env::var("TARGET").unwrap();
    let aarch64 = target.contains("aarch64");
    let i686 = target.contains("i686");
    let x86_64 = target.contains("x86_64");
    let x32 = target.ends_with("gnux32");
    let windows = target.contains("windows");
    let mingw = target.contains("windows-gnu");
    let linux = target.contains("unknown-linux");
    let android = target.contains("android");
    let apple = target.contains("apple");
    let ios = target.contains("apple-ios");
    let emscripten = target.contains("asm");
    let musl = target.contains("musl") || emscripten;
    let uclibc = target.contains("uclibc");
    let freebsd = target.contains("freebsd");
    let dragonfly = target.contains("dragonfly");
    let mips = target.contains("mips");
    let netbsd = target.contains("netbsd");
    let openbsd = target.contains("openbsd");
    let rumprun = target.contains("rumprun");
    let solaris = target.contains("solaris");
    let cloudabi = target.contains("cloudabi");
    let redox = target.contains("redox");
    let bsdlike = freebsd || apple || netbsd || openbsd || dragonfly;
    let mut cfg = ctest::TestGenerator::new();

    // Pull in extra goodies
    if linux || android || emscripten {
        cfg.define("_GNU_SOURCE", None);
    } else if netbsd {
        cfg.define("_NETBSD_SOURCE", Some("1"));
    } else if apple {
        cfg.define("__APPLE_USE_RFC_3542", None);
    } else if windows {
        cfg.define("_WIN32_WINNT", Some("0x8000"));
    } else if solaris {
        cfg.define("_XOPEN_SOURCE", Some("700"));
        cfg.define("__EXTENSIONS__", None);
        cfg.define("_LCONV_C99", None);
    } else if freebsd {
        cfg.define("_WITH_GETLINE", None);
    }

    // Android doesn't actually have in_port_t but it's much easier if we
    // provide one for us to test against
    if android {
        cfg.define("in_port_t", Some("uint16_t"));
    }

    cfg.header("errno.h")
        .header("fcntl.h")
        .header("limits.h")
        .header("locale.h")
        .header("stddef.h")
        .header("stdint.h")
        .header("stdio.h")
        .header("stdlib.h")
        .header("sys/stat.h")
        .header("sys/types.h")
        .header("time.h")
        .header("wchar.h");

    if windows {
        cfg.header("winsock2.h"); // must be before windows.h

        cfg.header("direct.h");
        cfg.header("io.h");
        cfg.header("sys/utime.h");
        cfg.header("windows.h");
        cfg.header("process.h");
        cfg.header("ws2ipdef.h");
        cfg.header("signal.h");

        if target.contains("gnu") {
            cfg.header("ws2tcpip.h");
        }
    } else {
        cfg.flag("-Wno-deprecated-declarations");

        cfg.header("ctype.h");
        cfg.header("dirent.h");
        if openbsd {
            cfg.header("sys/socket.h");
        }
        cfg.header("net/if.h");
        if !ios {
            cfg.header("net/route.h");
            cfg.header("net/if_arp.h");
        }
        cfg.header("netdb.h");
        cfg.header("netinet/in.h");
        cfg.header("netinet/ip.h");
        cfg.header("netinet/tcp.h");
        cfg.header("netinet/udp.h");
        cfg.header("resolv.h");
        cfg.header("pthread.h");
        cfg.header("dlfcn.h");
        cfg.header("signal.h");
        cfg.header("string.h");
        cfg.header("sys/file.h");
        cfg.header("sys/ioctl.h");
        cfg.header("sys/mman.h");
        cfg.header("sys/resource.h");
        cfg.header("sys/socket.h");
        if linux && !musl {
            cfg.header("linux/if.h");
            cfg.header("sys/auxv.h");
        }
        cfg.header("sys/time.h");
        cfg.header("sys/un.h");
        cfg.header("sys/wait.h");
        cfg.header("unistd.h");
        cfg.header("utime.h");
        cfg.header("pwd.h");
        cfg.header("grp.h");
        cfg.header("sys/utsname.h");
        if !solaris && !ios {
            cfg.header("sys/ptrace.h");
        }
        cfg.header("sys/mount.h");
        cfg.header("sys/uio.h");
        cfg.header("sched.h");
        cfg.header("termios.h");
        cfg.header("poll.h");
        cfg.header("syslog.h");
        cfg.header("semaphore.h");
        cfg.header("sys/statvfs.h");
        cfg.header("sys/times.h");
    }

    if android {
        if !aarch64 && !x86_64 {
            // time64_t is not define for aarch64 and x86_64
            // If included it will generate the error 'Your time_t is already 64-bit'
            cfg.header("time64.h");
        }
        cfg.header("arpa/inet.h");
        cfg.header("xlocale.h");
        cfg.header("utmp.h");
        cfg.header("ifaddrs.h");
        if i686 || x86_64 {
            cfg.header("sys/reg.h");
        }
    } else if !windows {
        cfg.header("glob.h");
        cfg.header("ifaddrs.h");
        cfg.header("langinfo.h");

        if !openbsd && !freebsd && !dragonfly && !solaris {
            cfg.header("sys/quota.h");
        }

        if !musl && !x32 && !solaris {
            cfg.header("sys/sysctl.h");
        }

        if !musl && !uclibc {
            if !netbsd && !openbsd && !uclibc {
                cfg.header("execinfo.h");
            }

            if openbsd {
                cfg.header("utmp.h");
            } else {
                cfg.header("utmpx.h");
            }
        }
    }

    if apple {
        cfg.header("spawn.h");
        cfg.header("mach-o/dyld.h");
        cfg.header("mach/mach_time.h");
        cfg.header("malloc/malloc.h");
        cfg.header("util.h");
        cfg.header("xlocale.h");
        cfg.header("sys/xattr.h");
        if target.starts_with("x86") && !ios {
            cfg.header("crt_externs.h");
        }
        cfg.header("netinet/in.h");
        cfg.header("sys/ipc.h");
        cfg.header("sys/sem.h");
        cfg.header("sys/shm.h");

        if !ios {
            cfg.header("sys/sys_domain.h");
            cfg.header("net/if_utun.h");
            cfg.header("net/bpf.h");
            cfg.header("net/route.h");
            cfg.header("netinet/if_ether.h");
            cfg.header("sys/proc_info.h");
            cfg.header("sys/kern_control.h");
        }
    }

    if bsdlike {
        cfg.header("sys/event.h");
        cfg.header("net/if_dl.h");
        if freebsd {
            cfg.header("net/bpf.h");
            cfg.header("libutil.h");
        } else {
            cfg.header("util.h");
        }
    }

    if linux || emscripten {
        cfg.header("mntent.h");
        cfg.header("mqueue.h");
        cfg.header("ucontext.h");
        if !uclibc {
            // optionally included in uclibc
            cfg.header("sys/xattr.h");
        }
        cfg.header("sys/ipc.h");
        cfg.header("sys/sem.h");
        cfg.header("sys/msg.h");
        cfg.header("sys/shm.h");
        cfg.header("sys/user.h");
        cfg.header("sys/timerfd.h");
        cfg.header("shadow.h");
        if !emscripten {
            cfg.header("linux/input.h");
            cfg.header("linux/falloc.h");
        }
        if x86_64 {
            cfg.header("sys/io.h");
        }
        if i686 || x86_64 {
            cfg.header("sys/reg.h");
        }
    }

    if linux || android || emscripten {
        cfg.header("malloc.h");
        cfg.header("net/ethernet.h");
        cfg.header("netpacket/packet.h");
        cfg.header("sched.h");
        cfg.header("sys/epoll.h");
        cfg.header("sys/eventfd.h");
        cfg.header("sys/prctl.h");
        cfg.header("sys/sendfile.h");
        cfg.header("sys/signalfd.h");
        cfg.header("sys/vfs.h");
        cfg.header("sys/syscall.h");
        cfg.header("sys/personality.h");
        cfg.header("sys/swap.h");
        cfg.header("pty.h");
        if !uclibc {
            cfg.header("sys/sysinfo.h");
        }
        cfg.header("sys/reboot.h");
        if !emscripten {
            cfg.header("linux/sockios.h");
            cfg.header("linux/netlink.h");
            cfg.header("linux/genetlink.h");
            cfg.header("linux/netfilter_ipv4.h");
            cfg.header("linux/netfilter_ipv6.h");
            cfg.header("linux/fs.h");
        }
        if !musl {
            cfg.header("asm/mman.h");
            cfg.header("linux/magic.h");
            cfg.header("linux/reboot.h");
            cfg.header("linux/netfilter/nf_tables.h");

            if !mips {
                cfg.header("linux/quota.h");
            }
        }
    }
    if solaris {
        cfg.header("sys/epoll.h");
    }

    if linux || android {
        cfg.header("sys/fsuid.h");
        cfg.header("linux/module.h");
        cfg.header("linux/seccomp.h");
        cfg.header("linux/if_ether.h");
        cfg.header("linux/if_tun.h");
        cfg.header("linux/net_tstamp.h");
        // DCCP support
        if !uclibc && !musl && !emscripten {
            cfg.header("linux/dccp.h");
        }

        if !musl || mips {
            cfg.header("linux/memfd.h");
        }
    }

    if linux {
        cfg.header("linux/random.h");
        cfg.header("elf.h");
        cfg.header("link.h");
        cfg.header("spawn.h");
    }

    if freebsd {
        cfg.header("mqueue.h");
        cfg.header("pthread_np.h");
        cfg.header("sched.h");
        cfg.header("ufs/ufs/quota.h");
        cfg.header("sys/extattr.h");
        cfg.header("sys/jail.h");
        cfg.header("sys/ipc.h");
        cfg.header("sys/msg.h");
        cfg.header("sys/shm.h");
        cfg.header("sys/procdesc.h");
        cfg.header("sys/rtprio.h");
        cfg.header("spawn.h");
    }

    if netbsd {
        cfg.header("mqueue.h");
        cfg.header("ufs/ufs/quota.h");
        cfg.header("ufs/ufs/quota1.h");
        cfg.header("sys/extattr.h");
        cfg.header("sys/ioctl_compat.h");

        // DCCP support
        cfg.header("netinet/dccp.h");
    }

    if openbsd {
        cfg.header("ufs/ufs/quota.h");
        cfg.header("pthread_np.h");
        cfg.header("sys/syscall.h");
    }

    if dragonfly {
        cfg.header("mqueue.h");
        cfg.header("ufs/ufs/quota.h");
        cfg.header("pthread_np.h");
        cfg.header("sys/ioctl_compat.h");
        cfg.header("sys/rtprio.h");
    }

    if solaris {
        cfg.header("port.h");
        cfg.header("ucontext.h");
        cfg.header("sys/filio.h");
        cfg.header("sys/loadavg.h");
    }

    if linux || freebsd || dragonfly || netbsd || apple || emscripten {
        if !uclibc {
            cfg.header("aio.h");
        }
    }

    if cloudabi || redox {
        cfg.header("strings.h");
    }

    cfg.type_name(move |ty, is_struct, is_union| {
        match ty {
            // Just pass all these through, no need for a "struct" prefix
            "FILE" | "fd_set" | "Dl_info" | "DIR" | "Elf32_Phdr"
            | "Elf64_Phdr" | "Elf32_Shdr" | "Elf64_Shdr" | "Elf32_Sym"
            | "Elf64_Sym" | "Elf32_Ehdr" | "Elf64_Ehdr" | "Elf32_Chdr"
            | "Elf64_Chdr" => ty.to_string(),

            // Fixup a few types on windows that don't actually exist.
            "time64_t" if windows => "__time64_t".to_string(),
            "ssize_t" if windows => "SSIZE_T".to_string(),
            // windows 
            "sighandler_t" if windows && !mingw => "_crt_signal_t".to_string(),
            "sighandler_t" if windows && mingw => "__p_sig_fn_t".to_string(),
            // OSX calls this something else
            "sighandler_t" if bsdlike => "sig_t".to_string(),

            t if is_union => format!("union {}", t),

            t if t.ends_with("_t") => t.to_string(),

            // Windows uppercase structs don't have `struct` in front, there's a
            // few special cases for windows, and then otherwise put `struct` in
            // front of everything.
            t if is_struct => {
                if windows && ty.chars().next().unwrap().is_uppercase() {
                    t.to_string()
                } else if windows && t == "stat" {
                    "struct __stat64".to_string()
                } else if windows && t == "utimbuf" {
                    "struct __utimbuf64".to_string()
                } else {
                    format!("struct {}", t)
                }
            }

            t => t.to_string(),
        }
    });

    let target2 = target.clone();
    cfg.field_name(move |struct_, field| {
        match field {
            "st_birthtime" if openbsd && struct_ == "stat" => {
                "__st_birthtime".to_string()
            }
            "st_birthtime_nsec" if openbsd && struct_ == "stat" => {
                "__st_birthtimensec".to_string()
            }
            // Our stat *_nsec fields normally don't actually exist but are part
            // of a timeval struct
            s if s.ends_with("_nsec") && struct_.starts_with("stat") => {
                if target2.contains("apple") {
                    s.replace("_nsec", "spec.tv_nsec")
                } else if target2.contains("android") {
                    s.to_string()
                } else {
                    s.replace("e_nsec", ".tv_nsec")
                }
            }
            "u64" if struct_ == "epoll_event" => "data.u64".to_string(),
            "type_"
                if (linux || freebsd || dragonfly)
                    && (struct_ == "input_event"
                        || struct_ == "input_mask"
                        || struct_ == "ff_effect"
                        || struct_ == "rtprio") =>
            {
                "type".to_string()
            }
            s => s.to_string(),
        }
    });

    cfg.skip_type(move |ty| {
        match ty {
            // sighandler_t is crazy across platforms
            "sighandler_t" => true,

            _ => false,
        }
    });

    cfg.skip_struct(move |ty| {
        match ty {
            "sockaddr_nl" => musl,

            // On Linux, the type of `ut_tv` field of `struct utmpx`
            // can be an anonymous struct, so an extra struct,
            // which is absent in glibc, has to be defined.
            "__timeval" if linux => true,

            // Fixed on feature=align with repr(packed(4))
            // Once repr_packed stabilizes we can fix this unconditionally
            // and remove this check.
            "kevent" | "shmid_ds" | "semid_ds" if apple && x86_64 => true,

            // This is actually a union, not a struct
            "sigval" => true,

            // Linux kernel headers used on musl are too old to have this
            // definition. Because it's tested on other Linux targets, skip it.
            "input_mask" if musl => true,

            // These structs have changed since unified headers in NDK r14b.
            // `st_atime` and `st_atime_nsec` have changed sign.
            // FIXME: unskip it for next major release
            "stat" | "stat64" if android => true,

            // These are tested as part of the linux_fcntl tests since there are
            // header conflicts when including them with all the other structs.
            "termios2" => true,

            // Present on historical versions of iOS but missing in more recent
            // SDKs
            "bpf_hdr" | "proc_taskinfo" | "proc_taskallinfo"
            | "proc_bsdinfo" | "proc_threadinfo" | "sockaddr_inarp"
            | "sockaddr_ctl" | "arphdr"
                if ios =>
            {
                true
            }

            _ => false,
        }
    });

    cfg.skip_signededness(move |c| {
        match c {
            "LARGE_INTEGER"
            | "mach_timebase_info_data_t"
            | "float"
            | "double" => true,
            // uuid_t is a struct, not an integer.
            "uuid_t" if dragonfly => true,
            n if n.starts_with("pthread") => true,
            // sem_t is a struct or pointer
            "sem_t" if openbsd || freebsd || dragonfly || netbsd => true,
            // mqd_t is a pointer on FreeBSD and DragonFly
            "mqd_t" if freebsd || dragonfly => true,

            // Just some typedefs on osx, no need to check their sign
            "posix_spawnattr_t" | "posix_spawn_file_actions_t" => true,

            // windows-isms
            n if n.starts_with("P") => true,
            n if n.starts_with("H") => true,
            n if n.starts_with("LP") => true,
            "__p_sig_fn_t" if mingw => true,
            _ => false,
        }
    });

    cfg.skip_const(move |name| {
        match name {
            // Apparently these don't exist in mingw headers?
            "MEM_RESET_UNDO"
            | "FILE_ATTRIBUTE_NO_SCRUB_DATA"
            | "FILE_ATTRIBUTE_INTEGRITY_STREAM"
            | "ERROR_NOTHING_TO_TERMINATE"
                if mingw =>
            {
                true
            }

            "SIG_DFL" | "SIG_ERR" | "SIG_IGN" => true, // sighandler_t weirdness
            "SIGUNUSED" => true,                       // removed in glibc 2.26

            // types on musl are defined a little differently
            n if musl && n.contains("__SIZEOF_PTHREAD") => true,

            // Skip constants not defined in MUSL but just passed down to the
            // kernel regardless
            "RLIMIT_NLIMITS"
            | "TCP_COOKIE_TRANSACTIONS"
            | "RLIMIT_RTTIME"
            | "MSG_COPY"
                if musl =>
            {
                true
            }
            // work around super old mips toolchain
            "SCHED_IDLE" | "SHM_NORESERVE" => mips,

            // weird signed extension or something like that?
            "MS_NOUSER" => true,
            "MS_RMT_MASK" => true, // updated in glibc 2.22 and musl 1.1.13

            // These OSX constants are flagged as deprecated
            "NOTE_EXIT_REPARENTED" | "NOTE_REAP" if apple => true,

            // These constants were removed in FreeBSD 11 (svn r273250) but will
            // still be accepted and ignored at runtime.
            "MAP_RENAME" | "MAP_NORESERVE" if freebsd => true,

            // These constants were removed in FreeBSD 11 (svn r262489),
            // and they've never had any legitimate use outside of the
            // base system anyway.
            "CTL_MAXID" | "KERN_MAXID" | "HW_MAXID" | "NET_MAXID"
            | "USER_MAXID"
                if freebsd =>
            {
                true
            }

            // These constants were added in FreeBSD 11
            "EVFILT_PROCDESC" | "EVFILT_SENDFILE" | "EVFILT_EMPTY"
            | "PD_CLOEXEC" | "PD_ALLOWED_AT_FORK"
                if freebsd =>
            {
                true
            }

            // These constants were added in FreeBSD 12
            "SF_USER_READAHEAD" | "SO_REUSEPORT_LB" if freebsd => true,

            // These OSX constants are removed in Sierra.
            // https://developer.apple.com/library/content/releasenotes/General/APIDiffsMacOS10_12/Swift/Darwin.html
            "KERN_KDENABLE_BG_TRACE" if apple => true,
            "KERN_KDDISABLE_BG_TRACE" if apple => true,

            // These constants were removed in OpenBSD 6 (https://git.io/v7gBO
            // https://git.io/v7gBq)
            "KERN_USERMOUNT" | "KERN_ARND" if openbsd => true,

            // These are either unimplemented or optionally built into uClibc
            "LC_CTYPE_MASK"
            | "LC_NUMERIC_MASK"
            | "LC_TIME_MASK"
            | "LC_COLLATE_MASK"
            | "LC_MONETARY_MASK"
            | "LC_MESSAGES_MASK"
            | "MADV_MERGEABLE"
            | "MADV_UNMERGEABLE"
            | "MADV_HWPOISON"
            | "IPV6_ADD_MEMBERSHIP"
            | "IPV6_DROP_MEMBERSHIP"
            | "IPV6_MULTICAST_LOOP"
            | "IPV6_V6ONLY"
            | "MAP_STACK"
            | "RTLD_DEEPBIND"
            | "SOL_IPV6"
            | "SOL_ICMPV6"
                if uclibc =>
            {
                true
            }

            // Musl uses old, patched kernel headers
            "FALLOC_FL_COLLAPSE_RANGE"
            | "FALLOC_FL_ZERO_RANGE"
            | "FALLOC_FL_INSERT_RANGE"
            | "FALLOC_FL_UNSHARE_RANGE"
            | "RENAME_NOREPLACE"
            | "RENAME_EXCHANGE"
            | "RENAME_WHITEOUT"
                if musl =>
            {
                true
            }

            // Both android and musl use old kernel headers
            // These are constants used in getrandom syscall
            "GRND_NONBLOCK" | "GRND_RANDOM" if musl || android => true,

            // Defined by libattr not libc on linux (hard to test).
            // See constant definition for more details.
            "ENOATTR" if android || linux => true,

            // On mips*-unknown-linux-gnu* CMSPAR cannot be included with the set of headers we
            // want to use here for testing. It's originally defined in asm/termbits.h, which is
            // also included by asm/termios.h, but not the standard termios.h. There's no way to
            // include both asm/termbits.h and termios.h and there's no way to include both
            // asm/termios.h and ioctl.h (+ some other headers) because of redeclared types.
            "CMSPAR" if mips && linux && !musl => true,

            // On mips Linux targets, MADV_SOFT_OFFLINE is currently missing, though it's been added but CI has too old
            // of a Linux version. Since it exists on all other Linux targets, just ignore this for now and remove once
            // it's been fixed in CI.
            "MADV_SOFT_OFFLINE" if mips && linux => true,

            // These constants are tested in a separate test program generated below because there
            // are header conflicts if we try to include the headers that define them here.
            "F_CANCELLK" | "F_ADD_SEALS" | "F_GET_SEALS" => true,
            "F_SEAL_SEAL" | "F_SEAL_SHRINK" | "F_SEAL_GROW"
            | "F_SEAL_WRITE" => true,
            "QFMT_VFS_OLD" | "QFMT_VFS_V0" | "QFMT_VFS_V1"
                if mips && linux =>
            {
                true
            } // Only on MIPS
            "BOTHER" => true,

            "MFD_CLOEXEC" | "MFD_ALLOW_SEALING" if !mips && musl => true,
            // MFD_HUGETLB is not available in some older libc versions on the CI builders. On the
            // x86_64 and i686 builders it seems to be available for all targets, so at least test
            // it there.
            "MFD_HUGETLB" if !(x86_64 || i686) || musl => true,

            "DT_FIFO" | "DT_CHR" | "DT_DIR" | "DT_BLK" | "DT_REG"
            | "DT_LNK" | "DT_SOCK"
                if solaris =>
            {
                true
            }
            "USRQUOTA" | "GRPQUOTA" if solaris => true,
            "PRIO_MIN" | "PRIO_MAX" if solaris => true,

            // These are defined for Solaris 11, but the crate is tested on illumos, where they are currently not defined
            "EADI"
            | "PORT_SOURCE_POSTWAIT"
            | "PORT_SOURCE_SIGNAL"
            | "PTHREAD_STACK_MIN" => true,

            // These change all the time from release to release of linux
            // distros, let's just not bother trying to verify them. They
            // shouldn't be used in code anyway...
            "AF_MAX" | "PF_MAX" => true,

            // These are not in a glibc release yet, only in kernel headers.
            "AF_XDP" | "PF_XDP" | "SOL_XDP" if linux => true,

            // Present on historical versions of iOS, but now removed in more
            // recent SDKs
            "ARPOP_REQUEST"
            | "ARPOP_REPLY"
            | "ATF_COM"
            | "ATF_PERM"
            | "ATF_PUBL"
            | "ATF_USETRAILERS"
            | "AF_SYS_CONTROL"
            | "SYSPROTO_EVENT"
            | "PROC_PIDTASKALLINFO"
            | "PROC_PIDTASKINFO"
            | "PROC_PIDTHREADINFO"
            | "UTUN_OPT_FLAGS"
            | "UTUN_OPT_IFNAME"
            | "BPF_ALIGNMENT"
            | "SYSPROTO_CONTROL"
                if ios =>
            {
                true
            }
            s if ios && s.starts_with("RTF_") => true,
            s if ios && s.starts_with("RTM_") => true,
            s if ios && s.starts_with("RTA_") => true,
            s if ios && s.starts_with("RTAX_") => true,
            s if ios && s.starts_with("RTV_") => true,
            s if ios && s.starts_with("DLT_") => true,

            _ => false,
        }
    });

    cfg.skip_fn(move |name| {
        // skip those that are manually verified
        match name {
            "execv" |       // crazy stuff with const/mut
            "execve" |
            "execvp" |
            "execvpe" |
            "fexecve" => true,

            "getrlimit" | "getrlimit64" |    // non-int in 1st arg
            "setrlimit" | "setrlimit64" |    // non-int in 1st arg
            "prlimit" | "prlimit64" |        // non-int in 2nd arg
            "strerror_r" if linux => true,   // actually xpg-something-or-other

            // int vs uint. Sorry musl, your prototype declarations are "correct" in the sense that
            // they match the interface defined by Linux verbatim, but they conflict with other
            // send*/recv* syscalls
            "sendmmsg" | "recvmmsg" if musl => true,

            // typed 2nd arg on linux and android
            "gettimeofday" if linux || android || freebsd || openbsd || dragonfly => true,

            // not declared in newer android toolchains
            "getdtablesize" if android => true,

            "dlerror" if android => true, // const-ness is added
            "dladdr" if musl || solaris => true, // const-ness only added recently

            // OSX has 'struct tm *const' which we can't actually represent in
            // Rust, but is close enough to *mut
            "timegm" if apple => true,

            // OSX's daemon is deprecated in 10.5 so we'll get a warning (which
            // we turn into an error) so just ignore it.
            "daemon" if apple => true,

            // Deprecated on OSX
            "sem_destroy" if apple => true,
            "sem_init" if apple => true,

            // These functions presumably exist on netbsd but don't look like
            // they're implemented on rumprun yet, just let them slide for now.
            // Some of them look like they have headers but then don't have
            // corresponding actual definitions either...
            "shm_open" |
            "shm_unlink" |
            "syscall" |
            "mq_open" |
            "mq_close" |
            "mq_getattr" |
            "mq_notify" |
            "mq_receive" |
            "mq_send" |
            "mq_setattr" |
            "mq_timedreceive" |
            "mq_timedsend" |
            "mq_unlink" |
            "ptrace" |
            "sigaltstack" if rumprun => true,

            // There seems to be a small error in EGLIBC's eventfd.h header. The
            // [underlying system call][1] always takes its first `count`
            // argument as an `unsigned int`, but [EGLIBC's <sys/eventfd.h>
            // header][2] declares it to take an `int`. [GLIBC's header][3]
            // matches the kernel.
            //
            // EGLIBC is no longer actively developed, and Debian, the largest
            // distribution that had been using it, switched back to GLIBC in
            // April 2015. So effectively all Linux <sys/eventfd.h> headers will
            // be using `unsigned int` soon.
            //
            // [1]: https://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/tree/fs/eventfd.c?id=refs/tags/v3.12.51#n397
            // [2]: http://bazaar.launchpad.net/~ubuntu-branches/ubuntu/trusty/eglibc/trusty/view/head:/sysdeps/unix/sysv/linux/sys/eventfd.h
            // [3]: https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/sys/eventfd.h;h=6295f32e937e779e74318eb9d3bdbe76aef8a8f3;hb=4e42b5b8f89f0e288e68be7ad70f9525aebc2cff#l34
            "eventfd" if linux => true,

            // The `uname` function in freebsd is now an inline wrapper that
            // delegates to another, but the symbol still exists, so don't check
            // the symbol.
            "uname" if freebsd => true,

            // FIXME: need to upgrade FreeBSD version; see https://github.com/rust-lang/libc/issues/938
            "setgrent" if freebsd => true,

            // aio_waitcomplete's return type changed between FreeBSD 10 and 11.
            "aio_waitcomplete" if freebsd => true,

            // lio_listio confuses the checker, probably because one of its
            // arguments is an array
            "lio_listio" if freebsd => true,
            "lio_listio" if musl => true,

            // Apparently the NDK doesn't have this defined on android, but
            // it's in a header file?
            "endpwent" if android => true,


            // These are either unimplemented or optionally built into uClibc
            // or "sysinfo", where it's defined but the structs in linux/sysinfo.h and sys/sysinfo.h
            // clash so it can't be tested
            "getxattr" | "lgetxattr" | "fgetxattr" | "setxattr" | "lsetxattr" | "fsetxattr" |
            "listxattr" | "llistxattr" | "flistxattr" | "removexattr" | "lremovexattr" |
            "fremovexattr" |
            "backtrace" |
            "sysinfo" | "newlocale" | "duplocale" | "freelocale" | "uselocale" |
            "nl_langinfo_l" | "wcslen" | "wcstombs" if uclibc => true,

            // Apparently res_init exists on Android, but isn't defined in a header:
            // https://mail.gnome.org/archives/commits-list/2013-May/msg01329.html
            "res_init" if android => true,

            // On macOS and iOS, res_init is available, but requires linking with libresolv:
            // http://blog.achernya.com/2013/03/os-x-has-silly-libsystem.html
            // See discussion for skipping here:
            // https://github.com/rust-lang/libc/pull/585#discussion_r114561460
            "res_init" if apple => true,

            // On Mac we don't use the default `close()`, instead using their $NOCANCEL variants.
            "close" if apple => true,

            // Definition of those functions as changed since unified headers from NDK r14b
            // These changes imply some API breaking changes but are still ABI compatible.
            // We can wait for the next major release to be compliant with the new API.
            // FIXME: unskip these for next major release
            "strerror_r" | "madvise" | "msync" | "mprotect" | "recvfrom" | "getpriority" |
            "setpriority" | "personality" if android || solaris => true,
            // In Android 64 bits, these functions have been fixed since unified headers.
            // Ignore these until next major version.
            "bind" | "writev" | "readv" | "sendmsg" | "recvmsg" if android && (aarch64 || x86_64) => true,

            // signal is defined with sighandler_t, so ignore
            "signal" if solaris => true,

            "cfmakeraw" | "cfsetspeed" if solaris => true,

            // FIXME: mincore is defined with caddr_t on Solaris.
            "mincore" if solaris => true,

            // These were all included in historical versions of iOS but appear
            // to be removed now
            "system" | "ptrace" if ios => true,

            _ => false,
        }
    });

    cfg.skip_static(move |name| {
        match name {
            // Internal constant, not declared in any headers.
            "__progname" if android => true,
            _ => false,
        }
    });

    cfg.skip_fn_ptrcheck(move |name| {
        match name {
            // dllimport weirdness?
            _ if windows => true,

            _ => false,
        }
    });

    cfg.skip_field_type(move |struct_, field| {
        // This is a weird union, don't check the type.
        (struct_ == "ifaddrs" && field == "ifa_ifu") ||
        // sighandler_t type is super weird
        (struct_ == "sigaction" && field == "sa_sigaction") ||
        // __timeval type is a patch which doesn't exist in glibc
        (linux && struct_ == "utmpx" && field == "ut_tv") ||
        // sigval is actually a union, but we pretend it's a struct
        (struct_ == "sigevent" && field == "sigev_value") ||
        // aio_buf is "volatile void*" and Rust doesn't understand volatile
        (struct_ == "aiocb" && field == "aio_buf") ||
        // stack_t.ss_sp's type changed from FreeBSD 10 to 11 in svn r294930
        (freebsd && struct_ == "stack_t" && field == "ss_sp") ||
        // type siginfo_t.si_addr changed from OpenBSD 6.0 to 6.1
        (openbsd && struct_ == "siginfo_t" && field == "si_addr") ||
        // this one is an anonymous union
        (linux && struct_ == "ff_effect" && field == "u")
    });

    cfg.skip_field(move |struct_, field| {
        // this is actually a union on linux, so we can't represent it well and
        // just insert some padding.
        (struct_ == "siginfo_t" && field == "_pad") ||
        // musl names this __dummy1 but it's still there
        (musl && struct_ == "glob_t" && field == "gl_flags") ||
        // musl seems to define this as an *anonymous* bitfield
        (musl && struct_ == "statvfs" && field == "__f_unused") ||
        // sigev_notify_thread_id is actually part of a sigev_un union
        (struct_ == "sigevent" && field == "sigev_notify_thread_id") ||
        // signalfd had SIGSYS fields added in Linux 4.18, but no libc release has them yet.
        (struct_ == "signalfd_siginfo" && (field == "ssi_addr_lsb" ||
                                           field == "_pad2" ||
                                           field == "ssi_syscall" ||
                                           field == "ssi_call_addr" ||
                                           field == "ssi_arch"))
    });

    cfg.fn_cname(move |name, cname| {
        if windows {
            cname.unwrap_or(name).to_string()
        } else {
            name.to_string()
        }
    });

    cfg.generate("../src/lib.rs", "main.rs");

    // On Linux or Android also generate another script for testing linux/fcntl declarations.
    // These cannot be tested normally because including both `linux/fcntl.h` and `fcntl.h`
    // fails on a lot of platforms.
    let mut cfg = ctest::TestGenerator::new();
    cfg.skip_type(|_| true)
        .skip_fn(|_| true)
        .skip_static(|_| true);
    if android || linux {
        // musl defines these directly in `fcntl.h`
        if musl {
            cfg.header("fcntl.h");
        } else {
            cfg.header("linux/fcntl.h");
        }
        if !musl {
            cfg.header("net/if.h");
            cfg.header("linux/if.h");
        }
        cfg.header("linux/quota.h");
        cfg.header("asm/termbits.h");
        cfg.skip_const(move |name| match name {
            "F_CANCELLK" | "F_ADD_SEALS" | "F_GET_SEALS" => false,
            "F_SEAL_SEAL" | "F_SEAL_SHRINK" | "F_SEAL_GROW"
            | "F_SEAL_WRITE" => false,
            "QFMT_VFS_OLD" | "QFMT_VFS_V0" | "QFMT_VFS_V1"
                if mips && linux =>
            {
                false
            }
            "BOTHER" => false,
            _ => true,
        });
        cfg.skip_struct(|s| s != "termios2");
        cfg.type_name(move |ty, is_struct, is_union| match ty {
            t if is_struct => format!("struct {}", t),
            t if is_union => format!("union {}", t),
            t => t.to_string(),
        });
    } else {
        cfg.skip_const(|_| true);
        cfg.skip_struct(|_| true);
    }
    cfg.generate("../src/lib.rs", "linux_fcntl.rs");
}