Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 1 | #![allow(unused_must_use)] |
| 2 | |
| 3 | extern crate gcc; |
| 4 | extern crate syntex_syntax as syntax; |
| 5 | |
| 6 | use std::env; |
| 7 | use std::fs::File; |
| 8 | use std::io::BufWriter; |
| 9 | use std::io::prelude::*; |
| 10 | use std::path::{Path, PathBuf}; |
| 11 | |
| 12 | use syntax::ast; |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 13 | use syntax::diagnostic::SpanHandler; |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 14 | use syntax::parse::token::InternedString; |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 15 | use syntax::attr::{self, ReprAttr}; |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 16 | use syntax::parse::{self, ParseSess}; |
| 17 | use syntax::visit::{self, Visitor}; |
| 18 | |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 19 | struct TestGenerator<'a> { |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 20 | rust: Box<Write>, |
| 21 | c: Box<Write>, |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 22 | sh: &'a SpanHandler, |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | fn main() { |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 26 | let target = env::var("TARGET").unwrap(); |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 27 | |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 28 | let sess = ParseSess::new(); |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 29 | let src = Path::new("../src/lib.rs"); |
| 30 | let cfg = Vec::new(); |
| 31 | let mut krate = parse::parse_crate_from_file(src, cfg, &sess); |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 32 | build_cfg(&mut krate.config, &target); |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 33 | |
| 34 | let mut gated_cfgs = Vec::new(); |
| 35 | let krate = syntax::config::strip_unconfigured_items(&sess.span_diagnostic, |
| 36 | krate, |
| 37 | &mut gated_cfgs); |
| 38 | |
| 39 | let out = PathBuf::from(env::var_os("OUT_DIR").unwrap()); |
| 40 | let rust_out = BufWriter::new(File::create(out.join("all.rs")).unwrap()); |
| 41 | let mut c_out = BufWriter::new(File::create(out.join("all.c")).unwrap()); |
| 42 | |
| 43 | writeln!(c_out, " |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 44 | #include <glob.h> |
| 45 | #include <ifaddrs.h> |
| 46 | #include <netdb.h> |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 47 | #include <netinet/in.h> |
| 48 | #include <netinet/ip.h> |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 49 | #include <pthread.h> |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 50 | #include <signal.h> |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 51 | #include <stdalign.h> |
| 52 | #include <stddef.h> |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 53 | #include <stdint.h> |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 54 | #include <sys/resource.h> |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 55 | #include <sys/socket.h> |
| 56 | #include <sys/stat.h> |
| 57 | #include <sys/time.h> |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 58 | #include <sys/types.h> |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 59 | #include <sys/un.h> |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 60 | #include <time.h> |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 61 | #include <utime.h> |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 62 | #include <wchar.h> |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 63 | "); |
| 64 | |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 65 | if target.contains("apple-darwin") { |
| 66 | writeln!(c_out, " |
| 67 | #include <mach/mach_time.h> |
| 68 | "); |
| 69 | } |
| 70 | |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 71 | visit::walk_crate(&mut TestGenerator { |
| 72 | rust: Box::new(rust_out), |
| 73 | c: Box::new(c_out), |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 74 | sh: &sess.span_diagnostic, |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 75 | }, &krate); |
| 76 | |
| 77 | gcc::Config::new() |
| 78 | .file(out.join("all.c")) |
| 79 | .compile("liball.a"); |
| 80 | } |
| 81 | |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 82 | fn build_cfg(cfg: &mut ast::CrateConfig, target: &str) { |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 83 | let (arch, target_pointer_width) = if target.starts_with("x86_64") { |
| 84 | ("x86_64", "64") |
| 85 | } else if target.starts_with("i686") { |
| 86 | ("x86", "32") |
| 87 | } else { |
| 88 | panic!("unknown arch/pointer width: {}", target) |
| 89 | }; |
| 90 | let (os, family, env) = if target.contains("unknown-linux") { |
| 91 | ("linux", "unix", "gnu") |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 92 | } else if target.contains("apple-darwin") { |
| 93 | ("macos", "unix", "") |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 94 | } else { |
| 95 | panic!("unknown os/family width: {}", target) |
| 96 | }; |
| 97 | |
| 98 | let mk = attr::mk_name_value_item_str; |
| 99 | let s = InternedString::new; |
| 100 | cfg.push(attr::mk_word_item(s(family))); |
| 101 | cfg.push(mk(s("target_os"), s(os))); |
| 102 | cfg.push(mk(s("target_family"), s(family))); |
| 103 | cfg.push(mk(s("target_arch"), s(arch))); |
| 104 | // skip endianness |
| 105 | cfg.push(mk(s("target_pointer_width"), s(target_pointer_width))); |
| 106 | cfg.push(mk(s("target_env"), s(env))); |
| 107 | } |
| 108 | |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 109 | impl<'a> TestGenerator<'a> { |
| 110 | fn test_type(&mut self, ty: &str) { |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 111 | let cty = if ty.starts_with("c_") { |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame^] | 112 | match &ty[2..].replace("long", " long")[..] { |
| 113 | s if s.starts_with("u") => format!("unsigned {}", &s[1..]), |
| 114 | "short" => format!("short"), |
| 115 | s if s.starts_with("s") => format!("signed {}", &s[1..]), |
| 116 | s => s.to_string(), |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 117 | } |
| 118 | } else { |
| 119 | (match ty { |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 120 | "sighandler_t" => return, |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 121 | ty => ty, |
| 122 | }).to_string() |
| 123 | }; |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 124 | self.test_size_align(ty, &cty); |
| 125 | } |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 126 | |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame^] | 127 | fn test_struct(&mut self, ty: &str, s: &ast::StructDef) { |
| 128 | let cty = match ty { |
| 129 | t if ty.starts_with("pthread") => t.to_string(), |
| 130 | "glob_t" => "glob_t".to_string(), |
| 131 | "ip6_mreq" => "struct ipv6_mreq".to_string(), |
| 132 | s => format!("struct {}", s), |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 133 | }; |
| 134 | self.test_size_align(ty, &cty); |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame^] | 135 | |
| 136 | writeln!(self.rust, r#" |
| 137 | #[test] |
| 138 | fn field_offset_size_{ty}() {{ |
| 139 | "#, ty = ty); |
| 140 | for field in s.fields.iter() { |
| 141 | let name = match field.node.kind { |
| 142 | ast::NamedField(name, ast::Public) => name, |
| 143 | ast::NamedField(_, ast::Inherited) => continue, |
| 144 | ast::UnnamedField(..) => panic!("no tuple structs in FFI"), |
| 145 | }; |
| 146 | |
| 147 | let cname = match &name.to_string()[..] { |
| 148 | s if s.ends_with("_nsec") && ty == "stat" => { |
| 149 | s.replace("_nsec", "spec.tv_nsec") |
| 150 | } |
| 151 | s => s.to_string(), |
| 152 | }; |
| 153 | |
| 154 | writeln!(self.c, r#" |
| 155 | uint64_t __test_offset_{ty}_{rust_field}() {{ |
| 156 | return offsetof({cty}, {c_field}); |
| 157 | }} |
| 158 | uint64_t __test_size_{ty}_{rust_field}() {{ |
| 159 | {cty}* foo = NULL; |
| 160 | return sizeof(foo->{c_field}); |
| 161 | }} |
| 162 | "#, ty = ty, cty = cty, rust_field = name, c_field = cname); |
| 163 | writeln!(self.rust, r#" |
| 164 | extern {{ |
| 165 | fn __test_offset_{ty}_{field}() -> u64; |
| 166 | fn __test_size_{ty}_{field}() -> u64; |
| 167 | }} |
| 168 | unsafe {{ |
| 169 | let foo = 0 as *const {ty}; |
| 170 | same(offset_of!({ty}, {field}), |
| 171 | __test_offset_{ty}_{field}(), |
| 172 | "field offset {field} of {ty}"); |
| 173 | same(mem::size_of_val(&(*foo).{field}) as u64, |
| 174 | __test_size_{ty}_{field}(), |
| 175 | "field size {field} of {ty}"); |
| 176 | }} |
| 177 | "#, ty = ty, field = name); |
| 178 | } |
| 179 | writeln!(self.rust, r#" |
| 180 | }} |
| 181 | "#); |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | fn test_size_align(&mut self, rust: &str, c: &str) { |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 185 | writeln!(self.c, r#" |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 186 | uint64_t __test_size_{ty}() {{ return sizeof({cty}); }} |
| 187 | uint64_t __test_align_{ty}() {{ return alignof({cty}); }} |
| 188 | "#, ty = rust, cty = c); |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 189 | writeln!(self.rust, r#" |
| 190 | #[test] |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 191 | fn size_align_{ty}() {{ |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 192 | extern {{ |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 193 | fn __test_size_{ty}() -> u64; |
| 194 | fn __test_align_{ty}() -> u64; |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 195 | }} |
| 196 | unsafe {{ |
Alex Crichton | 3e5155b | 2015-09-09 23:46:19 -0700 | [diff] [blame^] | 197 | same(mem::size_of::<{ty}>() as u64, |
| 198 | __test_size_{ty}(), "size"); |
| 199 | same(mem::align_of::<{ty}>() as u64, |
| 200 | __test_align_{ty}(), "align"); |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 201 | }} |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 202 | }} |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 203 | "#, ty = rust); |
| 204 | } |
| 205 | |
| 206 | fn assert_no_generics(&self, _i: &ast::Item, generics: &ast::Generics) { |
| 207 | assert!(generics.lifetimes.len() == 0); |
| 208 | assert!(generics.ty_params.len() == 0); |
| 209 | assert!(generics.where_clause.predicates.len() == 0); |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 213 | impl<'a, 'v> Visitor<'v> for TestGenerator<'a> { |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 214 | fn visit_item(&mut self, i: &'v ast::Item) { |
| 215 | match i.node { |
| 216 | ast::ItemTy(_, ref generics) => { |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame] | 217 | self.assert_no_generics(i, generics); |
| 218 | self.test_type(&i.ident.to_string()); |
| 219 | } |
| 220 | |
| 221 | ast::ItemStruct(ref s, ref generics) => { |
| 222 | self.assert_no_generics(i, generics); |
| 223 | let is_c = i.attrs.iter().any(|a| { |
| 224 | attr::find_repr_attrs(self.sh, a).iter().any(|a| { |
| 225 | *a == ReprAttr::ReprExtern |
| 226 | }) |
| 227 | }); |
| 228 | if !is_c { |
| 229 | panic!("{} is not marked #[repr(C)]", i.ident); |
| 230 | } |
| 231 | self.test_struct(&i.ident.to_string(), s); |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | _ => {} |
| 235 | } |
| 236 | visit::walk_item(self, i) |
| 237 | } |
| 238 | } |