forked from rust-lang/rust-bindgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
288 lines (258 loc) · 9.59 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
extern crate bindgen;
extern crate cc;
use bindgen::callbacks::{
DeriveInfo, IntKind, MacroParsingBehavior, ParseCallbacks,
};
use bindgen::{Builder, CargoCallbacks, EnumVariation, Formatter};
use std::collections::HashSet;
use std::env;
use std::path::PathBuf;
use std::sync::{Arc, Mutex, RwLock};
#[derive(Debug)]
struct MacroCallback {
macros: Arc<RwLock<HashSet<String>>>,
seen_hellos: Mutex<u32>,
seen_funcs: Mutex<u32>,
}
impl ParseCallbacks for MacroCallback {
fn will_parse_macro(&self, name: &str) -> MacroParsingBehavior {
self.macros.write().unwrap().insert(name.into());
if name == "MY_ANNOYING_MACRO" {
return MacroParsingBehavior::Ignore;
}
MacroParsingBehavior::Default
}
fn int_macro(&self, name: &str, _value: i64) -> Option<IntKind> {
match name {
"TESTMACRO_CUSTOMINTKIND_PATH" => Some(IntKind::Custom {
name: "crate::MacroInteger",
is_signed: true,
}),
_ => None,
}
}
fn str_macro(&self, name: &str, value: &[u8]) {
match name {
"TESTMACRO_STRING_EXPR" => {
assert_eq!(value, b"string");
*self.seen_hellos.lock().unwrap() += 1;
}
"TESTMACRO_STRING_EXPANDED" |
"TESTMACRO_STRING" |
"TESTMACRO_INTEGER" => {
// The integer test macro is, actually, not expected to show up here at all -- but
// should produce an error if it does.
assert_eq!(
value, b"Hello Preprocessor!",
"str_macro handle received unexpected value"
);
*self.seen_hellos.lock().unwrap() += 1;
}
_ => {}
}
}
fn func_macro(&self, name: &str, value: &[&[u8]]) {
match name {
"TESTMACRO_NONFUNCTIONAL" => {
panic!("func_macro was called for a non-functional macro");
}
"TESTMACRO_FUNCTIONAL_NONEMPTY(TESTMACRO_INTEGER)" => {
// Spaces are inserted into the right-hand side of a functional
// macro during reconstruction from the tokenization. This might
// change in the future, but it is safe by the definition of a
// token in C, whereas leaving the spaces out could change
// tokenization.
assert_eq!(value, &[b"-" as &[u8], b"TESTMACRO_INTEGER"]);
*self.seen_funcs.lock().unwrap() += 1;
}
"TESTMACRO_FUNCTIONAL_EMPTY(TESTMACRO_INTEGER)" => {
assert_eq!(value, &[] as &[&[u8]]);
*self.seen_funcs.lock().unwrap() += 1;
}
"TESTMACRO_FUNCTIONAL_TOKENIZED(a,b,c,d,e)" => {
assert_eq!(
value,
&[b"a" as &[u8], b"/", b"b", b"c", b"d", b"##", b"e"]
);
*self.seen_funcs.lock().unwrap() += 1;
}
"TESTMACRO_FUNCTIONAL_SPLIT(a,b)" => {
assert_eq!(value, &[b"b", b",", b"a"]);
*self.seen_funcs.lock().unwrap() += 1;
}
"TESTMACRO_STRING_FUNC_NON_UTF8(x)" => {
assert_eq!(
value,
&[b"(" as &[u8], b"x", b"\"\xff\xff\"", b")"]
);
*self.seen_funcs.lock().unwrap() += 1;
}
_ => {
// The system might provide lots of functional macros.
// Ensure we did not miss handling one that we meant to handle.
assert!(!name.starts_with("TESTMACRO_"), "name = {}", name);
}
}
}
fn item_name(&self, original_item_name: &str) -> Option<String> {
if original_item_name.starts_with("my_prefixed_") {
Some(
original_item_name
.trim_start_matches("my_prefixed_")
.to_string(),
)
} else if original_item_name.starts_with("MY_PREFIXED_") {
Some(
original_item_name
.trim_start_matches("MY_PREFIXED_")
.to_string(),
)
} else {
None
}
}
// Test the "custom derives" capability by adding `PartialEq` to the `Test` struct.
fn add_derives(&self, info: &DeriveInfo<'_>) -> Vec<String> {
if info.name == "Test" {
vec!["PartialEq".into()]
} else if info.name == "MyOrderedEnum" {
vec!["std::cmp::PartialOrd".into()]
} else {
vec![]
}
}
}
impl Drop for MacroCallback {
fn drop(&mut self) {
assert_eq!(
*self.seen_hellos.lock().unwrap(),
3,
"str_macro handle was not called once for all relevant macros"
);
assert_eq!(
*self.seen_funcs.lock().unwrap(),
5,
"func_macro handle was not called once for all relevant macros"
);
}
}
#[derive(Debug)]
struct WrappedVaListCallback;
impl ParseCallbacks for WrappedVaListCallback {
fn wrap_as_variadic_fn(&self, name: &str) -> Option<String> {
Some(name.to_owned() + "_wrapped")
}
}
fn setup_macro_test() {
cc::Build::new()
.cpp(true)
.file("cpp/Test.cc")
.include("include")
.compile("libtest.a");
let macros = Arc::new(RwLock::new(HashSet::new()));
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let out_rust_file = out_path.join("test.rs");
let out_rust_file_relative = out_rust_file
.strip_prefix(std::env::current_dir().unwrap().parent().unwrap())
.unwrap();
let out_dep_file = out_path.join("test.d");
let bindings = Builder::default()
.formatter(Formatter::None)
.enable_cxx_namespaces()
.default_enum_style(EnumVariation::Rust {
non_exhaustive: false,
})
.raw_line("pub use self::root::*;")
.raw_line("extern { fn my_prefixed_function_to_remove(i: i32); }")
.module_raw_line("root::testing", "pub type Bar = i32;")
.header("cpp/Test.h")
.clang_args(&["-x", "c++", "-std=c++11", "-I", "include"])
.parse_callbacks(Box::new(MacroCallback {
macros: macros.clone(),
seen_hellos: Mutex::new(0),
seen_funcs: Mutex::new(0),
}))
.blocklist_function("my_prefixed_function_to_remove")
.constified_enum("my_prefixed_enum_to_be_constified")
.opaque_type("my_prefixed_templated_foo<my_prefixed_baz>")
.depfile(out_rust_file_relative.display().to_string(), &out_dep_file)
.generate()
.expect("Unable to generate bindings");
assert!(macros.read().unwrap().contains("TESTMACRO"));
bindings
.write_to_file(&out_rust_file)
.expect("Couldn't write bindings!");
let observed_deps =
std::fs::read_to_string(out_dep_file).expect("Couldn't read depfile!");
let expected_deps = format!(
"{}: cpp/Test.h include/stub.h",
out_rust_file_relative.display()
);
assert_eq!(
observed_deps, expected_deps,
"including stub via include dir must produce correct dep path",
);
}
fn setup_wrap_static_fns_test() {
// GH-1090: https://github.com/rust-lang/rust-bindgen/issues/1090
// set output directory under /target so it is easy to clean generated files
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let out_rust_file = out_path.join("extern.rs");
let input_header_dir = PathBuf::from("../bindgen-tests/tests/headers/")
.canonicalize()
.expect("Cannot canonicalize libdir path");
let input_header_file_path = input_header_dir.join("wrap-static-fns.h");
let input_header_file_path_str = input_header_file_path
.to_str()
.expect("Path could not be converted to a str");
// generate external bindings with the external .c and .h files
let bindings = Builder::default()
.header(input_header_file_path_str)
.parse_callbacks(Box::new(CargoCallbacks))
.parse_callbacks(Box::new(WrappedVaListCallback))
.wrap_static_fns(true)
.wrap_static_fns_path(
out_path.join("wrap_static_fns").display().to_string(),
)
.clang_arg("-DUSE_VA_HEADER")
.generate()
.expect("Unable to generate bindings");
println!("cargo:rustc-link-lib=static=wrap_static_fns"); // tell cargo to link libextern
println!("bindings generated: {}", bindings);
let obj_path = out_path.join("wrap_static_fns.o");
let lib_path = out_path.join("libwrap_static_fns.a");
// build the external files to check if they work
let clang_output = std::process::Command::new("clang")
.arg("-c")
.arg("-o")
.arg(&obj_path)
.arg(out_path.join("wrap_static_fns.c"))
.arg("-DUSE_VA_HEADER")
.output()
.expect("`clang` command error");
if !clang_output.status.success() {
panic!(
"Could not compile object file:\n{}",
String::from_utf8_lossy(&clang_output.stderr)
);
}
let ar_output = std::process::Command::new("ar")
.arg("rcs")
.arg(lib_path)
.arg(obj_path)
.output()
.expect("`ar` command error");
if !ar_output.status.success() {
panic!(
"Could not emit library file:\n{}",
String::from_utf8_lossy(&ar_output.stderr)
);
}
bindings
.write_to_file(out_rust_file)
.expect("Could not write bindings to the Rust file");
}
fn main() {
setup_macro_test();
setup_wrap_static_fns_test();
}