xref: /rust-libc-0.2.174/libc-test/build.rs (revision d862a369)
1 #![deny(warnings)]
2 
3 extern crate ctest;
4 
5 use std::env;
6 
7 fn main() {
8     let target = env::var("TARGET").unwrap();
9     let x86_64 = target.contains("x86_64");
10     let windows = target.contains("windows");
11     let mingw = target.contains("windows-gnu");
12     let linux = target.contains("unknown-linux");
13     let android = target.contains("android");
14     let apple = target.contains("apple");
15     let musl = target.contains("musl");
16     let freebsd = target.contains("freebsd");
17     let dragonfly = target.contains("dragonfly");
18     let mips = target.contains("mips");
19     let netbsd = target.contains("netbsd");
20     let openbsd = target.contains("openbsd");
21     let rumprun = target.contains("rumprun");
22     let bsdlike = freebsd || apple || netbsd || openbsd || dragonfly;
23     let mut cfg = ctest::TestGenerator::new();
24 
25     // Pull in extra goodies
26     if linux || android {
27         cfg.define("_GNU_SOURCE", None);
28     } else if netbsd {
29         cfg.define("_NETBSD_SOURCE", Some("1"));
30     } else if windows {
31         cfg.define("_WIN32_WINNT", Some("0x8000"));
32     }
33 
34     // Android doesn't actually have in_port_t but it's much easier if we
35     // provide one for us to test against
36     if android {
37         cfg.define("in_port_t", Some("uint16_t"));
38     }
39 
40     cfg.header("errno.h")
41        .header("fcntl.h")
42        .header("limits.h")
43        .header("locale.h")
44        .header("stddef.h")
45        .header("stdint.h")
46        .header("stdio.h")
47        .header("stdlib.h")
48        .header("sys/stat.h")
49        .header("sys/types.h")
50        .header("time.h")
51        .header("wchar.h");
52 
53     if windows {
54         cfg.header("winsock2.h"); // must be before windows.h
55 
56         cfg.header("direct.h");
57         cfg.header("io.h");
58         cfg.header("sys/utime.h");
59         cfg.header("windows.h");
60         cfg.header("process.h");
61         cfg.header("ws2ipdef.h");
62 
63         if target.contains("gnu") {
64             cfg.header("ws2tcpip.h");
65         }
66     } else {
67         cfg.flag("-Wno-deprecated-declarations");
68 
69         cfg.header("ctype.h");
70         cfg.header("dirent.h");
71         if openbsd {
72             cfg.header("sys/socket.h");
73         }
74         cfg.header("net/if.h");
75         cfg.header("netdb.h");
76         cfg.header("netinet/in.h");
77         cfg.header("netinet/ip.h");
78         cfg.header("netinet/tcp.h");
79         cfg.header("pthread.h");
80         cfg.header("dlfcn.h");
81         cfg.header("signal.h");
82         cfg.header("string.h");
83         cfg.header("sys/file.h");
84         cfg.header("sys/ioctl.h");
85         cfg.header("sys/mman.h");
86         cfg.header("sys/resource.h");
87         cfg.header("sys/socket.h");
88         cfg.header("sys/time.h");
89         cfg.header("sys/un.h");
90         cfg.header("sys/wait.h");
91         cfg.header("unistd.h");
92         cfg.header("utime.h");
93         cfg.header("pwd.h");
94         cfg.header("grp.h");
95         cfg.header("sys/utsname.h");
96         cfg.header("sys/ptrace.h");
97         cfg.header("sys/mount.h");
98         cfg.header("sys/uio.h");
99         cfg.header("sched.h");
100         cfg.header("termios.h");
101         cfg.header("poll.h");
102         cfg.header("syslog.h");
103         cfg.header("semaphore.h");
104     }
105 
106     if android {
107         cfg.header("arpa/inet.h");
108         cfg.header("time64.h");
109         cfg.header("xlocale.h");
110         cfg.header("utmp.h");
111     } else if !windows {
112         cfg.header("glob.h");
113         cfg.header("ifaddrs.h");
114         cfg.header("sys/statvfs.h");
115         cfg.header("langinfo.h");
116 
117         if !openbsd && !freebsd && !dragonfly {
118             cfg.header("sys/quota.h");
119         }
120 
121         if !musl {
122             cfg.header("sys/sysctl.h");
123 
124             if !netbsd && !openbsd {
125                 cfg.header("execinfo.h");
126                 cfg.header("xlocale.h");
127             }
128 
129             if openbsd {
130                 cfg.header("utmp.h");
131             } else {
132                 cfg.header("utmpx.h");
133             }
134         }
135     }
136 
137     if apple {
138         cfg.header("mach-o/dyld.h");
139         cfg.header("mach/mach_time.h");
140         cfg.header("malloc/malloc.h");
141         cfg.header("util.h");
142         if target.starts_with("x86") {
143             cfg.header("crt_externs.h");
144         }
145     }
146 
147     if bsdlike {
148         cfg.header("sys/event.h");
149 
150         if freebsd {
151             cfg.header("libutil.h");
152         } else {
153             cfg.header("util.h");
154         }
155     }
156 
157     if linux {
158         cfg.header("mqueue.h");
159         cfg.header("ucontext.h");
160         cfg.header("sys/signalfd.h");
161         cfg.header("sys/xattr.h");
162         cfg.header("sys/ipc.h");
163         cfg.header("sys/msg.h");
164         cfg.header("sys/shm.h");
165         cfg.header("sys/fsuid.h");
166         cfg.header("pty.h");
167         cfg.header("shadow.h");
168     }
169 
170     if linux || android {
171         cfg.header("malloc.h");
172         cfg.header("net/ethernet.h");
173         cfg.header("netpacket/packet.h");
174         cfg.header("sched.h");
175         cfg.header("sys/epoll.h");
176         cfg.header("sys/eventfd.h");
177         cfg.header("sys/prctl.h");
178         cfg.header("sys/sendfile.h");
179         cfg.header("sys/vfs.h");
180         cfg.header("sys/syscall.h");
181         cfg.header("sys/sysinfo.h");
182         cfg.header("sys/reboot.h");
183         if !musl {
184             cfg.header("linux/netlink.h");
185             cfg.header("linux/magic.h");
186             cfg.header("linux/reboot.h");
187 
188             if !mips {
189                 cfg.header("linux/quota.h");
190             }
191         }
192     }
193 
194     if freebsd {
195         cfg.header("pthread_np.h");
196         cfg.header("sched.h");
197         cfg.header("ufs/ufs/quota.h");
198     }
199 
200     if netbsd {
201         cfg.header("ufs/ufs/quota.h");
202         cfg.header("ufs/ufs/quota1.h");
203         cfg.header("sys/ioctl_compat.h");
204     }
205 
206     if openbsd {
207         cfg.header("ufs/ufs/quota.h");
208         cfg.header("rpcsvc/rex.h");
209         cfg.header("pthread_np.h");
210         cfg.header("sys/syscall.h");
211     }
212 
213     if dragonfly {
214         cfg.header("ufs/ufs/quota.h");
215         cfg.header("pthread_np.h");
216         cfg.header("sys/ioctl_compat.h");
217     }
218 
219     if linux || freebsd || dragonfly || netbsd || apple {
220         cfg.header("aio.h");
221     }
222 
223     cfg.type_name(move |ty, is_struct| {
224         match ty {
225             // Just pass all these through, no need for a "struct" prefix
226             "FILE" |
227             "fd_set" |
228             "Dl_info" |
229             "DIR" => ty.to_string(),
230 
231             // Fixup a few types on windows that don't actually exist.
232             "time64_t" if windows => "__time64_t".to_string(),
233             "ssize_t" if windows => "SSIZE_T".to_string(),
234 
235             // OSX calls this something else
236             "sighandler_t" if bsdlike => "sig_t".to_string(),
237 
238             t if t.ends_with("_t") => t.to_string(),
239 
240             // Windows uppercase structs don't have `struct` in front, there's a
241             // few special cases for windows, and then otherwise put `struct` in
242             // front of everything.
243             t if is_struct => {
244                 if windows && ty.chars().next().unwrap().is_uppercase() {
245                     t.to_string()
246                 } else if windows && t == "stat" {
247                     "struct __stat64".to_string()
248                 } else if windows && t == "utimbuf" {
249                     "struct __utimbuf64".to_string()
250                 } else {
251                     format!("struct {}", t)
252                 }
253             }
254 
255             t => t.to_string(),
256         }
257     });
258 
259     let target2 = target.clone();
260     cfg.field_name(move |struct_, field| {
261         match field {
262             "st_birthtime"      if openbsd && struct_ == "stat" => "__st_birthtime".to_string(),
263             "st_birthtime_nsec" if openbsd && struct_ == "stat" => "__st_birthtimensec".to_string(),
264             // Our stat *_nsec fields normally don't actually exist but are part
265             // of a timeval struct
266             s if s.ends_with("_nsec") && struct_.starts_with("stat") => {
267                 if target2.contains("apple") {
268                     s.replace("_nsec", "spec.tv_nsec")
269                 } else if target2.contains("android") {
270                     s.to_string()
271                 } else {
272                     s.replace("e_nsec", ".tv_nsec")
273                 }
274             }
275             "u64" if struct_ == "epoll_event" => "data.u64".to_string(),
276             s => s.to_string(),
277         }
278     });
279 
280     cfg.skip_type(move |ty| {
281         match ty {
282             // sighandler_t is crazy across platforms
283             "sighandler_t" => true,
284 
285             _ => false
286         }
287     });
288 
289     cfg.skip_struct(move |ty| {
290         match ty {
291             "sockaddr_nl" => musl,
292 
293             // On Linux, the type of `ut_tv` field of `struct utmpx`
294             // can be an anonymous struct, so an extra struct,
295             // which is absent in glibc, has to be defined.
296             "__timeval" if linux => true,
297 
298             // The alignment of this is 4 on 64-bit OSX...
299             "kevent" if apple && x86_64 => true,
300 
301             // This is actually a union, not a struct
302             "sigval" => true,
303 
304             _ => false
305         }
306     });
307 
308     cfg.skip_signededness(move |c| {
309         match c {
310             "LARGE_INTEGER" |
311             "mach_timebase_info_data_t" |
312             "float" |
313             "double" => true,
314             // uuid_t is a struct, not an integer.
315             "uuid_t" if dragonfly => true,
316             n if n.starts_with("pthread") => true,
317             // sem_t is a struct or pointer
318             "sem_t" if openbsd || freebsd || dragonfly || rumprun => true,
319 
320             // windows-isms
321             n if n.starts_with("P") => true,
322             n if n.starts_with("H") => true,
323             n if n.starts_with("LP") => true,
324             _ => false,
325         }
326     });
327 
328     cfg.skip_const(move |name| {
329         match name {
330             // Apparently these don't exist in mingw headers?
331             "MEM_RESET_UNDO" |
332             "FILE_ATTRIBUTE_NO_SCRUB_DATA" |
333             "FILE_ATTRIBUTE_INTEGRITY_STREAM" |
334             "ERROR_NOTHING_TO_TERMINATE" if mingw => true,
335 
336             "SIG_IGN" => true, // sighandler_t weirdness
337 
338             // types on musl are defined a little differently
339             n if musl && n.contains("__SIZEOF_PTHREAD") => true,
340 
341             // Skip constants not defined in MUSL but just passed down to the
342             // kernel regardless
343             "RLIMIT_NLIMITS" |
344             "TCP_COOKIE_TRANSACTIONS" |
345             "RLIMIT_RTTIME" |
346             "MSG_COPY" if musl => true,
347             // work around super old mips toolchain
348             "SCHED_IDLE" | "SHM_NORESERVE" => mips,
349 
350             // weird signed extension or something like that?
351             "MS_NOUSER" => true,
352             "MS_RMT_MASK" => true, // updated in glibc 2.22 and musl 1.1.13
353 
354             // These OSX constants are flagged as deprecated
355             "NOTE_EXIT_REPARENTED" |
356             "NOTE_REAP" if apple => true,
357 
358             // The linux/quota.h header file which defines these can't be
359             // included with sys/quota.h currently on MIPS, so we don't include
360             // it and just ignore these constants
361             "QFMT_VFS_OLD" |
362             "QFMT_VFS_V0" if mips && linux => true,
363 
364             // These constants were removed in FreeBSD 11 (svn r273250) but will
365             // still be accepted and ignored at runtime.
366             "MAP_RENAME" |
367             "MAP_NORESERVE" if freebsd => true,
368 
369             // These constants were removed in FreeBSD 11 (svn r262489),
370             // and they've never had any legitimate use outside of the
371             // base system anyway.
372             "CTL_MAXID" |
373             "KERN_MAXID" |
374             "HW_MAXID" |
375             "USER_MAXID" if freebsd => true,
376 
377             // These OSX constants are removed in Sierra.
378             // https://developer.apple.com/library/content/releasenotes/General/APIDiffsMacOS10_12/Swift/Darwin.html
379             "KERN_KDENABLE_BG_TRACE" if apple => true,
380             "KERN_KDDISABLE_BG_TRACE" if apple => true,
381 
382             _ => false,
383         }
384     });
385 
386     cfg.skip_fn(move |name| {
387         // skip those that are manually verified
388         match name {
389             "execv" |       // crazy stuff with const/mut
390             "execve" |
391             "execvp" |
392             "execvpe" => true,
393 
394             "getrlimit" | "getrlimit64" |    // non-int in 1st arg
395             "setrlimit" | "setrlimit64" |    // non-int in 1st arg
396             "prlimit" | "prlimit64" |        // non-int in 2nd arg
397             "strerror_r" if linux => true,   // actually xpg-something-or-other
398 
399             // typed 2nd arg on linux and android
400             "gettimeofday" if linux || android || freebsd || openbsd || dragonfly => true,
401 
402             // not declared in newer android toolchains
403             "getdtablesize" if android => true,
404 
405             "dlerror" if android => true, // const-ness is added
406             "dladdr" if musl => true, // const-ness only added recently
407 
408             // OSX has 'struct tm *const' which we can't actually represent in
409             // Rust, but is close enough to *mut
410             "timegm" if apple => true,
411 
412             // OSX's daemon is deprecated in 10.5 so we'll get a warning (which
413             // we turn into an error) so just ignore it.
414             "daemon" if apple => true,
415 
416             // Deprecated on OSX
417             "sem_destroy" if apple => true,
418             "sem_init" if apple => true,
419 
420             // These functions presumably exist on netbsd but don't look like
421             // they're implemented on rumprun yet, just let them slide for now.
422             // Some of them look like they have headers but then don't have
423             // corresponding actual definitions either...
424             "shm_open" |
425             "shm_unlink" |
426             "syscall" |
427             "ptrace" |
428             "sigaltstack" if rumprun => true,
429 
430             // There seems to be a small error in EGLIBC's eventfd.h header. The
431             // [underlying system call][1] always takes its first `count`
432             // argument as an `unsigned int`, but [EGLIBC's <sys/eventfd.h>
433             // header][2] declares it to take an `int`. [GLIBC's header][3]
434             // matches the kernel.
435             //
436             // EGLIBC is no longer actively developed, and Debian, the largest
437             // distribution that had been using it, switched back to GLIBC in
438             // April 2015. So effectively all Linux <sys/eventfd.h> headers will
439             // be using `unsigned int` soon.
440             //
441             // [1]: https://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/tree/fs/eventfd.c?id=refs/tags/v3.12.51#n397
442             // [2]: http://bazaar.launchpad.net/~ubuntu-branches/ubuntu/trusty/eglibc/trusty/view/head:/sysdeps/unix/sysv/linux/sys/eventfd.h
443             // [3]: https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/sys/eventfd.h;h=6295f32e937e779e74318eb9d3bdbe76aef8a8f3;hb=4e42b5b8f89f0e288e68be7ad70f9525aebc2cff#l34
444             "eventfd" if linux => true,
445 
446             // The `uname` function in freebsd is now an inline wrapper that
447             // delegates to another, but the symbol still exists, so don't check
448             // the symbol.
449             "uname" if freebsd => true,
450 
451             // aio_waitcomplete's return type changed between FreeBSD 10 and 11.
452             "aio_waitcomplete" if freebsd => true,
453 
454             // lio_listio confuses the checker, probably because one of its
455             // arguments is an array
456             "lio_listio" if freebsd => true,
457             "lio_listio" if musl => true,
458 
459             // Apparently the NDK doesn't have this defined on android, but
460             // it's in a header file?
461             "endpwent" if android => true,
462 
463             _ => false,
464         }
465     });
466 
467     cfg.skip_fn_ptrcheck(move |name| {
468         match name {
469             // dllimport weirdness?
470             _ if windows => true,
471 
472             _ => false,
473         }
474     });
475 
476     cfg.skip_field_type(move |struct_, field| {
477         // This is a weird union, don't check the type.
478         (struct_ == "ifaddrs" && field == "ifa_ifu") ||
479         // sighandler_t type is super weird
480         (struct_ == "sigaction" && field == "sa_sigaction") ||
481         // __timeval type is a patch which doesn't exist in glibc
482         (linux && struct_ == "utmpx" && field == "ut_tv") ||
483         // sigval is actually a union, but we pretend it's a struct
484         (struct_ == "sigevent" && field == "sigev_value") ||
485         // aio_buf is "volatile void*" and Rust doesn't understand volatile
486         (struct_ == "aiocb" && field == "aio_buf") ||
487         // stack_t.ss_sp's type changed from FreeBSD 10 to 11 in svn r294930
488         (freebsd && struct_ == "stack_t" && field == "ss_sp")
489     });
490 
491     cfg.skip_field(move |struct_, field| {
492         // this is actually a union on linux, so we can't represent it well and
493         // just insert some padding.
494         (struct_ == "siginfo_t" && field == "_pad") ||
495         // musl names this __dummy1 but it's still there
496         (musl && struct_ == "glob_t" && field == "gl_flags") ||
497         // musl seems to define this as an *anonymous* bitfield
498         (musl && struct_ == "statvfs" && field == "__f_unused") ||
499         // sigev_notify_thread_id is actually part of a sigev_un union
500         (struct_ == "sigevent" && field == "sigev_notify_thread_id")
501     });
502 
503     cfg.fn_cname(move |name, cname| {
504         if windows {
505             cname.unwrap_or(name).to_string()
506         } else {
507             name.to_string()
508         }
509     });
510 
511     if env::var("SKIP_COMPILE").is_ok() {
512         cfg.generate_files("../src/lib.rs", "all.rs");
513     } else {
514         cfg.generate("../src/lib.rs", "all.rs");
515     }
516 }
517