Skip to content

Commit 38c9eeb

Browse files
committed
rust-langGH-1090: Integration tests
integration test: document the GH issue integration test (macro): same order in impl as the trait for consistency integration test: move each setup into its own function, just to save face inline static func integration test
1 parent 0f47713 commit 38c9eeb

File tree

5 files changed

+103
-27
lines changed

5 files changed

+103
-27
lines changed

bindgen-integration/build.rs

Lines changed: 70 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,14 @@ impl ParseCallbacks for MacroCallback {
2828
MacroParsingBehavior::Default
2929
}
3030

31-
fn item_name(&self, original_item_name: &str) -> Option<String> {
32-
if original_item_name.starts_with("my_prefixed_") {
33-
Some(
34-
original_item_name
35-
.trim_start_matches("my_prefixed_")
36-
.to_string(),
37-
)
38-
} else if original_item_name.starts_with("MY_PREFIXED_") {
39-
Some(
40-
original_item_name
41-
.trim_start_matches("MY_PREFIXED_")
42-
.to_string(),
43-
)
44-
} else {
45-
None
31+
fn int_macro(&self, name: &str, _value: i64) -> Option<IntKind> {
32+
match name {
33+
"TESTMACRO_CUSTOMINTKIND_PATH" => Some(IntKind::Custom {
34+
name: "crate::MacroInteger",
35+
is_signed: true,
36+
}),
37+
38+
_ => None,
4639
}
4740
}
4841

@@ -67,17 +60,6 @@ impl ParseCallbacks for MacroCallback {
6760
}
6861
}
6962

70-
fn int_macro(&self, name: &str, _value: i64) -> Option<IntKind> {
71-
match name {
72-
"TESTMACRO_CUSTOMINTKIND_PATH" => Some(IntKind::Custom {
73-
name: "crate::MacroInteger",
74-
is_signed: true,
75-
}),
76-
77-
_ => None,
78-
}
79-
}
80-
8163
fn func_macro(&self, name: &str, value: &[&[u8]]) {
8264
match name {
8365
"TESTMACRO_NONFUNCTIONAL" => {
@@ -122,6 +104,24 @@ impl ParseCallbacks for MacroCallback {
122104
}
123105
}
124106

107+
fn item_name(&self, original_item_name: &str) -> Option<String> {
108+
if original_item_name.starts_with("my_prefixed_") {
109+
Some(
110+
original_item_name
111+
.trim_start_matches("my_prefixed_")
112+
.to_string(),
113+
)
114+
} else if original_item_name.starts_with("MY_PREFIXED_") {
115+
Some(
116+
original_item_name
117+
.trim_start_matches("MY_PREFIXED_")
118+
.to_string(),
119+
)
120+
} else {
121+
None
122+
}
123+
}
124+
125125
// Test the "custom derives" capability by adding `PartialEq` to the `Test` struct.
126126
fn add_derives(&self, info: &DeriveInfo<'_>) -> Vec<String> {
127127
if info.name == "Test" {
@@ -149,7 +149,7 @@ impl Drop for MacroCallback {
149149
}
150150
}
151151

152-
fn main() {
152+
fn setup_macro_test() {
153153
cc::Build::new()
154154
.cpp(true)
155155
.file("cpp/Test.cc")
@@ -204,3 +204,46 @@ fn main() {
204204
"including stub via include dir must produce correct dep path",
205205
);
206206
}
207+
208+
fn setup_extern_test() {
209+
// GH-1090: https://github.com/rust-lang/rust-bindgen/issues/1090
210+
cc::Build::new()
211+
.cpp(true)
212+
.file("cpp/extern.c")
213+
.include("include")
214+
.compile("libextern.a");
215+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
216+
let out_rust_file = out_path.join("extern.rs");
217+
let out_rust_file_relative = out_rust_file
218+
.strip_prefix(std::env::current_dir().unwrap().parent().unwrap())
219+
.unwrap();
220+
let out_dep_file = out_path.join("extern.d");
221+
222+
let bindings = Builder::default()
223+
.rustfmt_bindings(false)
224+
.header("cpp/extern.h")
225+
.clang_args(&["-x", "c++", "-I", "include"])
226+
.depfile(out_rust_file_relative.display().to_string(), &out_dep_file)
227+
.generate_extern_functions(true)
228+
.generate()
229+
.expect("Unable to generate extern bindings");
230+
231+
bindings
232+
.write_to_file(&out_rust_file)
233+
.expect("Couldn't write bindings!");
234+
let observed_deps =
235+
std::fs::read_to_string(out_dep_file).expect("Couldn't read depfile!");
236+
let expected_deps = format!(
237+
"{}: /tmp/bindgen/extern.hpp cpp/extern.h include/extern_stub.h",
238+
out_rust_file_relative.display()
239+
);
240+
assert_eq!(
241+
observed_deps, expected_deps,
242+
"including stub via include dir must produce correct dep path",
243+
);
244+
}
245+
246+
fn main() {
247+
setup_macro_test();
248+
setup_extern_test();
249+
}

bindgen-integration/cpp/extern.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include "extern.h"
2+
3+
int foo__extern() { return foo(); }
4+
int bar__extern() { return bar(); }

bindgen-integration/cpp/extern.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include "extern_stub.h" // this bad path is made valid by a `-I include` clang arg
2+
3+
int foo__extern();
4+
int bar__extern();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
static inline int foo() {
2+
return 11;
3+
}
4+
static int bar() {
5+
return 1;
6+
}
7+
inline int baz() {
8+
return 2;
9+
}

bindgen-integration/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ mod bindings {
44
include!(concat!(env!("OUT_DIR"), "/test.rs"));
55
}
66

7+
mod extern_bindings {
8+
include!(concat!(env!("OUT_DIR"), "/extern.rs"));
9+
}
10+
711
use std::ffi::CStr;
812
use std::mem;
913
use std::os::raw::c_int;
@@ -286,3 +290,15 @@ fn test_custom_derive() {
286290
assert!(meter < lightyear);
287291
assert!(meter > micron);
288292
}
293+
294+
#[test]
295+
fn test_extern_bindings() {
296+
// GH-1090: https://github.com/rust-lang/rust-bindgen/issues/1090
297+
unsafe {
298+
let f = extern_bindings::foo();
299+
assert_eq!(11, f);
300+
301+
let b = extern_bindings::bar();
302+
assert_eq!(1, b);
303+
}
304+
}

0 commit comments

Comments
 (0)