Skip to content

Commit 8370e34

Browse files
committed
Generate opencv branch condition macros automatically
1 parent 1721fc4 commit 8370e34

File tree

5 files changed

+62
-108
lines changed

5 files changed

+62
-108
lines changed
Lines changed: 11 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,5 @@
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.
9-
/// # Examples
102
///
11-
/// Alternative import:
12-
/// ```
13-
/// opencv::not_opencv_branch_34! {
14-
/// use opencv::imgproc::LINE_8;
15-
/// }
16-
/// opencv::opencv_branch_34! {
17-
/// use opencv::core::LINE_8;
18-
/// }
19-
/// ```
20-
///
21-
/// Alternative function call:
22-
/// ```
23-
/// opencv::opencv_branch_34! {
24-
/// let mut cam = opencv::videoio::VideoCapture::new_default(0)?;
25-
/// }
26-
/// opencv::not_opencv_branch_34! {
27-
/// let mut cam = opencv::videoio::VideoCapture::new(0, videoio::CAP_ANY)?;
28-
/// }
29-
/// ```
30-
#[cfg(not(ocvrs_opencv_branch_34))]
31-
#[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.
373
/// # Examples
384
///
395
/// Alternative import:
@@ -55,20 +21,14 @@ macro_rules! opencv_branch_34 {
5521
/// let mut cam = opencv::videoio::VideoCapture::new(0, videoio::CAP_ANY)?;
5622
/// }
5723
/// ```
58-
#[cfg(not(ocvrs_opencv_branch_34))]
24+
#[cfg(ocvrs_opencv_branch_OPENCV_BRANCH)]
5925
#[macro_export]
60-
macro_rules! not_opencv_branch_34 {
26+
macro_rules! opencv_branch_OPENCV_BRANCH {
6127
($($tt:tt)*) => { $($tt)* }
6228
}
6329

64-
#[cfg(ocvrs_opencv_branch_34)]
65-
#[doc(hidden)]
66-
#[macro_export]
67-
macro_rules! not_opencv_branch_34 {
68-
($($tt:tt)*) => {};
69-
}
70-
7130
/// Conditional compilation macro based on OpenCV branch version for usage in external crates.
31+
///
7232
/// # Examples
7333
///
7434
/// Alternative import:
@@ -90,27 +50,14 @@ macro_rules! not_opencv_branch_34 {
9050
/// let mut cam = opencv::videoio::VideoCapture::new(0, videoio::CAP_ANY)?;
9151
/// }
9252
/// ```
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)]
53+
#[cfg(not(ocvrs_opencv_branch_OPENCV_BRANCH))]
10154
#[macro_export]
102-
macro_rules! opencv_branch_4 {
55+
macro_rules! opencv_branch_OPENCV_BRANCH {
10356
($($tt:tt)*) => {};
10457
}
10558

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-
11359
/// Conditional compilation macro based on OpenCV branch version for usage in external crates.
60+
///
11461
/// # Examples
11562
///
11663
/// Alternative import:
@@ -132,55 +79,14 @@ macro_rules! not_opencv_branch_4 {
13279
/// let mut cam = opencv::videoio::VideoCapture::new(0, videoio::CAP_ANY)?;
13380
/// }
13481
/// ```
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)]
82+
#[cfg(not(ocvrs_opencv_branch_OPENCV_BRANCH))]
16483
#[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)]
178-
#[macro_export]
179-
macro_rules! not_opencv_branch_5 {
84+
macro_rules! not_opencv_branch_OPENCV_BRANCH {
18085
($($tt:tt)*) => { $($tt)* }
18186
}
18287

18388
/// Conditional compilation macro based on OpenCV branch version for usage in external crates.
89+
///
18490
/// # Examples
18591
///
18692
/// Alternative import:
@@ -202,8 +108,8 @@ macro_rules! not_opencv_branch_5 {
202108
/// let mut cam = opencv::videoio::VideoCapture::new(0, videoio::CAP_ANY)?;
203109
/// }
204110
/// ```
205-
#[cfg(ocvrs_opencv_branch_5)]
111+
#[cfg(ocvrs_opencv_branch_OPENCV_BRANCH)]
206112
#[macro_export]
207-
macro_rules! not_opencv_branch_5 {
113+
macro_rules! not_opencv_branch_OPENCV_BRANCH {
208114
($($tt:tt)*) => {};
209115
}

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)