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