Skip to content

Commit 97d8643

Browse files
authored
allowing compiler.update for MSBuildToolchain (#19137)
* allowing compiler.update for MSBuildToolchain * fix breaking tests * fix unit tests
1 parent fac4b9b commit 97d8643

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

conan/tools/microsoft/toolchain.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
from jinja2 import Template
66

77
from conan.internal import check_duplicated_generator
8+
from conan.internal.api.detect.detect_vs import vs_installation_path
89
from conan.tools.build import build_jobs
910
from conan.tools.intel.intel_cc import IntelCC
1011
from conan.tools.microsoft.visual import VCVars, msvs_toolset, msvc_runtime_flag, \
11-
msvc_platform_from_arch
12+
msvc_platform_from_arch, vs_ide_version
1213
from conan.errors import ConanException
1314
from conan.internal.util.files import save, load
1415

1516

16-
class MSBuildToolchain(object):
17+
class MSBuildToolchain:
1718
"""
1819
MSBuildToolchain class generator
1920
"""
@@ -23,6 +24,9 @@ class MSBuildToolchain(object):
2324
_config_toolchain_props = textwrap.dedent("""\
2425
<?xml version="1.0" encoding="utf-8"?>
2526
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
27+
{% if toolset_version_full_path %}
28+
<Import Project="{{toolset_version_full_path}}" />
29+
{% endif %}
2630
<ItemDefinitionGroup>
2731
<ClCompile>
2832
<PreprocessorDefinitions>{{ defines }}%(PreprocessorDefinitions)</PreprocessorDefinitions>
@@ -76,6 +80,7 @@ def __init__(self, conanfile):
7680
#: setting, else, it'll be based on ``msvc`` version.
7781
self.toolset = msvs_toolset(conanfile)
7882
self.properties = {}
83+
self.toolset_version_full_path = _get_toolset_props(conanfile)
7984

8085
def _name_condition(self, settings):
8186
platform = msvc_platform_from_arch(settings.get_safe("arch"))
@@ -156,7 +161,8 @@ def format_macro(key, value):
156161
"compile_options": compile_options,
157162
"parallel": parallel,
158163
"properties": self.properties,
159-
"winsdk_version": winsdk_version
164+
"winsdk_version": winsdk_version,
165+
"toolset_version_full_path": self.toolset_version_full_path
160166
}
161167

162168
def _write_config_toolchain(self, config_filename):
@@ -217,3 +223,29 @@ def _get_extra_flags(self):
217223
exelinkflags = self._conanfile.conf.get("tools.build:exelinkflags", default=[], check_type=list)
218224
defines = self._conanfile.conf.get("tools.build:defines", default=[], check_type=list)
219225
return cxxflags, cflags, defines, sharedlinkflags, exelinkflags
226+
227+
228+
def _get_toolset_props(conanfile):
229+
msvc_update = conanfile.conf.get("tools.microsoft:msvc_update")
230+
compiler_update = msvc_update or conanfile.settings.get_safe("compiler.update")
231+
if compiler_update is None:
232+
return
233+
234+
vs_version = vs_ide_version(conanfile)
235+
if int(vs_version) <= 14:
236+
return
237+
vs_install_path = conanfile.conf.get("tools.microsoft.msbuild:installation_path")
238+
vs_path = vs_install_path or vs_installation_path(vs_version)
239+
if not vs_path or not os.path.isdir(vs_path):
240+
return
241+
242+
basebuild = os.path.normpath(os.path.join(vs_path, "VC/Auxiliary/Build"))
243+
# The equivalent of compiler 19.26 is toolset 14.26
244+
compiler_version = str(conanfile.settings.compiler.version)
245+
vcvars_ver = "14.{}{}".format(compiler_version[-1], compiler_update)
246+
for folder in os.listdir(basebuild):
247+
if not os.path.isdir(os.path.join(basebuild, folder)):
248+
continue
249+
if folder.startswith(vcvars_ver):
250+
result = folder
251+
return os.path.join(basebuild, result, f"Microsoft.VCToolsVersion.{result}.props")

test/functional/toolchains/microsoft/test_msbuildtoolchain.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,16 @@ def test_msbuildtoolchain_winsdk_version():
4848
# I have verified also opening VS IDE that the setting is correctly configured
4949
# because the test always run over vcvars that already activates it
5050
assert "amd64 - winsdk_version=10.0 - vcvars_ver=14.3" in client.out
51+
52+
53+
@pytest.mark.skipif(platform.system() != "Windows", reason="Requires Windows")
54+
def test_msbuildtoolchain_compiler_update():
55+
# It only works for update=8, because 19.38 is the compiler in Github actions!
56+
client = TestClient(path_with_spaces=False)
57+
client.run("new msbuild_lib -d name=hello -d version=0.1")
58+
# conantoolchain.props is already imported in the msbuild_exe tempalte
59+
client.run("create . -s arch=x86_64 -s compiler.version=193 -s compiler.update=8")
60+
# I have verified also opening VS IDE that the setting is correctly configured
61+
# because the test always run over vcvars that already activates it
62+
assert "amd64 - winsdk_version=None - vcvars_ver=14.38" in client.out
63+
assert "hello/0.1: _MSC_VER1938" in client.out

test/unittests/tools/microsoft/test_msbuild.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def test_msbuild_toolset():
5454
"os": ["Windows"],
5555
"arch": ["x86_64"]})
5656
conanfile = ConanFile(None)
57+
conanfile.conf = Conf()
5758
conanfile.settings = "os", "compiler", "build_type", "arch"
5859
conanfile.settings = settings
5960
conanfile.settings.build_type = "Release"
@@ -77,6 +78,7 @@ def test_msbuild_toolset():
7778
])
7879
def test_msbuild_toolset_for_intel_cc(mode, expected_toolset):
7980
conanfile = ConanFile()
81+
conanfile.conf = Conf()
8082
conanfile.settings = "os", "compiler", "build_type", "arch"
8183
conanfile.settings = Settings({"build_type": ["Release"],
8284
"compiler": {"intel-cc": {"version": ["2021.3"], "mode": [mode]},
@@ -277,4 +279,4 @@ def test_msbuildtoolchain_changing_flags_via_attributes():
277279
</ResourceCompile>"""
278280
assert expected_cl_compile in toolchain
279281
assert expected_link in toolchain
280-
assert expected_resource_compile in toolchain
282+
assert expected_resource_compile in toolchain

0 commit comments

Comments
 (0)