Skip to content

Commit

Permalink
Fix RuntimeLibrary deduced from profile in MSBuildToolchain (#17100)
Browse files Browse the repository at this point in the history
* fix runtime library deduced from profile in MSBuildToolchain

- take into account compiler.runtime_type instead of build_type (and fallback to build_type only if runtime_type is not set)
- properly compute runtime_library regardless of compiler (logic was broken for clang-cl for exemple)

* pass conanfile to msvc_runtime_flag

* test effect of compiler.runtime_type in props file generated by MSBuildToolchain

* formatting

* also test ClangCL in integration test of MSBuildToolchain
  • Loading branch information
SpaceIm authored Oct 14, 2024
1 parent f9ced79 commit 2aa8e08
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
29 changes: 9 additions & 20 deletions conan/tools/microsoft/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from conan.internal import check_duplicated_generator
from conan.tools.build import build_jobs
from conan.tools.intel.intel_cc import IntelCC
from conan.tools.microsoft.visual import VCVars, msvs_toolset
from conan.tools.microsoft.visual import VCVars, msvs_toolset, msvc_runtime_flag
from conan.errors import ConanException
from conans.util.files import save, load

Expand Down Expand Up @@ -67,7 +67,7 @@ def __init__(self, conanfile):
#: The build type. By default, the ``conanfile.settings.build_type`` value
self.configuration = conanfile.settings.build_type
#: The runtime flag. By default, it'll be based on the `compiler.runtime` setting.
self.runtime_library = self._runtime_library(conanfile.settings)
self.runtime_library = self._runtime_library()
#: cppstd value. By default, ``compiler.cppstd`` one.
self.cppstd = conanfile.settings.get_safe("compiler.cppstd")
self.cstd = conanfile.settings.get_safe("compiler.cstd")
Expand Down Expand Up @@ -107,24 +107,13 @@ def generate(self):
else:
VCVars(self._conanfile).generate()

@staticmethod
def _runtime_library(settings):
compiler = settings.compiler
runtime = settings.get_safe("compiler.runtime")
if compiler == "msvc" or compiler == "intel-cc":
build_type = settings.get_safe("build_type")
if build_type != "Debug":
runtime_library = {"static": "MultiThreaded",
"dynamic": "MultiThreadedDLL"}.get(runtime, "")
else:
runtime_library = {"static": "MultiThreadedDebug",
"dynamic": "MultiThreadedDebugDLL"}.get(runtime, "")
else:
runtime_library = {"MT": "MultiThreaded",
"MTd": "MultiThreadedDebug",
"MD": "MultiThreadedDLL",
"MDd": "MultiThreadedDebugDLL"}.get(runtime, "")
return runtime_library
def _runtime_library(self):
return {
"MT": "MultiThreaded",
"MTd": "MultiThreadedDebug",
"MD": "MultiThreadedDLL",
"MDd": "MultiThreadedDebugDLL",
}.get(msvc_runtime_flag(self._conanfile), "")

@property
def context_config_toolchain(self):
Expand Down
41 changes: 29 additions & 12 deletions test/integration/toolchains/microsoft/test_msbuild_toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@
import textwrap

import pytest
from parameterized import parameterized

from conan.test.utils.tools import TestClient


@parameterized.expand([("msvc", "190", "dynamic"),
("msvc", "191", "static")]
)
@pytest.mark.tool("visual_studio")
@pytest.mark.skipif(platform.system() != "Windows", reason="Only for windows")
def test_toolchain_win(compiler, version, runtime):
@pytest.mark.tool("visual_studio")
@pytest.mark.tool("clang", "16")
@pytest.mark.parametrize(
"compiler, version",
[
("msvc", "190"),
("msvc", "191"),
("clang", "16")
],
)
@pytest.mark.parametrize("runtime", ["dynamic", "static"])
@pytest.mark.parametrize("runtime_type", ["Release", "Debug"])
def test_toolchain_win(compiler, version, runtime, runtime_type):
client = TestClient(path_with_spaces=False)
settings = {"compiler": compiler,
"compiler.version": version,
"compiler.cppstd": "14",
"compiler.runtime": runtime,
"compiler.runtime_type": runtime_type,
"build_type": "Release",
"arch": "x86_64"}

Expand All @@ -39,11 +47,20 @@ def generate(self):
props = client.load("conantoolchain_release_x64.props")
assert "<IncludeExternals>true</IncludeExternals>" in props
assert "<LanguageStandard>stdcpp14</LanguageStandard>" in props
if version == "190":
assert "<PlatformToolset>v140</PlatformToolset>" in props
else:
assert "<PlatformToolset>v141</PlatformToolset>" in props
if compiler == "msvc":
if version == "190":
assert "<PlatformToolset>v140</PlatformToolset>" in props
elif version == "191":
assert "<PlatformToolset>v141</PlatformToolset>" in props
elif compiler == "clang":
assert "<PlatformToolset>ClangCl</PlatformToolset>" in props
if runtime == "dynamic":
assert "<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>" in props
if runtime_type == "Release":
assert "<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>" in props
else:
assert "<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>" in props
else:
assert "<RuntimeLibrary>MultiThreaded</RuntimeLibrary>" in props
if runtime_type == "Release":
assert "<RuntimeLibrary>MultiThreaded</RuntimeLibrary>" in props
else:
assert "<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>" in props

0 comments on commit 2aa8e08

Please sign in to comment.