Skip to content

Commit a7574b3

Browse files
committed
BINDGEN_EXTRA_CLANG_ARGS for command line mode issue-1723
1 parent 0f64106 commit a7574b3

File tree

1 file changed

+48
-38
lines changed

1 file changed

+48
-38
lines changed

src/lib.rs

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,8 +1435,8 @@ impl Builder {
14351435
self
14361436
}
14371437

1438-
/// Generate the Rust bindings using the options built up thus far.
1439-
pub fn generate(mut self) -> Result<Bindings, ()> {
1438+
/// Generate the Rust bindings options
1439+
pub fn generate_options(&mut self) -> &Self {
14401440
// Add any extra arguments from the environment to the clang command line.
14411441
if let Some(extra_clang_args) =
14421442
get_target_dependent_env_var("BINDGEN_EXTRA_CLANG_ARGS")
@@ -1451,7 +1451,7 @@ impl Builder {
14511451

14521452
// Transform input headers to arguments on the clang command line.
14531453
self.options.input_header = self.input_headers.pop();
1454-
self.options.extra_input_headers = self.input_headers;
1454+
self.options.extra_input_headers = self.input_headers.to_vec();
14551455
self.options.clang_args.extend(
14561456
self.options.extra_input_headers.iter().flat_map(|header| {
14571457
iter::once("-include".into())
@@ -1466,7 +1466,12 @@ impl Builder {
14661466
clang::UnsavedFile::new(&name, &contents)
14671467
}),
14681468
);
1469+
self
1470+
}
14691471

1472+
/// Generate the Rust bindings using the options built up thus far.
1473+
pub fn generate(mut self) -> Result<Bindings, ()> {
1474+
self.generate_options();
14701475
Bindings::generate(self.options)
14711476
}
14721477

@@ -1475,7 +1480,7 @@ impl Builder {
14751480
/// This is useful when debugging bindgen, using C-Reduce, or when filing
14761481
/// issues. The resulting file will be named something like `__bindgen.i` or
14771482
/// `__bindgen.ii`
1478-
pub fn dump_preprocessed_input(&self) -> io::Result<()> {
1483+
pub fn dump_preprocessed_input(&mut self) -> io::Result<()> {
14791484
let clang =
14801485
clang_sys::support::Clang::find(None, &[]).ok_or_else(|| {
14811486
io::Error::new(
@@ -1543,6 +1548,7 @@ impl Builder {
15431548
"__bindgen.i"
15441549
})?;
15451550
io::copy(&mut preprocessed, &mut file)?;
1551+
self.generate_options();
15461552

15471553
if child.wait()?.success() {
15481554
Ok(())
@@ -2189,39 +2195,8 @@ fn find_effective_target(clang_args: &[String]) -> (String, bool) {
21892195
}
21902196

21912197
impl Bindings {
2192-
/// Generate bindings for the given options.
2193-
pub(crate) fn generate(
2194-
mut options: BindgenOptions,
2195-
) -> Result<Bindings, ()> {
2196-
ensure_libclang_is_loaded();
2197-
2198-
#[cfg(feature = "runtime")]
2199-
debug!(
2200-
"Generating bindings, libclang at {}",
2201-
clang_sys::get_library().unwrap().path().display()
2202-
);
2203-
#[cfg(not(feature = "runtime"))]
2204-
debug!("Generating bindings, libclang linked");
2205-
2206-
options.build();
2207-
2208-
let (effective_target, explicit_target) =
2209-
find_effective_target(&options.clang_args);
2210-
2211-
let is_host_build =
2212-
rust_to_clang_target(HOST_TARGET) == effective_target;
2213-
2214-
// NOTE: The is_host_build check wouldn't be sound normally in some
2215-
// cases if we were to call a binary (if you have a 32-bit clang and are
2216-
// building on a 64-bit system for example). But since we rely on
2217-
// opening libclang.so, it has to be the same architecture and thus the
2218-
// check is fine.
2219-
if !explicit_target && !is_host_build {
2220-
options
2221-
.clang_args
2222-
.insert(0, format!("--target={}", effective_target));
2223-
};
2224-
2198+
/// Common part of generate()
2199+
fn generate_common(options: &mut BindgenOptions) {
22252200
fn detect_include_paths(options: &mut BindgenOptions) {
22262201
if !options.detect_include_paths {
22272202
return;
@@ -2298,8 +2273,43 @@ impl Bindings {
22982273
}
22992274
}
23002275
}
2276+
detect_include_paths(options);
2277+
}
2278+
2279+
/// Generate bindings for the given options.
2280+
pub(crate) fn generate(
2281+
mut options: BindgenOptions,
2282+
) -> Result<Bindings, ()> {
2283+
ensure_libclang_is_loaded();
2284+
2285+
#[cfg(feature = "runtime")]
2286+
debug!(
2287+
"Generating bindings, libclang at {}",
2288+
clang_sys::get_library().unwrap().path().display()
2289+
);
2290+
#[cfg(not(feature = "runtime"))]
2291+
debug!("Generating bindings, libclang linked");
2292+
2293+
options.build();
2294+
2295+
let (effective_target, explicit_target) =
2296+
find_effective_target(&options.clang_args);
2297+
2298+
let is_host_build =
2299+
rust_to_clang_target(HOST_TARGET) == effective_target;
2300+
2301+
// NOTE: The is_host_build check wouldn't be sound normally in some
2302+
// cases if we were to call a binary (if you have a 32-bit clang and are
2303+
// building on a 64-bit system for example). But since we rely on
2304+
// opening libclang.so, it has to be the same architecture and thus the
2305+
// check is fine.
2306+
if !explicit_target && !is_host_build {
2307+
options
2308+
.clang_args
2309+
.insert(0, format!("--target={}", effective_target));
2310+
};
23012311

2302-
detect_include_paths(&mut options);
2312+
Bindings::generate_common(&mut options);
23032313

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

0 commit comments

Comments
 (0)