1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmGlobalVisualStudioVersionedGenerator.h"
11 #include <cmext/string_view>
13 #include "cmsys/FStream.hxx"
14 #include "cmsys/Glob.hxx"
15 #include "cmsys/RegularExpression.hxx"
17 #include "cmGlobalGenerator.h"
18 #include "cmGlobalGeneratorFactory.h"
19 #include "cmMakefile.h"
20 #include "cmMessageType.h"
21 #include "cmStateTypes.h"
22 #include "cmStringAlgorithms.h"
23 #include "cmSystemTools.h"
24 #include "cmVSSetupHelper.h"
27 #ifndef IMAGE_FILE_MACHINE_ARM64
28 # define IMAGE_FILE_MACHINE_ARM64 0xaa64 // ARM64 Little-Endian
31 static bool VSIsWow64()
34 return IsWow64Process(GetCurrentProcess(), &isWow64
) && isWow64
;
37 static bool VSIsArm64Host()
39 typedef BOOL(WINAPI
* CM_ISWOW64PROCESS2
)(
40 HANDLE hProcess
, USHORT
* pProcessMachine
, USHORT
* pNativeMachine
);
42 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
43 # define CM_VS_GCC_DIAGNOSTIC_PUSHED
44 # pragma GCC diagnostic push
45 # pragma GCC diagnostic ignored "-Wcast-function-type"
47 static const CM_ISWOW64PROCESS2 s_IsWow64Process2Impl
=
48 (CM_ISWOW64PROCESS2
)GetProcAddress(
49 GetModuleHandleW(L
"api-ms-win-core-wow64-l1-1-1.dll"),
51 #ifdef CM_VS_GCC_DIAGNOSTIC_PUSHED
52 # pragma GCC diagnostic pop
53 # undef CM_VS_GCC_DIAGNOSTIC_PUSHED
56 USHORT processMachine
;
59 return s_IsWow64Process2Impl
&&
60 s_IsWow64Process2Impl(GetCurrentProcess(), &processMachine
,
62 nativeMachine
== IMAGE_FILE_MACHINE_ARM64
;
65 static bool VSHasDotNETFrameworkArm64()
67 std::string dotNetArm64
;
68 return cmSystemTools::ReadRegistryValue(
69 R
"(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework;InstallRootArm64)",
70 dotNetArm64
, cmSystemTools::KeyWOW64_64
);
73 static bool VSIsWindows11OrGreater()
75 cmSystemTools::WindowsVersion
const windowsVersion
=
76 cmSystemTools::GetWindowsVersion();
77 return (windowsVersion
.dwMajorVersion
> 10 ||
78 (windowsVersion
.dwMajorVersion
== 10 &&
79 windowsVersion
.dwMinorVersion
> 0) ||
80 (windowsVersion
.dwMajorVersion
== 10 &&
81 windowsVersion
.dwMinorVersion
== 0 &&
82 windowsVersion
.dwBuildNumber
>= 22000));
85 static std::string
VSHostPlatformName()
87 if (VSIsArm64Host()) {
95 #elif defined(_M_IA64)
104 static std::string
VSHostArchitecture(
105 cmGlobalVisualStudioGenerator::VSVersion v
)
107 if (VSIsArm64Host()) {
108 return v
>= cmGlobalVisualStudioGenerator::VSVersion::VS17
? "ARM64" : "";
115 #elif defined(_M_IA64)
117 #elif defined(_WIN64)
124 static unsigned int VSVersionToMajor(
125 cmGlobalVisualStudioGenerator::VSVersion v
)
128 case cmGlobalVisualStudioGenerator::VSVersion::VS14
:
130 case cmGlobalVisualStudioGenerator::VSVersion::VS15
:
132 case cmGlobalVisualStudioGenerator::VSVersion::VS16
:
134 case cmGlobalVisualStudioGenerator::VSVersion::VS17
:
140 static const char* VSVersionToToolset(
141 cmGlobalVisualStudioGenerator::VSVersion v
)
144 case cmGlobalVisualStudioGenerator::VSVersion::VS14
:
146 case cmGlobalVisualStudioGenerator::VSVersion::VS15
:
148 case cmGlobalVisualStudioGenerator::VSVersion::VS16
:
150 case cmGlobalVisualStudioGenerator::VSVersion::VS17
:
156 static std::string
VSVersionToMajorString(
157 cmGlobalVisualStudioGenerator::VSVersion v
)
160 case cmGlobalVisualStudioGenerator::VSVersion::VS14
:
162 case cmGlobalVisualStudioGenerator::VSVersion::VS15
:
164 case cmGlobalVisualStudioGenerator::VSVersion::VS16
:
166 case cmGlobalVisualStudioGenerator::VSVersion::VS17
:
172 static const char* VSVersionToAndroidToolset(
173 cmGlobalVisualStudioGenerator::VSVersion v
)
176 case cmGlobalVisualStudioGenerator::VSVersion::VS14
:
178 case cmGlobalVisualStudioGenerator::VSVersion::VS15
:
179 case cmGlobalVisualStudioGenerator::VSVersion::VS16
:
180 case cmGlobalVisualStudioGenerator::VSVersion::VS17
:
186 static const char vs15generatorName
[] = "Visual Studio 15 2017";
188 // Map generator name without year to name with year.
189 static const char* cmVS15GenName(const std::string
& name
, std::string
& genName
)
191 if (strncmp(name
.c_str(), vs15generatorName
,
192 sizeof(vs15generatorName
) - 6) != 0) {
195 const char* p
= name
.c_str() + sizeof(vs15generatorName
) - 6;
196 if (cmHasLiteralPrefix(p
, " 2017")) {
199 genName
= cmStrCat(vs15generatorName
, p
);
203 class cmGlobalVisualStudioVersionedGenerator::Factory15
204 : public cmGlobalGeneratorFactory
207 std::unique_ptr
<cmGlobalGenerator
> CreateGlobalGenerator(
208 const std::string
& name
, bool allowArch
, cmake
* cm
) const override
211 const char* p
= cmVS15GenName(name
, genName
);
213 return std::unique_ptr
<cmGlobalGenerator
>();
216 return std::unique_ptr
<cmGlobalGenerator
>(
217 new cmGlobalVisualStudioVersionedGenerator(
218 cmGlobalVisualStudioGenerator::VSVersion::VS15
, cm
, genName
, ""));
220 if (!allowArch
|| *p
++ != ' ') {
221 return std::unique_ptr
<cmGlobalGenerator
>();
223 if (strcmp(p
, "Win64") == 0) {
224 return std::unique_ptr
<cmGlobalGenerator
>(
225 new cmGlobalVisualStudioVersionedGenerator(
226 cmGlobalVisualStudioGenerator::VSVersion::VS15
, cm
, genName
, "x64"));
228 if (strcmp(p
, "ARM") == 0) {
229 return std::unique_ptr
<cmGlobalGenerator
>(
230 new cmGlobalVisualStudioVersionedGenerator(
231 cmGlobalVisualStudioGenerator::VSVersion::VS15
, cm
, genName
, "ARM"));
233 return std::unique_ptr
<cmGlobalGenerator
>();
236 cmDocumentationEntry
GetDocumentation() const override
238 return { cmStrCat(vs15generatorName
, " [arch]"),
239 "Generates Visual Studio 2017 project files. "
240 "Optional [arch] can be \"Win64\" or \"ARM\"." };
243 std::vector
<std::string
> GetGeneratorNames() const override
245 std::vector
<std::string
> names
;
246 names
.push_back(vs15generatorName
);
250 std::vector
<std::string
> GetGeneratorNamesWithPlatform() const override
252 std::vector
<std::string
> names
;
253 names
.emplace_back(cmStrCat(vs15generatorName
, " ARM"));
254 names
.emplace_back(cmStrCat(vs15generatorName
, " Win64"));
258 bool SupportsToolset() const override
{ return true; }
259 bool SupportsPlatform() const override
{ return true; }
261 std::vector
<std::string
> GetKnownPlatforms() const override
263 std::vector
<std::string
> platforms
;
264 platforms
.emplace_back("x64");
265 platforms
.emplace_back("Win32");
266 platforms
.emplace_back("ARM");
267 platforms
.emplace_back("ARM64");
271 std::string
GetDefaultPlatformName() const override
{ return "Win32"; }
274 std::unique_ptr
<cmGlobalGeneratorFactory
>
275 cmGlobalVisualStudioVersionedGenerator::NewFactory15()
277 return std::unique_ptr
<cmGlobalGeneratorFactory
>(new Factory15
);
280 static const char vs16generatorName
[] = "Visual Studio 16 2019";
281 static const char vs17generatorName
[] = "Visual Studio 17 2022";
283 // Map generator name without year to name with year.
284 static const char* cmVS16GenName(const std::string
& name
, std::string
& genName
)
286 if (strncmp(name
.c_str(), vs16generatorName
,
287 sizeof(vs16generatorName
) - 6) != 0) {
290 const char* p
= name
.c_str() + sizeof(vs16generatorName
) - 6;
291 if (cmHasLiteralPrefix(p
, " 2019")) {
294 genName
= cmStrCat(vs16generatorName
, p
);
298 static const char* cmVS17GenName(const std::string
& name
, std::string
& genName
)
300 if (strncmp(name
.c_str(), vs17generatorName
,
301 sizeof(vs17generatorName
) - 6) != 0) {
304 const char* p
= name
.c_str() + sizeof(vs17generatorName
) - 6;
305 if (cmHasLiteralPrefix(p
, " 2022")) {
308 genName
= cmStrCat(vs17generatorName
, p
);
312 class cmGlobalVisualStudioVersionedGenerator::Factory16
313 : public cmGlobalGeneratorFactory
316 std::unique_ptr
<cmGlobalGenerator
> CreateGlobalGenerator(
317 const std::string
& name
, bool /*allowArch*/, cmake
* cm
) const override
320 const char* p
= cmVS16GenName(name
, genName
);
322 return std::unique_ptr
<cmGlobalGenerator
>();
325 return std::unique_ptr
<cmGlobalGenerator
>(
326 new cmGlobalVisualStudioVersionedGenerator(
327 cmGlobalVisualStudioGenerator::VSVersion::VS16
, cm
, genName
, ""));
329 return std::unique_ptr
<cmGlobalGenerator
>();
332 cmDocumentationEntry
GetDocumentation() const override
334 return { std::string(vs16generatorName
),
335 "Generates Visual Studio 2019 project files. "
336 "Use -A option to specify architecture." };
339 std::vector
<std::string
> GetGeneratorNames() const override
341 std::vector
<std::string
> names
;
342 names
.push_back(vs16generatorName
);
346 std::vector
<std::string
> GetGeneratorNamesWithPlatform() const override
348 return std::vector
<std::string
>();
351 bool SupportsToolset() const override
{ return true; }
352 bool SupportsPlatform() const override
{ return true; }
354 std::vector
<std::string
> GetKnownPlatforms() const override
356 std::vector
<std::string
> platforms
;
357 platforms
.emplace_back("x64");
358 platforms
.emplace_back("Win32");
359 platforms
.emplace_back("ARM");
360 platforms
.emplace_back("ARM64");
361 platforms
.emplace_back("ARM64EC");
365 std::string
GetDefaultPlatformName() const override
367 return VSHostPlatformName();
371 std::unique_ptr
<cmGlobalGeneratorFactory
>
372 cmGlobalVisualStudioVersionedGenerator::NewFactory16()
374 return std::unique_ptr
<cmGlobalGeneratorFactory
>(new Factory16
);
377 class cmGlobalVisualStudioVersionedGenerator::Factory17
378 : public cmGlobalGeneratorFactory
381 std::unique_ptr
<cmGlobalGenerator
> CreateGlobalGenerator(
382 const std::string
& name
, bool /*allowArch*/, cmake
* cm
) const override
385 const char* p
= cmVS17GenName(name
, genName
);
387 return std::unique_ptr
<cmGlobalGenerator
>();
390 return std::unique_ptr
<cmGlobalGenerator
>(
391 new cmGlobalVisualStudioVersionedGenerator(
392 cmGlobalVisualStudioGenerator::VSVersion::VS17
, cm
, genName
, ""));
394 return std::unique_ptr
<cmGlobalGenerator
>();
397 cmDocumentationEntry
GetDocumentation() const override
399 return { std::string(vs17generatorName
),
400 "Generates Visual Studio 2022 project files. "
401 "Use -A option to specify architecture." };
404 std::vector
<std::string
> GetGeneratorNames() const override
406 std::vector
<std::string
> names
;
407 names
.push_back(vs17generatorName
);
411 std::vector
<std::string
> GetGeneratorNamesWithPlatform() const override
413 return std::vector
<std::string
>();
416 bool SupportsToolset() const override
{ return true; }
417 bool SupportsPlatform() const override
{ return true; }
419 std::vector
<std::string
> GetKnownPlatforms() const override
421 std::vector
<std::string
> platforms
;
422 platforms
.emplace_back("x64");
423 platforms
.emplace_back("Win32");
424 platforms
.emplace_back("ARM");
425 platforms
.emplace_back("ARM64");
426 platforms
.emplace_back("ARM64EC");
430 std::string
GetDefaultPlatformName() const override
432 return VSHostPlatformName();
436 std::unique_ptr
<cmGlobalGeneratorFactory
>
437 cmGlobalVisualStudioVersionedGenerator::NewFactory17()
439 return std::unique_ptr
<cmGlobalGeneratorFactory
>(new Factory17
);
442 cmGlobalVisualStudioVersionedGenerator::cmGlobalVisualStudioVersionedGenerator(
443 VSVersion version
, cmake
* cm
, const std::string
& name
,
444 std::string
const& platformInGeneratorName
)
445 : cmGlobalVisualStudio14Generator(cm
, name
, platformInGeneratorName
)
446 , vsSetupAPIHelper(VSVersionToMajor(version
))
448 this->Version
= version
;
449 this->ExpressEdition
= false;
450 this->DefaultPlatformToolset
= VSVersionToToolset(this->Version
);
451 this->DefaultAndroidToolset
= VSVersionToAndroidToolset(this->Version
);
452 this->DefaultCLFlagTableName
= VSVersionToToolset(this->Version
);
453 this->DefaultCSharpFlagTableName
= VSVersionToToolset(this->Version
);
454 this->DefaultLinkFlagTableName
= VSVersionToToolset(this->Version
);
455 if (this->Version
>= cmGlobalVisualStudioGenerator::VSVersion::VS16
) {
456 this->DefaultPlatformName
= VSHostPlatformName();
457 this->DefaultPlatformToolsetHostArchitecture
=
458 VSHostArchitecture(this->Version
);
460 if (this->Version
>= cmGlobalVisualStudioGenerator::VSVersion::VS17
) {
461 // FIXME: Search for an existing framework? Under '%ProgramFiles(x86)%',
462 // see 'Reference Assemblies\Microsoft\Framework\.NETFramework'.
463 // Use a version installed by VS 2022 without a separate component.
464 this->DefaultTargetFrameworkVersion
= "v4.7.2";
468 bool cmGlobalVisualStudioVersionedGenerator::MatchesGeneratorName(
469 const std::string
& name
) const
472 switch (this->Version
) {
473 case cmGlobalVisualStudioGenerator::VSVersion::VS14
:
475 case cmGlobalVisualStudioGenerator::VSVersion::VS15
:
476 if (cmVS15GenName(name
, genName
)) {
477 return genName
== this->GetName();
480 case cmGlobalVisualStudioGenerator::VSVersion::VS16
:
481 if (cmVS16GenName(name
, genName
)) {
482 return genName
== this->GetName();
485 case cmGlobalVisualStudioGenerator::VSVersion::VS17
:
486 if (cmVS17GenName(name
, genName
)) {
487 return genName
== this->GetName();
494 bool cmGlobalVisualStudioVersionedGenerator::SetGeneratorInstance(
495 std::string
const& i
, cmMakefile
* mf
)
497 if (this->LastGeneratorInstanceString
&&
498 i
== *(this->LastGeneratorInstanceString
)) {
499 this->SetVSVersionVar(mf
);
503 if (!this->ParseGeneratorInstance(i
, mf
)) {
507 if (!this->GeneratorInstanceVersion
.empty()) {
508 std::string
const majorStr
= VSVersionToMajorString(this->Version
);
509 cmsys::RegularExpression
versionRegex(
510 cmStrCat('^', majorStr
, R
"(\.[0-9]+\.[0-9]+\.[0-9]+$)"));
511 if (!versionRegex
.find(this->GeneratorInstanceVersion
)) {
513 MessageType::FATAL_ERROR
,
514 cmStrCat("Generator\n"
518 "given instance specification\n"
522 "but the version field is not 4 integer components"
529 std::string vsInstance
;
532 if (!this->vsSetupAPIHelper
.SetVSInstance(
533 this->GeneratorInstance
, this->GeneratorInstanceVersion
)) {
534 std::ostringstream e
;
535 /* clang-format off */
538 " " << this->GetName() << "\n"
539 "could not find specified instance of Visual Studio:\n"
541 /* clang-format on */
542 if (!this->GeneratorInstance
.empty() &&
543 this->GeneratorInstanceVersion
.empty() &&
544 cmSystemTools::FileIsDirectory(this->GeneratorInstance
)) {
546 "The directory exists, but the instance is not known to the "
547 "Visual Studio Installer, and no 'version=' field was given.";
549 mf
->IssueMessage(MessageType::FATAL_ERROR
, e
.str());
552 } else if (!this->vsSetupAPIHelper
.GetVSInstanceInfo(vsInstance
)) {
554 MessageType::FATAL_ERROR
,
555 cmStrCat("Generator\n"
559 "could not find any instance of Visual Studio.\n"));
563 // Save the selected instance persistently.
564 std::string genInstance
= mf
->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
565 if (vsInstance
!= genInstance
) {
566 this->CMakeInstance
->AddCacheEntry("CMAKE_GENERATOR_INSTANCE", vsInstance
,
567 "Generator instance identifier.",
568 cmStateEnums::INTERNAL
);
571 this->SetVSVersionVar(mf
);
573 // The selected instance may have a different MSBuild than previously found.
574 this->MSBuildCommandInitialized
= false;
576 this->LastGeneratorInstanceString
= i
;
581 bool cmGlobalVisualStudioVersionedGenerator::ParseGeneratorInstance(
582 std::string
const& is
, cmMakefile
* mf
)
584 this->GeneratorInstance
.clear();
585 this->GeneratorInstanceVersion
.clear();
587 std::vector
<std::string
> const fields
= cmTokenize(is
, ",");
588 auto fi
= fields
.begin();
589 if (fi
== fields
.end()) {
593 // The first field may be the VS instance.
594 if (fi
->find('=') == fi
->npos
) {
595 this->GeneratorInstance
= *fi
;
599 std::set
<std::string
> handled
;
601 // The rest of the fields must be key=value pairs.
602 for (; fi
!= fields
.end(); ++fi
) {
603 std::string::size_type pos
= fi
->find('=');
604 if (pos
== fi
->npos
) {
606 MessageType::FATAL_ERROR
,
607 cmStrCat("Generator\n"
611 "given instance specification\n"
615 "that contains a field after the first ',' with no '='."));
618 std::string
const key
= fi
->substr(0, pos
);
619 std::string
const value
= fi
->substr(pos
+ 1);
620 if (!handled
.insert(key
).second
) {
621 mf
->IssueMessage(MessageType::FATAL_ERROR
,
622 cmStrCat("Generator\n"
626 "given instance specification\n"
630 "that contains duplicate field key '",
634 if (!this->ProcessGeneratorInstanceField(key
, value
)) {
635 mf
->IssueMessage(MessageType::FATAL_ERROR
,
636 cmStrCat("Generator\n"
640 "given instance specification\n"
644 "that contains invalid field '",
653 void cmGlobalVisualStudioVersionedGenerator::SetVSVersionVar(cmMakefile
* mf
)
655 if (cm::optional
<std::string
> vsVer
= this->GetVSInstanceVersion()) {
656 mf
->AddDefinition("CMAKE_VS_VERSION_BUILD_NUMBER", *vsVer
);
660 bool cmGlobalVisualStudioVersionedGenerator::ProcessGeneratorInstanceField(
661 std::string
const& key
, std::string
const& value
)
663 if (key
== "version"_s
) {
664 this->GeneratorInstanceVersion
= value
;
670 bool cmGlobalVisualStudioVersionedGenerator::GetVSInstance(
671 std::string
& dir
) const
673 return vsSetupAPIHelper
.GetVSInstanceInfo(dir
);
676 cm::optional
<std::string
>
677 cmGlobalVisualStudioVersionedGenerator::GetVSInstanceVersion() const
679 cm::optional
<std::string
> result
;
680 std::string vsInstanceVersion
;
681 if (vsSetupAPIHelper
.GetVSInstanceVersion(vsInstanceVersion
)) {
682 result
= vsInstanceVersion
;
687 bool cmGlobalVisualStudioVersionedGenerator::IsStdOutEncodingSupported() const
689 // Supported from Visual Studio 16.7 Preview 3.
690 if (this->Version
> cmGlobalVisualStudioGenerator::VSVersion::VS16
) {
693 if (this->Version
< cmGlobalVisualStudioGenerator::VSVersion::VS16
) {
696 static std::string
const vsVer16_7_P2
= "16.7.30128.36";
697 cm::optional
<std::string
> vsVer
= this->GetVSInstanceVersion();
699 cmSystemTools::VersionCompareGreaterEq(*vsVer
, vsVer16_7_P2
));
702 bool cmGlobalVisualStudioVersionedGenerator::IsUtf8EncodingSupported() const
704 // Supported from Visual Studio 16.10 Preview 2.
705 if (this->Version
> cmGlobalVisualStudioGenerator::VSVersion::VS16
) {
708 if (this->Version
< cmGlobalVisualStudioGenerator::VSVersion::VS16
) {
711 static std::string
const vsVer16_10_P2
= "16.10.31213.239";
712 cm::optional
<std::string
> vsVer
= this->GetVSInstanceVersion();
714 cmSystemTools::VersionCompareGreaterEq(*vsVer
, vsVer16_10_P2
));
717 bool cmGlobalVisualStudioVersionedGenerator::IsScanDependenciesSupported()
720 // Supported from Visual Studio 17.6 Preview 7.
721 if (this->Version
> cmGlobalVisualStudioGenerator::VSVersion::VS17
) {
724 if (this->Version
< cmGlobalVisualStudioGenerator::VSVersion::VS17
) {
727 static std::string
const vsVer17_6_P7
= "17.6.33706.43";
728 cm::optional
<std::string
> vsVer
= this->GetVSInstanceVersion();
730 cmSystemTools::VersionCompareGreaterEq(*vsVer
, vsVer17_6_P7
));
734 cmGlobalVisualStudioVersionedGenerator::GetAndroidApplicationTypeRevision()
737 switch (this->Version
) {
738 case cmGlobalVisualStudioGenerator::VSVersion::VS14
:
740 case cmGlobalVisualStudioGenerator::VSVersion::VS15
:
741 case cmGlobalVisualStudioGenerator::VSVersion::VS16
:
742 case cmGlobalVisualStudioGenerator::VSVersion::VS17
:
748 cmGlobalVisualStudioVersionedGenerator::AuxToolset
749 cmGlobalVisualStudioVersionedGenerator::FindAuxToolset(
750 std::string
& version
, std::string
& props
) const
752 if (version
.empty()) {
753 return AuxToolset::None
;
756 std::string instancePath
;
757 this->GetVSInstance(instancePath
);
758 cmSystemTools::ConvertToUnixSlashes(instancePath
);
760 // Translate three-component format accepted by "vcvarsall -vcvars_ver=".
761 cmsys::RegularExpression
threeComponentRegex(
762 "^([0-9]+\\.[0-9]+)\\.[0-9][0-9][0-9][0-9][0-9]$");
763 // The two-component format represents the two major components of the
764 // three-component format
765 cmsys::RegularExpression
twoComponentRegex("^([0-9]+\\.[0-9]+)$");
766 if (threeComponentRegex
.find(version
)) {
767 // Load "VC/Auxiliary/Build/*/Microsoft.VCToolsVersion.*.txt" files
768 // with two matching components to check their three-component version.
769 std::string
const& twoComponent
= threeComponentRegex
.match(1);
770 std::string pattern
=
771 cmStrCat(instancePath
, "/VC/Auxiliary/Build/"_s
, twoComponent
,
772 "*/Microsoft.VCToolsVersion."_s
, twoComponent
, "*.txt"_s
);
774 glob
.SetRecurseThroughSymlinks(false);
775 if (glob
.FindFiles(pattern
)) {
776 for (std::string
const& txt
: glob
.GetFiles()) {
778 cmsys::ifstream
fin(txt
.c_str());
779 if (fin
&& std::getline(fin
, ver
)) {
780 // Strip trailing whitespace.
781 ver
= ver
.substr(0, ver
.find_first_not_of("0123456789."));
782 // If the three-component version matches, translate it to
783 // that used by the "Microsoft.VCToolsVersion.*.txt" file name.
784 if (ver
== version
) {
785 cmsys::RegularExpression
extractVersion(
786 "VCToolsVersion\\.([0-9.]+)\\.txt$");
787 if (extractVersion
.find(txt
)) {
788 version
= extractVersion
.match(1);
795 } else if (twoComponentRegex
.find(version
)) {
796 std::string
const& twoComponent
= twoComponentRegex
.match(1);
797 std::string pattern
=
798 cmStrCat(instancePath
, "/VC/Auxiliary/Build/"_s
, twoComponent
,
799 "*/Microsoft.VCToolsVersion."_s
, twoComponent
, "*.txt"_s
);
801 glob
.SetRecurseThroughSymlinks(false);
802 if (glob
.FindFiles(pattern
) && !glob
.GetFiles().empty()) {
803 // Since we are only using the first two components of the
804 // toolset version, we require a single match.
805 if (glob
.GetFiles().size() == 1) {
806 std::string
const& txt
= glob
.GetFiles()[0];
808 cmsys::ifstream
fin(txt
.c_str());
809 if (fin
&& std::getline(fin
, ver
)) {
810 // Strip trailing whitespace.
811 ver
= ver
.substr(0, ver
.find_first_not_of("0123456789."));
812 // We assume the version is correct, since it is the only one that
814 cmsys::RegularExpression
extractVersion(
815 "VCToolsVersion\\.([0-9.]+)\\.txt$");
816 if (extractVersion
.find(txt
)) {
817 version
= extractVersion
.match(1);
821 props
= cmStrCat(instancePath
, "/VC/Auxiliary/Build/"_s
);
822 return AuxToolset::PropsIndeterminate
;
827 if (cmSystemTools::VersionCompareGreaterEq(version
, "14.20")) {
828 props
= cmStrCat(instancePath
, "/VC/Auxiliary/Build."_s
, version
,
829 "/Microsoft.VCToolsVersion."_s
, version
, ".props"_s
);
830 if (cmSystemTools::PathExists(props
)) {
831 return AuxToolset::PropsExist
;
834 props
= cmStrCat(instancePath
, "/VC/Auxiliary/Build/"_s
, version
,
835 "/Microsoft.VCToolsVersion."_s
, version
, ".props"_s
);
836 if (cmSystemTools::PathExists(props
)) {
837 return AuxToolset::PropsExist
;
840 // Accept the toolset version that is default in the current VS version
841 // by matching the name later VS versions will use for the SxS props files.
842 std::string vcToolsetVersion
;
843 if (this->vsSetupAPIHelper
.GetVCToolsetVersion(vcToolsetVersion
)) {
844 // Accept an exact-match (three-component version).
845 if (version
== vcToolsetVersion
) {
846 return AuxToolset::Default
;
849 // Accept known SxS props file names using four version components
850 // in VS versions later than the current.
851 if (version
== "14.28.16.9"_s
&& vcToolsetVersion
== "14.28.29910"_s
) {
852 return AuxToolset::Default
;
854 if (version
== "14.29.16.10"_s
&& vcToolsetVersion
== "14.29.30037"_s
) {
855 return AuxToolset::Default
;
857 if (version
== "14.29.16.11"_s
&& vcToolsetVersion
== "14.29.30133"_s
) {
858 return AuxToolset::Default
;
861 // The first two components of the default toolset version typically
862 // match the name used by later VS versions for the SxS props files.
863 cmsys::RegularExpression
twoComponent("^([0-9]+\\.[0-9]+)");
864 if (twoComponent
.find(version
)) {
865 std::string
const versionPrefix
= cmStrCat(twoComponent
.match(1), '.');
866 if (cmHasPrefix(vcToolsetVersion
, versionPrefix
)) {
867 return AuxToolset::Default
;
872 return AuxToolset::PropsMissing
;
875 bool cmGlobalVisualStudioVersionedGenerator::SelectWindowsStoreToolset(
876 std::string
& toolset
) const
878 if (cmHasLiteralPrefix(this->SystemVersion
, "10.0")) {
879 if (this->IsWindowsStoreToolsetInstalled() &&
880 this->IsWindowsDesktopToolsetInstalled()) {
881 toolset
= VSVersionToToolset(this->Version
);
886 return this->cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
890 bool cmGlobalVisualStudioVersionedGenerator::IsWindowsDesktopToolsetInstalled()
893 return vsSetupAPIHelper
.IsVSInstalled();
896 bool cmGlobalVisualStudioVersionedGenerator::IsWindowsStoreToolsetInstalled()
899 return vsSetupAPIHelper
.IsWin10SDKInstalled();
902 bool cmGlobalVisualStudioVersionedGenerator::IsWin81SDKInstalled() const
904 // Does the VS installer tool know about one?
905 if (vsSetupAPIHelper
.IsWin81SDKInstalled()) {
909 // Does the registry know about one (e.g. from VS 2015)?
910 std::string win81Root
;
911 if (cmSystemTools::ReadRegistryValue(
912 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
913 "Windows Kits\\Installed Roots;KitsRoot81",
914 win81Root
, cmSystemTools::KeyWOW64_32
) ||
915 cmSystemTools::ReadRegistryValue(
916 "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
917 "Windows Kits\\Installed Roots;KitsRoot81",
918 win81Root
, cmSystemTools::KeyWOW64_32
)) {
919 return cmSystemTools::FileExists(
920 cmStrCat(win81Root
, "/include/um/windows.h"), true);
926 cmGlobalVisualStudioVersionedGenerator::GetWindows10SDKMaxVersionDefault(
929 return std::string();
932 cm::optional
<std::string
>
933 cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommandEarly(cmMakefile
* mf
)
935 std::string instance
= mf
->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
936 if (!this->SetGeneratorInstance(instance
, mf
)) {
937 cmSystemTools::SetFatalErrorOccurred();
940 return this->cmGlobalVisualStudio14Generator::FindMSBuildCommandEarly(mf
);
943 std::string
cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommand()
947 // Ask Visual Studio Installer tool.
949 if (vsSetupAPIHelper
.GetVSInstanceInfo(vs
)) {
950 if (this->Version
>= cmGlobalVisualStudioGenerator::VSVersion::VS17
) {
951 if (VSIsArm64Host()) {
952 if (VSHasDotNETFrameworkArm64()) {
953 msbuild
= cmStrCat(vs
, "/MSBuild/Current/Bin/arm64/MSBuild.exe");
954 if (cmSystemTools::FileExists(msbuild
)) {
958 if (VSIsWindows11OrGreater()) {
959 msbuild
= cmStrCat(vs
, "/MSBuild/Current/Bin/amd64/MSBuild.exe");
960 if (cmSystemTools::FileExists(msbuild
)) {
965 msbuild
= cmStrCat(vs
, "/MSBuild/Current/Bin/amd64/MSBuild.exe");
966 if (cmSystemTools::FileExists(msbuild
)) {
971 msbuild
= cmStrCat(vs
, "/MSBuild/Current/Bin/MSBuild.exe");
972 if (cmSystemTools::FileExists(msbuild
)) {
975 msbuild
= cmStrCat(vs
, "/MSBuild/15.0/Bin/MSBuild.exe");
976 if (cmSystemTools::FileExists(msbuild
)) {
981 msbuild
= "MSBuild.exe";
985 std::string
cmGlobalVisualStudioVersionedGenerator::FindDevEnvCommand()
989 // Ask Visual Studio Installer tool.
991 if (vsSetupAPIHelper
.GetVSInstanceInfo(vs
)) {
992 devenv
= cmStrCat(vs
, "/Common7/IDE/devenv.com");
993 if (cmSystemTools::FileExists(devenv
)) {
998 devenv
= "devenv.com";