check OpenCV version during build
MODIFY
Cargo.toml
Cargo.toml
@@ -27,6 +27,7 @@ cpp_build = "0.5.2"
glob = "0.3"
pkg-config = "0.3"
rayon = "1.0"
+semver = "0.9"
[dev-dependencies]
matches = "0.1"
@@ -35,7 +36,7 @@ matches = "0.1"
default = ["opencv_34"]
buildtime_bindgen = []
opencv_34 = []
-opencv_41 = [] # not useful yet
+opencv_41 = []
[package.metadata.docs.rs]
rustc-args = [ "--cfg feature=\"docs-only\"" ]

MODIFY
build.rs
build.rs
use std::{
- env,
collections::HashSet,
+ env,
error::Error,
ffi::OsString,
fs::{self, File, OpenOptions},
@@ -11,6 +11,7 @@ use std::{
};
use rayon::prelude::*;
+use semver::{Version, VersionReq};
use glob::glob;
@@ -23,6 +24,16 @@ fn link_wrapper() -> Result<pkg_config::Library, Box<dyn Error>> {
panic!("package opencv is not found by pkg-config")
};
+ if cfg!(feature = "opencv_34") {
+ if !VersionReq::parse("~3.4")?.matches(&Version::parse(&opencv.version)?) {
+ panic!("OpenCV version from pkg-config: {} must be from 3.4 branch because of the feature: opencv_34", opencv.version);
+ }
+ } else if cfg!(feature = "opencv_4") {
+ if !VersionReq::parse("~4.1")?.matches(&Version::parse(&opencv.version)?) {
+ panic!("OpenCV version from pkg-config: {} must be from 4.1 branch because of the feature: opencv_41", opencv.version);
+ }
+ }
+
// add 3rdparty lib dit. pkgconfig forgets it somehow.
let third_party_dir1 = format!("{}/share/OpenCV/3rdparty/lib", pkg_config::get_variable(pkg_name, "prefix")?);
println!("cargo:rustc-link-search=native={}", third_party_dir1);

check OpenCV version during build