55from jinja2 import Template
66
77from conan .internal import check_duplicated_generator
8+ from conan .internal .api .detect .detect_vs import vs_installation_path
89from conan .tools .build import build_jobs
910from conan .tools .intel .intel_cc import IntelCC
1011from conan .tools .microsoft .visual import VCVars , msvs_toolset , msvc_runtime_flag , \
11- msvc_platform_from_arch
12+ msvc_platform_from_arch , vs_ide_version
1213from conan .errors import ConanException
1314from 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" )
0 commit comments