opencv-rust icon indicating copy to clipboard operation
opencv-rust copied to clipboard

Support for Minimal OpenCV Builds (e.g., opencv-mobile) with Selective Module Binding

Open topcoco opened this issue 6 months ago • 1 comments

I'm using a minimal build of OpenCV (opencv-mobile : https://github.com/nihui/opencv-mobile ) which intentionally excludes certain modules (like CUDA) to reduce binary size. However, when using rust-opencv, the build system attempts to generate bindings for all OpenCV modules, even when the corresponding headers/files are missing from my installation.

​​Problem Example:​​ My opencv-mobile build deliberately removes opencv2/core/cuda.hpp since I don't need CUDA functionality. Despite specifying only the modules I need in Cargo.toml:

opencv = { version = "0.94.4", features = ["imgproc"], default-features = false }

The binding generator still tries to parse CUDA headers and fails with:

fatal error: 'opencv2/core/cuda.hpp' file not found

​​Request:​​ Could rust-opencv be modified to:

Respect missing headers in minimal OpenCV builds without failing?
Allow truly selective module binding generation?
Skip dependencies on optional components (like CUDA) when they're unavailable?

This would better support minimal OpenCV distributions where users explicitly want to exclude certain components.

​​Build Log Excerpt:​​

error: failed to run custom build command for opencv v0.94.4

