Skip to content

Commit ddc570d

Browse files
committed
move input headers and clang args fields to options and state
1 parent c791cc3 commit ddc570d

File tree

2 files changed

+50
-38
lines changed

2 files changed

+50
-38
lines changed

src/ir/context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ impl BindgenContext {
523523
clang::TranslationUnit::parse(
524524
&index,
525525
"",
526-
&options.clang_args,
526+
&state.clang_args,
527527
&state.input_unsaved_files,
528528
parse_options,
529529
).expect("libclang error; possible causes include:
@@ -541,10 +541,10 @@ If you encounter an error missing from this list, please file an issue or a PR!"
541541

542542
// depfiles need to include the explicitly listed headers too
543543
let mut deps = BTreeSet::default();
544-
if let Some(filename) = &options.input_header {
544+
if let Some(filename) = &state.input_header {
545545
deps.insert(filename.clone());
546546
}
547-
deps.extend(options.extra_input_headers.iter().cloned());
547+
deps.extend(state.extra_input_headers.iter().cloned());
548548

549549
BindgenContext {
550550
items: vec![Some(root_module)],

src/lib.rs

+47-35
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ impl Default for CodegenConfig {
225225
pub struct Builder {
226226
state: BindgenState,
227227
options: BindgenOptions,
228-
input_headers: Vec<String>,
229228
// Tuples of unsaved file contents of the form (name, contents).
230229
input_header_contents: Vec<(String, String)>,
231230
}
@@ -254,7 +253,7 @@ impl Builder {
254253
pub fn command_line_flags(&self) -> Vec<String> {
255254
let mut output_vector: Vec<String> = Vec::new();
256255

257-
if let Some(header) = self.input_headers.last().cloned() {
256+
if let Some(header) = self.options.input_headers.last().cloned() {
258257
// Positional argument 'header'
259258
output_vector.push(header);
260259
}
@@ -602,10 +601,12 @@ impl Builder {
602601
output_vector.extend(self.options.clang_args.iter().cloned());
603602
}
604603

605-
if self.input_headers.len() > 1 {
604+
if self.options.input_headers.len() > 1 {
606605
// To pass more than one header, we need to pass all but the last
607606
// header via the `-include` clang arg
608-
for header in &self.input_headers[..self.input_headers.len() - 1] {
607+
for header in &self.options.input_headers
608+
[..self.options.input_headers.len() - 1]
609+
{
609610
output_vector.push("-include".to_string());
610611
output_vector.push(header.clone());
611612
}
@@ -637,7 +638,7 @@ impl Builder {
637638
/// .unwrap();
638639
/// ```
639640
pub fn header<T: Into<String>>(mut self, header: T) -> Builder {
640-
self.input_headers.push(header.into());
641+
self.options.input_headers.push(header.into());
641642
self
642643
}
643644

@@ -1533,13 +1534,14 @@ impl Builder {
15331534
/// Generate the Rust bindings using the options built up thus far.
15341535
pub fn generate(mut self) -> Result<Bindings, BindgenError> {
15351536
// Add any extra arguments from the environment to the clang command line.
1536-
self.options.clang_args.extend(get_extra_clang_args());
1537+
self.state.clang_args.extend(get_extra_clang_args());
15371538

15381539
// Transform input headers to arguments on the clang command line.
1539-
self.options.input_header = self.input_headers.pop();
1540-
self.options.extra_input_headers = self.input_headers;
1541-
self.options.clang_args.extend(
1542-
self.options
1540+
self.state.input_header = self.options.input_headers.pop();
1541+
self.state.extra_input_headers =
1542+
std::mem::take(&mut self.options.input_headers);
1543+
self.state.clang_args.extend(
1544+
self.state
15431545
.extra_input_headers
15441546
.iter()
15451547
.flat_map(|header| ["-include".into(), header.to_string()]),
@@ -1575,10 +1577,10 @@ impl Builder {
15751577
let mut wrapper_contents = String::new();
15761578

15771579
// Whether we are working with C or C++ inputs.
1578-
let mut is_cpp = args_are_cpp(&self.options.clang_args);
1580+
let mut is_cpp = args_are_cpp(&self.state.clang_args);
15791581

15801582
// For each input header, add `#include "$header"`.
1581-
for header in &self.input_headers {
1583+
for header in &self.options.input_headers {
15821584
is_cpp |= file_is_cpp(header);
15831585

15841586
wrapper_contents.push_str("#include \"");
@@ -1616,7 +1618,7 @@ impl Builder {
16161618
.arg(&wrapper_path)
16171619
.stdout(Stdio::piped());
16181620

1619-
for a in &self.options.clang_args {
1621+
for a in &self.state.clang_args {
16201622
cmd.arg(a);
16211623
}
16221624

@@ -1938,11 +1940,8 @@ struct BindgenOptions {
19381940
/// The set of arguments to pass straight through to Clang.
19391941
clang_args: Vec<String>,
19401942

1941-
/// The input header file.
1942-
input_header: Option<String>,
1943-
1944-
/// Any additional input header files.
1945-
extra_input_headers: Vec<String>,
1943+
/// The input header files.
1944+
input_headers: Vec<String>,
19461945

19471946
/// Which kind of items should we generate? By default, we'll generate all
19481947
/// of them.
@@ -2125,8 +2124,7 @@ impl Default for BindgenOptions {
21252124
raw_lines: Default::default(),
21262125
module_lines: Default::default(),
21272126
clang_args: Default::default(),
2128-
input_header: Default::default(),
2129-
extra_input_headers: Default::default(),
2127+
input_headers: Default::default(),
21302128
codegen_config: CodegenConfig::all(),
21312129
conservative_inline_namespaces: Default::default(),
21322130
generate_comments: true,
@@ -2251,6 +2249,15 @@ struct BindgenState {
22512249
/// The set of types that we should not derive `PartialEq` for.
22522250
no_partialeq_types: RegexSet,
22532251

2252+
/// The set of arguments to pass straight through to Clang.
2253+
clang_args: Vec<String>,
2254+
2255+
/// The input header file.
2256+
input_header: Option<String>,
2257+
2258+
/// Any additional input header files.
2259+
extra_input_headers: Vec<String>,
2260+
22542261
/// The set of types that we should not derive `Copy` for.
22552262
no_copy_types: RegexSet,
22562263

@@ -2281,6 +2288,8 @@ impl ::std::panic::UnwindSafe for BindgenState {}
22812288

22822289
impl BindgenState {
22832290
fn build(&mut self, options: &BindgenOptions) {
2291+
self.clang_args.extend_from_slice(&options.clang_args);
2292+
22842293
let mut regex_sets = [
22852294
(&mut self.allowlisted_vars, &options.allowlisted_vars),
22862295
(&mut self.allowlisted_types, &options.allowlisted_types),
@@ -2448,7 +2457,7 @@ impl Bindings {
24482457
/// Generate bindings for the given options.
24492458
pub(crate) fn generate(
24502459
mut state: BindgenState,
2451-
mut options: BindgenOptions,
2460+
options: BindgenOptions,
24522461
) -> Result<Self, BindgenError> {
24532462
ensure_libclang_is_loaded();
24542463

@@ -2463,7 +2472,7 @@ impl Bindings {
24632472
state.build(&options);
24642473

24652474
let (effective_target, explicit_target) =
2466-
find_effective_target(&options.clang_args);
2475+
find_effective_target(&state.clang_args);
24672476

24682477
let is_host_build =
24692478
rust_to_clang_target(HOST_TARGET) == effective_target;
@@ -2474,12 +2483,15 @@ impl Bindings {
24742483
// opening libclang.so, it has to be the same architecture and thus the
24752484
// check is fine.
24762485
if !explicit_target && !is_host_build {
2477-
options
2486+
state
24782487
.clang_args
24792488
.insert(0, format!("--target={}", effective_target));
24802489
};
24812490

2482-
fn detect_include_paths(options: &mut BindgenOptions) {
2491+
fn detect_include_paths(
2492+
options: &BindgenOptions,
2493+
state: &mut BindgenState,
2494+
) {
24832495
if !options.detect_include_paths {
24842496
return;
24852497
}
@@ -2488,7 +2500,7 @@ impl Bindings {
24882500
// promote them to `-isystem`.
24892501
let clang_args_for_clang_sys = {
24902502
let mut last_was_include_prefix = false;
2491-
options
2503+
state
24922504
.clang_args
24932505
.iter()
24942506
.filter(|arg| {
@@ -2534,8 +2546,8 @@ impl Bindings {
25342546
debug!("Found clang: {:?}", clang);
25352547

25362548
// Whether we are working with C or C++ inputs.
2537-
let is_cpp = args_are_cpp(&options.clang_args) ||
2538-
options.input_header.as_deref().map_or(false, file_is_cpp);
2549+
let is_cpp = args_are_cpp(&state.clang_args) ||
2550+
state.input_header.as_deref().map_or(false, file_is_cpp);
25392551

25402552
let search_paths = if is_cpp {
25412553
clang.cpp_search_paths
@@ -2546,14 +2558,14 @@ impl Bindings {
25462558
if let Some(search_paths) = search_paths {
25472559
for path in search_paths.into_iter() {
25482560
if let Ok(path) = path.into_os_string().into_string() {
2549-
options.clang_args.push("-isystem".to_owned());
2550-
options.clang_args.push(path);
2561+
state.clang_args.push("-isystem".to_owned());
2562+
state.clang_args.push(path);
25512563
}
25522564
}
25532565
}
25542566
}
25552567

2556-
detect_include_paths(&mut options);
2568+
detect_include_paths(&options, &mut state);
25572569

25582570
#[cfg(unix)]
25592571
fn can_read(perms: &std::fs::Permissions) -> bool {
@@ -2566,7 +2578,7 @@ impl Bindings {
25662578
true
25672579
}
25682580

2569-
if let Some(h) = options.input_header.as_ref() {
2581+
if let Some(h) = state.input_header.as_ref() {
25702582
let path = Path::new(h);
25712583
if let Ok(md) = std::fs::metadata(path) {
25722584
if md.is_dir() {
@@ -2577,17 +2589,17 @@ impl Bindings {
25772589
path.into(),
25782590
));
25792591
}
2580-
options.clang_args.push(h.clone())
2592+
state.clang_args.push(h.clone())
25812593
} else {
25822594
return Err(BindgenError::NotExist(path.into()));
25832595
}
25842596
}
25852597

25862598
for (idx, f) in state.input_unsaved_files.iter().enumerate() {
2587-
if idx != 0 || options.input_header.is_some() {
2588-
options.clang_args.push("-include".to_owned());
2599+
if idx != 0 || state.input_header.is_some() {
2600+
state.clang_args.push("-include".to_owned());
25892601
}
2590-
options.clang_args.push(f.name.to_str().unwrap().to_owned())
2602+
state.clang_args.push(f.name.to_str().unwrap().to_owned())
25912603
}
25922604

25932605
debug!("Fixed-up options: {:?}", options);

0 commit comments

Comments
 (0)