Allow supplementing clang include path
MODIFY
README.md
README.md
@@ -185,6 +185,11 @@ The following variables are rarely used, but you might need them under some circ
* cmake
* vcpkg
+* `OPENCV_CLANG_STDLIB_PATH`
+ Path that contains the stdlib headers for parsing with libclang. Should be used only as a workaround for
+ the rare cases where it doesn't get picked up automatically. Should help with issues like
+ [this](https://github.com/twistedfall/opencv-rust/issues/125).
+
The following variables affect the building the of the `opencv` crate, but belong to external components:
* `PKG_CONFIG_PATH`

MODIFY
binding-generator/src/bin/binding-generator.rs
binding-generator/src/bin/binding-generator.rs
@@ -26,6 +26,6 @@ fn main() {
}
let clang = Clang::new().expect("Cannot initialize clang");
let bindings_writer = RustBindingWriter::new(&src_cpp_dir, &out_dir, module, version, debug);
- Generator::new(&opencv_header_dir, &src_cpp_dir, module, &clang)
+ Generator::new(None, &opencv_header_dir, &src_cpp_dir, module, &clang)
.process_file(&module_file, bindings_writer);
}

MODIFY
binding-generator/src/generator.rs
binding-generator/src/generator.rs
@@ -313,10 +313,14 @@ impl<V: GeneratorVisitor> Drop for OpenCVWalker<'_, V> {
}
impl<'m, 'c> Generator<'m, 'c> {
- pub fn new(opencv_include_dir: &Path, src_cpp_dir: &Path, module: &'m str, clang: &'c Clang) -> Self {
+ pub fn new(clang_stdlib_include_dir: Option<&Path>, opencv_include_dir: &Path, src_cpp_dir: &Path, module: &'m str, clang: &'c Clang) -> Self {
let clang_bin = clang_sys::support::Clang::find(None, &[]).expect("Can't find clang binary");
+ let mut clang_include_dirs = clang_bin.cpp_search_paths.unwrap_or_default();
+ if let Some(clang_stdlib_include_dir) = clang_stdlib_include_dir {
+ clang_include_dirs.push(canonicalize(clang_stdlib_include_dir.to_path_buf()).expect("Cannot canonicalize clang_stdlib_include_dir"))
+ }
Self {
- clang_include_dirs: clang_bin.cpp_search_paths.unwrap_or_default(),
+ clang_include_dirs,
opencv_include_dir: canonicalize(opencv_include_dir).expect("Can't canonicalize opencv_include_dir"),
src_cpp_dir: canonicalize(src_cpp_dir).expect("Can't canonicalize src_cpp_dir"),
module,

MODIFY
build.rs
build.rs
@@ -18,6 +18,7 @@ use shlex::Shlex;
#[cfg(feature = "buildtime-bindgen")]
mod generator {
use std::{
+ env,
ffi::OsStr,
fs::{self, DirEntry, File, OpenOptions},
io::{self, BufRead, BufReader, Write},
@@ -112,7 +113,9 @@ mod generator {
version,
false,
);
- let gen = binding_generator::Generator::new(&opencv_header_dir, &*SRC_CPP_DIR, module, &clang);
+ let clang_stdlib_include_dir = env::var_os("OPENCV_CLANG_STDLIB_PATH")
+ .map(|p| PathBuf::from(p));
+ let gen = binding_generator::Generator::new(clang_stdlib_include_dir.as_deref(), &opencv_header_dir, &*SRC_CPP_DIR, module, &clang);
if !shown_args.compare_and_swap(false, true, Ordering::Relaxed) {
eprintln!("=== Clang command line args: {:#?}", gen.build_clang_command_line_args());
}
@@ -296,7 +299,7 @@ static MANIFEST_DIR: Lazy<PathBuf> = Lazy::new(|| PathBuf::from(env::var_os("CAR
static SRC_DIR: Lazy<PathBuf> = Lazy::new(|| MANIFEST_DIR.join("src"));
static SRC_CPP_DIR: Lazy<PathBuf> = Lazy::new(|| MANIFEST_DIR.join("src_cpp"));
-static ENV_VARS: [&str; 14] = [
+static ENV_VARS: [&str; 15] = [
"OPENCV_HEADER_DIR",
"OPENCV_PACKAGE_NAME",
"OPENCV_PKGCONFIG_NAME",
@@ -307,6 +310,7 @@ static ENV_VARS: [&str; 14] = [
"OPENCV_LINK_PATHS",
"OPENCV_INCLUDE_PATHS",
"OPENCV_DISABLE_PROBES",
+ "OPENCV_CLANG_STDLIB_PATH",
"CMAKE_PREFIX_PATH",
"OpenCV_DIR",
"PKG_CONFIG_PATH",

Allow supplementing clang include path