Caused by: process didn't exit successfully: /home/coco/RustroverProjects/untitled5/target/debug/build/opencv-e33045fc197b86be/build-script-build (exit status: 101) --- stdout cargo::rustc-check-cfg=cfg(ocvrs_opencv_branch_34) cargo::rustc-check-cfg=cfg(ocvrs_opencv_branch_4) cargo::rustc-check-cfg=cfg(ocvrs_opencv_branch_5) cargo::rustc-check-cfg=cfg(ocvrs_has_module_3d) cargo::rustc-check-cfg=cfg(ocvrs_has_module_alphamat) cargo::rustc-check-cfg=cfg(ocvrs_has_module_aruco) cargo::rustc-check-cfg=cfg(ocvrs_has_module_aruco_detector) cargo::rustc-check-cfg=cfg(ocvrs_has_module_barcode) cargo::rustc-check-cfg=cfg(ocvrs_has_module_bgsegm) cargo::rustc-check-cfg=cfg(ocvrs_has_module_bioinspired) cargo::rustc-check-cfg=cfg(ocvrs_has_module_calib) cargo::rustc-check-cfg=cfg(ocvrs_has_module_calib3d) cargo::rustc-check-cfg=cfg(ocvrs_has_module_ccalib) cargo::rustc-check-cfg=cfg(ocvrs_has_module_core) cargo::rustc-check-cfg=cfg(ocvrs_has_module_cudaarithm) cargo::rustc-check-cfg=cfg(ocvrs_has_module_cudabgsegm) cargo::rustc-check-cfg=cfg(ocvrs_has_module_cudacodec) cargo::rustc-check-cfg=cfg(ocvrs_has_module_cudafeatures2d) cargo::rustc-check-cfg=cfg(ocvrs_has_module_cudafilters) cargo::rustc-check-cfg=cfg(ocvrs_has_module_cudaimgproc) cargo::rustc-check-cfg=cfg(ocvrs_has_module_cudalegacy) cargo::rustc-check-cfg=cfg(ocvrs_has_module_cudaobjdetect) cargo::rustc-check-cfg=cfg(ocvrs_has_module_cudaoptflow) cargo::rustc-check-cfg=cfg(ocvrs_has_module_cudastereo) cargo::rustc-check-cfg=cfg(ocvrs_has_module_cudawarping) cargo::rustc-check-cfg=cfg(ocvrs_has_module_cvv) cargo::rustc-check-cfg=cfg(ocvrs_has_module_dnn) cargo::rustc-check-cfg=cfg(ocvrs_has_module_dnn_superres) cargo::rustc-check-cfg=cfg(ocvrs_has_module_dpm) cargo::rustc-check-cfg=cfg(ocvrs_has_module_face) cargo::rustc-check-cfg=cfg(ocvrs_has_module_features) cargo::rustc-check-cfg=cfg(ocvrs_has_module_features2d) cargo::rustc-check-cfg=cfg(ocvrs_has_module_flann) cargo::rustc-check-cfg=cfg(ocvrs_has_module_freetype) cargo::rustc-check-cfg=cfg(ocvrs_has_module_fuzzy) cargo::rustc-check-cfg=cfg(ocvrs_has_module_gapi) cargo::rustc-check-cfg=cfg(ocvrs_has_module_hdf) cargo::rustc-check-cfg=cfg(ocvrs_has_module_hfs) cargo::rustc-check-cfg=cfg(ocvrs_has_module_highgui) cargo::rustc-check-cfg=cfg(ocvrs_has_module_img_hash) cargo::rustc-check-cfg=cfg(ocvrs_has_module_imgcodecs) cargo::rustc-check-cfg=cfg(ocvrs_has_module_imgproc) cargo::rustc-check-cfg=cfg(ocvrs_has_module_intensity_transform) cargo::rustc-check-cfg=cfg(ocvrs_has_module_line_descriptor) cargo::rustc-check-cfg=cfg(ocvrs_has_module_mcc) cargo::rustc-check-cfg=cfg(ocvrs_has_module_ml) cargo::rustc-check-cfg=cfg(ocvrs_has_module_objdetect) cargo::rustc-check-cfg=cfg(ocvrs_has_module_optflow) cargo::rustc-check-cfg=cfg(ocvrs_has_module_ovis) cargo::rustc-check-cfg=cfg(ocvrs_has_module_phase_unwrapping) cargo::rustc-check-cfg=cfg(ocvrs_has_module_photo) cargo::rustc-check-cfg=cfg(ocvrs_has_module_plot) cargo::rustc-check-cfg=cfg(ocvrs_has_module_quality) cargo::rustc-check-cfg=cfg(ocvrs_has_module_rapid) cargo::rustc-check-cfg=cfg(ocvrs_has_module_rgbd) cargo::rustc-check-cfg=cfg(ocvrs_has_module_saliency) cargo::rustc-check-cfg=cfg(ocvrs_has_module_sfm) cargo::rustc-check-cfg=cfg(ocvrs_has_module_shape) cargo::rustc-check-cfg=cfg(ocvrs_has_module_signal) cargo::rustc-check-cfg=cfg(ocvrs_has_module_stereo) cargo::rustc-check-cfg=cfg(ocvrs_has_module_stitching) cargo::rustc-check-cfg=cfg(ocvrs_has_module_structured_light) cargo::rustc-check-cfg=cfg(ocvrs_has_module_superres) cargo::rustc-check-cfg=cfg(ocvrs_has_module_surface_matching) cargo::rustc-check-cfg=cfg(ocvrs_has_module_text) cargo::rustc-check-cfg=cfg(ocvrs_has_module_tracking) cargo::rustc-check-cfg=cfg(ocvrs_has_module_video) cargo::rustc-check-cfg=cfg(ocvrs_has_module_videoio) cargo::rustc-check-cfg=cfg(ocvrs_has_module_videostab) cargo::rustc-check-cfg=cfg(ocvrs_has_module_viz) cargo::rustc-check-cfg=cfg(ocvrs_has_module_wechat_qrcode) cargo::rustc-check-cfg=cfg(ocvrs_has_module_xfeatures2d) cargo::rustc-check-cfg=cfg(ocvrs_has_module_ximgproc) cargo::rustc-check-cfg=cfg(ocvrs_has_module_xobjdetect) cargo::rustc-check-cfg=cfg(ocvrs_has_module_xphoto) cargo::rustc-check-cfg=cfg(ocvrs_has_module_xstereo) cargo::rustc-check-cfg=cfg(ocvrs_has_inherent_feature_hfloat) cargo::rustc-check-cfg=cfg(ocvrs_has_inherent_feature_opencl) cargo::rustc-cfg=ocvrs_opencv_branch_4 cargo::rustc-cfg=ocvrs_has_module_core cargo::rustc-cfg=ocvrs_has_module_imgproc cargo::rustc-cfg=ocvrs_has_inherent_feature_hfloat cargo::rerun-if-env-changed=OPENCV_PACKAGE_NAME cargo::rerun-if-env-changed=OPENCV_PKGCONFIG_NAME cargo::rerun-if-env-changed=OPENCV_CMAKE_NAME cargo::rerun-if-env-changed=OPENCV_CMAKE_BIN cargo::rerun-if-env-changed=OPENCV_VCPKG_NAME cargo::rerun-if-env-changed=OPENCV_LINK_LIBS cargo::rerun-if-env-changed=OPENCV_LINK_PATHS cargo::rerun-if-env-changed=OPENCV_INCLUDE_PATHS cargo::rerun-if-env-changed=OPENCV_DISABLE_PROBES cargo::rerun-if-env-changed=OPENCV_MSVC_CRT cargo::rerun-if-env-changed=CMAKE_PREFIX_PATH cargo::rerun-if-env-changed=OpenCV_DIR cargo::rerun-if-env-changed=PKG_CONFIG_PATH cargo::rerun-if-env-changed=VCPKG_ROOT cargo::rerun-if-env-changed=VCPKGRS_DYNAMIC cargo::rerun-if-env-changed=VCPKGRS_TRIPLET cargo::rerun-if-env-changed=OCVRS_DOCS_GENERATE_DIR cargo::rerun-if-env-changed=DOCS_RS cargo::rerun-if-changed=/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp/xfeatures2d.hpp cargo::rerun-if-changed=/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp/ccalib.hpp cargo::rerun-if-changed=/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp/manual-core.cpp cargo::rerun-if-changed=/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp/face.hpp cargo::rerun-if-changed=/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp/aruco.hpp cargo::rerun-if-changed=/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp/videoio.hpp cargo::rerun-if-changed=/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp/sfm.hpp cargo::rerun-if-changed=/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp/core.hpp cargo::rerun-if-changed=/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp/ocvrs_common.hpp cargo::rerun-if-changed=/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp/hdf.hpp cargo::rerun-if-changed=/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp/gapi.hpp cargo::rerun-if-changed=/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp/dnn.hpp cargo::rerun-if-changed=/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp/bioinspired.hpp cargo::rerun-if-changed=/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp/alphamat.hpp cargo::rerun-if-changed=/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp/photo.hpp cargo::rerun-if-changed=Cargo.toml

