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
!= nullptr &&
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::VS12
:
130 case cmGlobalVisualStudioGenerator::VSVersion::VS14
:
132 case cmGlobalVisualStudioGenerator::VSVersion::VS15
:
134 case cmGlobalVisualStudioGenerator::VSVersion::VS16
:
136 case cmGlobalVisualStudioGenerator::VSVersion::VS17
:
142 static const char* VSVersionToToolset(
143 cmGlobalVisualStudioGenerator::VSVersion v
)
146 case cmGlobalVisualStudioGenerator::VSVersion::VS12
:
148 case cmGlobalVisualStudioGenerator::VSVersion::VS14
:
150 case cmGlobalVisualStudioGenerator::VSVersion::VS15
:
152 case cmGlobalVisualStudioGenerator::VSVersion::VS16
:
154 case cmGlobalVisualStudioGenerator::VSVersion::VS17
:
160 static std::string
VSVersionToMajorString(
161 cmGlobalVisualStudioGenerator::VSVersion v
)
164 case cmGlobalVisualStudioGenerator::VSVersion::VS12
:
166 case cmGlobalVisualStudioGenerator::VSVersion::VS14
:
168 case cmGlobalVisualStudioGenerator::VSVersion::VS15
:
170 case cmGlobalVisualStudioGenerator::VSVersion::VS16
:
172 case cmGlobalVisualStudioGenerator::VSVersion::VS17
:
178 static const char* VSVersionToAndroidToolset(
179 cmGlobalVisualStudioGenerator::VSVersion v
)
182 case cmGlobalVisualStudioGenerator::VSVersion::VS12
:
184 case cmGlobalVisualStudioGenerator::VSVersion::VS14
:
186 case cmGlobalVisualStudioGenerator::VSVersion::VS15
:
187 case cmGlobalVisualStudioGenerator::VSVersion::VS16
:
188 case cmGlobalVisualStudioGenerator::VSVersion::VS17
:
194 static const char vs15generatorName
[] = "Visual Studio 15 2017";
196 // Map generator name without year to name with year.
197 static const char* cmVS15GenName(const std::string
& name
, std::string
& genName
)
199 if (strncmp(name
.c_str(), vs15generatorName
,
200 sizeof(vs15generatorName
) - 6) != 0) {
203 const char* p
= name
.c_str() + sizeof(vs15generatorName
) - 6;
204 if (cmHasLiteralPrefix(p
, " 2017")) {
207 genName
= cmStrCat(vs15generatorName
, p
);
211 class cmGlobalVisualStudioVersionedGenerator::Factory15
212 : public cmGlobalGeneratorFactory
215 std::unique_ptr
<cmGlobalGenerator
> CreateGlobalGenerator(
216 const std::string
& name
, bool allowArch
, cmake
* cm
) const override
219 const char* p
= cmVS15GenName(name
, genName
);
221 return std::unique_ptr
<cmGlobalGenerator
>();
224 return std::unique_ptr
<cmGlobalGenerator
>(
225 new cmGlobalVisualStudioVersionedGenerator(
226 cmGlobalVisualStudioGenerator::VSVersion::VS15
, cm
, genName
, ""));
228 if (!allowArch
|| *p
++ != ' ') {
229 return std::unique_ptr
<cmGlobalGenerator
>();
231 if (strcmp(p
, "Win64") == 0) {
232 return std::unique_ptr
<cmGlobalGenerator
>(
233 new cmGlobalVisualStudioVersionedGenerator(
234 cmGlobalVisualStudioGenerator::VSVersion::VS15
, cm
, genName
, "x64"));
236 if (strcmp(p
, "ARM") == 0) {
237 return std::unique_ptr
<cmGlobalGenerator
>(
238 new cmGlobalVisualStudioVersionedGenerator(
239 cmGlobalVisualStudioGenerator::VSVersion::VS15
, cm
, genName
, "ARM"));
241 return std::unique_ptr
<cmGlobalGenerator
>();
244 cmDocumentationEntry
GetDocumentation() const override
246 return { cmStrCat(vs15generatorName
, " [arch]"),
247 "Generates Visual Studio 2017 project files. "
248 "Optional [arch] can be \"Win64\" or \"ARM\"." };
251 std::vector
<std::string
> GetGeneratorNames() const override
253 std::vector
<std::string
> names
;
254 names
.push_back(vs15generatorName
);
258 std::vector
<std::string
> GetGeneratorNamesWithPlatform() const override
260 std::vector
<std::string
> names
;
261 names
.emplace_back(cmStrCat(vs15generatorName
, " ARM"));
262 names
.emplace_back(cmStrCat(vs15generatorName
, " Win64"));
266 bool SupportsToolset() const override
{ return true; }
267 bool SupportsPlatform() const override
{ return true; }
269 std::vector
<std::string
> GetKnownPlatforms() const override
271 std::vector
<std::string
> platforms
;
272 platforms
.emplace_back("x64");
273 platforms
.emplace_back("Win32");
274 platforms
.emplace_back("ARM");
275 platforms
.emplace_back("ARM64");
279 std::string
GetDefaultPlatformName() const override
{ return "Win32"; }
282 std::unique_ptr
<cmGlobalGeneratorFactory
>
283 cmGlobalVisualStudioVersionedGenerator::NewFactory15()
285 return std::unique_ptr
<cmGlobalGeneratorFactory
>(new Factory15
);
288 static const char vs16generatorName
[] = "Visual Studio 16 2019";
289 static const char vs17generatorName
[] = "Visual Studio 17 2022";
291 // Map generator name without year to name with year.
292 static const char* cmVS16GenName(const std::string
& name
, std::string
& genName
)
294 if (strncmp(name
.c_str(), vs16generatorName
,
295 sizeof(vs16generatorName
) - 6) != 0) {
298 const char* p
= name
.c_str() + sizeof(vs16generatorName
) - 6;
299 if (cmHasLiteralPrefix(p
, " 2019")) {
302 genName
= cmStrCat(vs16generatorName
, p
);
306 static const char* cmVS17GenName(const std::string
& name
, std::string
& genName
)
308 if (strncmp(name
.c_str(), vs17generatorName
,
309 sizeof(vs17generatorName
) - 6) != 0) {
312 const char* p
= name
.c_str() + sizeof(vs17generatorName
) - 6;
313 if (cmHasLiteralPrefix(p
, " 2022")) {
316 genName
= cmStrCat(vs17generatorName
, p
);
320 class cmGlobalVisualStudioVersionedGenerator::Factory16
321 : public cmGlobalGeneratorFactory
324 std::unique_ptr
<cmGlobalGenerator
> CreateGlobalGenerator(
325 const std::string
& name
, bool /*allowArch*/, cmake
* cm
) const override
328 const char* p
= cmVS16GenName(name
, genName
);
330 return std::unique_ptr
<cmGlobalGenerator
>();
333 return std::unique_ptr
<cmGlobalGenerator
>(
334 new cmGlobalVisualStudioVersionedGenerator(
335 cmGlobalVisualStudioGenerator::VSVersion::VS16
, cm
, genName
, ""));
337 return std::unique_ptr
<cmGlobalGenerator
>();
340 cmDocumentationEntry
GetDocumentation() const override
342 return { std::string(vs16generatorName
),
343 "Generates Visual Studio 2019 project files. "
344 "Use -A option to specify architecture." };
347 std::vector
<std::string
> GetGeneratorNames() const override
349 std::vector
<std::string
> names
;
350 names
.push_back(vs16generatorName
);
354 std::vector
<std::string
> GetGeneratorNamesWithPlatform() const override
356 return std::vector
<std::string
>();
359 bool SupportsToolset() const override
{ return true; }
360 bool SupportsPlatform() const override
{ return true; }
362 std::vector
<std::string
> GetKnownPlatforms() const override
364 std::vector
<std::string
> platforms
;
365 platforms
.emplace_back("x64");
366 platforms
.emplace_back("Win32");
367 platforms
.emplace_back("ARM");
368 platforms
.emplace_back("ARM64");
369 platforms
.emplace_back("ARM64EC");
373 std::string
GetDefaultPlatformName() const override
375 return VSHostPlatformName();
379 std::unique_ptr
<cmGlobalGeneratorFactory
>
380 cmGlobalVisualStudioVersionedGenerator::NewFactory16()
382 return std::unique_ptr
<cmGlobalGeneratorFactory
>(new Factory16
);
385 class cmGlobalVisualStudioVersionedGenerator::Factory17
386 : public cmGlobalGeneratorFactory
389 std::unique_ptr
<cmGlobalGenerator
> CreateGlobalGenerator(
390 const std::string
& name
, bool /*allowArch*/, cmake
* cm
) const override
393 const char* p
= cmVS17GenName(name
, genName
);
395 return std::unique_ptr
<cmGlobalGenerator
>();
398 return std::unique_ptr
<cmGlobalGenerator
>(
399 new cmGlobalVisualStudioVersionedGenerator(
400 cmGlobalVisualStudioGenerator::VSVersion::VS17
, cm
, genName
, ""));
402 return std::unique_ptr
<cmGlobalGenerator
>();
405 cmDocumentationEntry
GetDocumentation() const override
407 return { std::string(vs17generatorName
),
408 "Generates Visual Studio 2022 project files. "
409 "Use -A option to specify architecture." };
412 std::vector
<std::string
> GetGeneratorNames() const override
414 std::vector
<std::string
> names
;
415 names
.push_back(vs17generatorName
);
419 std::vector
<std::string
> GetGeneratorNamesWithPlatform() const override
421 return std::vector
<std::string
>();
424 bool SupportsToolset() const override
{ return true; }
425 bool SupportsPlatform() const override
{ return true; }
427 std::vector
<std::string
> GetKnownPlatforms() const override
429 std::vector
<std::string
> platforms
;
430 platforms
.emplace_back("x64");
431 platforms
.emplace_back("Win32");
432 platforms
.emplace_back("ARM");
433 platforms
.emplace_back("ARM64");
434 platforms
.emplace_back("ARM64EC");
438 std::string
GetDefaultPlatformName() const override
440 return VSHostPlatformName();
444 std::unique_ptr
<cmGlobalGeneratorFactory
>
445 cmGlobalVisualStudioVersionedGenerator::NewFactory17()
447 return std::unique_ptr
<cmGlobalGeneratorFactory
>(new Factory17
);
450 cmGlobalVisualStudioVersionedGenerator::cmGlobalVisualStudioVersionedGenerator(
451 VSVersion version
, cmake
* cm
, const std::string
& name
,
452 std::string
const& platformInGeneratorName
)
453 : cmGlobalVisualStudio14Generator(cm
, name
, platformInGeneratorName
)
454 , vsSetupAPIHelper(VSVersionToMajor(version
))
456 this->Version
= version
;
457 this->ExpressEdition
= false;
458 this->DefaultPlatformToolset
= VSVersionToToolset(this->Version
);
459 this->DefaultAndroidToolset
= VSVersionToAndroidToolset(this->Version
);
460 this->DefaultCLFlagTableName
= VSVersionToToolset(this->Version
);
461 this->DefaultCSharpFlagTableName
= VSVersionToToolset(this->Version
);
462 this->DefaultLinkFlagTableName
= VSVersionToToolset(this->Version
);
463 if (this->Version
>= cmGlobalVisualStudioGenerator::VSVersion::VS16
) {
464 this->DefaultPlatformName
= VSHostPlatformName();
465 this->DefaultPlatformToolsetHostArchitecture
=
466 VSHostArchitecture(this->Version
);
468 if (this->Version
>= cmGlobalVisualStudioGenerator::VSVersion::VS17
) {
469 // FIXME: Search for an existing framework? Under '%ProgramFiles(x86)%',
470 // see 'Reference Assemblies\Microsoft\Framework\.NETFramework'.
471 // Use a version installed by VS 2022 without a separate component.
472 this->DefaultTargetFrameworkVersion
= "v4.7.2";
476 bool cmGlobalVisualStudioVersionedGenerator::MatchesGeneratorName(
477 const std::string
& name
) const
480 switch (this->Version
) {
481 case cmGlobalVisualStudioGenerator::VSVersion::VS12
:
482 case cmGlobalVisualStudioGenerator::VSVersion::VS14
:
484 case cmGlobalVisualStudioGenerator::VSVersion::VS15
:
485 if (cmVS15GenName(name
, genName
)) {
486 return genName
== this->GetName();
489 case cmGlobalVisualStudioGenerator::VSVersion::VS16
:
490 if (cmVS16GenName(name
, genName
)) {
491 return genName
== this->GetName();
494 case cmGlobalVisualStudioGenerator::VSVersion::VS17
:
495 if (cmVS17GenName(name
, genName
)) {
496 return genName
== this->GetName();
503 bool cmGlobalVisualStudioVersionedGenerator::SetGeneratorInstance(
504 std::string
const& i
, cmMakefile
* mf
)
506 if (this->LastGeneratorInstanceString
&&
507 i
== *(this->LastGeneratorInstanceString
)) {
508 this->SetVSVersionVar(mf
);
512 if (!this->ParseGeneratorInstance(i
, mf
)) {
516 if (!this->GeneratorInstanceVersion
.empty()) {
517 std::string
const majorStr
= VSVersionToMajorString(this->Version
);
518 cmsys::RegularExpression
versionRegex(
519 cmStrCat('^', majorStr
, R
"(\.[0-9]+\.[0-9]+\.[0-9]+$)"));
520 if (!versionRegex
.find(this->GeneratorInstanceVersion
)) {
522 MessageType::FATAL_ERROR
,
523 cmStrCat("Generator\n"
527 "given instance specification\n"
531 "but the version field is not 4 integer components"
538 std::string vsInstance
;
541 if (!this->vsSetupAPIHelper
.SetVSInstance(
542 this->GeneratorInstance
, this->GeneratorInstanceVersion
)) {
543 std::ostringstream e
;
544 /* clang-format off */
547 " " << this->GetName() << "\n"
548 "could not find specified instance of Visual Studio:\n"
550 /* clang-format on */
551 if (!this->GeneratorInstance
.empty() &&
552 this->GeneratorInstanceVersion
.empty() &&
553 cmSystemTools::FileIsDirectory(this->GeneratorInstance
)) {
555 "The directory exists, but the instance is not known to the "
556 "Visual Studio Installer, and no 'version=' field was given.";
558 mf
->IssueMessage(MessageType::FATAL_ERROR
, e
.str());
561 } else if (!this->vsSetupAPIHelper
.GetVSInstanceInfo(vsInstance
)) {
563 MessageType::FATAL_ERROR
,
564 cmStrCat("Generator\n"
568 "could not find any instance of Visual Studio.\n"));
572 // Save the selected instance persistently.
573 std::string genInstance
= mf
->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
574 if (vsInstance
!= genInstance
) {
575 this->CMakeInstance
->AddCacheEntry("CMAKE_GENERATOR_INSTANCE", vsInstance
,
576 "Generator instance identifier.",
577 cmStateEnums::INTERNAL
);
580 this->SetVSVersionVar(mf
);
582 // The selected instance may have a different MSBuild than previously found.
583 this->MSBuildCommandInitialized
= false;
585 this->LastGeneratorInstanceString
= i
;
590 bool cmGlobalVisualStudioVersionedGenerator::ParseGeneratorInstance(
591 std::string
const& is
, cmMakefile
* mf
)
593 this->GeneratorInstance
.clear();
594 this->GeneratorInstanceVersion
.clear();
596 std::vector
<std::string
> const fields
= cmTokenize(is
, ",");
597 auto fi
= fields
.begin();
598 if (fi
== fields
.end()) {
602 // The first field may be the VS instance.
603 if (fi
->find('=') == fi
->npos
) {
604 this->GeneratorInstance
= *fi
;
608 std::set
<std::string
> handled
;
610 // The rest of the fields must be key=value pairs.
611 for (; fi
!= fields
.end(); ++fi
) {
612 std::string::size_type pos
= fi
->find('=');
613 if (pos
== fi
->npos
) {
615 MessageType::FATAL_ERROR
,
616 cmStrCat("Generator\n"
620 "given instance specification\n"
624 "that contains a field after the first ',' with no '='."));
627 std::string
const key
= fi
->substr(0, pos
);
628 std::string
const value
= fi
->substr(pos
+ 1);
629 if (!handled
.insert(key
).second
) {
630 mf
->IssueMessage(MessageType::FATAL_ERROR
,
631 cmStrCat("Generator\n"
635 "given instance specification\n"
639 "that contains duplicate field key '",
643 if (!this->ProcessGeneratorInstanceField(key
, value
)) {
644 mf
->IssueMessage(MessageType::FATAL_ERROR
,
645 cmStrCat("Generator\n"
649 "given instance specification\n"
653 "that contains invalid field '",
662 void cmGlobalVisualStudioVersionedGenerator::SetVSVersionVar(cmMakefile
* mf
)
664 if (cm::optional
<std::string
> vsVer
= this->GetVSInstanceVersion()) {
665 mf
->AddDefinition("CMAKE_VS_VERSION_BUILD_NUMBER", *vsVer
);
669 bool cmGlobalVisualStudioVersionedGenerator::ProcessGeneratorInstanceField(
670 std::string
const& key
, std::string
const& value
)
672 if (key
== "version"_s
) {
673 this->GeneratorInstanceVersion
= value
;
679 bool cmGlobalVisualStudioVersionedGenerator::GetVSInstance(
680 std::string
& dir
) const
682 return vsSetupAPIHelper
.GetVSInstanceInfo(dir
);
685 cm::optional
<std::string
>
686 cmGlobalVisualStudioVersionedGenerator::GetVSInstanceVersion() const
688 cm::optional
<std::string
> result
;
689 std::string vsInstanceVersion
;
690 if (vsSetupAPIHelper
.GetVSInstanceVersion(vsInstanceVersion
)) {
691 result
= vsInstanceVersion
;
696 bool cmGlobalVisualStudioVersionedGenerator::IsStdOutEncodingSupported() const
698 // Supported from Visual Studio 16.7 Preview 3.
699 if (this->Version
> cmGlobalVisualStudioGenerator::VSVersion::VS16
) {
702 if (this->Version
< cmGlobalVisualStudioGenerator::VSVersion::VS16
) {
705 static std::string
const vsVer16_7_P2
= "16.7.30128.36";
706 cm::optional
<std::string
> vsVer
= this->GetVSInstanceVersion();
708 cmSystemTools::VersionCompareGreaterEq(*vsVer
, vsVer16_7_P2
));
711 bool cmGlobalVisualStudioVersionedGenerator::IsUtf8EncodingSupported() const
713 // Supported from Visual Studio 16.10 Preview 2.
714 if (this->Version
> cmGlobalVisualStudioGenerator::VSVersion::VS16
) {
717 if (this->Version
< cmGlobalVisualStudioGenerator::VSVersion::VS16
) {
720 static std::string
const vsVer16_10_P2
= "16.10.31213.239";
721 cm::optional
<std::string
> vsVer
= this->GetVSInstanceVersion();
723 cmSystemTools::VersionCompareGreaterEq(*vsVer
, vsVer16_10_P2
));
726 bool cmGlobalVisualStudioVersionedGenerator::IsScanDependenciesSupported()
729 // Supported from Visual Studio 17.6 Preview 7.
730 if (this->Version
> cmGlobalVisualStudioGenerator::VSVersion::VS17
) {
733 if (this->Version
< cmGlobalVisualStudioGenerator::VSVersion::VS17
) {
736 static std::string
const vsVer17_6_P7
= "17.6.33706.43";
737 cm::optional
<std::string
> vsVer
= this->GetVSInstanceVersion();
739 cmSystemTools::VersionCompareGreaterEq(*vsVer
, vsVer17_6_P7
));
743 cmGlobalVisualStudioVersionedGenerator::GetAndroidApplicationTypeRevision()
746 switch (this->Version
) {
747 case cmGlobalVisualStudioGenerator::VSVersion::VS12
:
749 case cmGlobalVisualStudioGenerator::VSVersion::VS14
:
751 case cmGlobalVisualStudioGenerator::VSVersion::VS15
:
752 case cmGlobalVisualStudioGenerator::VSVersion::VS16
:
753 case cmGlobalVisualStudioGenerator::VSVersion::VS17
:
759 cmGlobalVisualStudioVersionedGenerator::AuxToolset
760 cmGlobalVisualStudioVersionedGenerator::FindAuxToolset(
761 std::string
& version
, std::string
& props
) const
763 if (version
.empty()) {
764 return AuxToolset::None
;
767 std::string instancePath
;
768 this->GetVSInstance(instancePath
);
769 cmSystemTools::ConvertToUnixSlashes(instancePath
);
771 // Translate three-component format accepted by "vcvarsall -vcvars_ver=".
772 cmsys::RegularExpression
threeComponentRegex(
773 "^([0-9]+\\.[0-9]+)\\.[0-9][0-9][0-9][0-9][0-9]$");
774 // The two-component format represents the two major components of the
775 // three-component format
776 cmsys::RegularExpression
twoComponentRegex("^([0-9]+\\.[0-9]+)$");
777 if (threeComponentRegex
.find(version
)) {
778 // Load "VC/Auxiliary/Build/*/Microsoft.VCToolsVersion.*.txt" files
779 // with two matching components to check their three-component version.
780 std::string
const& twoComponent
= threeComponentRegex
.match(1);
781 std::string pattern
=
782 cmStrCat(instancePath
, "/VC/Auxiliary/Build/"_s
, twoComponent
,
783 "*/Microsoft.VCToolsVersion."_s
, twoComponent
, "*.txt"_s
);
785 glob
.SetRecurseThroughSymlinks(false);
786 if (glob
.FindFiles(pattern
)) {
787 for (std::string
const& txt
: glob
.GetFiles()) {
789 cmsys::ifstream
fin(txt
.c_str());
790 if (fin
&& std::getline(fin
, ver
)) {
791 // Strip trailing whitespace.
792 ver
= ver
.substr(0, ver
.find_first_not_of("0123456789."));
793 // If the three-component version matches, translate it to
794 // that used by the "Microsoft.VCToolsVersion.*.txt" file name.
795 if (ver
== version
) {
796 cmsys::RegularExpression
extractVersion(
797 "VCToolsVersion\\.([0-9.]+)\\.txt$");
798 if (extractVersion
.find(txt
)) {
799 version
= extractVersion
.match(1);
806 } else if (twoComponentRegex
.find(version
)) {
807 std::string
const& twoComponent
= twoComponentRegex
.match(1);
808 std::string pattern
=
809 cmStrCat(instancePath
, "/VC/Auxiliary/Build/"_s
, twoComponent
,
810 "*/Microsoft.VCToolsVersion."_s
, twoComponent
, "*.txt"_s
);
812 glob
.SetRecurseThroughSymlinks(false);
813 if (glob
.FindFiles(pattern
) && !glob
.GetFiles().empty()) {
814 // Since we are only using the first two components of the
815 // toolset version, we require a single match.
816 if (glob
.GetFiles().size() == 1) {
817 std::string
const& txt
= glob
.GetFiles()[0];
819 cmsys::ifstream
fin(txt
.c_str());
820 if (fin
&& std::getline(fin
, ver
)) {
821 // Strip trailing whitespace.
822 ver
= ver
.substr(0, ver
.find_first_not_of("0123456789."));
823 // We assume the version is correct, since it is the only one that
825 cmsys::RegularExpression
extractVersion(
826 "VCToolsVersion\\.([0-9.]+)\\.txt$");
827 if (extractVersion
.find(txt
)) {
828 version
= extractVersion
.match(1);
832 props
= cmStrCat(instancePath
, "/VC/Auxiliary/Build/"_s
);
833 return AuxToolset::PropsIndeterminate
;
838 if (cmSystemTools::VersionCompareGreaterEq(version
, "14.20")) {
839 props
= cmStrCat(instancePath
, "/VC/Auxiliary/Build."_s
, version
,
840 "/Microsoft.VCToolsVersion."_s
, version
, ".props"_s
);
841 if (cmSystemTools::PathExists(props
)) {
842 return AuxToolset::PropsExist
;
845 props
= cmStrCat(instancePath
, "/VC/Auxiliary/Build/"_s
, version
,
846 "/Microsoft.VCToolsVersion."_s
, version
, ".props"_s
);
847 if (cmSystemTools::PathExists(props
)) {
848 return AuxToolset::PropsExist
;
851 // Accept the toolset version that is default in the current VS version
852 // by matching the name later VS versions will use for the SxS props files.
853 std::string vcToolsetVersion
;
854 if (this->vsSetupAPIHelper
.GetVCToolsetVersion(vcToolsetVersion
)) {
855 // Accept an exact-match (three-component version).
856 if (version
== vcToolsetVersion
) {
857 return AuxToolset::Default
;
860 // Accept known SxS props file names using four version components
861 // in VS versions later than the current.
862 if (version
== "14.28.16.9"_s
&& vcToolsetVersion
== "14.28.29910"_s
) {
863 return AuxToolset::Default
;
865 if (version
== "14.29.16.10"_s
&& vcToolsetVersion
== "14.29.30037"_s
) {
866 return AuxToolset::Default
;
868 if (version
== "14.29.16.11"_s
&& vcToolsetVersion
== "14.29.30133"_s
) {
869 return AuxToolset::Default
;
872 // The first two components of the default toolset version typically
873 // match the name used by later VS versions for the SxS props files.
874 cmsys::RegularExpression
twoComponent("^([0-9]+\\.[0-9]+)");
875 if (twoComponent
.find(version
)) {
876 std::string
const versionPrefix
= cmStrCat(twoComponent
.match(1), '.');
877 if (cmHasPrefix(vcToolsetVersion
, versionPrefix
)) {
878 return AuxToolset::Default
;
883 return AuxToolset::PropsMissing
;
886 bool cmGlobalVisualStudioVersionedGenerator::SelectWindowsStoreToolset(
887 std::string
& toolset
) const
889 if (cmHasLiteralPrefix(this->SystemVersion
, "10.0")) {
890 if (this->IsWindowsStoreToolsetInstalled() &&
891 this->IsWindowsDesktopToolsetInstalled()) {
892 toolset
= VSVersionToToolset(this->Version
);
897 return this->cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
901 bool cmGlobalVisualStudioVersionedGenerator::IsWindowsDesktopToolsetInstalled()
904 return vsSetupAPIHelper
.IsVSInstalled();
907 bool cmGlobalVisualStudioVersionedGenerator::IsWindowsStoreToolsetInstalled()
910 return vsSetupAPIHelper
.IsWin10SDKInstalled();
913 bool cmGlobalVisualStudioVersionedGenerator::IsWin81SDKInstalled() const
915 // Does the VS installer tool know about one?
916 if (vsSetupAPIHelper
.IsWin81SDKInstalled()) {
920 // Does the registry know about one (e.g. from VS 2015)?
921 std::string win81Root
;
922 if (cmSystemTools::ReadRegistryValue(
923 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
924 "Windows Kits\\Installed Roots;KitsRoot81",
925 win81Root
, cmSystemTools::KeyWOW64_32
) ||
926 cmSystemTools::ReadRegistryValue(
927 "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
928 "Windows Kits\\Installed Roots;KitsRoot81",
929 win81Root
, cmSystemTools::KeyWOW64_32
)) {
930 return cmSystemTools::FileExists(
931 cmStrCat(win81Root
, "/include/um/windows.h"), true);
937 cmGlobalVisualStudioVersionedGenerator::GetWindows10SDKMaxVersionDefault(
940 return std::string();
943 cm::optional
<std::string
>
944 cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommandEarly(cmMakefile
* mf
)
946 std::string instance
= mf
->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
947 if (!this->SetGeneratorInstance(instance
, mf
)) {
948 cmSystemTools::SetFatalErrorOccurred();
951 return this->cmGlobalVisualStudio14Generator::FindMSBuildCommandEarly(mf
);
954 std::string
cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommand()
958 // Ask Visual Studio Installer tool.
960 if (vsSetupAPIHelper
.GetVSInstanceInfo(vs
)) {
961 if (this->Version
>= cmGlobalVisualStudioGenerator::VSVersion::VS17
) {
962 if (VSIsArm64Host()) {
963 if (VSHasDotNETFrameworkArm64()) {
964 msbuild
= cmStrCat(vs
, "/MSBuild/Current/Bin/arm64/MSBuild.exe");
965 if (cmSystemTools::FileExists(msbuild
)) {
969 if (VSIsWindows11OrGreater()) {
970 msbuild
= cmStrCat(vs
, "/MSBuild/Current/Bin/amd64/MSBuild.exe");
971 if (cmSystemTools::FileExists(msbuild
)) {
976 msbuild
= cmStrCat(vs
, "/MSBuild/Current/Bin/amd64/MSBuild.exe");
977 if (cmSystemTools::FileExists(msbuild
)) {
982 msbuild
= cmStrCat(vs
, "/MSBuild/Current/Bin/MSBuild.exe");
983 if (cmSystemTools::FileExists(msbuild
)) {
986 msbuild
= cmStrCat(vs
, "/MSBuild/15.0/Bin/MSBuild.exe");
987 if (cmSystemTools::FileExists(msbuild
)) {
992 msbuild
= "MSBuild.exe";
996 std::string
cmGlobalVisualStudioVersionedGenerator::FindDevEnvCommand()
1000 // Ask Visual Studio Installer tool.
1002 if (vsSetupAPIHelper
.GetVSInstanceInfo(vs
)) {
1003 devenv
= cmStrCat(vs
, "/Common7/IDE/devenv.com");
1004 if (cmSystemTools::FileExists(devenv
)) {
1009 devenv
= "devenv.com";