Skip to content

Commit 72ca5bf

Browse files
committed
Generate opencv branch condition macros automatically
1 parent 1721fc4 commit 72ca5bf

File tree

5 files changed

+59
-109
lines changed

5 files changed

+59
-109
lines changed
Lines changed: 8 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
#[cfg(ocvrs_opencv_branch_34)]
2-
#[doc(hidden)]
3-
#[macro_export]
4-
macro_rules! opencv_branch_34 {
5-
($($tt:tt)*) => { $($tt)* }
6-
}
7-
81
/// Conditional compilation macro based on OpenCV branch version for usage in external crates.
92
/// # Examples
103
///
@@ -27,47 +20,12 @@ macro_rules! opencv_branch_34 {
2720
/// let mut cam = opencv::videoio::VideoCapture::new(0, videoio::CAP_ANY)?;
2821
/// }
2922
/// ```
30-
#[cfg(not(ocvrs_opencv_branch_34))]
23+
#[cfg(ocvrs_opencv_branch_OPENCV_BRANCH)]
3124
#[macro_export]
32-
macro_rules! opencv_branch_34 {
33-
($($tt:tt)*) => {};
34-
}
35-
36-
/// Conditional compilation macro based on OpenCV branch version for usage in external crates.
37-
/// # Examples
38-
///
39-
/// Alternative import:
40-
/// ```
41-
/// opencv::not_opencv_branch_34! {
42-
/// use opencv::imgproc::LINE_8;
43-
/// }
44-
/// opencv::opencv_branch_34! {
45-
/// use opencv::core::LINE_8;
46-
/// }
47-
/// ```
48-
///
49-
/// Alternative function call:
50-
/// ```
51-
/// opencv::opencv_branch_34! {
52-
/// let mut cam = opencv::videoio::VideoCapture::new_default(0)?;
53-
/// }
54-
/// opencv::not_opencv_branch_34! {
55-
/// let mut cam = opencv::videoio::VideoCapture::new(0, videoio::CAP_ANY)?;
56-
/// }
57-
/// ```
58-
#[cfg(not(ocvrs_opencv_branch_34))]
59-
#[macro_export]
60-
macro_rules! not_opencv_branch_34 {
25+
macro_rules! opencv_branch_OPENCV_BRANCH {
6126
($($tt:tt)*) => { $($tt)* }
6227
}
6328

64-
#[cfg(ocvrs_opencv_branch_34)]
65-
#[doc(hidden)]
66-
#[macro_export]
67-
macro_rules! not_opencv_branch_34 {
68-
($($tt:tt)*) => {};
69-
}
70-
7129
/// Conditional compilation macro based on OpenCV branch version for usage in external crates.
7230
/// # Examples
7331
///
@@ -90,26 +48,12 @@ macro_rules! not_opencv_branch_34 {
9048
/// let mut cam = opencv::videoio::VideoCapture::new(0, videoio::CAP_ANY)?;
9149
/// }
9250
/// ```
93-
#[cfg(ocvrs_opencv_branch_4)]
94-
#[macro_export]
95-
macro_rules! opencv_branch_4 {
96-
($($tt:tt)*) => { $($tt)* }
97-
}
98-
99-
#[cfg(not(ocvrs_opencv_branch_4))]
100-
#[doc(hidden)]
51+
#[cfg(not(ocvrs_opencv_branch_OPENCV_BRANCH))]
10152
#[macro_export]
102-
macro_rules! opencv_branch_4 {
53+
macro_rules! opencv_branch_OPENCV_BRANCH {
10354
($($tt:tt)*) => {};
10455
}
10556

106-
#[cfg(not(ocvrs_opencv_branch_4))]
107-
#[doc(hidden)]
108-
#[macro_export]
109-
macro_rules! not_opencv_branch_4 {
110-
($($tt:tt)*) => { $($tt)* }
111-
}
112-
11357
/// Conditional compilation macro based on OpenCV branch version for usage in external crates.
11458
/// # Examples
11559
///
@@ -132,51 +76,9 @@ macro_rules! not_opencv_branch_4 {
13276
/// let mut cam = opencv::videoio::VideoCapture::new(0, videoio::CAP_ANY)?;
13377
/// }
13478
/// ```
135-
#[cfg(ocvrs_opencv_branch_4)]
136-
#[macro_export]
137-
macro_rules! not_opencv_branch_4 {
138-
($($tt:tt)*) => {};
139-
}
140-
141-
/// Conditional compilation macro based on OpenCV branch version for usage in external crates.
142-
/// # Examples
143-
///
144-
/// Alternative import:
145-
/// ```
146-
/// opencv::opencv_branch_4! {
147-
/// use opencv::imgproc::LINE_8;
148-
/// }
149-
/// opencv::not_opencv_branch_4! {
150-
/// use opencv::core::LINE_8;
151-
/// }
152-
/// ```
153-
///
154-
/// Alternative function call:
155-
/// ```
156-
/// opencv::opencv_branch_34! {
157-
/// let mut cam = opencv::videoio::VideoCapture::new_default(0)?;
158-
/// }
159-
/// opencv::not_opencv_branch_34! {
160-
/// let mut cam = opencv::videoio::VideoCapture::new(0, videoio::CAP_ANY)?;
161-
/// }
162-
/// ```
163-
#[cfg(ocvrs_opencv_branch_5)]
164-
#[macro_export]
165-
macro_rules! opencv_branch_5 {
166-
($($tt:tt)*) => { $($tt)* }
167-
}
168-
169-
#[cfg(not(ocvrs_opencv_branch_5))]
170-
#[doc(hidden)]
171-
#[macro_export]
172-
macro_rules! opencv_branch_5 {
173-
($($tt:tt)*) => {};
174-
}
175-
176-
#[cfg(not(ocvrs_opencv_branch_5))]
177-
#[doc(hidden)]
79+
#[cfg(not(ocvrs_opencv_branch_OPENCV_BRANCH))]
17880
#[macro_export]
179-
macro_rules! not_opencv_branch_5 {
81+
macro_rules! not_opencv_branch_OPENCV_BRANCH {
18082
($($tt:tt)*) => { $($tt)* }
18183
}
18284

@@ -202,8 +104,8 @@ macro_rules! not_opencv_branch_5 {
202104
/// let mut cam = opencv::videoio::VideoCapture::new(0, videoio::CAP_ANY)?;
203105
/// }
204106
/// ```
205-
#[cfg(ocvrs_opencv_branch_5)]
107+
#[cfg(ocvrs_opencv_branch_OPENCV_BRANCH)]
206108
#[macro_export]
207-
macro_rules! not_opencv_branch_5 {
109+
macro_rules! not_opencv_branch_OPENCV_BRANCH {
208110
($($tt:tt)*) => {};
209111
}

build/generator.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
use std::collections::HashMap;
2+
use std::fs::File;
3+
use std::io::{BufWriter, Write};
24
use std::ops::Deref;
35
use std::path::{Path, PathBuf};
46
use std::process::Command;
57
use std::time::Instant;
6-
use std::{env, fs, thread};
8+
use std::{env, fs, io, thread};
79

810
use collector::Collector;
911
use opencv_binding_generator::{Generator, IteratorExt};
1012

1113
use super::docs::transfer_bindings_to_docs;
12-
use super::{files_with_predicate, Library, Result, OUT_DIR, SRC_CPP_DIR, SRC_DIR};
14+
use super::{files_with_predicate, Library, Result, OUT_DIR, SRC_CPP_DIR, SRC_DIR, SUPPORTED_OPENCV_BRANCHES};
1315

1416
#[path = "generator/collector.rs"]
1517
mod collector;
@@ -63,6 +65,7 @@ impl<'r> BindingGenerator<'r> {
6365
&OUT_DIR,
6466
)
6567
.collect_bindings()?;
68+
self.generate_opencv_branch_cond_macros()?;
6669

6770
if let Some(target_docs_dir) = target_docs_dir {
6871
if !target_docs_dir.exists() {
@@ -74,6 +77,16 @@ impl<'r> BindingGenerator<'r> {
7477
Ok(())
7578
}
7679

80+
fn generate_opencv_branch_cond_macros(&self) -> Result<()> {
81+
static COND_MACRO_TPL: &str = include_str!("cond_macros/opencv_branch.rs");
82+
83+
let mut cond_macros_file = BufWriter::new(File::create(OUT_DIR.join("opencv/cond_macros.rs"))?);
84+
for (_, branch) in SUPPORTED_OPENCV_BRANCHES {
85+
write_replace(COND_MACRO_TPL, "OPENCV_BRANCH", branch, &mut cond_macros_file)?;
86+
}
87+
Ok(())
88+
}
89+
7790
fn run(&self, opencv_header_dir: &Path, opencv: &Library) -> Result<()> {
7891
let additional_include_dirs = opencv
7992
.include_paths
@@ -201,3 +214,14 @@ impl Deref for Jobserver {
201214
&self.client
202215
}
203216
}
217+
218+
fn write_replace(mut content: &str, search: &str, replace: &str, mut to: impl Write) -> io::Result<()> {
219+
while let Some(search_idx) = content.find(search) {
220+
let (unchanged_content, new_content) = content.split_at(search_idx);
221+
to.write_all(unchanged_content.as_bytes())?;
222+
to.write_all(replace.as_bytes())?;
223+
content = &new_content[search.len()..];
224+
}
225+
to.write_all(content.as_bytes())?;
226+
Ok(())
227+
}

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,3 @@ pub mod mod_prelude {
5858
}
5959

6060
pub mod boxed_ref;
61-
mod cond_macros;

src/opencv.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@
2323
pub mod hub {
2424
include!(concat!(env!("OUT_DIR"), "/opencv/hub.rs"));
2525
}
26+
mod cond_macros {
27+
include!(concat!(env!("OUT_DIR"), "/opencv/cond_macros.rs"));
28+
}

tests/cond_macros.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use opencv::{
2+
not_opencv_branch_34, not_opencv_branch_4, not_opencv_branch_5, opencv_branch_34, opencv_branch_4, opencv_branch_5,
3+
opencv_has_module_core,
4+
};
5+
6+
#[test]
7+
fn test_opencv_branch_cond_macros() {
8+
opencv_branch_5! { let cond_macro_branch_5 = true; }
9+
not_opencv_branch_5! { let cond_macro_branch_5 = false; }
10+
let cfg_branch_5 = cfg!(ocvrs_opencv_branch_5);
11+
assert_eq!(cond_macro_branch_5, cfg_branch_5);
12+
13+
opencv_branch_4! { let cond_macro_branch_4 = true; }
14+
not_opencv_branch_4! { let cond_macro_branch_4 = false; }
15+
let cfg_branch_4 = cfg!(ocvrs_opencv_branch_4);
16+
assert_eq!(cond_macro_branch_4, cfg_branch_4);
17+
18+
opencv_branch_34! { let cond_macro_branch_34 = true; }
19+
not_opencv_branch_34! { let cond_macro_branch_34 = false; }
20+
let cfg_branch_34 = cfg!(ocvrs_opencv_branch_34);
21+
assert_eq!(cond_macro_branch_34, cfg_branch_34);
22+
}

0 commit comments

Comments
 (0)