Make OpenCV modules available as crate features.
Make OpenCV modules available as crate features.
MODIFY
Cargo.toml
Cargo.toml
@@ -60,6 +60,86 @@ once_cell = "1.0"
[features]
clang-runtime = ["clang/runtime"]
docs-only = []
+default = ["all-modules"]
+
+# OpenCV modules
+all-modules = [
+ "alphamat", "aruco", "barcode", "bgsegm", "bioinspired", "calib3d", "ccalib", "cudaarithm",
+ "cudabgsegm", "cudacodec", "cudafeatures2d", "cudafilters", "cudaimgproc", "cudaobjdetect",
+ "cudaoptflow", "cudastereo", "cudawarping", "cvv", "dnn", "dnn_superres", "dpm", "face",
+ "features2d", "flann", "freetype", "fuzzy", "gapi", "hdf", "hfs", "highgui", "img_hash",
+ "imgcodecs", "imgproc", "intensity_transform", "line_descriptor", "mcc", "ml", "objdetect",
+ "optflow", "ovis", "phase_unwrapping", "photo", "plot", "quality", "rapid", "rgbd", "saliency",
+ "sfm", "shape", "stereo", "stitching", "structured_light", "superres", "surface_matching",
+ "text", "tracking", "video", "videoio", "videostab", "viz", "xfeatures2d", "ximgproc",
+ "xobjdetect", "xphoto", "wechat_qrcode",
+]
+
+alphamat = []
+aruco = []
+barcode = []
+bgsegm = []
+bioinspired = []
+calib3d = []
+ccalib = []
+cudaarithm = []
+cudabgsegm = []
+cudacodec = []
+cudafeatures2d = []
+cudafilters = []
+cudaimgproc = []
+cudaobjdetect = []
+cudaoptflow = []
+cudastereo = []
+cudawarping = []
+cvv = []
+dnn = []
+dnn_superres = []
+dpm = []
+face = []
+features2d = []
+flann = []
+freetype = []
+fuzzy = []
+gapi = []
+hdf = []
+hfs = []
+highgui = []
+img_hash = []
+imgcodecs = []
+imgproc = []
+intensity_transform = []
+line_descriptor = []
+mcc = []
+ml = []
+objdetect = []
+optflow = []
+ovis = []
+phase_unwrapping = []
+photo = []
+plot = []
+quality = []
+rapid = []
+rgbd = []
+saliency = []
+sfm = []
+shape = []
+stereo = []
+stitching = []
+structured_light = []
+superres = []
+surface_matching = []
+text = []
+tracking = []
+video = []
+videoio = []
+videostab = []
+viz = []
+xfeatures2d = []
+ximgproc = []
+xobjdetect = []
+xphoto = []
+wechat_qrcode = []
[build-dependencies]
binding-generator = { package = "opencv-binding-generator", version = "0.33.0", path = "binding-generator" }

MODIFY
README.md
README.md
@@ -217,10 +217,8 @@ The following variables are rarely used, but you might need them under some circ
* vcpkg
* `OPENCV_MODULE_WHITELIST` and `OPENCV_MODULE_BLACKLIST`
- Comma separated lists that affect modules that get their bindings generated. Setting whitelist will only
- generate the specified modules, setting blacklist will exclude the specified modules from generation. If the
- same module is specified in both list it will be excluded (i.e. blacklist has precedence). E.g.
- "core,dnn,features2d" .
+ Not used anymore. These used to be used to select modules that get their binding generated. We have switched to
+ using cargo features for module selection. Please see the section on features to learn how to switch.
The following variables affect the building the of the `opencv` crate, but belong to external components:
@@ -258,6 +256,10 @@ The following variables affect the building the of the `opencv` crate, but belon
* `clang-runtime` - enables the runtime detection of libclang (`runtime` feature of `clang-sys`). Useful as a
workaround for when your dependencies (like `bindgen`) pull in `clang-sys` with hard `runtime` feature.
* `docs-only` - internal usage, for building docs on [docs.rs](https://docs.rs/opencv)
+* `all-modules` - include all OpenCV modules. This is the default.
+* If you don't want to include all modules you can specify the module name as a feature. Make sure to include
+ all dependent modules, e.g.
+ `opencv = { version = ..., default-features = false, features = ["calib3d", "features2d", "flann"]}`.
## API details

MODIFY
build.rs
build.rs
@@ -35,7 +35,7 @@ static OPENCV_BRANCH_32: Lazy<VersionReq> = Lazy::new(|| VersionReq::parse("~3.2
static OPENCV_BRANCH_34: Lazy<VersionReq> = Lazy::new(|| VersionReq::parse("~3.4").expect("Can't parse OpenCV 3.4 version requirement"));
static OPENCV_BRANCH_4: Lazy<VersionReq> = Lazy::new(|| VersionReq::parse("~4").expect("Can't parse OpenCV 4 version requirement"));
-static ENV_VARS: [&str; 16] = [
+static ENV_VARS: [&str; 14] = [
"OPENCV_PACKAGE_NAME",
"OPENCV_PKGCONFIG_NAME",
"OPENCV_CMAKE_NAME",
@@ -45,8 +45,6 @@ static ENV_VARS: [&str; 16] = [
"OPENCV_LINK_PATHS",
"OPENCV_INCLUDE_PATHS",
"OPENCV_DISABLE_PROBES",
- "OPENCV_MODULE_WHITELIST",
- "OPENCV_MODULE_BLACKLIST",
"CMAKE_PREFIX_PATH",
"OpenCV_DIR",
"PKG_CONFIG_PATH",
@@ -148,29 +146,21 @@ fn make_modules(opencv_dir: &Path) -> Result<()> {
"opencv",
"opencv_modules",
].iter().copied().collect();
- let env_whitelist = env::var("OPENCV_MODULE_WHITELIST").ok();
- let env_whitelist = env_whitelist.as_ref()
- .map(|wl| wl.split(',')
- .map(|e| e.trim())
- .collect::<HashSet<_>>()
- );
- let env_blacklist = env::var("OPENCV_MODULE_BLACKLIST").ok();
- let env_blacklist = env_blacklist.as_ref()
- .map(|wl| wl.split(',')
- .map(|e| e.trim())
- .collect::<HashSet<_>>()
- );
+ let enable_modules = [
+ "core"
+ ].iter().map(ToString::to_string).chain(env::vars_os().filter_map(|(k, _)|
+ k.to_str().and_then(|s| s.strip_prefix("CARGO_FEATURE_").map(str::to_lowercase))
+ )).collect::<HashSet<_>>();
- let modules: Vec<String> = glob(&format!("{}/*.hpp", opencv_dir.to_str().ok_or("Can't OpenCV header directory to UTF-8 string")?))?
+ let modules = glob(&format!("{}/*.hpp", opencv_dir.to_str().ok_or("Can't OpenCV header directory to UTF-8 string")?))?
.filter_map(|entry| {
let entry = entry.expect("Can't get path for module file");
let module = entry.file_stem()
.and_then(OsStr::to_str).expect("Can't calculate file stem");
Some(module)
- .filter(|m| !ignore_modules.contains(m))
- .filter(|m| env_blacklist.as_ref().map_or(true, |bl| !bl.contains(m)))
- .filter(|m| env_whitelist.as_ref().map_or(true, |wl| wl.contains(m)))
+ .filter(|&m| !ignore_modules.contains(m))
+ .filter(|&m| enable_modules.contains(m))
.map(str::to_string)
})
.collect();

Make OpenCV modules available as crate features. (#298)

Make OpenCV modules available as crate features.