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_") { |
| 112 | let rest = ty[2..].replace("long", " long"); |
| 113 | if rest.starts_with("u") { |
| 114 | format!("unsigned {}", &rest[1..]) |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 115 | } else if rest.starts_with("s") && rest != "short" { |
| 116 | format!("signed {}", &rest[1..]) |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 117 | } else { |
| 118 | rest |
| 119 | } |
| 120 | } else { |
| 121 | (match ty { |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 122 | "sighandler_t" => return, |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 123 | ty => ty, |
| 124 | }).to_string() |
| 125 | }; |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame^] | 126 | self.test_size_align(ty, &cty); |
| 127 | } |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 128 | |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame^] | 129 | fn test_struct(&mut self, ty: &str, _s: &ast::StructDef) { |
| 130 | let cty = if ty.starts_with("pthread") || ty == "glob_t" { |
| 131 | ty.to_string() |
| 132 | } else if ty == "ip6_mreq" { |
| 133 | "struct ipv6_mreq".to_string() |
| 134 | } else { |
| 135 | format!("struct {}", ty) |
| 136 | }; |
| 137 | self.test_size_align(ty, &cty); |
| 138 | } |
| 139 | |
| 140 | fn test_size_align(&mut self, rust: &str, c: &str) { |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 141 | writeln!(self.c, r#" |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame^] | 142 | uint64_t __test_size_{ty}() {{ return sizeof({cty}); }} |
| 143 | uint64_t __test_align_{ty}() {{ return alignof({cty}); }} |
| 144 | "#, ty = rust, cty = c); |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 145 | writeln!(self.rust, r#" |
| 146 | #[test] |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame^] | 147 | fn size_align_{ty}() {{ |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 148 | extern {{ |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame^] | 149 | fn __test_size_{ty}() -> u64; |
| 150 | fn __test_align_{ty}() -> u64; |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 151 | }} |
| 152 | unsafe {{ |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame^] | 153 | let a = mem::size_of::<{ty}>() as u64; |
| 154 | let b = __test_size_{ty}(); |
| 155 | assert!(a == b, "bad size: rust {{}} != c {{}}", a, b); |
| 156 | let a = mem::align_of::<{ty}>() as u64; |
| 157 | let b = __test_align_{ty}(); |
| 158 | assert!(a == b, "bad align: rust {{}} != c {{}}", a, b); |
Alex Crichton | 1608306 | 2015-09-09 22:59:24 -0700 | [diff] [blame] | 159 | }} |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 160 | }} |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame^] | 161 | "#, ty = rust); |
| 162 | } |
| 163 | |
| 164 | fn assert_no_generics(&self, _i: &ast::Item, generics: &ast::Generics) { |
| 165 | assert!(generics.lifetimes.len() == 0); |
| 166 | assert!(generics.ty_params.len() == 0); |
| 167 | assert!(generics.where_clause.predicates.len() == 0); |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame^] | 171 | impl<'a, 'v> Visitor<'v> for TestGenerator<'a> { |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 172 | fn visit_item(&mut self, i: &'v ast::Item) { |
| 173 | match i.node { |
| 174 | ast::ItemTy(_, ref generics) => { |
Alex Crichton | a9adfbf | 2015-09-09 23:21:27 -0700 | [diff] [blame^] | 175 | self.assert_no_generics(i, generics); |
| 176 | self.test_type(&i.ident.to_string()); |
| 177 | } |
| 178 | |
| 179 | ast::ItemStruct(ref s, ref generics) => { |
| 180 | self.assert_no_generics(i, generics); |
| 181 | let is_c = i.attrs.iter().any(|a| { |
| 182 | attr::find_repr_attrs(self.sh, a).iter().any(|a| { |
| 183 | *a == ReprAttr::ReprExtern |
| 184 | }) |
| 185 | }); |
| 186 | if !is_c { |
| 187 | panic!("{} is not marked #[repr(C)]", i.ident); |
| 188 | } |
| 189 | self.test_struct(&i.ident.to_string(), s); |
Alex Crichton | 8e5f0cd | 2015-09-09 22:46:19 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | _ => {} |
| 193 | } |
| 194 | visit::walk_item(self, i) |
| 195 | } |
| 196 | } |