blob: 4ee2ca6212cccb2758d47b6f66df7bc979cc17f6 [file] [log] [blame]
Alex Crichtonb5da7c02015-09-16 17:44:40 -07001#![deny(warnings)]
2
Alex Crichtond11e9142015-09-15 23:28:52 -07003extern crate ctest;
Alex Crichton8e5f0cd2015-09-09 22:46:19 -07004
5use std::env;
Alex Crichton8e5f0cd2015-09-09 22:46:19 -07006
Alex Crichtond11e9142015-09-15 23:28:52 -07007fn main() {
8 let target = env::var("TARGET").unwrap();
9 let windows = target.contains("windows");
10 let mingw = target.contains("windows-gnu");
Alex Crichtoncd9b33e2015-09-17 14:47:40 -070011 let linux = target.contains("unknown-linux");
Alex Crichton1ff96102015-09-17 15:09:02 -070012 let android = target.contains("android");
Alex Crichtonbaef6112015-09-19 23:20:53 -070013 let apple = target.contains("apple");
Alex Crichton15b83c22015-09-17 17:25:52 -070014 let musl = target.contains("musl");
Alex Crichtona1b948e2015-09-18 11:54:32 -070015 let freebsd = target.contains("freebsd");
Alex Crichtonbaef6112015-09-19 23:20:53 -070016 let bsdlike = freebsd || apple;
Alex Crichtond11e9142015-09-15 23:28:52 -070017 let mut cfg = ctest::TestGenerator::new();
Alex Crichton8e5f0cd2015-09-09 22:46:19 -070018
Alex Crichtond11e9142015-09-15 23:28:52 -070019 // Pull in extra goodies on linux/mingw
Alex Crichton15b83c22015-09-17 17:25:52 -070020 if target.contains("unknown-linux") {
Alex Crichtond11e9142015-09-15 23:28:52 -070021 cfg.define("_GNU_SOURCE", None);
Alex Crichton1ff96102015-09-17 15:09:02 -070022 } else if windows {
Alex Crichtond11e9142015-09-15 23:28:52 -070023 cfg.define("_WIN32_WINNT", Some("0x8000"));
Alex Crichton0df7c102015-09-10 16:35:37 -070024 }
25
Alex Crichton094f44d2015-09-16 16:27:23 -070026 // Android doesn't actually have in_port_t but it's much easier if we
27 // provide one for us to test against
Alex Crichton1ff96102015-09-17 15:09:02 -070028 if android {
Alex Crichton094f44d2015-09-16 16:27:23 -070029 cfg.define("in_port_t", Some("uint16_t"));
30 }
31
Alex Crichtond11e9142015-09-15 23:28:52 -070032 cfg.header("errno.h")
33 .header("fcntl.h")
34 .header("limits.h")
35 .header("stddef.h")
36 .header("stdint.h")
37 .header("stdio.h")
38 .header("stdlib.h")
39 .header("sys/stat.h")
40 .header("sys/types.h")
41 .header("time.h")
42 .header("wchar.h");
Alex Crichtonac2bd852015-09-10 17:21:20 -070043
Alex Crichton1ff96102015-09-17 15:09:02 -070044 if windows {
Alex Crichtond11e9142015-09-15 23:28:52 -070045 cfg.header("winsock2.h"); // must be before windows.h
46
47 cfg.header("direct.h");
48 cfg.header("io.h");
49 cfg.header("sys/utime.h");
50 cfg.header("windows.h");
51 cfg.header("process.h");
52 cfg.header("ws2ipdef.h");
53
54 if target.contains("gnu") {
55 cfg.header("ws2tcpip.h");
Alex Crichton310d6232015-09-10 10:56:31 -070056 }
Alex Crichtond11e9142015-09-15 23:28:52 -070057 } else {
58 cfg.header("ctype.h");
59 cfg.header("dirent.h");
60 cfg.header("net/if.h");
61 cfg.header("netdb.h");
62 cfg.header("netinet/in.h");
63 cfg.header("netinet/ip.h");
64 cfg.header("netinet/tcp.h");
65 cfg.header("pthread.h");
Alex Crichton1ff96102015-09-17 15:09:02 -070066 cfg.header("dlfcn.h");
Alex Crichtond11e9142015-09-15 23:28:52 -070067 cfg.header("signal.h");
68 cfg.header("string.h");
69 cfg.header("sys/file.h");
70 cfg.header("sys/ioctl.h");
71 cfg.header("sys/mman.h");
72 cfg.header("sys/resource.h");
73 cfg.header("sys/socket.h");
74 cfg.header("sys/time.h");
75 cfg.header("sys/un.h");
76 cfg.header("sys/wait.h");
77 cfg.header("unistd.h");
78 cfg.header("utime.h");
Alex Crichtoncd9b33e2015-09-17 14:47:40 -070079 cfg.header("pwd.h");
80 cfg.header("grp.h");
Alex Crichtoncd9b33e2015-09-17 14:47:40 -070081 }
Alex Crichtona1b948e2015-09-18 11:54:32 -070082
83 if android {
84 cfg.header("arpa/inet.h");
85 } else if !windows {
86 cfg.header("glob.h");
87 cfg.header("ifaddrs.h");
88
89 if !musl {
90 cfg.header("execinfo.h");
91 cfg.header("sys/sysctl.h");
92 }
93 }
94
Alex Crichtonbaef6112015-09-19 23:20:53 -070095 if apple {
Alex Crichtona1b948e2015-09-18 11:54:32 -070096 cfg.header("mach-o/dyld.h");
97 cfg.header("mach/mach_time.h");
98 cfg.header("malloc/malloc.h");
Alex Crichtonbaef6112015-09-19 23:20:53 -070099 if target.starts_with("x86") {
100 cfg.header("crt_externs.h");
101 }
Alex Crichtona1b948e2015-09-18 11:54:32 -0700102 }
103
104 if linux || android {
105 cfg.header("netpacket/packet.h");
106 cfg.header("net/ethernet.h");
107 cfg.header("malloc.h");
Alex Crichtoncd9b33e2015-09-17 14:47:40 -0700108 cfg.header("sys/prctl.h");
Alex Crichton310d6232015-09-10 10:56:31 -0700109 }
110
Alex Crichtona1b948e2015-09-18 11:54:32 -0700111 if freebsd {
112 cfg.header("pthread_np.h");
113 }
114
Alex Crichtond11e9142015-09-15 23:28:52 -0700115 cfg.type_name(move |ty, is_struct| {
Alex Crichton310d6232015-09-10 10:56:31 -0700116 match ty {
Alex Crichton31504842015-09-10 23:43:41 -0700117 // Just pass all these through, no need for a "struct" prefix
Alex Crichton22378822015-09-10 19:59:23 -0700118 "FILE" |
Alex Crichtoncd9b33e2015-09-17 14:47:40 -0700119 "DIR" => ty.to_string(),
120
121 // Fixup a few types on windows that don't actually exist.
122 "time64_t" if windows => "__time64_t".to_string(),
123 "ssize_t" if windows => "SSIZE_T".to_string(),
124
Alex Crichtonde9736d2015-09-17 15:47:44 -0700125 // OSX calls this something else
Alex Crichtona1b948e2015-09-18 11:54:32 -0700126 "sighandler_t" if bsdlike => "sig_t".to_string(),
Alex Crichtonde9736d2015-09-17 15:47:44 -0700127
Alex Crichtoncd9b33e2015-09-17 14:47:40 -0700128 t if t.ends_with("_t") => t.to_string(),
Alex Crichton310d6232015-09-10 10:56:31 -0700129
Alex Crichton31504842015-09-10 23:43:41 -0700130 // Windows uppercase structs don't have `struct` in front, there's a
131 // few special cases for windows, and then otherwise put `struct` in
132 // front of everything.
Alex Crichtond11e9142015-09-15 23:28:52 -0700133 t if is_struct => {
Alex Crichton13a6f2d2015-09-10 18:10:58 -0700134 if windows && ty.chars().next().unwrap().is_uppercase() {
135 t.to_string()
136 } else if windows && t == "stat" {
137 "struct __stat64".to_string()
Alex Crichton7da9b102015-09-10 20:57:14 -0700138 } else if windows && t == "utimbuf" {
139 "struct __utimbuf64".to_string()
Alex Crichton13a6f2d2015-09-10 18:10:58 -0700140 } else {
141 format!("struct {}", t)
142 }
143 }
Alex Crichton310d6232015-09-10 10:56:31 -0700144
145 t => t.to_string(),
146 }
Alex Crichtond11e9142015-09-15 23:28:52 -0700147 });
Alex Crichton310d6232015-09-10 10:56:31 -0700148
Alex Crichtond11e9142015-09-15 23:28:52 -0700149 let target2 = target.clone();
150 cfg.field_name(move |struct_, field| {
Alex Crichton310d6232015-09-10 10:56:31 -0700151 match field {
Alex Crichton31504842015-09-10 23:43:41 -0700152 // Our stat *_nsec fields normally don't actually exist but are part
153 // of a timeval struct
Alex Crichton310d6232015-09-10 10:56:31 -0700154 s if s.ends_with("_nsec") && struct_ == "stat" => {
Alex Crichtonbaef6112015-09-19 23:20:53 -0700155 if target2.contains("apple") {
Alex Crichton310d6232015-09-10 10:56:31 -0700156 s.replace("_nsec", "spec.tv_nsec")
Alex Crichtond11e9142015-09-15 23:28:52 -0700157 } else if target2.contains("android") {
Alex Crichtond3d77922015-09-11 17:03:39 -0700158 s.to_string()
Alex Crichton310d6232015-09-10 10:56:31 -0700159 } else {
160 s.replace("e_nsec", ".tv_nsec")
161 }
162 }
163 s => s.to_string(),
164 }
Alex Crichtond11e9142015-09-15 23:28:52 -0700165 });
Alex Crichton310d6232015-09-10 10:56:31 -0700166
Alex Crichtond11e9142015-09-15 23:28:52 -0700167 cfg.skip_type(move |ty| {
Alex Crichton310d6232015-09-10 10:56:31 -0700168 match ty {
Alex Crichtond3d77922015-09-11 17:03:39 -0700169 // sighandler_t is crazy across platforms
Alex Crichtond11e9142015-09-15 23:28:52 -0700170 "sighandler_t" => true,
Alex Crichtond3d77922015-09-11 17:03:39 -0700171
Alex Crichtond11e9142015-09-15 23:28:52 -0700172 _ => false
Alex Crichton310d6232015-09-10 10:56:31 -0700173 }
Alex Crichtond11e9142015-09-15 23:28:52 -0700174 });
Alex Crichton16083062015-09-09 22:59:24 -0700175
Alex Crichtond11e9142015-09-15 23:28:52 -0700176 cfg.skip_signededness(|c| {
Alex Crichton6d3cfdb2015-09-15 14:53:01 -0700177 match c {
Alex Crichton7b28c272015-09-15 20:56:16 -0700178 "LARGE_INTEGER" |
Alex Crichtoneef03da2015-09-15 16:57:06 -0700179 "mach_timebase_info_data_t" |
Alex Crichton7b28c272015-09-15 20:56:16 -0700180 "float" |
Alex Crichtond11e9142015-09-15 23:28:52 -0700181 "double" => true,
182 n if n.starts_with("pthread") => true,
Alex Crichton18469182015-09-15 20:57:42 -0700183
184 // windows-isms
Alex Crichtond11e9142015-09-15 23:28:52 -0700185 n if n.starts_with("P") => true,
186 n if n.starts_with("H") => true,
187 n if n.starts_with("LP") => true,
188 _ => false,
Alex Crichton6d3cfdb2015-09-15 14:53:01 -0700189 }
Alex Crichtond11e9142015-09-15 23:28:52 -0700190 });
Alex Crichton6d3cfdb2015-09-15 14:53:01 -0700191
Alex Crichtond11e9142015-09-15 23:28:52 -0700192 cfg.skip_const(move |name| {
Alex Crichton31504842015-09-10 23:43:41 -0700193 match name {
Alex Crichtoncd9b33e2015-09-17 14:47:40 -0700194 // Apparently these don't exist in mingw headers?
Alex Crichton31504842015-09-10 23:43:41 -0700195 "MEM_RESET_UNDO" |
196 "FILE_ATTRIBUTE_NO_SCRUB_DATA" |
197 "FILE_ATTRIBUTE_INTEGRITY_STREAM" |
Alex Crichtond11e9142015-09-15 23:28:52 -0700198 "ERROR_NOTHING_TO_TERMINATE" if mingw => true,
Alex Crichtoncd9b33e2015-09-17 14:47:40 -0700199
Alex Crichtond11e9142015-09-15 23:28:52 -0700200 "SIG_IGN" => true, // sighandler_t weirdness
Alex Crichtoncd9b33e2015-09-17 14:47:40 -0700201
Alex Crichton15b83c22015-09-17 17:25:52 -0700202 // types on musl are defined a little differently
Alex Crichton3a572fd2015-10-29 16:52:25 -0700203 n if musl && n.contains("__SIZEOF_PTHREAD") => true,
Alex Crichton15b83c22015-09-17 17:25:52 -0700204
Alex Crichtond11e9142015-09-15 23:28:52 -0700205 _ => false,
Alex Crichton31504842015-09-10 23:43:41 -0700206 }
Alex Crichtond11e9142015-09-15 23:28:52 -0700207 });
Alex Crichton31504842015-09-10 23:43:41 -0700208
Alex Crichtoncd9b33e2015-09-17 14:47:40 -0700209 cfg.skip_fn(move |name| {
210 // skip those that are manually verifiedmanually verified
Alex Crichton22378822015-09-10 19:59:23 -0700211 match name {
Alex Crichtoncd9b33e2015-09-17 14:47:40 -0700212 "execv" | // crazy stuff with const/mut
Alex Crichton22378822015-09-10 19:59:23 -0700213 "execve" |
214 "execvp" |
Alex Crichtoncd9b33e2015-09-17 14:47:40 -0700215 "execvpe" => true,
216
217 "getrlimit" | // non-int in 1st arg
218 "setrlimit" | // non-int in 1st arg
Alex Crichtoncd9b33e2015-09-17 14:47:40 -0700219 "strerror_r" if linux => true, // actually xpg-something-or-other
220
Alex Crichton1ff96102015-09-17 15:09:02 -0700221 // typed 2nd arg on linux and android
Alex Crichtona1b948e2015-09-18 11:54:32 -0700222 "gettimeofday" if linux || android || freebsd => true,
Alex Crichton1ff96102015-09-17 15:09:02 -0700223
224 "dlerror" if android => true, // const-ness is added
225
Alex Crichtond11e9142015-09-15 23:28:52 -0700226 _ => false,
Alex Crichton22378822015-09-10 19:59:23 -0700227 }
Alex Crichtond11e9142015-09-15 23:28:52 -0700228 });
Alex Crichton22378822015-09-10 19:59:23 -0700229
Alex Crichtone0f4d102015-09-16 09:48:14 -0700230 // Windows dllimport oddness?
231 cfg.skip_fn_ptrcheck(move |_| windows);
232
Alex Crichton15b83c22015-09-17 17:25:52 -0700233 cfg.skip_field_type(move |struct_, field| {
Alex Crichtonf3b97482015-09-16 14:13:20 -0700234 // This is a weird union, don't check the type.
Alex Crichtoncd9b33e2015-09-17 14:47:40 -0700235 (struct_ == "ifaddrs" && field == "ifa_ifu") ||
236 // sighandler_t type is super weird
237 (struct_ == "sigaction" && field == "sa_sigaction")
238 });
239
Alex Crichton15b83c22015-09-17 17:25:52 -0700240 cfg.skip_field(move |struct_, field| {
Alex Crichtoncd9b33e2015-09-17 14:47:40 -0700241 // this is actually a union on linux, so we can't represent it well and
242 // just insert some padding.
Alex Crichton15b83c22015-09-17 17:25:52 -0700243 (struct_ == "siginfo_t" && field == "_pad") ||
244 // musl names this __dummy1 but it's still there
245 (musl && struct_ == "glob_t" && field == "gl_flags")
Alex Crichtonf3b97482015-09-16 14:13:20 -0700246 });
247
Alex Crichtond11e9142015-09-15 23:28:52 -0700248 cfg.generate("../src/lib.rs", "all.rs");
Alex Crichton310d6232015-09-10 10:56:31 -0700249}