Skip to content

Commit 25e1030

Browse files
committed
Issue 1723
1 parent 669dc1b commit 25e1030

File tree

1 file changed

+47
-39
lines changed

1 file changed

+47
-39
lines changed

src/lib.rs

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,8 +1335,8 @@ impl Builder {
13351335
self
13361336
}
13371337

1338-
/// Generate the Rust bindings using the options built up thus far.
1339-
pub fn generate(mut self) -> Result<Bindings, ()> {
1338+
/// Generate the Rust bindings options
1339+
pub fn generate_options(mut self) -> Self {
13401340
// Add any extra arguments from the environment to the clang command line.
13411341
if let Some(extra_clang_args) =
13421342
env::var("BINDGEN_EXTRA_CLANG_ARGS").ok()
@@ -1364,24 +1364,28 @@ impl Builder {
13641364
clang::UnsavedFile::new(&name, &contents)
13651365
}),
13661366
);
1367+
self
1368+
}
13671369

1368-
Bindings::generate(self.options)
1370+
/// Generate the Rust bindings using the options built up thus far.
1371+
pub fn generate(self) -> Result<Bindings, ()> {
1372+
Bindings::generate(Builder::generate_options(self).options)
13691373
}
13701374

13711375
/// Preprocess and dump the input header files to disk.
13721376
///
13731377
/// This is useful when debugging bindgen, using C-Reduce, or when filing
13741378
/// issues. The resulting file will be named something like `__bindgen.i` or
13751379
/// `__bindgen.ii`
1376-
pub fn dump_preprocessed_input(&self) -> io::Result<()> {
1380+
pub fn dump_preprocessed_input(&mut self) -> io::Result<()> {
13771381
let clang =
13781382
clang_sys::support::Clang::find(None, &[]).ok_or_else(|| {
13791383
io::Error::new(
13801384
io::ErrorKind::Other,
13811385
"Cannot find clang executable",
13821386
)
13831387
})?;
1384-
1388+
// Builder::generate_options(self);
13851389
// The contents of a wrapper file that includes all the input header
13861390
// files.
13871391
let mut wrapper_contents = String::new();
@@ -2011,39 +2015,8 @@ fn find_effective_target(clang_args: &[String]) -> (String, bool) {
20112015
}
20122016

20132017
impl Bindings {
2014-
/// Generate bindings for the given options.
2015-
pub(crate) fn generate(
2016-
mut options: BindgenOptions,
2017-
) -> Result<Bindings, ()> {
2018-
ensure_libclang_is_loaded();
2019-
2020-
#[cfg(feature = "runtime")]
2021-
debug!(
2022-
"Generating bindings, libclang at {}",
2023-
clang_sys::get_library().unwrap().path().display()
2024-
);
2025-
#[cfg(not(feature = "runtime"))]
2026-
debug!("Generating bindings, libclang linked");
2027-
2028-
options.build();
2029-
2030-
let (effective_target, explicit_target) =
2031-
find_effective_target(&options.clang_args);
2032-
2033-
let is_host_build =
2034-
rust_to_clang_target(HOST_TARGET) == effective_target;
2035-
2036-
// NOTE: The is_host_build check wouldn't be sound normally in some
2037-
// cases if we were to call a binary (if you have a 32-bit clang and are
2038-
// building on a 64-bit system for example). But since we rely on
2039-
// opening libclang.so, it has to be the same architecture and thus the
2040-
// check is fine.
2041-
if !explicit_target && !is_host_build {
2042-
options
2043-
.clang_args
2044-
.insert(0, format!("--target={}", effective_target));
2045-
};
2046-
2018+
/// Common part of generate()
2019+
fn generate_common(options: &mut BindgenOptions) {
20472020
fn detect_include_paths(options: &mut BindgenOptions) {
20482021
if !options.detect_include_paths {
20492022
return;
@@ -2120,8 +2093,43 @@ impl Bindings {
21202093
}
21212094
}
21222095
}
2096+
detect_include_paths(options);
2097+
}
2098+
2099+
/// Generate bindings for the given options.
2100+
pub(crate) fn generate(
2101+
mut options: BindgenOptions,
2102+
) -> Result<Bindings, ()> {
2103+
ensure_libclang_is_loaded();
2104+
2105+
#[cfg(feature = "runtime")]
2106+
debug!(
2107+
"Generating bindings, libclang at {}",
2108+
clang_sys::get_library().unwrap().path().display()
2109+
);
2110+
#[cfg(not(feature = "runtime"))]
2111+
debug!("Generating bindings, libclang linked");
2112+
2113+
options.build();
2114+
2115+
let (effective_target, explicit_target) =
2116+
find_effective_target(&options.clang_args);
2117+
2118+
let is_host_build =
2119+
rust_to_clang_target(HOST_TARGET) == effective_target;
2120+
2121+
// NOTE: The is_host_build check wouldn't be sound normally in some
2122+
// cases if we were to call a binary (if you have a 32-bit clang and are
2123+
// building on a 64-bit system for example). But since we rely on
2124+
// opening libclang.so, it has to be the same architecture and thus the
2125+
// check is fine.
2126+
if !explicit_target && !is_host_build {
2127+
options
2128+
.clang_args
2129+
.insert(0, format!("--target={}", effective_target));
2130+
};
21232131

2124-
detect_include_paths(&mut options);
2132+
Bindings::generate_common(&mut options);
21252133

21262134
#[cfg(unix)]
21272135
fn can_read(perms: &std::fs::Permissions) -> bool {

0 commit comments

Comments
 (0)