diff options
author | Alex Crichton <alex@alexcrichton.com> | 2015-09-09 22:46:19 -0700 |
---|---|---|
committer | Alex Crichton <alex@alexcrichton.com> | 2015-09-09 22:46:19 -0700 |
commit | 8e5f0cd827041b0713d6f302504ec66fbe251e5e (patch) | |
tree | 830343cc6957c6edc2d5f4257dea10f65464efa3 | |
parent | dafaca90f0950c17e81e420172b9661c77d128fb (diff) | |
download | rust-libc-8e5f0cd827041b0713d6f302504ec66fbe251e5e.tar.gz |
Add skeleton testing framework
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | libc-test/Cargo.lock | 100 | ||||
-rw-r--r-- | libc-test/Cargo.toml | 17 | ||||
-rw-r--r-- | libc-test/build.rs | 130 | ||||
-rw-r--r-- | libc-test/src/lib.rs | 0 | ||||
-rw-r--r-- | libc-test/tests/all.rs | 5 |
6 files changed, 254 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore index 4fffb2f89c..a9d37c560c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -/target -/Cargo.lock +target +Cargo.lock diff --git a/libc-test/Cargo.lock b/libc-test/Cargo.lock new file mode 100644 index 0000000000..23d47b0a50 --- /dev/null +++ b/libc-test/Cargo.lock @@ -0,0 +1,100 @@ +[root] +name = "libc-test" +version = "0.1.0" +dependencies = [ + "gcc 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.1.10", + "syntex_syntax 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "advapi32-sys" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bitflags" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "gcc" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "kernel32-sys" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.1.10" + +[[package]] +name = "libc" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "log" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc-serialize" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "syntex_syntax" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", + "term 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "term" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-xid" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + diff --git a/libc-test/Cargo.toml b/libc-test/Cargo.toml new file mode 100644 index 0000000000..dfad9b7273 --- /dev/null +++ b/libc-test/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "libc-test" +version = "0.1.0" +authors = ["Alex Crichton <alex@alexcrichton.com>"] +build = "build.rs" + +[dependencies] +libc = { path = ".." } + +[build-dependencies] +syntex_syntax = "*" +gcc = "0.3" + +[lib] +name = "libc_test" +test = false +doctest = false diff --git a/libc-test/build.rs b/libc-test/build.rs new file mode 100644 index 0000000000..556e7575a7 --- /dev/null +++ b/libc-test/build.rs @@ -0,0 +1,130 @@ +#![allow(unused_must_use)] + +extern crate gcc; +extern crate syntex_syntax as syntax; + +use std::env; +use std::fs::File; +use std::io::BufWriter; +use std::io::prelude::*; +use std::path::{Path, PathBuf}; + +use syntax::ast; +use syntax::parse::token::InternedString; +use syntax::attr; +use syntax::parse::{self, ParseSess}; +use syntax::visit::{self, Visitor}; + +struct TestGenerator { + rust: Box<Write>, + c: Box<Write>, +} + +fn main() { + let sess = ParseSess::new(); + + let src = Path::new("../src/lib.rs"); + let cfg = Vec::new(); + let mut krate = parse::parse_crate_from_file(src, cfg, &sess); + build_cfg(&mut krate.config); + + let mut gated_cfgs = Vec::new(); + let krate = syntax::config::strip_unconfigured_items(&sess.span_diagnostic, + krate, + &mut gated_cfgs); + + let out = PathBuf::from(env::var_os("OUT_DIR").unwrap()); + let rust_out = BufWriter::new(File::create(out.join("all.rs")).unwrap()); + let mut c_out = BufWriter::new(File::create(out.join("all.c")).unwrap()); + + writeln!(c_out, " +#include <netinet/in.h> +#include <netinet/ip.h> +#include <signal.h> +#include <stdint.h> +#include <sys/types.h> +#include <time.h> +"); + + visit::walk_crate(&mut TestGenerator { + rust: Box::new(rust_out), + c: Box::new(c_out), + }, &krate); + + gcc::Config::new() + .file(out.join("all.c")) + .compile("liball.a"); +} + +fn build_cfg(cfg: &mut ast::CrateConfig) { + let target = env::var("TARGET").unwrap(); + + let (arch, target_pointer_width) = if target.starts_with("x86_64") { + ("x86_64", "64") + } else if target.starts_with("i686") { + ("x86", "32") + } else { + panic!("unknown arch/pointer width: {}", target) + }; + let (os, family, env) = if target.contains("unknown-linux") { + ("linux", "unix", "gnu") + } else { + panic!("unknown os/family width: {}", target) + }; + + let mk = attr::mk_name_value_item_str; + let s = InternedString::new; + cfg.push(attr::mk_word_item(s(family))); + cfg.push(mk(s("target_os"), s(os))); + cfg.push(mk(s("target_family"), s(family))); + cfg.push(mk(s("target_arch"), s(arch))); + // skip endianness + cfg.push(mk(s("target_pointer_width"), s(target_pointer_width))); + cfg.push(mk(s("target_env"), s(env))); +} + +impl TestGenerator { + fn typedef(&mut self, ty: &str) { + let cty = if ty.starts_with("c_") { + let rest = ty[2..].replace("long", " long"); + if rest.starts_with("u") { + format!("unsigned {}", &rest[1..]) + } else { + rest + } + } else { + (match ty { + ty => ty, + }).to_string() + }; + writeln!(self.c, r#" + uint64_t ty_{ty}_size() {{ + return sizeof({cty}); + }} + "#, ty = ty, cty = cty); + writeln!(self.rust, r#" + #[test] + fn test_{ty}_size() {{ + extern {{ fn ty_{ty}_size() -> u64; }} + assert_eq!(mem::size_of::<libc::{ty}>() as u64, + unsafe {{ ty_{ty}_size() }}); + }} + "#, ty = ty); + } +} + +impl<'v> Visitor<'v> for TestGenerator { + fn visit_item(&mut self, i: &'v ast::Item) { + match i.node { + ast::ItemTy(_, ref generics) => { + assert!(generics.lifetimes.len() == 0); + assert!(generics.ty_params.len() == 0); + assert!(generics.where_clause.predicates.len() == 0); + self.typedef(&i.ident.to_string()); + } + + _ => {} + } + visit::walk_item(self, i) + } +} diff --git a/libc-test/src/lib.rs b/libc-test/src/lib.rs new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/libc-test/src/lib.rs diff --git a/libc-test/tests/all.rs b/libc-test/tests/all.rs new file mode 100644 index 0000000000..1f615b3cfe --- /dev/null +++ b/libc-test/tests/all.rs @@ -0,0 +1,5 @@ +extern crate libc; +extern crate libc_test; + +#[cfg(test)] +include!(concat!(env!("OUT_DIR"), "/all.rs")); |