Skip to content

Commit f8b9f38

Browse files
committed
Add the way to select MSVC CRT library
1 parent e008501 commit f8b9f38

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ The following variables are rarely used, but you might need them under some circ
281281
cmake (cmake related environment variables are applicable with this probe)
282282
* vcpkg
283283

284+
* `OPENCV_MSVC_CRT`
285+
Allows selecting the CRT library when building with MSVC for Windows. Allowed values are `"static"` for `/MT`
286+
and `"dynamic"` for `/MD`.
287+
284288
The following variables affect the building the of the `opencv` crate, but belong to external components:
285289

286290
* `PKG_CONFIG_PATH`

build.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static OPENCV_BRANCH_4: Lazy<VersionReq> =
5151

5252
/// Environment vars that affect the build, the source will be rebuilt if those change, the contents of those vars will also
5353
/// be present in the debug log
54-
static AFFECTING_ENV_VARS: [&str; 17] = [
54+
static AFFECTING_ENV_VARS: [&str; 18] = [
5555
"OPENCV_PACKAGE_NAME",
5656
"OPENCV_PKGCONFIG_NAME",
5757
"OPENCV_CMAKE_NAME",
@@ -61,6 +61,7 @@ static AFFECTING_ENV_VARS: [&str; 17] = [
6161
"OPENCV_LINK_PATHS",
6262
"OPENCV_INCLUDE_PATHS",
6363
"OPENCV_DISABLE_PROBES",
64+
"OPENCV_MSVC_CRT",
6465
"CMAKE_PREFIX_PATH",
6566
"OpenCV_DIR",
6667
"PKG_CONFIG_PATH",
@@ -246,6 +247,15 @@ fn build_compiler(opencv: &Library) -> cc::Build {
246247

247248
if *TARGET_ENV_MSVC {
248249
out.flag_if_supported("-std=c++14"); // clang says error: 'auto' return without trailing return type; deduced return types are a C++14 extension
250+
if let Ok(crt) = env::var("OPENCV_MSVC_CRT") {
251+
if crt.trim().to_lowercase() == "dynamic" {
252+
out.static_crt(false);
253+
} else if crt.trim().to_lowercase() == "static" {
254+
out.static_crt(true);
255+
} else {
256+
panic!("Invalid value of OPENCV_MSVC_CRT var, expected \"static\" or \"dynamic\"");
257+
}
258+
}
249259
}
250260
if out.get_compiler().is_like_msvc() {
251261
out.flag("-std:c++latest")

0 commit comments

Comments
 (0)