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 "cmDocumentationEntry.h"
18 #include "cmGlobalGenerator.h"
19 #include "cmGlobalGeneratorFactory.h"
20 #include "cmMakefile.h"
21 #include "cmMessageType.h"
22 #include "cmStateTypes.h"
23 #include "cmStringAlgorithms.h"
24 #include "cmSystemTools.h"
25 #include "cmVSSetupHelper.h"
28 #ifndef IMAGE_FILE_MACHINE_ARM64
29 # define IMAGE_FILE_MACHINE_ARM64 0xaa64 // ARM64 Little-Endian
32 static bool VSIsWow64()
35 return IsWow64Process(GetCurrentProcess(), &isWow64) && isWow64;
38 static bool VSIsArm64Host()
40 typedef BOOL(WINAPI * CM_ISWOW64PROCESS2)(
41 HANDLE hProcess, USHORT * pProcessMachine, USHORT * pNativeMachine);
43 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
44 # define CM_VS_GCC_DIAGNOSTIC_PUSHED
45 # pragma GCC diagnostic push
46 # pragma GCC diagnostic ignored "-Wcast-function-type"
48 static const CM_ISWOW64PROCESS2 s_IsWow64Process2Impl =
49 (CM_ISWOW64PROCESS2)GetProcAddress(
50 GetModuleHandleW(L"api-ms-win-core-wow64-l1-1-1.dll"),
52 #ifdef CM_VS_GCC_DIAGNOSTIC_PUSHED
53 # pragma GCC diagnostic pop
54 # undef CM_VS_GCC_DIAGNOSTIC_PUSHED
57 USHORT processMachine, nativeMachine;
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 "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()) {
89 } else if (VSIsWow64()) {
94 #elif defined(_M_IA64)
104 static std::string VSHostArchitecture(
105 cmGlobalVisualStudioGenerator::VSVersion v)
107 if (VSIsArm64Host()) {
108 return v >= cmGlobalVisualStudioGenerator::VSVersion::VS17 ? "ARM64" : "";
109 } else if (VSIsWow64()) {
114 #elif defined(_M_IA64)
116 #elif defined(_WIN64)
124 static unsigned int VSVersionToMajor(
125 cmGlobalVisualStudioGenerator::VSVersion v)
128 case cmGlobalVisualStudioGenerator::VSVersion::VS9:
130 case cmGlobalVisualStudioGenerator::VSVersion::VS10:
132 case cmGlobalVisualStudioGenerator::VSVersion::VS11:
134 case cmGlobalVisualStudioGenerator::VSVersion::VS12:
136 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
138 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
140 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
142 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
148 static const char* VSVersionToToolset(
149 cmGlobalVisualStudioGenerator::VSVersion v)
152 case cmGlobalVisualStudioGenerator::VSVersion::VS9:
154 case cmGlobalVisualStudioGenerator::VSVersion::VS10:
156 case cmGlobalVisualStudioGenerator::VSVersion::VS11:
158 case cmGlobalVisualStudioGenerator::VSVersion::VS12:
160 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
162 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
164 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
166 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
172 static std::string VSVersionToMajorString(
173 cmGlobalVisualStudioGenerator::VSVersion v)
176 case cmGlobalVisualStudioGenerator::VSVersion::VS9:
178 case cmGlobalVisualStudioGenerator::VSVersion::VS10:
180 case cmGlobalVisualStudioGenerator::VSVersion::VS11:
182 case cmGlobalVisualStudioGenerator::VSVersion::VS12:
184 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
186 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
188 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
190 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
196 static const char* VSVersionToAndroidToolset(
197 cmGlobalVisualStudioGenerator::VSVersion v)
200 case cmGlobalVisualStudioGenerator::VSVersion::VS9:
201 case cmGlobalVisualStudioGenerator::VSVersion::VS10:
202 case cmGlobalVisualStudioGenerator::VSVersion::VS11:
203 case cmGlobalVisualStudioGenerator::VSVersion::VS12:
205 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
207 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
208 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
209 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
215 static const char vs15generatorName[] = "Visual Studio 15 2017";
217 // Map generator name without year to name with year.
218 static const char* cmVS15GenName(const std::string& name, std::string& genName)
220 if (strncmp(name.c_str(), vs15generatorName,
221 sizeof(vs15generatorName) - 6) != 0) {
224 const char* p = name.c_str() + sizeof(vs15generatorName) - 6;
225 if (cmHasLiteralPrefix(p, " 2017")) {
228 genName = std::string(vs15generatorName) + p;
232 class cmGlobalVisualStudioVersionedGenerator::Factory15
233 : public cmGlobalGeneratorFactory
236 std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
237 const std::string& name, bool allowArch, cmake* cm) const override
240 const char* p = cmVS15GenName(name, genName);
242 return std::unique_ptr<cmGlobalGenerator>();
245 return std::unique_ptr<cmGlobalGenerator>(
246 new cmGlobalVisualStudioVersionedGenerator(
247 cmGlobalVisualStudioGenerator::VSVersion::VS15, cm, genName, ""));
249 if (!allowArch || *p++ != ' ') {
250 return std::unique_ptr<cmGlobalGenerator>();
252 if (strcmp(p, "Win64") == 0) {
253 return std::unique_ptr<cmGlobalGenerator>(
254 new cmGlobalVisualStudioVersionedGenerator(
255 cmGlobalVisualStudioGenerator::VSVersion::VS15, cm, genName, "x64"));
257 if (strcmp(p, "ARM") == 0) {
258 return std::unique_ptr<cmGlobalGenerator>(
259 new cmGlobalVisualStudioVersionedGenerator(
260 cmGlobalVisualStudioGenerator::VSVersion::VS15, cm, genName, "ARM"));
262 return std::unique_ptr<cmGlobalGenerator>();
265 void GetDocumentation(cmDocumentationEntry& entry) const override
267 entry.Name = std::string(vs15generatorName) + " [arch]";
268 entry.Brief = "Generates Visual Studio 2017 project files. "
269 "Optional [arch] can be \"Win64\" or \"ARM\".";
272 std::vector<std::string> GetGeneratorNames() const override
274 std::vector<std::string> names;
275 names.push_back(vs15generatorName);
279 std::vector<std::string> GetGeneratorNamesWithPlatform() const override
281 std::vector<std::string> names;
282 names.push_back(vs15generatorName + std::string(" ARM"));
283 names.push_back(vs15generatorName + std::string(" Win64"));
287 bool SupportsToolset() const override { return true; }
288 bool SupportsPlatform() const override { return true; }
290 std::vector<std::string> GetKnownPlatforms() const override
292 std::vector<std::string> platforms;
293 platforms.emplace_back("x64");
294 platforms.emplace_back("Win32");
295 platforms.emplace_back("ARM");
296 platforms.emplace_back("ARM64");
300 std::string GetDefaultPlatformName() const override { return "Win32"; }
303 std::unique_ptr<cmGlobalGeneratorFactory>
304 cmGlobalVisualStudioVersionedGenerator::NewFactory15()
306 return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory15);
309 static const char vs16generatorName[] = "Visual Studio 16 2019";
310 static const char vs17generatorName[] = "Visual Studio 17 2022";
312 // Map generator name without year to name with year.
313 static const char* cmVS16GenName(const std::string& name, std::string& genName)
315 if (strncmp(name.c_str(), vs16generatorName,
316 sizeof(vs16generatorName) - 6) != 0) {
319 const char* p = name.c_str() + sizeof(vs16generatorName) - 6;
320 if (cmHasLiteralPrefix(p, " 2019")) {
323 genName = std::string(vs16generatorName) + p;
327 static const char* cmVS17GenName(const std::string& name, std::string& genName)
329 if (strncmp(name.c_str(), vs17generatorName,
330 sizeof(vs17generatorName) - 6) != 0) {
333 const char* p = name.c_str() + sizeof(vs17generatorName) - 6;
334 if (cmHasLiteralPrefix(p, " 2022")) {
337 genName = std::string(vs17generatorName) + p;
341 class cmGlobalVisualStudioVersionedGenerator::Factory16
342 : public cmGlobalGeneratorFactory
345 std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
346 const std::string& name, bool /*allowArch*/, cmake* cm) const override
349 const char* p = cmVS16GenName(name, genName);
351 return std::unique_ptr<cmGlobalGenerator>();
354 return std::unique_ptr<cmGlobalGenerator>(
355 new cmGlobalVisualStudioVersionedGenerator(
356 cmGlobalVisualStudioGenerator::VSVersion::VS16, cm, genName, ""));
358 return std::unique_ptr<cmGlobalGenerator>();
361 void GetDocumentation(cmDocumentationEntry& entry) const override
363 entry.Name = std::string(vs16generatorName);
364 entry.Brief = "Generates Visual Studio 2019 project files. "
365 "Use -A option to specify architecture.";
368 std::vector<std::string> GetGeneratorNames() const override
370 std::vector<std::string> names;
371 names.push_back(vs16generatorName);
375 std::vector<std::string> GetGeneratorNamesWithPlatform() const override
377 return std::vector<std::string>();
380 bool SupportsToolset() const override { return true; }
381 bool SupportsPlatform() const override { return true; }
383 std::vector<std::string> GetKnownPlatforms() const override
385 std::vector<std::string> platforms;
386 platforms.emplace_back("x64");
387 platforms.emplace_back("Win32");
388 platforms.emplace_back("ARM");
389 platforms.emplace_back("ARM64");
390 platforms.emplace_back("ARM64EC");
394 std::string GetDefaultPlatformName() const override
396 return VSHostPlatformName();
400 std::unique_ptr<cmGlobalGeneratorFactory>
401 cmGlobalVisualStudioVersionedGenerator::NewFactory16()
403 return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory16);
406 class cmGlobalVisualStudioVersionedGenerator::Factory17
407 : public cmGlobalGeneratorFactory
410 std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
411 const std::string& name, bool /*allowArch*/, cmake* cm) const override
414 const char* p = cmVS17GenName(name, genName);
416 return std::unique_ptr<cmGlobalGenerator>();
419 return std::unique_ptr<cmGlobalGenerator>(
420 new cmGlobalVisualStudioVersionedGenerator(
421 cmGlobalVisualStudioGenerator::VSVersion::VS17, cm, genName, ""));
423 return std::unique_ptr<cmGlobalGenerator>();
426 void GetDocumentation(cmDocumentationEntry& entry) const override
428 entry.Name = std::string(vs17generatorName);
429 entry.Brief = "Generates Visual Studio 2022 project files. "
430 "Use -A option to specify architecture.";
433 std::vector<std::string> GetGeneratorNames() const override
435 std::vector<std::string> names;
436 names.push_back(vs17generatorName);
440 std::vector<std::string> GetGeneratorNamesWithPlatform() const override
442 return std::vector<std::string>();
445 bool SupportsToolset() const override { return true; }
446 bool SupportsPlatform() const override { return true; }
448 std::vector<std::string> GetKnownPlatforms() const override
450 std::vector<std::string> platforms;
451 platforms.emplace_back("x64");
452 platforms.emplace_back("Win32");
453 platforms.emplace_back("ARM");
454 platforms.emplace_back("ARM64");
455 platforms.emplace_back("ARM64EC");
459 std::string GetDefaultPlatformName() const override
461 return VSHostPlatformName();
465 std::unique_ptr<cmGlobalGeneratorFactory>
466 cmGlobalVisualStudioVersionedGenerator::NewFactory17()
468 return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory17);
471 cmGlobalVisualStudioVersionedGenerator::cmGlobalVisualStudioVersionedGenerator(
472 VSVersion version, cmake* cm, const std::string& name,
473 std::string const& platformInGeneratorName)
474 : cmGlobalVisualStudio14Generator(cm, name, platformInGeneratorName)
475 , vsSetupAPIHelper(VSVersionToMajor(version))
477 this->Version = version;
478 this->ExpressEdition = false;
479 this->DefaultPlatformToolset = VSVersionToToolset(this->Version);
480 this->DefaultAndroidToolset = VSVersionToAndroidToolset(this->Version);
481 this->DefaultCLFlagTableName = VSVersionToToolset(this->Version);
482 this->DefaultCSharpFlagTableName = VSVersionToToolset(this->Version);
483 this->DefaultLinkFlagTableName = VSVersionToToolset(this->Version);
484 if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS16) {
485 this->DefaultPlatformName = VSHostPlatformName();
486 this->DefaultPlatformToolsetHostArchitecture =
487 VSHostArchitecture(this->Version);
489 if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS17) {
490 // FIXME: Search for an existing framework? Under '%ProgramFiles(x86)%',
491 // see 'Reference Assemblies\Microsoft\Framework\.NETFramework'.
492 // Use a version installed by VS 2022 without a separate component.
493 this->DefaultTargetFrameworkVersion = "v4.7.2";
497 bool cmGlobalVisualStudioVersionedGenerator::MatchesGeneratorName(
498 const std::string& name) const
501 switch (this->Version) {
502 case cmGlobalVisualStudioGenerator::VSVersion::VS9:
503 case cmGlobalVisualStudioGenerator::VSVersion::VS10:
504 case cmGlobalVisualStudioGenerator::VSVersion::VS11:
505 case cmGlobalVisualStudioGenerator::VSVersion::VS12:
506 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
508 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
509 if (cmVS15GenName(name, genName)) {
510 return genName == this->GetName();
513 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
514 if (cmVS16GenName(name, genName)) {
515 return genName == this->GetName();
518 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
519 if (cmVS17GenName(name, genName)) {
520 return genName == this->GetName();
527 bool cmGlobalVisualStudioVersionedGenerator::SetGeneratorInstance(
528 std::string const& i, cmMakefile* mf)
530 if (this->LastGeneratorInstanceString &&
531 i == *(this->LastGeneratorInstanceString)) {
535 if (!this->ParseGeneratorInstance(i, mf)) {
539 if (!this->GeneratorInstanceVersion.empty()) {
540 std::string const majorStr = VSVersionToMajorString(this->Version);
541 cmsys::RegularExpression versionRegex(
542 cmStrCat("^", majorStr, "\\.[0-9]+\\.[0-9]+\\.[0-9]+$"));
543 if (!versionRegex.find(this->GeneratorInstanceVersion)) {
544 std::ostringstream e;
545 /* clang-format off */
548 " " << this->GetName() << "\n"
549 "given instance specification\n"
551 "but the version field is not 4 integer components"
552 " starting in " << majorStr << "."
554 /* clang-format on */
555 mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
560 std::string vsInstance;
563 if (!this->vsSetupAPIHelper.SetVSInstance(
564 this->GeneratorInstance, this->GeneratorInstanceVersion)) {
565 std::ostringstream e;
566 /* clang-format off */
569 " " << this->GetName() << "\n"
570 "could not find specified instance of Visual Studio:\n"
572 /* clang-format on */
573 if (!this->GeneratorInstance.empty() &&
574 this->GeneratorInstanceVersion.empty() &&
575 cmSystemTools::FileIsDirectory(this->GeneratorInstance)) {
577 "The directory exists, but the instance is not known to the "
578 "Visual Studio Installer, and no 'version=' field was given.";
580 mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
583 } else if (!this->vsSetupAPIHelper.GetVSInstanceInfo(vsInstance)) {
584 std::ostringstream e;
585 /* clang-format off */
588 " " << this->GetName() << "\n"
589 "could not find any instance of Visual Studio.\n";
590 /* clang-format on */
591 mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
595 // Save the selected instance persistently.
596 std::string genInstance = mf->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
597 if (vsInstance != genInstance) {
598 this->CMakeInstance->AddCacheEntry("CMAKE_GENERATOR_INSTANCE", vsInstance,
599 "Generator instance identifier.",
600 cmStateEnums::INTERNAL);
603 // The selected instance may have a different MSBuild than previously found.
604 this->MSBuildCommandInitialized = false;
606 this->LastGeneratorInstanceString = i;
611 bool cmGlobalVisualStudioVersionedGenerator::ParseGeneratorInstance(
612 std::string const& is, cmMakefile* mf)
614 this->GeneratorInstance.clear();
615 this->GeneratorInstanceVersion.clear();
617 std::vector<std::string> const fields = cmTokenize(is, ",");
618 std::vector<std::string>::const_iterator fi = fields.begin();
619 if (fi == fields.end()) {
623 // The first field may be the VS instance.
624 if (fi->find('=') == fi->npos) {
625 this->GeneratorInstance = *fi;
629 std::set<std::string> handled;
631 // The rest of the fields must be key=value pairs.
632 for (; fi != fields.end(); ++fi) {
633 std::string::size_type pos = fi->find('=');
634 if (pos == fi->npos) {
635 std::ostringstream e;
636 /* clang-format off */
639 " " << this->GetName() << "\n"
640 "given instance specification\n"
642 "that contains a field after the first ',' with no '='."
644 /* clang-format on */
645 mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
648 std::string const key = fi->substr(0, pos);
649 std::string const value = fi->substr(pos + 1);
650 if (!handled.insert(key).second) {
651 std::ostringstream e;
652 /* clang-format off */
655 " " << this->GetName() << "\n"
656 "given instance specification\n"
658 "that contains duplicate field key '" << key << "'."
660 /* clang-format on */
661 mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
664 if (!this->ProcessGeneratorInstanceField(key, value)) {
665 std::ostringstream e;
666 /* clang-format off */
669 " " << this->GetName() << "\n"
670 "given instance specification\n"
672 "that contains invalid field '" << *fi << "'."
674 /* clang-format on */
675 mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
683 bool cmGlobalVisualStudioVersionedGenerator::ProcessGeneratorInstanceField(
684 std::string const& key, std::string const& value)
686 if (key == "version") {
687 this->GeneratorInstanceVersion = value;
693 bool cmGlobalVisualStudioVersionedGenerator::GetVSInstance(
694 std::string& dir) const
696 return vsSetupAPIHelper.GetVSInstanceInfo(dir);
699 cm::optional<std::string>
700 cmGlobalVisualStudioVersionedGenerator::GetVSInstanceVersion() const
702 cm::optional<std::string> result;
703 std::string vsInstanceVersion;
704 if (vsSetupAPIHelper.GetVSInstanceVersion(vsInstanceVersion)) {
705 result = vsInstanceVersion;
710 bool cmGlobalVisualStudioVersionedGenerator::IsStdOutEncodingSupported() const
712 // Supported from Visual Studio 16.7 Preview 3.
713 if (this->Version > cmGlobalVisualStudioGenerator::VSVersion::VS16) {
716 if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS16) {
719 static std::string const vsVer16_7_P2 = "16.7.30128.36";
720 cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
722 cmSystemTools::VersionCompareGreaterEq(*vsVer, vsVer16_7_P2));
725 bool cmGlobalVisualStudioVersionedGenerator::IsUtf8EncodingSupported() const
727 // Supported from Visual Studio 16.10 Preview 2.
728 if (this->Version > cmGlobalVisualStudioGenerator::VSVersion::VS16) {
731 if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS16) {
734 static std::string const vsVer16_10_P2 = "16.10.31213.239";
735 cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
737 cmSystemTools::VersionCompareGreaterEq(*vsVer, vsVer16_10_P2));
741 cmGlobalVisualStudioVersionedGenerator::GetAndroidApplicationTypeRevision()
744 switch (this->Version) {
745 case cmGlobalVisualStudioGenerator::VSVersion::VS9:
746 case cmGlobalVisualStudioGenerator::VSVersion::VS10:
747 case cmGlobalVisualStudioGenerator::VSVersion::VS11:
748 case cmGlobalVisualStudioGenerator::VSVersion::VS12:
750 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
752 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
753 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
754 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
760 cmGlobalVisualStudioVersionedGenerator::AuxToolset
761 cmGlobalVisualStudioVersionedGenerator::FindAuxToolset(
762 std::string& version, std::string& props) const
764 if (version.empty()) {
765 return AuxToolset::None;
768 std::string instancePath;
769 this->GetVSInstance(instancePath);
770 cmSystemTools::ConvertToUnixSlashes(instancePath);
772 // Translate three-component format accepted by "vcvarsall -vcvars_ver=".
773 cmsys::RegularExpression threeComponent(
774 "^([0-9]+\\.[0-9]+)\\.[0-9][0-9][0-9][0-9][0-9]$");
775 if (threeComponent.find(version)) {
776 // Load "VC/Auxiliary/Build/*/Microsoft.VCToolsVersion.*.txt" files
777 // with two matching components to check their three-component version.
778 std::string const& twoComponent = threeComponent.match(1);
779 std::string pattern =
780 cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s, twoComponent,
781 "*/Microsoft.VCToolsVersion."_s, twoComponent, "*.txt"_s);
783 glob.SetRecurseThroughSymlinks(false);
784 if (glob.FindFiles(pattern)) {
785 for (std::string const& txt : glob.GetFiles()) {
787 cmsys::ifstream fin(txt.c_str());
788 if (fin && std::getline(fin, ver)) {
789 // Strip trailing whitespace.
790 ver = ver.substr(0, ver.find_first_not_of("0123456789."));
791 // If the three-component version matches, translate it to
792 // that used by the "Microsoft.VCToolsVersion.*.txt" file name.
793 if (ver == version) {
794 cmsys::RegularExpression extractVersion(
795 "VCToolsVersion\\.([0-9.]+)\\.txt$");
796 if (extractVersion.find(txt)) {
797 version = extractVersion.match(1);
806 if (cmSystemTools::VersionCompareGreaterEq(version, "14.20")) {
807 props = cmStrCat(instancePath, "/VC/Auxiliary/Build."_s, version,
808 "/Microsoft.VCToolsVersion."_s, version, ".props"_s);
809 if (cmSystemTools::PathExists(props)) {
810 return AuxToolset::PropsExist;
813 props = cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s, version,
814 "/Microsoft.VCToolsVersion."_s, version, ".props"_s);
815 if (cmSystemTools::PathExists(props)) {
816 return AuxToolset::PropsExist;
819 // Accept the toolset version that is default in the current VS version
820 // by matching the name later VS versions will use for the SxS props files.
821 std::string vcToolsetVersion;
822 if (this->vsSetupAPIHelper.GetVCToolsetVersion(vcToolsetVersion)) {
823 // Accept an exact-match (three-component version).
824 if (version == vcToolsetVersion) {
825 return AuxToolset::Default;
828 // Accept known SxS props file names using four version components
829 // in VS versions later than the current.
830 if (version == "14.28.16.9" && vcToolsetVersion == "14.28.29910") {
831 return AuxToolset::Default;
833 if (version == "14.29.16.10" && vcToolsetVersion == "14.29.30037") {
834 return AuxToolset::Default;
836 if (version == "14.29.16.11" && vcToolsetVersion == "14.29.30133") {
837 return AuxToolset::Default;
840 // The first two components of the default toolset version typically
841 // match the name used by later VS versions for the SxS props files.
842 cmsys::RegularExpression twoComponent("^([0-9]+\\.[0-9]+)");
843 if (twoComponent.find(version)) {
844 std::string const versionPrefix = cmStrCat(twoComponent.match(1), '.');
845 if (cmHasPrefix(vcToolsetVersion, versionPrefix)) {
846 return AuxToolset::Default;
851 return AuxToolset::PropsMissing;
854 bool cmGlobalVisualStudioVersionedGenerator::InitializeWindows(cmMakefile* mf)
856 // If the Win 8.1 SDK is installed then we can select a SDK matching
857 // the target Windows version.
858 if (this->IsWin81SDKInstalled()) {
859 // VS 2019 does not default to 8.1 so specify it explicitly when needed.
860 if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS16 &&
861 !cmSystemTools::VersionCompareGreater(this->SystemVersion, "8.1")) {
862 this->SetWindowsTargetPlatformVersion("8.1", mf);
865 return cmGlobalVisualStudio14Generator::InitializeWindows(mf);
867 // Otherwise we must choose a Win 10 SDK even if we are not targeting
869 return this->SelectWindows10SDK(mf, false);
872 bool cmGlobalVisualStudioVersionedGenerator::SelectWindowsStoreToolset(
873 std::string& toolset) const
875 if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
876 if (this->IsWindowsStoreToolsetInstalled() &&
877 this->IsWindowsDesktopToolsetInstalled()) {
878 toolset = VSVersionToToolset(this->Version);
884 return this->cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
888 bool cmGlobalVisualStudioVersionedGenerator::IsWindowsDesktopToolsetInstalled()
891 return vsSetupAPIHelper.IsVSInstalled();
894 bool cmGlobalVisualStudioVersionedGenerator::IsWindowsStoreToolsetInstalled()
897 return vsSetupAPIHelper.IsWin10SDKInstalled();
900 bool cmGlobalVisualStudioVersionedGenerator::IsWin81SDKInstalled() const
902 // Does the VS installer tool know about one?
903 if (vsSetupAPIHelper.IsWin81SDKInstalled()) {
907 // Does the registry know about one (e.g. from VS 2015)?
908 std::string win81Root;
909 if (cmSystemTools::ReadRegistryValue(
910 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
911 "Windows Kits\\Installed Roots;KitsRoot81",
912 win81Root, cmSystemTools::KeyWOW64_32) ||
913 cmSystemTools::ReadRegistryValue(
914 "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
915 "Windows Kits\\Installed Roots;KitsRoot81",
916 win81Root, cmSystemTools::KeyWOW64_32)) {
917 return cmSystemTools::FileExists(win81Root + "/include/um/windows.h",
924 cmGlobalVisualStudioVersionedGenerator::GetWindows10SDKMaxVersionDefault(
927 return std::string();
930 cm::optional<std::string>
931 cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommandEarly(cmMakefile* mf)
933 std::string instance = mf->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
934 if (!this->SetGeneratorInstance(instance, mf)) {
935 cmSystemTools::SetFatalErrorOccurred();
938 return this->cmGlobalVisualStudio14Generator::FindMSBuildCommandEarly(mf);
941 std::string cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommand()
945 // Ask Visual Studio Installer tool.
947 if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
948 if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS17) {
949 if (VSIsArm64Host()) {
950 if (VSHasDotNETFrameworkArm64()) {
951 msbuild = vs + "/MSBuild/Current/Bin/arm64/MSBuild.exe";
952 if (cmSystemTools::FileExists(msbuild)) {
956 if (VSIsWindows11OrGreater()) {
957 msbuild = vs + "/MSBuild/Current/Bin/amd64/MSBuild.exe";
958 if (cmSystemTools::FileExists(msbuild)) {
963 msbuild = vs + "/MSBuild/Current/Bin/amd64/MSBuild.exe";
964 if (cmSystemTools::FileExists(msbuild)) {
969 msbuild = vs + "/MSBuild/Current/Bin/MSBuild.exe";
970 if (cmSystemTools::FileExists(msbuild)) {
973 msbuild = vs + "/MSBuild/15.0/Bin/MSBuild.exe";
974 if (cmSystemTools::FileExists(msbuild)) {
979 msbuild = "MSBuild.exe";
983 std::string cmGlobalVisualStudioVersionedGenerator::FindDevEnvCommand()
987 // Ask Visual Studio Installer tool.
989 if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
990 devenv = vs + "/Common7/IDE/devenv.com";
991 if (cmSystemTools::FileExists(devenv)) {
996 devenv = "devenv.com";