Skip to content

Commit 7a6aa38

Browse files
committed
Implement macros for conditional modules
1 parent f76365b commit 7a6aa38

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1044
-491
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ half = { version = "2, <2.5.0", optional = true }
2828
libc = "0.2"
2929
num-traits = "0.2"
3030
once_cell = "1"
31+
opencv-binding-generator = { version = "0.96.1", path = "binding-generator" }
3132
# version 0.8.20 doesn't contain the deficiency mentioned in https://deps.rs/crate/opencv/0.59.0#vulnerabilities
3233
rgb = { version = "0.8.20", default-features = false, features = ["argb"], optional = true }
3334

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::ops::ControlFlow;
55
use std::path::{Path, PathBuf};
66

77
use opencv_binding_generator::writer::RustNativeBindingWriter;
8-
use opencv_binding_generator::{line_reader, Generator};
8+
use opencv_binding_generator::{line_reader, Generator, SupportedModule};
99

1010
fn get_version_header(header_dir: &Path) -> Option<PathBuf> {
1111
let out = header_dir.join("opencv2/core/version.hpp");
@@ -69,7 +69,10 @@ fn main() {
6969
let src_cpp_dir = PathBuf::from(args.next().expect("2nd argument must be dir with custom cpp"));
7070
let out_dir = PathBuf::from(args.next().expect("3rd argument must be output dir"));
7171
let module = args.next().expect("4th argument must be module name");
72-
let module = module.to_str().expect("Not a valid module name");
72+
let module = module
73+
.to_str()
74+
.and_then(SupportedModule::try_from_opencv_name)
75+
.expect("Not a valid module name");
7376
let version = get_version_from_headers(&opencv_header_dir).expect("Can't find the version in the headers");
7477
let arg_additional_include_dirs = args.next();
7578
let additional_include_dirs = arg_additional_include_dirs

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ use std::rc::Rc;
88
use clang::{Entity, EntityKind};
99
use opencv_binding_generator::{
1010
opencv_module_from_path, Class, Constness, EntityExt, EntityWalkerExt, EntityWalkerVisitor, Func, Generator, GeneratorEnv,
11-
Pred,
11+
Pred, SupportedModule,
1212
};
1313

1414
struct FunctionFinder<'tu> {
15-
pub module: &'tu str,
15+
pub module: SupportedModule,
1616
pub gen_env: GeneratorEnv<'tu>,
1717
}
1818

@@ -72,7 +72,7 @@ fn main() {
7272
let opencv_header_dirs = args.map(PathBuf::from);
7373
// module -> usage_section -> (name, preds)
7474
let global_usage_tracking = Rc::new(RefCell::new(HashMap::<
75-
String,
75+
SupportedModule,
7676
HashMap<&'static str, HashSet<UsageTrackerOwned>>,
7777
>::new()));
7878
for opencv_header_dir in opencv_header_dirs {
@@ -85,26 +85,25 @@ fn main() {
8585
.filter(|p| p.is_file() && p.extension().is_some_and(|e| e == "hpp"))
8686
.filter_map(|mut p| {
8787
p.set_extension("");
88-
p.file_name().and_then(|f| f.to_str()).map(|f| f.to_string())
88+
p.file_name()
89+
.and_then(|f| f.to_str())
90+
.and_then(SupportedModule::try_from_opencv_name)
8991
});
9092
let gen = Generator::new(&opencv_header_dir, &[], &src_cpp_dir);
9193
for module in modules {
92-
println!(" {module}");
93-
gen.pre_process(&module, false, {
94+
println!(" {}", module.opencv_name());
95+
gen.pre_process(module, false, {
9496
let global_usage_tracking = Rc::clone(&global_usage_tracking);
9597
|root_entity| {
9698
let global_usage_tracking = global_usage_tracking; // force move
97-
let mut gen_env = GeneratorEnv::global(&module, root_entity);
99+
let mut gen_env = GeneratorEnv::global(module, root_entity);
98100
gen_env.settings.start_usage_tracking();
99-
let mut function_finder = FunctionFinder {
100-
module: &module,
101-
gen_env,
102-
};
101+
let mut function_finder = FunctionFinder { module, gen_env };
103102
root_entity.walk_opencv_entities(&mut function_finder);
104103

105104
let usage_tracking = function_finder.gen_env.settings.finish_usage_tracking();
106105
let mut global_usage_tracking = global_usage_tracking.borrow_mut();
107-
let module_usage_tracking = global_usage_tracking.entry(module.to_string()).or_default();
106+
let module_usage_tracking = global_usage_tracking.entry(module).or_default();
108107
for (usage_section, new_usage_tracking) in usage_tracking {
109108
let new_usage_tracking: HashSet<UsageTrackerOwned> = new_usage_tracking
110109
.into_iter()

binding-generator/src/class.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
111111
pub fn type_ref(&self) -> TypeRef<'tu, 'ge> {
112112
match self {
113113
&Self::Clang { entity, gen_env, .. } => TypeRef::new(entity.get_type().expect("Can't get class type"), gen_env),
114-
Self::Desc(desc) => TypeRef::guess(desc.cpp_fullname.as_ref(), Rc::clone(&desc.rust_module)),
114+
Self::Desc(desc) => TypeRef::guess(desc.cpp_fullname.as_ref(), desc.rust_module),
115115
}
116116
}
117117

@@ -421,7 +421,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
421421
let accessor_generator = move |fld: &Field<'tu, 'ge>| {
422422
let doc_comment = Rc::from(fld.doc_comment());
423423
let def_loc = fld.file_line_name().location;
424-
let rust_module = Rc::from(fld.rust_module());
424+
let rust_module = fld.rust_module();
425425
let mut fld_type_ref = fld.type_ref();
426426
let fld_refname = fld.cpp_name(CppNameStyle::Reference);
427427
if let Some(type_hint) = gen_env.settings.property_override.get(fld_refname.as_ref()) {
@@ -457,7 +457,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
457457
Constness::Const,
458458
return_kind,
459459
fld_declname,
460-
Rc::clone(&rust_module),
460+
rust_module,
461461
[],
462462
fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Const),
463463
)
@@ -476,7 +476,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
476476
Constness::Mut,
477477
return_kind,
478478
format!("{fld_declname}Mut"),
479-
Rc::clone(&rust_module),
479+
rust_module,
480480
[],
481481
fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Mut),
482482
)
@@ -497,7 +497,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
497497
fld_const,
498498
return_kind,
499499
fld_declname,
500-
Rc::clone(&rust_module),
500+
rust_module,
501501
[],
502502
fld_type_ref.as_ref().clone(),
503503
)
@@ -526,7 +526,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
526526
Constness::Mut,
527527
ReturnKind::InfallibleNaked,
528528
format!("set{first_letter}{rest}"),
529-
Rc::clone(&rust_module),
529+
rust_module,
530530
[Field::new_desc(FieldDesc {
531531
cpp_fullname: "val".into(),
532532
type_ref: fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Const),

binding-generator/src/class/desc.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::rc::Rc;
22

33
use super::{Class, ClassKind, TemplateKind};
44
use crate::element::ExcludeKind;
5+
use crate::SupportedModule;
56

67
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
78
pub struct ClassDesc<'tu, 'ge> {
@@ -12,11 +13,11 @@ pub struct ClassDesc<'tu, 'ge> {
1213
pub template_kind: TemplateKind<'tu, 'ge>,
1314
pub bases: Rc<[Class<'tu, 'ge>]>,
1415
pub cpp_fullname: Rc<str>,
15-
pub rust_module: Rc<str>,
16+
pub rust_module: SupportedModule,
1617
}
1718

1819
impl<'tu, 'ge> ClassDesc<'tu, 'ge> {
19-
pub fn boxed(cpp_refname: impl Into<Rc<str>>, rust_module: impl Into<Rc<str>>) -> Self {
20+
pub fn boxed(cpp_refname: impl Into<Rc<str>>, rust_module: SupportedModule) -> Self {
2021
Self {
2122
kind: ClassKind::Boxed,
2223
is_abstract: false,
@@ -25,18 +26,18 @@ impl<'tu, 'ge> ClassDesc<'tu, 'ge> {
2526
template_kind: TemplateKind::No,
2627
bases: Rc::new([]),
2728
cpp_fullname: cpp_refname.into(),
28-
rust_module: rust_module.into(),
29+
rust_module,
2930
}
3031
}
3132

32-
pub fn simple(cpp_refname: impl Into<Rc<str>>, rust_module: impl Into<Rc<str>>) -> Self {
33+
pub fn simple(cpp_refname: impl Into<Rc<str>>, rust_module: SupportedModule) -> Self {
3334
Self {
3435
kind: ClassKind::Simple,
3536
..Self::boxed(cpp_refname, rust_module)
3637
}
3738
}
3839

39-
pub fn system(cpp_refname: impl Into<Rc<str>>, rust_module: impl Into<Rc<str>>) -> Self {
40+
pub fn system(cpp_refname: impl Into<Rc<str>>, rust_module: SupportedModule) -> Self {
4041
Self {
4142
kind: ClassKind::System,
4243
..Self::boxed(cpp_refname, rust_module)
@@ -45,75 +46,75 @@ impl<'tu, 'ge> ClassDesc<'tu, 'ge> {
4546

4647
/// `cv::Scalar_`
4748
pub fn cv_scalar_() -> Class<'tu, 'ge> {
48-
Class::new_desc(Self::simple("cv::Scalar_", "core"))
49+
Class::new_desc(Self::simple("cv::Scalar_", SupportedModule::Core))
4950
}
5051

5152
/// `cv::Size_`
5253
pub fn cv_size_() -> Class<'tu, 'ge> {
53-
Class::new_desc(Self::simple("cv::Size_", "core"))
54+
Class::new_desc(Self::simple("cv::Size_", SupportedModule::Core))
5455
}
5556

5657
/// `cv::Point_`
5758
pub fn cv_point_() -> Class<'tu, 'ge> {
58-
Class::new_desc(Self::simple("cv::Point_", "core"))
59+
Class::new_desc(Self::simple("cv::Point_", SupportedModule::Core))
5960
}
6061

6162
/// `cv::Vec`
6263
pub fn cv_vec() -> Class<'tu, 'ge> {
63-
Class::new_desc(Self::simple("cv::Vec", "core"))
64+
Class::new_desc(Self::simple("cv::Vec", SupportedModule::Core))
6465
}
6566

6667
/// `cv::String`
6768
pub fn cv_string() -> Class<'tu, 'ge> {
68-
Class::new_desc(Self::system("cv::String", "core"))
69+
Class::new_desc(Self::system("cv::String", SupportedModule::Core))
6970
}
7071

7172
/// `std::String`
7273
pub fn std_string() -> Class<'tu, 'ge> {
73-
Class::new_desc(Self::system("std::string", "core"))
74+
Class::new_desc(Self::system("std::string", SupportedModule::Core))
7475
}
7576

7677
/// `cv::MatConstIterator`
7778
pub fn cv_matconstiterator() -> Class<'tu, 'ge> {
78-
Class::new_desc(Self::boxed("cv::MatConstIterator", "core"))
79+
Class::new_desc(Self::boxed("cv::MatConstIterator", SupportedModule::Core))
7980
}
8081

8182
/// `cv::Mat`
8283
pub fn cv_mat() -> Class<'tu, 'ge> {
83-
Class::new_desc(Self::boxed("cv::Mat", "core"))
84+
Class::new_desc(Self::boxed("cv::Mat", SupportedModule::Core))
8485
}
8586

8687
/// `cv::UMat`
8788
pub fn cv_umat() -> Class<'tu, 'ge> {
88-
Class::new_desc(Self::boxed("cv::UMat", "core"))
89+
Class::new_desc(Self::boxed("cv::UMat", SupportedModule::Core))
8990
}
9091

9192
pub fn cv_input_array() -> Class<'tu, 'ge> {
92-
Class::new_desc(Self::boxed("cv::_InputArray", "core"))
93+
Class::new_desc(Self::boxed("cv::_InputArray", SupportedModule::Core))
9394
}
9495

9596
/// `cv::_OutputArray`
9697
pub fn cv_output_array() -> Class<'tu, 'ge> {
97-
Class::new_desc(Self::boxed("cv::_OutputArray", "core"))
98+
Class::new_desc(Self::boxed("cv::_OutputArray", SupportedModule::Core))
9899
}
99100

100101
/// `cv::_InputOutputArray`
101102
pub fn cv_input_output_array() -> Class<'tu, 'ge> {
102-
Class::new_desc(Self::boxed("cv::_InputOutputArray", "core"))
103+
Class::new_desc(Self::boxed("cv::_InputOutputArray", SupportedModule::Core))
103104
}
104105

105106
/// `cv::Feature2D`
106107
pub fn cv_feature2d() -> Class<'tu, 'ge> {
107-
Class::new_desc(Self::boxed("cv::Feature2D", "features2d"))
108+
Class::new_desc(Self::boxed("cv::Feature2D", SupportedModule::Features2d))
108109
}
109110

110111
/// `cv::dnn::DictValue`
111112
pub fn cv_dnn_dict_value() -> Class<'tu, 'ge> {
112-
Class::new_desc(Self::boxed("cv::dnn::DictValue", "dnn"))
113+
Class::new_desc(Self::boxed("cv::dnn::DictValue", SupportedModule::Dnn))
113114
}
114115

115116
/// `cv::Feature2D`
116117
pub fn cv_keypoint() -> Class<'tu, 'ge> {
117-
Class::new_desc(Self::boxed("cv::KeyPoint", "core"))
118+
Class::new_desc(Self::boxed("cv::KeyPoint", SupportedModule::Core))
118119
}
119120
}

binding-generator/src/element.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::path::{Component, Path};
66
use clang::{Accessibility, Entity, EntityKind};
77

88
use crate::type_ref::CppNameStyle;
9-
use crate::{settings, IteratorExt, StrExt, StringExt};
9+
use crate::{settings, IteratorExt, StrExt, StringExt, SupportedModule};
1010

1111
pub const UNNAMED: &str = "unnamed";
1212

@@ -265,8 +265,9 @@ fn opencv_module_component(path: &Path) -> Option<&OsStr> {
265265
}
266266

267267
/// Return OpenCV module from the given path
268-
pub fn opencv_module_from_path(path: &Path) -> Option<&str> {
268+
pub fn opencv_module_from_path(path: &Path) -> Option<SupportedModule> {
269269
opencv_module_component(path)
270270
.and_then(|m| m.to_str())
271271
.and_then(|m| m.strip_suffix(".hpp").or(Some(m)))
272+
.and_then(SupportedModule::try_from_opencv_name)
272273
}

binding-generator/src/func.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
182182
return_kind: self.return_kind(),
183183
cpp_name,
184184
rust_custom_leafname: self.rust_custom_leafname().map(Rc::from),
185-
rust_module: self.rust_module().into(),
185+
rust_module: self.rust_module(),
186186
doc_comment,
187187
def_loc,
188188
rust_generic_decls: Rc::new([]),

binding-generator/src/func/desc.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::debug::DefinitionLocation;
66
use crate::field::Field;
77
use crate::func::ReturnKind;
88
use crate::type_ref::{Constness, TypeRef, TypeRefDesc};
9-
use crate::{Class, Func, FuncTypeHint};
9+
use crate::{Class, Func, FuncTypeHint, SupportedModule};
1010

1111
#[derive(Clone, Debug)]
1212
pub struct FuncDesc<'tu, 'ge> {
@@ -17,7 +17,7 @@ pub struct FuncDesc<'tu, 'ge> {
1717
pub return_kind: ReturnKind,
1818
pub cpp_name: Rc<str>,
1919
pub rust_custom_leafname: Option<Rc<str>>,
20-
pub rust_module: Rc<str>,
20+
pub rust_module: SupportedModule,
2121
pub doc_comment: Rc<str>,
2222
pub def_loc: DefinitionLocation,
2323
pub rust_generic_decls: Rc<[(String, String)]>,
@@ -35,7 +35,7 @@ impl<'tu, 'ge> FuncDesc<'tu, 'ge> {
3535
constness: Constness,
3636
return_kind: ReturnKind,
3737
cpp_name: impl Into<Rc<str>>,
38-
rust_module: impl Into<Rc<str>>,
38+
rust_module: SupportedModule,
3939
arguments: impl Into<Rc<[Field<'tu, 'ge>]>>,
4040
return_type_ref: TypeRef<'tu, 'ge>,
4141
) -> Self {
@@ -46,7 +46,7 @@ impl<'tu, 'ge> FuncDesc<'tu, 'ge> {
4646
return_kind,
4747
cpp_name: cpp_name.into(),
4848
rust_custom_leafname: None,
49-
rust_module: rust_module.into(),
49+
rust_module,
5050
doc_comment: "".into(),
5151
def_loc: DefinitionLocation::Generated,
5252
rust_generic_decls: Rc::new([]),
@@ -120,7 +120,7 @@ impl<'tu, 'ge> FuncDesc<'tu, 'ge> {
120120
Constness::Mut,
121121
ReturnKind::InfallibleNaked,
122122
"delete",
123-
"<unused>",
123+
SupportedModule::Core,
124124
[],
125125
TypeRefDesc::void(),
126126
)

binding-generator/src/func/func_matcher.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ mod test {
213213
use crate::func::{FuncDesc, FuncKind, ReturnKind};
214214
use crate::type_ref::{Constness, TypeRef, TypeRefDesc, TypeRefTypeHint};
215215
use crate::writer::rust_native::type_ref::Lifetime;
216-
use crate::Func;
216+
use crate::{Func, SupportedModule};
217217

218218
#[test]
219219
fn test_func_matcher() {
@@ -223,7 +223,7 @@ mod test {
223223
Constness::Mut,
224224
ReturnKind::Fallible,
225225
"_InputArray",
226-
"core",
226+
SupportedModule::Core,
227227
[
228228
Field::new_desc(FieldDesc::new(
229229
"vec",
@@ -301,7 +301,7 @@ mod test {
301301
Constness::Mut,
302302
ReturnKind::Fallible,
303303
"_InputArray",
304-
"core",
304+
SupportedModule::Core,
305305
[
306306
Field::new_desc(FieldDesc::new(
307307
"vec",

0 commit comments

Comments
 (0)