Skip to content

Commit eb53d8c

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

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-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

+41-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,10 @@ 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[..self.options.input_headers.len() - 1] {
609608
output_vector.push("-include".to_string());
610609
output_vector.push(header.clone());
611610
}
@@ -637,7 +636,7 @@ impl Builder {
637636
/// .unwrap();
638637
/// ```
639638
pub fn header<T: Into<String>>(mut self, header: T) -> Builder {
640-
self.input_headers.push(header.into());
639+
self.options.input_headers.push(header.into());
641640
self
642641
}
643642

@@ -1533,13 +1532,13 @@ impl Builder {
15331532
/// Generate the Rust bindings using the options built up thus far.
15341533
pub fn generate(mut self) -> Result<Bindings, BindgenError> {
15351534
// Add any extra arguments from the environment to the clang command line.
1536-
self.options.clang_args.extend(get_extra_clang_args());
1535+
self.state.clang_args.extend(get_extra_clang_args());
15371536

15381537
// 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
1538+
self.state.input_header = self.options.input_headers.pop();
1539+
self.state.extra_input_headers = std::mem::take(&mut self.options.input_headers);
1540+
self.state.clang_args.extend(
1541+
self.state
15431542
.extra_input_headers
15441543
.iter()
15451544
.flat_map(|header| ["-include".into(), header.to_string()]),
@@ -1575,10 +1574,10 @@ impl Builder {
15751574
let mut wrapper_contents = String::new();
15761575

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

15801579
// For each input header, add `#include "$header"`.
1581-
for header in &self.input_headers {
1580+
for header in &self.options.input_headers {
15821581
is_cpp |= file_is_cpp(header);
15831582

15841583
wrapper_contents.push_str("#include \"");
@@ -1616,7 +1615,7 @@ impl Builder {
16161615
.arg(&wrapper_path)
16171616
.stdout(Stdio::piped());
16181617

1619-
for a in &self.options.clang_args {
1618+
for a in &self.state.clang_args {
16201619
cmd.arg(a);
16211620
}
16221621

@@ -1938,11 +1937,8 @@ struct BindgenOptions {
19381937
/// The set of arguments to pass straight through to Clang.
19391938
clang_args: Vec<String>,
19401939

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

19471943
/// Which kind of items should we generate? By default, we'll generate all
19481944
/// of them.
@@ -2125,8 +2121,7 @@ impl Default for BindgenOptions {
21252121
raw_lines: Default::default(),
21262122
module_lines: Default::default(),
21272123
clang_args: Default::default(),
2128-
input_header: Default::default(),
2129-
extra_input_headers: Default::default(),
2124+
input_headers: Default::default(),
21302125
codegen_config: CodegenConfig::all(),
21312126
conservative_inline_namespaces: Default::default(),
21322127
generate_comments: true,
@@ -2251,6 +2246,15 @@ struct BindgenState {
22512246
/// The set of types that we should not derive `PartialEq` for.
22522247
no_partialeq_types: RegexSet,
22532248

2249+
/// The set of arguments to pass straight through to Clang.
2250+
clang_args: Vec<String>,
2251+
2252+
/// The input header file.
2253+
input_header: Option<String>,
2254+
2255+
/// Any additional input header files.
2256+
extra_input_headers: Vec<String>,
2257+
22542258
/// The set of types that we should not derive `Copy` for.
22552259
no_copy_types: RegexSet,
22562260

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

22822286
impl BindgenState {
22832287
fn build(&mut self, options: &BindgenOptions) {
2288+
self.clang_args.extend_from_slice(&options.clang_args);
2289+
22842290
let mut regex_sets = [
22852291
(&mut self.allowlisted_vars, &options.allowlisted_vars),
22862292
(&mut self.allowlisted_types, &options.allowlisted_types),
@@ -2448,7 +2454,7 @@ impl Bindings {
24482454
/// Generate bindings for the given options.
24492455
pub(crate) fn generate(
24502456
mut state: BindgenState,
2451-
mut options: BindgenOptions,
2457+
options: BindgenOptions,
24522458
) -> Result<Self, BindgenError> {
24532459
ensure_libclang_is_loaded();
24542460

@@ -2463,7 +2469,7 @@ impl Bindings {
24632469
state.build(&options);
24642470

24652471
let (effective_target, explicit_target) =
2466-
find_effective_target(&options.clang_args);
2472+
find_effective_target(&state.clang_args);
24672473

24682474
let is_host_build =
24692475
rust_to_clang_target(HOST_TARGET) == effective_target;
@@ -2474,12 +2480,12 @@ impl Bindings {
24742480
// opening libclang.so, it has to be the same architecture and thus the
24752481
// check is fine.
24762482
if !explicit_target && !is_host_build {
2477-
options
2483+
state
24782484
.clang_args
24792485
.insert(0, format!("--target={}", effective_target));
24802486
};
24812487

2482-
fn detect_include_paths(options: &mut BindgenOptions) {
2488+
fn detect_include_paths(options: &BindgenOptions, state: &mut BindgenState) {
24832489
if !options.detect_include_paths {
24842490
return;
24852491
}
@@ -2488,7 +2494,7 @@ impl Bindings {
24882494
// promote them to `-isystem`.
24892495
let clang_args_for_clang_sys = {
24902496
let mut last_was_include_prefix = false;
2491-
options
2497+
state
24922498
.clang_args
24932499
.iter()
24942500
.filter(|arg| {
@@ -2534,8 +2540,8 @@ impl Bindings {
25342540
debug!("Found clang: {:?}", clang);
25352541

25362542
// 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);
2543+
let is_cpp = args_are_cpp(&state.clang_args) ||
2544+
state.input_header.as_deref().map_or(false, file_is_cpp);
25392545

25402546
let search_paths = if is_cpp {
25412547
clang.cpp_search_paths
@@ -2546,14 +2552,14 @@ impl Bindings {
25462552
if let Some(search_paths) = search_paths {
25472553
for path in search_paths.into_iter() {
25482554
if let Ok(path) = path.into_os_string().into_string() {
2549-
options.clang_args.push("-isystem".to_owned());
2550-
options.clang_args.push(path);
2555+
state.clang_args.push("-isystem".to_owned());
2556+
state.clang_args.push(path);
25512557
}
25522558
}
25532559
}
25542560
}
25552561

2556-
detect_include_paths(&mut options);
2562+
detect_include_paths(&options, &mut state);
25572563

25582564
#[cfg(unix)]
25592565
fn can_read(perms: &std::fs::Permissions) -> bool {
@@ -2566,7 +2572,7 @@ impl Bindings {
25662572
true
25672573
}
25682574

2569-
if let Some(h) = options.input_header.as_ref() {
2575+
if let Some(h) = state.input_header.as_ref() {
25702576
let path = Path::new(h);
25712577
if let Ok(md) = std::fs::metadata(path) {
25722578
if md.is_dir() {
@@ -2577,17 +2583,17 @@ impl Bindings {
25772583
path.into(),
25782584
));
25792585
}
2580-
options.clang_args.push(h.clone())
2586+
state.clang_args.push(h.clone())
25812587
} else {
25822588
return Err(BindgenError::NotExist(path.into()));
25832589
}
25842590
}
25852591

25862592
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());
2593+
if idx != 0 || state.input_header.is_some() {
2594+
state.clang_args.push("-include".to_owned());
25892595
}
2590-
options.clang_args.push(f.name.to_str().unwrap().to_owned())
2596+
state.clang_args.push(f.name.to_str().unwrap().to_owned())
25912597
}
25922598

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

0 commit comments

Comments
 (0)