Skip to content

Commit 6053d99

Browse files
author
bors-servo
authored
Auto merge of #828 - upsuper:windows-test, r=emilio
Have tests pass on Windows These changes allow `cargo test` to pass on Windows. There are two followup work we should do at some point: * setup AppVeyor so that we don't break Windows development either * convert `no_system_header_includes` to be pure Rust so that it supports Windows natively
2 parents 87bd3b9 + 2944834 commit 6053d99

File tree

2 files changed

+108
-8
lines changed

2 files changed

+108
-8
lines changed

tests/expectations/tests/test_multiple_header_calls_in_builder.rs

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,92 @@ extern "C" {
99
::std::os::raw::c_int)
1010
-> ::std::os::raw::c_int>;
1111
}
12-
#[repr(u32)]
13-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
14-
pub enum Foo { Bar = 0, Qux = 1, }
15-
#[repr(i32)]
16-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
17-
pub enum Neg { MinusOne = -1, One = 1, }
12+
pub type Char = ::std::os::raw::c_char;
13+
pub type SChar = ::std::os::raw::c_schar;
14+
pub type UChar = ::std::os::raw::c_uchar;
15+
#[repr(C)]
16+
#[derive(Debug, Copy)]
17+
pub struct Test {
18+
pub ch: ::std::os::raw::c_char,
19+
pub u: ::std::os::raw::c_uchar,
20+
pub d: ::std::os::raw::c_schar,
21+
pub cch: ::std::os::raw::c_char,
22+
pub cu: ::std::os::raw::c_uchar,
23+
pub cd: ::std::os::raw::c_schar,
24+
pub Cch: Char,
25+
pub Cu: UChar,
26+
pub Cd: SChar,
27+
pub Ccch: Char,
28+
pub Ccu: UChar,
29+
pub Ccd: SChar,
30+
}
31+
#[test]
32+
fn bindgen_test_layout_Test() {
33+
assert_eq!(::std::mem::size_of::<Test>() , 12usize , concat ! (
34+
"Size of: " , stringify ! ( Test ) ));
35+
assert_eq! (::std::mem::align_of::<Test>() , 1usize , concat ! (
36+
"Alignment of " , stringify ! ( Test ) ));
37+
assert_eq! (unsafe {
38+
& ( * ( 0 as * const Test ) ) . ch as * const _ as usize } ,
39+
0usize , concat ! (
40+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
41+
stringify ! ( ch ) ));
42+
assert_eq! (unsafe {
43+
& ( * ( 0 as * const Test ) ) . u as * const _ as usize } ,
44+
1usize , concat ! (
45+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
46+
stringify ! ( u ) ));
47+
assert_eq! (unsafe {
48+
& ( * ( 0 as * const Test ) ) . d as * const _ as usize } ,
49+
2usize , concat ! (
50+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
51+
stringify ! ( d ) ));
52+
assert_eq! (unsafe {
53+
& ( * ( 0 as * const Test ) ) . cch as * const _ as usize } ,
54+
3usize , concat ! (
55+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
56+
stringify ! ( cch ) ));
57+
assert_eq! (unsafe {
58+
& ( * ( 0 as * const Test ) ) . cu as * const _ as usize } ,
59+
4usize , concat ! (
60+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
61+
stringify ! ( cu ) ));
62+
assert_eq! (unsafe {
63+
& ( * ( 0 as * const Test ) ) . cd as * const _ as usize } ,
64+
5usize , concat ! (
65+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
66+
stringify ! ( cd ) ));
67+
assert_eq! (unsafe {
68+
& ( * ( 0 as * const Test ) ) . Cch as * const _ as usize } ,
69+
6usize , concat ! (
70+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
71+
stringify ! ( Cch ) ));
72+
assert_eq! (unsafe {
73+
& ( * ( 0 as * const Test ) ) . Cu as * const _ as usize } ,
74+
7usize , concat ! (
75+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
76+
stringify ! ( Cu ) ));
77+
assert_eq! (unsafe {
78+
& ( * ( 0 as * const Test ) ) . Cd as * const _ as usize } ,
79+
8usize , concat ! (
80+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
81+
stringify ! ( Cd ) ));
82+
assert_eq! (unsafe {
83+
& ( * ( 0 as * const Test ) ) . Ccch as * const _ as usize } ,
84+
9usize , concat ! (
85+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
86+
stringify ! ( Ccch ) ));
87+
assert_eq! (unsafe {
88+
& ( * ( 0 as * const Test ) ) . Ccu as * const _ as usize } ,
89+
10usize , concat ! (
90+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
91+
stringify ! ( Ccu ) ));
92+
assert_eq! (unsafe {
93+
& ( * ( 0 as * const Test ) ) . Ccd as * const _ as usize } ,
94+
11usize , concat ! (
95+
"Alignment of field: " , stringify ! ( Test ) , "::" ,
96+
stringify ! ( Ccd ) ));
97+
}
98+
impl Clone for Test {
99+
fn clone(&self) -> Self { *self }
100+
}

tests/tests.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use bindgen::{Builder, builder, clang_version};
77
use std::fs;
88
use std::io::{BufRead, BufReader, Error, ErrorKind, Read, Write};
99
use std::path::PathBuf;
10-
use std::process::Command;
1110

1211
#[path="../src/options.rs"]
1312
mod options;
@@ -137,6 +136,20 @@ fn create_bindgen_builder(header: &PathBuf) -> Result<Option<Builder>, Error> {
137136
}
138137
}
139138

139+
// Windows platform has various different conventions than *nix platforms,
140+
// e.g. default enum underlying type, struct padding, mangling. Most tests
141+
// were written and checked on Linux and macOS, and thus they could fail on
142+
// Windows. We just make those tests targetting Linux instead as far as one
143+
// isn't annotated for a specific target.
144+
if cfg!(target_os = "windows") {
145+
if flags.iter().all(|flag| !flag.starts_with("--target=")) {
146+
if !flags.iter().any(|flag| flag == "--") {
147+
flags.push("--".into());
148+
}
149+
flags.push("--target=x86_64-unknown-linux".into());
150+
}
151+
}
152+
140153
// Fool builder_from_flags() into believing it has real env::args_os...
141154
// - add "bindgen" as executable name 0th element
142155
// - add header filename as 1st element
@@ -206,7 +219,7 @@ extern \"C\" {
206219
fn test_multiple_header_calls_in_builder() {
207220
let actual = builder()
208221
.header(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/headers/func_ptr.h"))
209-
.header(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/headers/enum.h"))
222+
.header(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/headers/char.h"))
210223
.generate()
211224
.unwrap()
212225
.to_string();
@@ -229,7 +242,11 @@ fn test_multiple_header_calls_in_builder() {
229242
}
230243

231244
#[test]
245+
// Doesn't support executing sh file on Windows.
246+
// We may want to implement it in Rust so that we support all systems.
247+
#[cfg(not(target_os = "windows"))]
232248
fn no_system_header_includes() {
249+
use std::process::Command;
233250
assert!(Command::new("./ci/no-includes.sh")
234251
.current_dir(env!("CARGO_MANIFEST_DIR"))
235252
.spawn()

0 commit comments

Comments
 (0)