Skip to content

Commit 768d272

Browse files
committed
Add algorithm_hint inherent feature
1 parent c447958 commit 768d272

File tree

10 files changed

+400
-343
lines changed

10 files changed

+400
-343
lines changed

build.rs

Lines changed: 18 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::time::Instant;
66

77
use binding_generator::handle_running_binding_generator;
88
use docs::handle_running_in_docsrs;
9+
use enums::{InherentFeature, SUPPORTED_INHERENT_FEATURES, SUPPORTED_MODULES};
910
use generator::BindingGenerator;
1011
use header::IncludePath;
1112
use library::Library;
@@ -19,6 +20,8 @@ mod binding_generator;
1920
pub mod cmake_probe;
2021
#[path = "build/docs.rs"]
2122
mod docs;
23+
#[path = "build/enums.rs"]
24+
mod enums;
2225
#[path = "build/generator.rs"]
2326
mod generator;
2427
#[path = "build/header.rs"]
@@ -63,84 +66,6 @@ static AFFECTING_ENV_VARS: [&str; 18] = [
6366

6467
static SUPPORTED_OPENCV_BRANCHES: [(&str, &str); 3] = [("~3.4", "34"), ("~4", "4"), ("~5", "5")];
6568

66-
static SUPPORTED_MODULES: [SupportedModule; 73] = [
67-
SupportedModule::ThreeD,
68-
SupportedModule::AlphaMat,
69-
SupportedModule::Aruco,
70-
SupportedModule::ArucoDetector,
71-
SupportedModule::Barcode,
72-
SupportedModule::BgSegm,
73-
SupportedModule::Bioinspired,
74-
SupportedModule::Calib,
75-
SupportedModule::Calib3d,
76-
SupportedModule::CCalib,
77-
SupportedModule::Core,
78-
SupportedModule::CudaArithm,
79-
SupportedModule::CudaBgSegm,
80-
SupportedModule::CudaCodec,
81-
SupportedModule::CudaFeatures2d,
82-
SupportedModule::CudaFilters,
83-
SupportedModule::CudaImgProc,
84-
SupportedModule::CudaLegacy,
85-
SupportedModule::CudaObjDetect,
86-
SupportedModule::CudaOptFlow,
87-
SupportedModule::CudaStereo,
88-
SupportedModule::CudaWarping,
89-
SupportedModule::Cvv,
90-
SupportedModule::Dnn,
91-
SupportedModule::DnnSuperRes,
92-
SupportedModule::Dpm,
93-
SupportedModule::Face,
94-
SupportedModule::Features,
95-
SupportedModule::Features2d,
96-
SupportedModule::Flann,
97-
SupportedModule::Freetype,
98-
SupportedModule::Fuzzy,
99-
SupportedModule::Gapi,
100-
SupportedModule::Hdf,
101-
SupportedModule::Hfs,
102-
SupportedModule::HighGui,
103-
SupportedModule::ImgHash,
104-
SupportedModule::ImgCodecs,
105-
SupportedModule::ImgProc,
106-
SupportedModule::IntensityTransform,
107-
SupportedModule::LineDescriptor,
108-
SupportedModule::Mcc,
109-
SupportedModule::Ml,
110-
SupportedModule::ObjDetect,
111-
SupportedModule::OptFlow,
112-
SupportedModule::Ovis,
113-
SupportedModule::PhaseUnwrapping,
114-
SupportedModule::Photo,
115-
SupportedModule::Plot,
116-
SupportedModule::Quality,
117-
SupportedModule::Rapid,
118-
SupportedModule::Rgbd,
119-
SupportedModule::Saliency,
120-
SupportedModule::Sfm,
121-
SupportedModule::Shape,
122-
SupportedModule::Signal,
123-
SupportedModule::Stereo,
124-
SupportedModule::Stitching,
125-
SupportedModule::StructuredLight,
126-
SupportedModule::SuperRes,
127-
SupportedModule::SurfaceMatching,
128-
SupportedModule::Text,
129-
SupportedModule::Tracking,
130-
SupportedModule::Video,
131-
SupportedModule::VideoIo,
132-
SupportedModule::VideoStab,
133-
SupportedModule::Viz,
134-
SupportedModule::WechatQrCode,
135-
SupportedModule::XFeatures2d,
136-
SupportedModule::XImgProc,
137-
SupportedModule::XObjDetect,
138-
SupportedModule::XPhoto,
139-
SupportedModule::XStereo,
140-
];
141-
142-
static SUPPORTED_INHERENT_FEATURES: [&str; 3] = ["hfloat", "opencl", "cuda"];
143-
14469
/// The contents of these vars will be present in the debug log, but will not cause the source rebuild
14570
static DEBUG_ENV_VARS: [&str; 1] = ["PATH"];
14671

@@ -227,18 +152,23 @@ fn emit_opencv_branch(opencv: &Library) {
227152
}
228153

229154
fn emit_inherent_features(opencv: &Library) {
155+
let mut fake_features = Vec::with_capacity(2);
230156
if VersionReq::parse(">=4.10")
231157
.expect("Static version requirement")
232158
.matches(&opencv.version)
233159
{
234-
// hfloat is not an actual OpenCV feature specified in the cvconfig.h, but something that's supported starting with 4.10
235-
println!("cargo::rustc-cfg=ocvrs_has_inherent_feature_hfloat");
236-
}
237-
for inherent_feature in &opencv.inherent_features {
238-
if SUPPORTED_INHERENT_FEATURES.contains(&inherent_feature.as_str()) {
239-
println!("cargo::rustc-cfg=ocvrs_has_inherent_feature_{inherent_feature}");
160+
fake_features.push(InherentFeature::Hfloat);
161+
if VersionReq::parse(">=4.11")
162+
.expect("Static version requirement")
163+
.matches(&opencv.version)
164+
{
165+
fake_features.push(InherentFeature::AlgorithmHint);
240166
}
241167
}
168+
let detected_supported_inherent_features = opencv.inherent_features.iter().flat_map(|f| InherentFeature::try_from_str(f));
169+
for inherent_feature in detected_supported_inherent_features.chain(fake_features) {
170+
println!("cargo::rustc-cfg=ocvrs_has_inherent_feature_{}", inherent_feature.as_str());
171+
}
242172
}
243173

244174
fn emit_modules(modules: &[SupportedModule]) {
@@ -360,7 +290,10 @@ fn main() -> Result<()> {
360290
println!("cargo::rustc-check-cfg=cfg(ocvrs_has_module_{})", module.opencv_name());
361291
}
362292
for inherent_feature in SUPPORTED_INHERENT_FEATURES {
363-
println!("cargo::rustc-check-cfg=cfg(ocvrs_has_inherent_feature_{inherent_feature})");
293+
println!(
294+
"cargo::rustc-check-cfg=cfg(ocvrs_has_inherent_feature_{})",
295+
inherent_feature.as_str()
296+
);
364297
}
365298

366299
if matches!(handle_running_in_docsrs(), GenerateFullBindings::Stop) {

build/cond_macros/opencv_branch.rs

Lines changed: 0 additions & 115 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/// Conditional compilation macro based on OpenCV branch version for usage in external crates.
2+
///
3+
/// # Examples
4+
///
5+
/// Alternative import:
6+
/// ```
7+
/// opencv::not_opencv_branch_34! {
8+
/// use opencv::imgproc::LINE_8;
9+
/// }
10+
/// opencv::opencv_branch_34! {
11+
/// use opencv::core::LINE_8;
12+
/// }
13+
/// ```
14+
///
15+
/// Alternative function call:
16+
/// ```
17+
/// let mut cam = opencv::opencv_branch_34! {
18+
/// {
19+
/// opencv::videoio::VideoCapture::new_default(0)?
20+
/// } else {
21+
/// opencv::videoio::VideoCapture::new(0, videoio::CAP_ANY)?
22+
/// }
23+
/// };
24+
/// ```
25+
#[cfg({{ cfg_condition_prefix }}ocvrs_opencv_branch_{{ feature }}{{ cfg_condition_suffix }})]
26+
#[macro_export]
27+
macro_rules! opencv_branch_{{ feature }} {
28+
($bl_pos:block else $bl_neg:block) => { {{ macro_body_block }} };
29+
($($tt:tt)*) => { {{ macro_body_tt }} }
30+
}
31+
32+
/// Conditional compilation macro based on OpenCV branch version for usage in external crates.
33+
///
34+
/// # Examples
35+
///
36+
/// Alternative import:
37+
/// ```
38+
/// opencv::not_opencv_branch_34! {
39+
/// use opencv::imgproc::LINE_8;
40+
/// }
41+
/// opencv::opencv_branch_34! {
42+
/// use opencv::core::LINE_8;
43+
/// }
44+
/// ```
45+
#[cfg({{ cfg_condition_prefix }}ocvrs_opencv_branch_{{ feature }}{{ cfg_condition_suffix }})]
46+
#[macro_export]
47+
macro_rules! not_opencv_branch_{{ feature }} {
48+
($($tt:tt)*) => { {{ macro_body_not_tt }} }
49+
}
50+
51+

build/cond_macros/opencv_inherent_feature.rs

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/// Conditional compilation macro based on whether OpenCV was built with or without the {{ feature }} feature
2+
///
3+
/// The macro has two forms:
4+
/// 1. Two blocks, separated by the `else` keyword. The first block will be compiled if the OpenCV feature is enabled, the second
5+
/// one — if it's not. Note that both blocks must have the same return type.
6+
/// 2. Plain token tree, for usage on the item level, e.g., for `use` imports. The code inside will be compiled if the OpenCV
7+
/// feature is enabled and completely skipped if it's not.
8+
///
9+
/// # Examples
10+
///
11+
/// Two blocks with `else`:
12+
/// ```
13+
/// let cuda_available = opencv::opencv_has_inherent_feature_cuda! {
14+
/// {
15+
/// true
16+
/// } else {
17+
/// false
18+
/// }
19+
/// };
20+
/// ```
21+
///
22+
/// Plain token tree:
23+
/// ```
24+
/// opencv::opencv_has_inherent_feature_cuda! {
25+
/// use opencv::core::GpuMat;
26+
/// }
27+
/// ```
28+
#[cfg({{ cfg_condition_prefix }}ocvrs_has_inherent_feature_{{ feature }}{{ cfg_condition_suffix }})]
29+
#[macro_export]
30+
macro_rules! opencv_has_inherent_feature_{{ feature }} {
31+
($bl_pos:block else $bl_neg:block) => { {{ macro_body_block }} };
32+
($($tt:tt)*) => { {{ macro_body_tt }} };
33+
}
34+
35+

0 commit comments

Comments
 (0)