--- stderr === Crate version: 0.94.4 === Environment configuration: === OPENCV_PACKAGE_NAME = None === OPENCV_PKGCONFIG_NAME = None === OPENCV_CMAKE_NAME = None === OPENCV_CMAKE_BIN = None === OPENCV_VCPKG_NAME = None === OPENCV_LINK_LIBS = Some("static=opencv_highgui,static=opencv_videoio,static=opencv_imgproc,static=opencv_core,z,jpeg,png,webp") === OPENCV_LINK_PATHS = Some("/home/coco/RustroverProjects/nnpp2/opencv-mobile-4.11.0-ubuntu-2404/lib") === OPENCV_INCLUDE_PATHS = Some("/home/coco/RustroverProjects/nnpp2/opencv-mobile-4.11.0-ubuntu-2404/include/opencv4") === OPENCV_DISABLE_PROBES = Some("pkg_config,cmake,vcpkg") === OPENCV_MSVC_CRT = None === CMAKE_PREFIX_PATH = None === OpenCV_DIR = None === PKG_CONFIG_PATH = None === VCPKG_ROOT = None === VCPKGRS_DYNAMIC = None === VCPKGRS_TRIPLET = None === OCVRS_DOCS_GENERATE_DIR = None === DOCS_RS = None === PATH = Some("/home/coco/.cargo/bin:/usr/bin:/usr/bin:/home/coco/anaconda3/bin:/home/coco/anaconda3/condabin:/home/coco/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin") === Enabled features: === CLANG_RUNTIME === IMGPROC === Detected probe priority boost based on environment vars: pkg_config: false, cmake: false, vcpkg: false === Probing the OpenCV library in the following order: environment, pkg_config, cmake, vcpkg_cmake, vcpkg === Configuring OpenCV library from the environment: === include_paths: /home/coco/RustroverProjects/nnpp2/opencv-mobile-4.11.0-ubuntu-2404/include/opencv4 === link_paths: /home/coco/RustroverProjects/nnpp2/opencv-mobile-4.11.0-ubuntu-2404/lib === link_libs: static=opencv_highgui,static=opencv_videoio,static=opencv_imgproc,static=opencv_core,z,jpeg,png,webp === Successfully probed using: environment === OpenCV library configuration: Library { include_paths: [ "/home/coco/RustroverProjects/nnpp2/opencv-mobile-4.11.0-ubuntu-2404/include/opencv4", ], version: Version { major: 4, minor: 11, patch: 0, }, enabled_features: [ "pthread", ], cargo_metadata: [ "cargo::rustc-link-search=/home/coco/RustroverProjects/nnpp2/opencv-mobile-4.11.0-ubuntu-2404/lib", "cargo::rustc-link-lib=opencv_highgui", "cargo::rustc-link-lib=opencv_videoio", "cargo::rustc-link-lib=opencv_imgproc", "cargo::rustc-link-lib=opencv_core", "cargo::rustc-link-lib=z", "cargo::rustc-link-lib=jpeg", "cargo::rustc-link-lib=png", "cargo::rustc-link-lib=webp", ], } === Found OpenCV version: 4.11.0 in headers located at: /home/coco/RustroverProjects/nnpp2/opencv-mobile-4.11.0-ubuntu-2404/include/opencv4 === Detected OpenCV module header dir at: /home/coco/RustroverProjects/nnpp2/opencv-mobile-4.11.0-ubuntu-2404/include/opencv4/opencv2 === Generating code in: /home/coco/RustroverProjects/untitled5/target/debug/build/opencv-7da52440a0b622ec/out === Placing generated bindings into: /home/coco/RustroverProjects/untitled5/target/debug/build/opencv-7da52440a0b622ec/out/opencv === Using OpenCV headers from: /home/coco/RustroverProjects/nnpp2/opencv-mobile-4.11.0-ubuntu-2404/include/opencv4 === Clang: Ubuntu clang version 16.0.6 (23ubuntu4) === Clang command line args: [ "-isystem/usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13", "-isystem/usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/x86_64-linux-gnu/c++/13", "-isystem/usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/backward", "-isystem/usr/lib/llvm-16/lib/clang/16/include", "-isystem/usr/local/include", "-isystem/usr/include/x86_64-linux-gnu", "-isystem/usr/include", "-I/home/coco/RustroverProjects/nnpp2/opencv-mobile-4.11.0-ubuntu-2404/include/opencv4", "-F/home/coco/RustroverProjects/nnpp2/opencv-mobile-4.11.0-ubuntu-2404/include/opencv4", "-I/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp", "-F/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp", "-DOCVRS_PARSING_HEADERS", "-includeocvrs_common.hpp", "-std=c++17", ] === Using environment job server with the the amount of available jobs: 16 === Generating 2 modules === Running: "/home/coco/RustroverProjects/untitled5/target/debug/build/opencv-e33045fc197b86be/build-script-build" "/home/coco/RustroverProjects/nnpp2/opencv-mobile-4.11.0-ubuntu-2404/include/opencv4" "/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp" "/home/coco/RustroverProjects/untitled5/target/debug/build/opencv-7da52440a0b622ec/out" "core" "" === Running: "/home/coco/RustroverProjects/untitled5/target/debug/build/opencv-e33045fc197b86be/build-script-build" "/home/coco/RustroverProjects/nnpp2/opencv-mobile-4.11.0-ubuntu-2404/include/opencv4" "/home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp" "/home/coco/RustroverProjects/untitled5/target/debug/build/opencv-7da52440a0b622ec/out" "imgproc" "" === WARNING: 1 diagnostic messages === /home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/src_cpp/core.hpp:12:10: fatal error: 'opencv2/core/cuda.hpp' file not found

thread 'main' panicked at /home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-binding-generator-0.96.1/src/generator.rs:424:17: === Errors during header parsing note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

thread 'gen-core' panicked at /home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/build/generator.rs:127:37: Failed to run the bindings generator for module: core note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

thread 'main' panicked at /home/coco/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/opencv-0.94.4/build/generator.rs:137:36: Generator process panicked: Any { .. } === Generated: imgproc in 1.410224813s

topcoco avatar Jun 24 '25 11:06 topcoco

Hi, I was not aware of such OpenCV mod before, I'll take a look.

Respect missing headers in minimal OpenCV builds without failing?

Can you please check crate v0.95, the CUDA problem should be fixed in it.

Allow truly selective module binding generation?

In this case CUDA is not a separate module, but a extension to some of the modules. From version 0.95 it should now be easier to take that into account.

Skip dependencies on optional components (like CUDA) when they're unavailable?

It's not a dependency per se, but you can now use opencv_has_inherent_feature_cuda! macro to do conditional compilation based on whether CUDA is enabled in OpenCV.

twistedfall avatar Jun 25 '25 18:06 twistedfall