Alex Crichton | b5da7c0 | 2015-09-16 17:44:40 -0700 | [diff] [blame] | 1 | #![deny(warnings)] |
| 2 | |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 3 | extern crate ctest; |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 4 | |
| 5 | use std::env; |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 6 | |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 7 | fn main() { |
| 8 | let target = env::var("TARGET").unwrap(); |
| 9 | let windows = target.contains("windows"); |
| 10 | let mingw = target.contains("windows-gnu"); |
Alex Crichton | cd9b33e | 2015-09-17 14:47:40 -0700 | [diff] [blame] | 11 | let linux = target.contains("unknown-linux"); |
Alex Crichton | 1ff9610 | 2015-09-17 15:09:02 -0700 | [diff] [blame] | 12 | let android = target.contains("android"); |
Alex Crichton | baef611 | 2015-09-19 23:20:53 -0700 | [diff] [blame] | 13 | let apple = target.contains("apple"); |
Alex Crichton | 15b83c2 | 2015-09-17 17:25:52 -0700 | [diff] [blame] | 14 | let musl = target.contains("musl"); |
Alex Crichton | a1b948e | 2015-09-18 11:54:32 -0700 | [diff] [blame] | 15 | let freebsd = target.contains("freebsd"); |
Alex Crichton | baef611 | 2015-09-19 23:20:53 -0700 | [diff] [blame] | 16 | let bsdlike = freebsd || apple; |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 17 | let mut cfg = ctest::TestGenerator::new(); |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 18 | |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 19 | // Pull in extra goodies on linux/mingw |
Alex Crichton | 15b83c2 | 2015-09-17 17:25:52 -0700 | [diff] [blame] | 20 | if target.contains("unknown-linux") { |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 21 | cfg.define("_GNU_SOURCE", None); |
Alex Crichton | 1ff9610 | 2015-09-17 15:09:02 -0700 | [diff] [blame] | 22 | } else if windows { |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 23 | cfg.define("_WIN32_WINNT", Some("0x8000")); |
Alex Crichton | 0df7c10 | 2015-09-10 16:35:37 -0700 | [diff] [blame] | 24 | } |
| 25 | |
Alex Crichton | 094f44d | 2015-09-16 16:27:23 -0700 | [diff] [blame] | 26 | // Android doesn't actually have in_port_t but it's much easier if we |
| 27 | // provide one for us to test against |
Alex Crichton | 1ff9610 | 2015-09-17 15:09:02 -0700 | [diff] [blame] | 28 | if android { |
Alex Crichton | 094f44d | 2015-09-16 16:27:23 -0700 | [diff] [blame] | 29 | cfg.define("in_port_t", Some("uint16_t")); |
| 30 | } |
| 31 | |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 32 | 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 Crichton | ac2bd85 | 2015-09-10 17:21:20 -0700 | [diff] [blame] | 43 | |
Alex Crichton | 1ff9610 | 2015-09-17 15:09:02 -0700 | [diff] [blame] | 44 | if windows { |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 45 | 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 Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 56 | } |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 57 | } 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 Crichton | 1ff9610 | 2015-09-17 15:09:02 -0700 | [diff] [blame] | 66 | cfg.header("dlfcn.h"); |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 67 | 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 Crichton | cd9b33e | 2015-09-17 14:47:40 -0700 | [diff] [blame] | 79 | cfg.header("pwd.h"); |
| 80 | cfg.header("grp.h"); |
Alex Crichton | cd9b33e | 2015-09-17 14:47:40 -0700 | [diff] [blame] | 81 | } |
Alex Crichton | a1b948e | 2015-09-18 11:54:32 -0700 | [diff] [blame] | 82 | |
| 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 Crichton | baef611 | 2015-09-19 23:20:53 -0700 | [diff] [blame] | 95 | if apple { |
Alex Crichton | a1b948e | 2015-09-18 11:54:32 -0700 | [diff] [blame] | 96 | cfg.header("mach-o/dyld.h"); |
| 97 | cfg.header("mach/mach_time.h"); |
| 98 | cfg.header("malloc/malloc.h"); |
Alex Crichton | baef611 | 2015-09-19 23:20:53 -0700 | [diff] [blame] | 99 | if target.starts_with("x86") { |
| 100 | cfg.header("crt_externs.h"); |
| 101 | } |
Alex Crichton | a1b948e | 2015-09-18 11:54:32 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | if linux || android { |
| 105 | cfg.header("netpacket/packet.h"); |
| 106 | cfg.header("net/ethernet.h"); |
| 107 | cfg.header("malloc.h"); |
Alex Crichton | cd9b33e | 2015-09-17 14:47:40 -0700 | [diff] [blame] | 108 | cfg.header("sys/prctl.h"); |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Alex Crichton | a1b948e | 2015-09-18 11:54:32 -0700 | [diff] [blame] | 111 | if freebsd { |
| 112 | cfg.header("pthread_np.h"); |
| 113 | } |
| 114 | |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 115 | cfg.type_name(move |ty, is_struct| { |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 116 | match ty { |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 117 | // Just pass all these through, no need for a "struct" prefix |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 118 | "FILE" | |
Alex Crichton | 07d3a0d | 2015-10-30 10:21:32 -0700 | [diff] [blame] | 119 | "fd_set" | |
Alex Crichton | 88d23e7 | 2015-11-02 17:03:19 -0800 | [diff] [blame^] | 120 | "Dl_info" | |
Alex Crichton | cd9b33e | 2015-09-17 14:47:40 -0700 | [diff] [blame] | 121 | "DIR" => ty.to_string(), |
| 122 | |
| 123 | // Fixup a few types on windows that don't actually exist. |
| 124 | "time64_t" if windows => "__time64_t".to_string(), |
| 125 | "ssize_t" if windows => "SSIZE_T".to_string(), |
| 126 | |
Alex Crichton | de9736d | 2015-09-17 15:47:44 -0700 | [diff] [blame] | 127 | // OSX calls this something else |
Alex Crichton | a1b948e | 2015-09-18 11:54:32 -0700 | [diff] [blame] | 128 | "sighandler_t" if bsdlike => "sig_t".to_string(), |
Alex Crichton | de9736d | 2015-09-17 15:47:44 -0700 | [diff] [blame] | 129 | |
Alex Crichton | cd9b33e | 2015-09-17 14:47:40 -0700 | [diff] [blame] | 130 | t if t.ends_with("_t") => t.to_string(), |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 131 | |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 132 | // Windows uppercase structs don't have `struct` in front, there's a |
| 133 | // few special cases for windows, and then otherwise put `struct` in |
| 134 | // front of everything. |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 135 | t if is_struct => { |
Alex Crichton | 13a6f2d | 2015-09-10 18:10:58 -0700 | [diff] [blame] | 136 | if windows && ty.chars().next().unwrap().is_uppercase() { |
| 137 | t.to_string() |
| 138 | } else if windows && t == "stat" { |
| 139 | "struct __stat64".to_string() |
Alex Crichton | 7da9b10 | 2015-09-10 20:57:14 -0700 | [diff] [blame] | 140 | } else if windows && t == "utimbuf" { |
| 141 | "struct __utimbuf64".to_string() |
Alex Crichton | 13a6f2d | 2015-09-10 18:10:58 -0700 | [diff] [blame] | 142 | } else { |
| 143 | format!("struct {}", t) |
| 144 | } |
| 145 | } |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 146 | |
| 147 | t => t.to_string(), |
| 148 | } |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 149 | }); |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 150 | |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 151 | let target2 = target.clone(); |
| 152 | cfg.field_name(move |struct_, field| { |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 153 | match field { |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 154 | // Our stat *_nsec fields normally don't actually exist but are part |
| 155 | // of a timeval struct |
Alex Crichton | 7482522 | 2015-10-29 17:36:55 -0700 | [diff] [blame] | 156 | s if s.ends_with("_nsec") && struct_.starts_with("stat") => { |
Alex Crichton | baef611 | 2015-09-19 23:20:53 -0700 | [diff] [blame] | 157 | if target2.contains("apple") { |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 158 | s.replace("_nsec", "spec.tv_nsec") |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 159 | } else if target2.contains("android") { |
Alex Crichton | d3d7792 | 2015-09-11 17:03:39 -0700 | [diff] [blame] | 160 | s.to_string() |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 161 | } else { |
| 162 | s.replace("e_nsec", ".tv_nsec") |
| 163 | } |
| 164 | } |
| 165 | s => s.to_string(), |
| 166 | } |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 167 | }); |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 168 | |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 169 | cfg.skip_type(move |ty| { |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 170 | match ty { |
Alex Crichton | d3d7792 | 2015-09-11 17:03:39 -0700 | [diff] [blame] | 171 | // sighandler_t is crazy across platforms |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 172 | "sighandler_t" => true, |
Alex Crichton | d3d7792 | 2015-09-11 17:03:39 -0700 | [diff] [blame] | 173 | |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 174 | _ => false |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 175 | } |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 176 | }); |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 177 | |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 178 | cfg.skip_signededness(|c| { |
Alex Crichton | 6d3cfdb | 2015-09-15 14:53:01 -0700 | [diff] [blame] | 179 | match c { |
Alex Crichton | 7b28c27 | 2015-09-15 20:56:16 -0700 | [diff] [blame] | 180 | "LARGE_INTEGER" | |
Alex Crichton | eef03da | 2015-09-15 16:57:06 -0700 | [diff] [blame] | 181 | "mach_timebase_info_data_t" | |
Alex Crichton | 7b28c27 | 2015-09-15 20:56:16 -0700 | [diff] [blame] | 182 | "float" | |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 183 | "double" => true, |
| 184 | n if n.starts_with("pthread") => true, |
Alex Crichton | 1846918 | 2015-09-15 20:57:42 -0700 | [diff] [blame] | 185 | |
| 186 | // windows-isms |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 187 | n if n.starts_with("P") => true, |
| 188 | n if n.starts_with("H") => true, |
| 189 | n if n.starts_with("LP") => true, |
| 190 | _ => false, |
Alex Crichton | 6d3cfdb | 2015-09-15 14:53:01 -0700 | [diff] [blame] | 191 | } |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 192 | }); |
Alex Crichton | 6d3cfdb | 2015-09-15 14:53:01 -0700 | [diff] [blame] | 193 | |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 194 | cfg.skip_const(move |name| { |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 195 | match name { |
Alex Crichton | cd9b33e | 2015-09-17 14:47:40 -0700 | [diff] [blame] | 196 | // Apparently these don't exist in mingw headers? |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 197 | "MEM_RESET_UNDO" | |
| 198 | "FILE_ATTRIBUTE_NO_SCRUB_DATA" | |
| 199 | "FILE_ATTRIBUTE_INTEGRITY_STREAM" | |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 200 | "ERROR_NOTHING_TO_TERMINATE" if mingw => true, |
Alex Crichton | cd9b33e | 2015-09-17 14:47:40 -0700 | [diff] [blame] | 201 | |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 202 | "SIG_IGN" => true, // sighandler_t weirdness |
Alex Crichton | cd9b33e | 2015-09-17 14:47:40 -0700 | [diff] [blame] | 203 | |
Alex Crichton | 15b83c2 | 2015-09-17 17:25:52 -0700 | [diff] [blame] | 204 | // types on musl are defined a little differently |
Alex Crichton | 3a572fd | 2015-10-29 16:52:25 -0700 | [diff] [blame] | 205 | n if musl && n.contains("__SIZEOF_PTHREAD") => true, |
Alex Crichton | 15b83c2 | 2015-09-17 17:25:52 -0700 | [diff] [blame] | 206 | |
Alex Crichton | 7482522 | 2015-10-29 17:36:55 -0700 | [diff] [blame] | 207 | // Skip constants not defined in MUSL but just passed down to the |
| 208 | // kernel regardless |
| 209 | "RLIMIT_NLIMITS" | |
| 210 | "TCP_COOKIE_TRANSACTIONS" | |
| 211 | "RLIMIT_RTTIME" if musl => true, |
| 212 | |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 213 | _ => false, |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 214 | } |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 215 | }); |
Alex Crichton | 3150484 | 2015-09-10 23:43:41 -0700 | [diff] [blame] | 216 | |
Alex Crichton | cd9b33e | 2015-09-17 14:47:40 -0700 | [diff] [blame] | 217 | cfg.skip_fn(move |name| { |
| 218 | // skip those that are manually verifiedmanually verified |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 219 | match name { |
Alex Crichton | cd9b33e | 2015-09-17 14:47:40 -0700 | [diff] [blame] | 220 | "execv" | // crazy stuff with const/mut |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 221 | "execve" | |
| 222 | "execvp" | |
Alex Crichton | cd9b33e | 2015-09-17 14:47:40 -0700 | [diff] [blame] | 223 | "execvpe" => true, |
| 224 | |
Alex Crichton | 7482522 | 2015-10-29 17:36:55 -0700 | [diff] [blame] | 225 | "getrlimit" | "getrlimit64" | // non-int in 1st arg |
| 226 | "setrlimit" | "setrlimit64" | // non-int in 1st arg |
Alex Crichton | cd9b33e | 2015-09-17 14:47:40 -0700 | [diff] [blame] | 227 | "strerror_r" if linux => true, // actually xpg-something-or-other |
| 228 | |
Alex Crichton | 1ff9610 | 2015-09-17 15:09:02 -0700 | [diff] [blame] | 229 | // typed 2nd arg on linux and android |
Alex Crichton | a1b948e | 2015-09-18 11:54:32 -0700 | [diff] [blame] | 230 | "gettimeofday" if linux || android || freebsd => true, |
Alex Crichton | 1ff9610 | 2015-09-17 15:09:02 -0700 | [diff] [blame] | 231 | |
| 232 | "dlerror" if android => true, // const-ness is added |
Alex Crichton | 88d23e7 | 2015-11-02 17:03:19 -0800 | [diff] [blame^] | 233 | "dladdr" if musl => true, // const-ness only added recently |
Alex Crichton | 1ff9610 | 2015-09-17 15:09:02 -0700 | [diff] [blame] | 234 | |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 235 | _ => false, |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 236 | } |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 237 | }); |
Alex Crichton | 2237882 | 2015-09-10 19:59:23 -0700 | [diff] [blame] | 238 | |
Alex Crichton | e0f4d10 | 2015-09-16 09:48:14 -0700 | [diff] [blame] | 239 | // Windows dllimport oddness? |
| 240 | cfg.skip_fn_ptrcheck(move |_| windows); |
| 241 | |
Alex Crichton | 15b83c2 | 2015-09-17 17:25:52 -0700 | [diff] [blame] | 242 | cfg.skip_field_type(move |struct_, field| { |
Alex Crichton | f3b9748 | 2015-09-16 14:13:20 -0700 | [diff] [blame] | 243 | // This is a weird union, don't check the type. |
Alex Crichton | cd9b33e | 2015-09-17 14:47:40 -0700 | [diff] [blame] | 244 | (struct_ == "ifaddrs" && field == "ifa_ifu") || |
| 245 | // sighandler_t type is super weird |
| 246 | (struct_ == "sigaction" && field == "sa_sigaction") |
| 247 | }); |
| 248 | |
Alex Crichton | 15b83c2 | 2015-09-17 17:25:52 -0700 | [diff] [blame] | 249 | cfg.skip_field(move |struct_, field| { |
Alex Crichton | cd9b33e | 2015-09-17 14:47:40 -0700 | [diff] [blame] | 250 | // this is actually a union on linux, so we can't represent it well and |
| 251 | // just insert some padding. |
Alex Crichton | 15b83c2 | 2015-09-17 17:25:52 -0700 | [diff] [blame] | 252 | (struct_ == "siginfo_t" && field == "_pad") || |
| 253 | // musl names this __dummy1 but it's still there |
| 254 | (musl && struct_ == "glob_t" && field == "gl_flags") |
Alex Crichton | f3b9748 | 2015-09-16 14:13:20 -0700 | [diff] [blame] | 255 | }); |
| 256 | |
Alex Crichton | d11e914 | 2015-09-15 23:28:52 -0700 | [diff] [blame] | 257 | cfg.generate("../src/lib.rs", "all.rs"); |
Alex Crichton | 310d623 | 2015-09-10 10:56:31 -0700 | [diff] [blame] | 258 | } |