Skip to content

Commit 633086d

Browse files
committed
Deglobalize FUNC_EXCLUDE
1 parent 474141e commit 633086d

File tree

6 files changed

+201
-87
lines changed

6 files changed

+201
-87
lines changed

binding-generator/src/bin/settings-cleanup.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use opencv_binding_generator::{
1414
struct FunctionFinder<'tu, 'f> {
1515
pub module: &'tu str,
1616
pub gen_env: GeneratorEnv<'tu>,
17-
pub func_exclude_unused: RefCell<&'f mut HashSet<&'static str>>,
1817
pub func_cfg_attr_unused: RefCell<&'f mut HashSet<&'static str>>,
1918
}
2019

@@ -24,13 +23,13 @@ impl<'tu, 'f> FunctionFinder<'tu, 'f> {
2423
self.gen_env.settings.arg_override.get(&mut matcher);
2524
self.gen_env.settings.return_override.get(&mut matcher);
2625
self.gen_env.settings.force_infallible.get(&mut matcher);
26+
self.gen_env.settings.func_companion_tweak.get(&mut matcher);
2727
self.gen_env.settings.func_replace.get(&mut matcher);
2828
self.gen_env.settings.func_specialize.get(&mut matcher);
2929
self.gen_env.settings.func_unsafe.get(&mut matcher);
3030

3131
let identifier = f.identifier();
3232

33-
self.func_exclude_unused.borrow_mut().remove(identifier.as_str());
3433
self.func_cfg_attr_unused.borrow_mut().remove(identifier.as_str());
3534
}
3635
}
@@ -86,7 +85,6 @@ fn main() {
8685
let mut args = env::args_os().skip(1);
8786
let src_cpp_dir = PathBuf::from(args.next().expect("2nd argument must be dir with custom cpp"));
8887
let opencv_header_dirs = args.map(PathBuf::from);
89-
let mut func_exclude_unused = settings::FUNC_EXCLUDE.clone();
9088
let mut func_cfg_attr_unused = settings::FUNC_CFG_ATTR.keys().copied().collect::<HashSet<_>>();
9189
// module -> usage_section -> (name, preds)
9290
let global_usage_tracking = Rc::new(RefCell::new(HashMap::<
@@ -117,7 +115,6 @@ fn main() {
117115
let mut function_finder = FunctionFinder {
118116
module: &module,
119117
gen_env,
120-
func_exclude_unused: RefCell::new(&mut func_exclude_unused),
121118
func_cfg_attr_unused: RefCell::new(&mut func_cfg_attr_unused),
122119
};
123120
root_entity.walk_opencv_entities(&mut function_finder);
@@ -163,8 +160,6 @@ fn main() {
163160
}
164161
}
165162
}
166-
println!("Unused entries in settings::FUNC_EXCLUDE ({}):", func_exclude_unused.len());
167-
show(func_exclude_unused);
168163
println!("Unused entries in settings::FUNC_CFG_ATTR ({}):", func_cfg_attr_unused.len());
169164
show(func_cfg_attr_unused);
170165
}

binding-generator/src/func.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use crate::type_ref::{Constness, CppNameStyle, FishStyle, TypeRefDesc, TypeRefTy
2222
use crate::writer::rust_native::element::RustElement;
2323
use crate::writer::rust_native::type_ref::TypeRefExt;
2424
use crate::{
25-
debug, settings, Class, CowMapBorrowedExt, DefaultElement, Element, EntityExt, Field, GeneratedType, GeneratorEnv,
26-
IteratorExt, NameDebug, NameStyle, StrExt, StringExt, TypeRef,
25+
debug, Class, CowMapBorrowedExt, DefaultElement, Element, EntityExt, Field, GeneratedType, GeneratorEnv, IteratorExt,
26+
NameDebug, NameStyle, StrExt, StringExt, TypeRef,
2727
};
2828

2929
mod desc;
@@ -126,7 +126,7 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
126126
for spec in spec_values {
127127
generic.extend_sep(", ", &spec().cpp_name(CppNameStyle::Reference));
128128
}
129-
let mut desc = self.to_desc(InheritConfig::empty().kind().arguments().return_type_ref());
129+
let mut desc = self.to_desc_with_skip_config(InheritConfig::empty().kind().arguments().return_type_ref());
130130
let rust_custom_leafname = Some(if spec.0.contains('+') {
131131
spec.0.replacen('+', &self.rust_leafname(FishStyle::No), 1).into()
132132
} else {
@@ -142,7 +142,7 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
142142
Self::Desc(desc)
143143
}
144144

145-
pub(crate) fn to_desc(&self, skip_config: InheritConfig) -> Rc<FuncDesc<'tu, 'ge>> {
145+
pub(crate) fn to_desc_with_skip_config(&self, skip_config: InheritConfig) -> Rc<FuncDesc<'tu, 'ge>> {
146146
match self {
147147
Func::Clang { .. } => {
148148
let kind = if skip_config.kind {
@@ -245,7 +245,7 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
245245
match self {
246246
Func::Clang { .. } => {
247247
if inherit_config.any_enabled() {
248-
let mut desc = self.to_desc(inherit_config);
248+
let mut desc = self.to_desc_with_skip_config(inherit_config);
249249
transfer(Rc::make_mut(&mut desc), ancestor, inherit_config);
250250
*self = Func::Desc(desc);
251251
}
@@ -645,12 +645,14 @@ impl Element for Func<'_, '_> {
645645
.with_is_excluded(|| {
646646
let kind = self.kind();
647647
let identifier = self.identifier();
648-
let is_unavailable = match self {
649-
Func::Clang { entity, .. } => entity.get_availability() == Availability::Unavailable,
648+
let is_excluded = match self {
649+
Func::Clang { entity, gen_env, .. } => {
650+
entity.get_availability() == Availability::Unavailable
651+
|| gen_env.settings.func_exclude.contains(identifier.as_str())
652+
}
650653
Func::Desc(_) => false,
651654
};
652-
is_unavailable
653-
|| settings::FUNC_EXCLUDE.contains(identifier.as_str())
655+
is_excluded
654656
|| self.is_generic()
655657
|| self.arguments().iter().any(|a| a.type_ref().exclude_kind().is_ignored())
656658
|| kind.as_operator().map_or(false, |(_, kind)| match kind {

binding-generator/src/settings.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ pub use element_exclude_kind::ELEMENT_EXCLUDE_KIND;
5050
pub use element_export_tweak::ELEMENT_EXPORT_TWEAK;
5151
pub use force_infallible::{force_infallible_factory, ForceInfallible};
5252
pub use func_cfg_attr::FUNC_CFG_ATTR;
53-
pub use func_exclude::FUNC_EXCLUDE;
53+
pub use func_companion_tweak::{func_companion_tweak_factory, CompanionTweak, FuncCompanionTweak};
54+
pub use func_exclude::{func_exclude_factory, FuncExclude};
5455
pub use func_inject::{func_inject_factory, FuncFactory, FuncInject};
5556
pub use func_rename::{func_rename_factory, FuncRename};
5657
pub use func_replace::{func_replace_factory, FuncInheritFactory, FuncReplace};
@@ -73,6 +74,7 @@ mod element_exclude_kind;
7374
mod element_export_tweak;
7475
mod force_infallible;
7576
mod func_cfg_attr;
77+
mod func_companion_tweak;
7678
mod func_exclude;
7779
mod func_inject;
7880
mod func_rename;
@@ -91,6 +93,8 @@ pub struct Settings {
9193
pub arg_override: ArgOverride,
9294
pub return_override: ReturnOverride,
9395
pub force_infallible: ForceInfallible,
96+
pub func_companion_tweak: FuncCompanionTweak,
97+
pub func_exclude: FuncExclude,
9498
pub func_inject: FuncInject,
9599
pub func_rename: FuncRename,
96100
pub func_replace: FuncReplace,
@@ -107,6 +111,8 @@ impl Settings {
107111
arg_override: ArgOverride::empty(),
108112
return_override: ReturnOverride::empty(),
109113
force_infallible: ForceInfallible::empty(),
114+
func_companion_tweak: FuncCompanionTweak::empty(),
115+
func_exclude: FuncExclude::default(),
110116
func_inject: FuncInject::default(),
111117
func_rename: FuncRename::default(),
112118
func_replace: FuncReplace::empty(),
@@ -123,6 +129,8 @@ impl Settings {
123129
arg_override: arg_override_factory(module),
124130
return_override: return_override_factory(module),
125131
force_infallible: force_infallible_factory(module),
132+
func_companion_tweak: func_companion_tweak_factory(module),
133+
func_exclude: func_exclude_factory(module),
126134
func_inject: func_inject_factory(module),
127135
func_rename: func_rename_factory(module),
128136
func_replace: func_replace_factory(module),
@@ -138,6 +146,7 @@ impl Settings {
138146
self.arg_override.start_usage_tracking();
139147
self.return_override.start_usage_tracking();
140148
self.force_infallible.start_usage_tracking();
149+
self.func_companion_tweak.start_usage_tracking();
141150
self.func_replace.start_usage_tracking();
142151
self.func_specialize.start_usage_tracking();
143152
self.func_unsafe.start_usage_tracking();
@@ -148,6 +157,7 @@ impl Settings {
148157
("ARG_OVERRIDE", self.arg_override.finish_usage_tracking()),
149158
("RETURN_OVERRIDE", self.return_override.finish_usage_tracking()),
150159
("FORCE_INFALLIBLE", self.force_infallible.finish_usage_tracking()),
160+
("FUNC_COMPANION_TWEAK", self.func_companion_tweak.finish_usage_tracking()),
151161
("FUNC_REPLACE", self.func_replace.finish_usage_tracking()),
152162
("FUNC_SPECIALIZE", self.func_specialize.finish_usage_tracking()),
153163
("FUNC_UNSAFE", self.func_unsafe.finish_usage_tracking()),
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::collections::HashMap;
2+
3+
use crate::func::FuncMatcher;
4+
5+
pub type FuncCompanionTweak = FuncMatcher<'static, CompanionTweak>;
6+
7+
#[derive(Debug, Clone, Copy)]
8+
pub enum CompanionTweak {
9+
SkipDefault,
10+
}
11+
12+
pub fn func_companion_tweak_factory(module: &str) -> FuncCompanionTweak {
13+
match module {
14+
"text" => FuncMatcher::create(HashMap::from([(
15+
"cv::text::OCRBeamSearchDecoder::create",
16+
vec![(
17+
pred!(
18+
mut,
19+
[
20+
"classifier",
21+
"vocabulary",
22+
"transition_probabilities_table",
23+
"emission_probabilities_table",
24+
"mode",
25+
"beam_size"
26+
],
27+
),
28+
CompanionTweak::SkipDefault, // with OpenCV 4.2 this leads to https://github.com/twistedfall/opencv-rust/issues/505
29+
)],
30+
)])),
31+
_ => FuncCompanionTweak::empty(),
32+
}
33+
}
34+
35+
impl CompanionTweak {
36+
pub fn skip_default(&self) -> bool {
37+
match self {
38+
CompanionTweak::SkipDefault => true,
39+
}
40+
}
41+
}
Lines changed: 88 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
use std::collections::HashSet;
22

3-
use once_cell::sync::Lazy;
3+
pub type FuncExclude = HashSet<&'static str>;
44

5-
/// map of functions to exclude, value is Func.identifier()
6-
pub static FUNC_EXCLUDE: Lazy<HashSet<&str>> = Lazy::new(|| {
5+
pub fn func_exclude_factory(module: &str) -> FuncExclude {
6+
match module {
7+
"core" => core_factory(),
8+
"cudaimgproc" => cudaimgproc_factory(),
9+
"dnn" => dnn_factory(),
10+
"gapi" => gapi_factory(),
11+
"hdf" => hdf_factory(),
12+
"imgproc" => imgproc_factory(),
13+
"objdetect" => objdetect_factory(),
14+
"optflow" => optflow_factory(),
15+
"stitching" => stitching_factory(),
16+
"surface_matching" => surface_matching_factory(),
17+
"tracking" => tracking_factory(),
18+
_ => FuncExclude::default(),
19+
}
20+
}
21+
22+
fn core_factory() -> FuncExclude {
723
HashSet::from([
8-
// ### core ###
924
"cv_AsyncArray__getImpl_const",
10-
"cv_Mat_Mat_const_MatR_const_RangeX", // duplicate of cv_Mat_Mat_Mat_VectorOfRange, but with pointers
11-
"cv_Mat_copySize_const_MatR", // internal function
12-
"cv_Mat_operator___const_const_RangeX", // duplicate of cv_Mat_operator___const_const_vectorLRangeGR, but with pointers
13-
"cv_Mat_push_back__const_voidX", // internal method
14-
"cv_Mat_operatorST_const_MatR", // unsafe with the safe version that moves
25+
"cv_Mat_Mat_const_MatR_const_RangeX", // duplicate of cv_Mat_Mat_Mat_VectorOfRange, but with pointers
26+
"cv_Mat_copySize_const_MatR", // internal function
27+
"cv_Mat_operator___const_const_RangeX", // duplicate of cv_Mat_operator___const_const_vectorLRangeGR, but with pointers
28+
"cv_Mat_push_back__const_voidX", // internal method
29+
"cv_Mat_operatorST_const_MatR", // unsafe with the safe version that moves
1530
"cv_UMat_UMat_const_UMatR_const_RangeX", // duplicate of cv_UMat_UMat_UMat_VectorOfRange, but with pointers
16-
"cv_UMat_copySize_const_UMatR", // internal function
31+
"cv_UMat_copySize_const_UMatR", // internal function
1732
"cv_UMat_operator___const_const_RangeX", // duplicate of cv_UMat_operator___const_const_vectorLRangeGR, but with pointers
18-
"cv_RNG_MT19937_operator__", // the same as calling to_u32() or next()
33+
"cv_RNG_MT19937_operator__", // the same as calling to_u32() or next()
1934
"cv_addImpl_int_const_charX",
2035
"cv_calcCovarMatrix_const_MatX_int_MatR_MatR_int_int", // duplicate of cv_calcCovarMatrix_const__InputArrayR_const__OutputArrayR_const__InputOutputArrayR_int_int, but with pointers
2136
"cv_cv_abs_short",
@@ -33,28 +48,57 @@ pub static FUNC_EXCLUDE: Lazy<HashSet<&str>> = Lazy::new(|| {
3348
"cv_useCollection",
3449
"cv_vconcat_const_MatX_size_t_const__OutputArrayR", // duplicate of cv_vconcat_VectorOfMat_Mat, but with pointers
3550
"cv_Mat_Mat_MatRR", // Mat::copy that takes arg by value
36-
// ### cudaimgproc ###
51+
// those function are marked as CV_EXPORTS, but they are missing from the shared libraries
52+
"cv_MatConstIterator_MatConstIterator_const_MatX_const_intX",
53+
"cv_SparseMatConstIterator_operatorSS",
54+
"cv_SparseMatIterator_SparseMatIterator_SparseMatX_const_intX",
55+
"cv__OutputArray__OutputArray_const_vectorLGpuMatGR",
56+
"cv_cuda_convertFp16_const__InputArrayR_const__OutputArrayR_StreamR",
57+
"cv_getImpl_vectorLintGR_vectorLStringGR",
58+
])
59+
}
60+
61+
fn cudaimgproc_factory() -> FuncExclude {
62+
HashSet::from([
3763
"cv_cuda_histEven_const__InputArrayR_GpuMatXX_intXX_intXX_intXX_StreamR", // slice of boxed objects
3864
"cv_cuda_histRange_const__InputArrayR_GpuMatXX_const_GpuMatXX_StreamR", // slice of boxed objects
39-
// ### dnn ###
65+
])
66+
}
67+
68+
fn dnn_factory() -> FuncExclude {
69+
HashSet::from([
4070
"cv_dnn_DictValue_DictValue_const_StringR", // effectively duplicate of cv_dnn_DictValue_DictValue_const_charX
4171
"cv_dnn_Layer_finalize_const_vectorLMatXGR_vectorLMatGR", // dup of cv_dnn_Layer_finalize_const__InputArrayR_const__OutputArrayR
4272
"cv_dnn_Model_operator_cv_dnn_Net_const", // fixme, should generate fine, it's a dup of get_network_() anyway
43-
// ### gapi ###
73+
// those function are marked as CV_EXPORTS, but they are missing from the shared libraries
74+
"cv_dnn_BackendNode_BackendNode_int",
75+
])
76+
}
77+
78+
fn gapi_factory() -> FuncExclude {
79+
HashSet::from([
4480
"cv_MediaFrame_IAdapter_access_Access", // use of deleted function ‘cv::MediaFrame::View::View(const cv::MediaFrame::View&)’
4581
"cv_MediaFrame_access_const_Access", // use of deleted function ‘cv::MediaFrame::View::View(const cv::MediaFrame::View&)’
4682
"cv_RMat_Adapter_access_Access", // use of deleted function ‘cv::RMat::View::View(const cv::RMat::View&)’
4783
"cv_RMat_IAdapter_access_Access", // use of deleted function ‘cv::RMat::View::View(const cv::RMat::View&)’
4884
"cv_RMat_access_const_Access", // use of deleted function ‘cv::RMat::View::View(const cv::RMat::View&)’
49-
// ### hdf ###
85+
])
86+
}
87+
88+
fn hdf_factory() -> FuncExclude {
89+
HashSet::from([
5090
"cv_hdf_HDF5_dscreate_const_const_int_const_int_const_int_const_StringR_const_int_const_intX", // has corresponding Vector version
5191
"cv_hdf_HDF5_dsinsert_const_const__InputArrayR_const_StringR_const_intX", // has corresponding Vector version
5292
"cv_hdf_HDF5_dsinsert_const_const__InputArrayR_const_StringR_const_intX_const_intX", // has corresponding Vector version
5393
"cv_hdf_HDF5_dsread_const_const__OutputArrayR_const_StringR_const_intX", // has corresponding Vector version
5494
"cv_hdf_HDF5_dsread_const_const__OutputArrayR_const_StringR_const_intX_const_intX", // has corresponding Vector version
5595
"cv_hdf_HDF5_dswrite_const_const__InputArrayR_const_StringR_const_intX", // has corresponding Vector version
5696
"cv_hdf_HDF5_dswrite_const_const__InputArrayR_const_StringR_const_intX_const_intX", // has corresponding Vector version
57-
// ### imgproc ###
97+
])
98+
}
99+
100+
fn imgproc_factory() -> FuncExclude {
101+
HashSet::from([
58102
"cv_calcBackProject_const_MatX_int_const_intX_const_SparseMatR_const__OutputArrayR_const_floatXX_double_bool", // slice pointers
59103
"cv_calcBackProject_const_MatX_int_const_intX_const__InputArrayR_const__OutputArrayR_const_floatXX_double_bool", // slice pointers
60104
"cv_calcHist_const_MatX_int_const_intX_const__InputArrayR_SparseMatR_int_const_intX_const_floatXX_bool_bool", // slice pointers
@@ -65,30 +109,40 @@ pub static FUNC_EXCLUDE: Lazy<HashSet<&str>> = Lazy::new(|| {
65109
"cv_fillPoly_const__InputOutputArrayR_const_PointXX_const_intX_int_const_ScalarR_int_int_Point",
66110
"cv_polylines_MatR_const_PointXX_const_intX_int_bool_const_ScalarR_int_int_int", // 3.2 3.4
67111
"cv_polylines_const__InputOutputArrayR_const_PointXX_const_intX_int_bool_const_ScalarR_int_int_int",
68-
// ### objdetect ###
112+
])
113+
}
114+
115+
fn objdetect_factory() -> FuncExclude {
116+
HashSet::from([
69117
"cv_QRCodeDetector_detectAndDecodeMulti_const_const__InputArrayR_vectorLstringGR_const__OutputArrayR_const__OutputArrayR", // fixme: stores data to Vector<String>, that doesn't work yet
70-
// ### optflow ###
118+
])
119+
}
120+
121+
fn optflow_factory() -> FuncExclude {
122+
HashSet::from([
71123
"cv_optflow_GPCTrainingSamples_operator_cv_optflow_GPCSamplesVector", // support of "operator &" missing
72-
// ### text ###
73-
"cv_text_OCRBeamSearchDecoder_create_const_PtrLClassifierCallbackG_const_stringR_const__InputArrayR_const__InputArrayR", // with OpenCV 4.2 this leads to https://github.com/twistedfall/opencv-rust/issues/505
74-
"cv_text_OCRBeamSearchDecoder_create_const_PtrLClassifierCallbackG_const_StringR_const__InputArrayR_const__InputArrayR", // with OpenCV 4.2 this leads to https://github.com/twistedfall/opencv-rust/issues/505
124+
])
125+
}
126+
127+
fn stitching_factory() -> FuncExclude {
128+
HashSet::from([
75129
// those function are marked as CV_EXPORTS, but they are missing from the shared libraries
76-
// ### core ###
77-
"cv_MatConstIterator_MatConstIterator_const_MatX_const_intX",
78-
"cv_SparseMatConstIterator_operatorSS",
79-
"cv_SparseMatIterator_SparseMatIterator_SparseMatX_const_intX",
80-
"cv__OutputArray__OutputArray_const_vectorLGpuMatGR",
81-
"cv_cuda_convertFp16_const__InputArrayR_const__OutputArrayR_StreamR",
82-
"cv_getImpl_vectorLintGR_vectorLStringGR",
83-
// ### dnn ###
84-
"cv_dnn_BackendNode_BackendNode_int",
85-
// ### stitching ###
86130
"cv_createStitcherScans_bool",
87131
"cv_createStitcher_bool",
88-
// ### surface_matching ###
132+
])
133+
}
134+
135+
fn surface_matching_factory() -> FuncExclude {
136+
HashSet::from([
137+
// those function are marked as CV_EXPORTS, but they are missing from the shared libraries
89138
"cv_ppf_match_3d_PPF3DDetector_read_const_FileNodeR",
90139
"cv_ppf_match_3d_PPF3DDetector_write_const_FileStorageR",
91-
// ### tracking ###
140+
])
141+
}
142+
143+
fn tracking_factory() -> FuncExclude {
144+
HashSet::from([
145+
// those function are marked as CV_EXPORTS, but they are missing from the shared libraries
92146
"cv_CvFeatureParams_CvFeatureParams",
93147
"cv_CvFeatureParams_create_FeatureType",
94148
"cv_CvFeatureParams_create_int",
@@ -106,4 +160,4 @@ pub static FUNC_EXCLUDE: Lazy<HashSet<&str>> = Lazy::new(|| {
106160
"cv_SparseMatConstIterator_operatorSS",
107161
"cv__OutputArray__OutputArray_const_vectorLGpuMatGR",
108162
])
109-
});
163+
}

0 commit comments

Comments
 (0)