Repository: conan-io/conan-center-index
Issue: [package] libjpeg-turbo: lies to consumer about runtime

While creating a recipe for hdf4, and adding libjpeg-turbo as an optional replacement for libjpeg dependency, I've seen a warning about msvc runtime when linking my test_package against hdf4 and its dependencies.

Indeed jpeg-turbo's CMakeLists.txt overrides runtime if option WITH_CRT_DLL is off, which is the default value:
https://github.com/libjpeg-turbo/libjpeg-turbo/blob/166e34213e4f4e2363ce058a7bcc69fd03e38b76/CMakeLists.txt#L303

```cmake
if(MSVC)
  option(WITH_CRT_DLL
    "Link all ${CMAKE_PROJECT_NAME} libraries and executables with the C run-time DLL (msvcr*.dll) instead of the static C run-time library (libcmt*.lib.)  The default is to use the C run-time DLL only with the libraries and executables that need it."
    FALSE)
  if(NOT WITH_CRT_DLL)
    # Use the static C library for all build types
    foreach(var CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
      CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
      if(${var} MATCHES "/MD")
        string(REGEX REPLACE "/MD" "/MT" ${var} "${${var}}")
      endif()
    endforeach()
  endif()
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3 /wd4996")
endif()
```

Be adviced that, if shared, it is again overriden in https://github.com/libjpeg-turbo/libjpeg-turbo/blob/166e34213e4f4e2363ce058a7bcc69fd03e38b76/sharedlib/CMakeLists.txt#L12:

```cmake
if(MSVC)
  # Build all configurations against shared C library
  foreach(var CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
    CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
    if(${var} MATCHES "/MT")
      string(REGEX REPLACE "/MT" "/MD" ${var} "${${var}}")
    endif()
  endforeach()
endif()
```

Therefore: current recipe links static libjpeg-turbo with static runtime, and shared libjpeg-turbo with shared runtime whatever consumer's setting.

Changed files:
recipes/libjpeg-turbo/all/conanfile.py

diff --git a/recipes/libjpeg-turbo/all/conanfile.py b/recipes/libjpeg-turbo/all/conanfile.py
index d4ee90949..c4cd7dc1f 100644
--- a/recipes/libjpeg-turbo/all/conanfile.py
+++ b/recipes/libjpeg-turbo/all/conanfile.py
@@ -83,14 +83,23 @@ class LibjpegTurboConan(ConanFile):
         self._cmake.definitions["WITH_TURBOJPEG"] = self.options.turbojpeg
         self._cmake.definitions["WITH_JAVA"] = self.options.java
         self._cmake.definitions["WITH_12BIT"] = self.options.enable12bit
+        if self.settings.compiler == "Visual Studio":
+            self._cmake.definitions["WITH_CRT_DLL"] = True # avoid replacing /MD by /MT in compiler flags
         self._cmake.configure(build_folder=self._build_subfolder)
         return self._cmake
 
-    def build(self):
+    def _patch_sources(self):
         # use standard GNUInstallDirs.cmake - custom one is broken
         tools.replace_in_file(os.path.join(self._source_subfolder, "CMakeLists.txt"),
                               "include(cmakescripts/GNUInstallDirs.cmake)",
                               "include(GNUInstallDirs)")
+        # do not override /MT by /MD if shared
+        tools.replace_in_file(os.path.join(self._source_subfolder, "sharedlib", "CMakeLists.txt"),
+                              """string(REGEX REPLACE "/MT" "/MD" ${var} "${${var}}")""",
+                              "")
+
+    def build(self):
+        self._patch_sources()
         cmake = self._configure_cmake()
         cmake.build()