xref: /rust-libc-0.2.174/libc-test/build.rs (revision a097ff6d)
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.header("ctype.h");
68         cfg.header("dirent.h");
69         if openbsd {
70             cfg.header("sys/socket.h");
71         }
72         cfg.header("net/if.h");
73         cfg.header("netdb.h");
74         cfg.header("netinet/in.h");
75         cfg.header("netinet/ip.h");
76         cfg.header("netinet/tcp.h");
77         cfg.header("pthread.h");
78         cfg.header("dlfcn.h");
79         cfg.header("signal.h");
80         cfg.header("string.h");
81         cfg.header("sys/file.h");
82         cfg.header("sys/ioctl.h");
83         cfg.header("sys/mman.h");
84         cfg.header("sys/resource.h");
85         cfg.header("sys/socket.h");
86         cfg.header("sys/time.h");
87         cfg.header("sys/un.h");
88         cfg.header("sys/wait.h");
89         cfg.header("unistd.h");
90         cfg.header("utime.h");
91         cfg.header("pwd.h");
92         cfg.header("grp.h");
93         cfg.header("sys/utsname.h");
94         cfg.header("sys/ptrace.h");
95         cfg.header("sys/mount.h");
96         cfg.header("sys/uio.h");
97         cfg.header("sched.h");
98         cfg.header("termios.h");
99         cfg.header("poll.h");
100         cfg.header("syslog.h");
101         cfg.header("semaphore.h");
102     }
103 
104     if android {
105         cfg.header("arpa/inet.h");
106         cfg.header("time64.h");
107         cfg.header("xlocale.h");
108         cfg.header("utmp.h");
109     } else if !windows {
110         cfg.header("glob.h");
111         cfg.header("ifaddrs.h");
112         cfg.header("sys/statvfs.h");
113         cfg.header("langinfo.h");
114 
115         if !openbsd && !freebsd && !dragonfly {
116             cfg.header("sys/quota.h");
117         }
118 
119         if !musl {
120             cfg.header("sys/sysctl.h");
121 
122             if !netbsd && !openbsd {
123                 cfg.header("execinfo.h");
124                 cfg.header("xlocale.h");
125             }
126 
127             if openbsd {
128                 cfg.header("utmp.h");
129             } else {
130                 cfg.header("utmpx.h");
131             }
132         }
133     }
134 
135     if apple {
136         cfg.header("mach-o/dyld.h");
137         cfg.header("mach/mach_time.h");
138         cfg.header("malloc/malloc.h");
139         cfg.header("util.h");
140         if target.starts_with("x86") {
141             cfg.header("crt_externs.h");
142         }
143     }
144 
145     if bsdlike {
146         cfg.header("sys/event.h");
147 
148         if freebsd {
149             cfg.header("libutil.h");
150         } else {
151             cfg.header("util.h");
152         }
153     }
154 
155     if linux {
156         cfg.header("mqueue.h");
157         cfg.header("ucontext.h");
158         cfg.header("sys/signalfd.h");
159         cfg.header("sys/xattr.h");
160         cfg.header("sys/ipc.h");
161         cfg.header("sys/shm.h");
162         cfg.header("pty.h");
163     }
164 
165     if linux || android {
166         cfg.header("malloc.h");
167         cfg.header("net/ethernet.h");
168         cfg.header("netpacket/packet.h");
169         cfg.header("sched.h");
170         cfg.header("sys/epoll.h");
171         cfg.header("sys/eventfd.h");
172         cfg.header("sys/prctl.h");
173         cfg.header("sys/sendfile.h");
174         cfg.header("sys/vfs.h");
175         cfg.header("sys/syscall.h");
176         cfg.header("sys/sysinfo.h");
177         cfg.header("sys/reboot.h");
178         if !musl {
179             cfg.header("linux/netlink.h");
180             cfg.header("linux/magic.h");
181             cfg.header("linux/reboot.h");
182 
183             if !mips {
184                 cfg.header("linux/quota.h");
185             }
186         }
187     }
188 
189     if freebsd {
190         cfg.header("pthread_np.h");
191         cfg.header("sched.h");
192         cfg.header("ufs/ufs/quota.h");
193     }
194 
195     if netbsd {
196         cfg.header("ufs/ufs/quota.h");
197         cfg.header("ufs/ufs/quota1.h");
198         cfg.header("sys/ioctl_compat.h");
199     }
200 
201     if openbsd {
202         cfg.header("ufs/ufs/quota.h");
203         cfg.header("rpcsvc/rex.h");
204         cfg.header("pthread_np.h");
205         cfg.header("sys/syscall.h");
206     }
207 
208     if dragonfly {
209         cfg.header("ufs/ufs/quota.h");
210         cfg.header("pthread_np.h");
211         cfg.header("sys/ioctl_compat.h");
212     }
213 
214     cfg.type_name(move |ty, is_struct| {
215         match ty {
216             // Just pass all these through, no need for a "struct" prefix
217             "FILE" |
218             "fd_set" |
219             "Dl_info" |
220             "DIR" => ty.to_string(),
221 
222             // Fixup a few types on windows that don't actually exist.
223             "time64_t" if windows => "__time64_t".to_string(),
224             "ssize_t" if windows => "SSIZE_T".to_string(),
225 
226             // OSX calls this something else
227             "sighandler_t" if bsdlike => "sig_t".to_string(),
228 
229             t if t.ends_with("_t") => t.to_string(),
230 
231             // Windows uppercase structs don't have `struct` in front, there's a
232             // few special cases for windows, and then otherwise put `struct` in
233             // front of everything.
234             t if is_struct => {
235                 if windows && ty.chars().next().unwrap().is_uppercase() {
236                     t.to_string()
237                 } else if windows && t == "stat" {
238                     "struct __stat64".to_string()
239                 } else if windows && t == "utimbuf" {
240                     "struct __utimbuf64".to_string()
241                 } else {
242                     format!("struct {}", t)
243                 }
244             }
245 
246             t => t.to_string(),
247         }
248     });
249 
250     let target2 = target.clone();
251     cfg.field_name(move |struct_, field| {
252         match field {
253             "st_birthtime"      if openbsd && struct_ == "stat" => "__st_birthtime".to_string(),
254             "st_birthtime_nsec" if openbsd && struct_ == "stat" => "__st_birthtimensec".to_string(),
255             // Our stat *_nsec fields normally don't actually exist but are part
256             // of a timeval struct
257             s if s.ends_with("_nsec") && struct_.starts_with("stat") => {
258                 if target2.contains("apple") {
259                     s.replace("_nsec", "spec.tv_nsec")
260                 } else if target2.contains("android") {
261                     s.to_string()
262                 } else {
263                     s.replace("e_nsec", ".tv_nsec")
264                 }
265             }
266             "u64" if struct_ == "epoll_event" => "data.u64".to_string(),
267             s => s.to_string(),
268         }
269     });
270 
271     cfg.skip_type(move |ty| {
272         match ty {
273             // sighandler_t is crazy across platforms
274             "sighandler_t" => true,
275 
276             _ => false
277         }
278     });
279 
280     cfg.skip_struct(move |ty| {
281         match ty {
282             "sockaddr_nl" => musl,
283 
284             // The alignment of this is 4 on 64-bit OSX...
285             "kevent" if apple && x86_64 => true,
286 
287             _ => false
288         }
289     });
290 
291     cfg.skip_signededness(move |c| {
292         match c {
293             "LARGE_INTEGER" |
294             "mach_timebase_info_data_t" |
295             "float" |
296             "double" => true,
297             // uuid_t is a struct, not an integer.
298             "uuid_t" if dragonfly => true,
299             n if n.starts_with("pthread") => true,
300             // sem_t is a struct or pointer
301             "sem_t" if openbsd || freebsd || rumprun => true,
302 
303             // windows-isms
304             n if n.starts_with("P") => true,
305             n if n.starts_with("H") => true,
306             n if n.starts_with("LP") => true,
307             _ => false,
308         }
309     });
310 
311     cfg.skip_const(move |name| {
312         match name {
313             // Apparently these don't exist in mingw headers?
314             "MEM_RESET_UNDO" |
315             "FILE_ATTRIBUTE_NO_SCRUB_DATA" |
316             "FILE_ATTRIBUTE_INTEGRITY_STREAM" |
317             "ERROR_NOTHING_TO_TERMINATE" if mingw => true,
318 
319             "SIG_IGN" => true, // sighandler_t weirdness
320 
321             // types on musl are defined a little differently
322             n if musl && n.contains("__SIZEOF_PTHREAD") => true,
323 
324             // Skip constants not defined in MUSL but just passed down to the
325             // kernel regardless
326             "RLIMIT_NLIMITS" |
327             "TCP_COOKIE_TRANSACTIONS" |
328             "RLIMIT_RTTIME" if musl => true,
329             // work around super old mips toolchain
330             "SCHED_IDLE" | "SHM_NORESERVE" => mips,
331 
332             // weird signed extension or something like that?
333             "MS_NOUSER" => true,
334             "MS_RMT_MASK" => true, // updated in glibc 2.22 and musl 1.1.13
335 
336             // These OSX constants are flagged as deprecated
337             "NOTE_EXIT_REPARENTED" |
338             "NOTE_REAP" if apple => true,
339 
340             // The linux/quota.h header file which defines these can't be
341             // included with sys/quota.h currently on MIPS, so we don't include
342             // it and just ignore these constants
343             "QFMT_VFS_OLD" |
344             "QFMT_VFS_V0" if mips && linux => true,
345 
346             _ => false,
347         }
348     });
349 
350     cfg.skip_fn(move |name| {
351         // skip those that are manually verified
352         match name {
353             "execv" |       // crazy stuff with const/mut
354             "execve" |
355             "execvp" |
356             "execvpe" => true,
357 
358             "getrlimit" | "getrlimit64" |    // non-int in 1st arg
359             "setrlimit" | "setrlimit64" |    // non-int in 1st arg
360             "prlimit" | "prlimit64" |        // non-int in 2nd arg
361             "strerror_r" if linux => true,   // actually xpg-something-or-other
362 
363             // typed 2nd arg on linux and android
364             "gettimeofday" if linux || android || freebsd || openbsd || dragonfly => true,
365 
366             // not declared in newer android toolchains
367             "getdtablesize" if android => true,
368 
369             "dlerror" if android => true, // const-ness is added
370             "dladdr" if musl => true, // const-ness only added recently
371 
372             // OSX has 'struct tm *const' which we can't actually represent in
373             // Rust, but is close enough to *mut
374             "timegm" if apple => true,
375 
376             // OSX's daemon is deprecated in 10.5 so we'll get a warning (which
377             // we turn into an error) so just ignore it.
378             "daemon" if apple => true,
379 
380             // Deprecated on OSX
381             "sem_destroy" if apple => true,
382             "sem_init" if apple => true,
383 
384             // These functions presumably exist on netbsd but don't look like
385             // they're implemented on rumprun yet, just let them slide for now.
386             // Some of them look like they have headers but then don't have
387             // corresponding actual definitions either...
388             "shm_open" |
389             "shm_unlink" |
390             "syscall" |
391             "ptrace" |
392             "sigaltstack" if rumprun => true,
393 
394             // There seems to be a small error in EGLIBC's eventfd.h header. The
395             // [underlying system call][1] always takes its first `count`
396             // argument as an `unsigned int`, but [EGLIBC's <sys/eventfd.h>
397             // header][2] declares it to take an `int`. [GLIBC's header][3]
398             // matches the kernel.
399             //
400             // EGLIBC is no longer actively developed, and Debian, the largest
401             // distribution that had been using it, switched back to GLIBC in
402             // April 2015. So effectively all Linux <sys/eventfd.h> headers will
403             // be using `unsigned int` soon.
404             //
405             // [1]: https://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/tree/fs/eventfd.c?id=refs/tags/v3.12.51#n397
406             // [2]: http://bazaar.launchpad.net/~ubuntu-branches/ubuntu/trusty/eglibc/trusty/view/head:/sysdeps/unix/sysv/linux/sys/eventfd.h
407             // [3]: https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/sys/eventfd.h;h=6295f32e937e779e74318eb9d3bdbe76aef8a8f3;hb=4e42b5b8f89f0e288e68be7ad70f9525aebc2cff#l34
408             "eventfd" if linux => true,
409 
410             // The `uname` funcion in freebsd is now an inline wrapper that
411             // delegates to another, but the symbol still exists, so don't check
412             // the symbol.
413             "uname" if freebsd => true,
414 
415             _ => false,
416         }
417     });
418 
419     cfg.skip_fn_ptrcheck(move |name| {
420         match name {
421             // dllimport weirdness?
422             _ if windows => true,
423 
424             _ => false,
425         }
426     });
427 
428     cfg.skip_field_type(move |struct_, field| {
429         // This is a weird union, don't check the type.
430         (struct_ == "ifaddrs" && field == "ifa_ifu") ||
431         // sighandler_t type is super weird
432         (struct_ == "sigaction" && field == "sa_sigaction")
433     });
434 
435     cfg.skip_field(move |struct_, field| {
436         // this is actually a union on linux, so we can't represent it well and
437         // just insert some padding.
438         (struct_ == "siginfo_t" && field == "_pad") ||
439         // musl names this __dummy1 but it's still there
440         (musl && struct_ == "glob_t" && field == "gl_flags") ||
441         // musl seems to define this as an *anonymous* bitfield
442         (musl && struct_ == "statvfs" && field == "__f_unused")
443     });
444 
445     cfg.fn_cname(move |name, cname| {
446         if windows {
447             cname.unwrap_or(name).to_string()
448         } else {
449             name.to_string()
450         }
451     });
452 
453     if env::var("SKIP_COMPILE").is_ok() {
454         cfg.generate_files("../src/lib.rs", "all.rs");
455     } else {
456         cfg.generate("../src/lib.rs", "all.rs");
457     }
458 }
459