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::VS11:
132 case cmGlobalVisualStudioGenerator::VSVersion::VS12:
134 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
136 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
138 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
140 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
146 static const char* VSVersionToToolset(
147 cmGlobalVisualStudioGenerator::VSVersion v)
150 case cmGlobalVisualStudioGenerator::VSVersion::VS9:
152 case cmGlobalVisualStudioGenerator::VSVersion::VS11:
154 case cmGlobalVisualStudioGenerator::VSVersion::VS12:
156 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
158 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
160 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
162 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
168 static std::string VSVersionToMajorString(
169 cmGlobalVisualStudioGenerator::VSVersion v)
172 case cmGlobalVisualStudioGenerator::VSVersion::VS9:
174 case cmGlobalVisualStudioGenerator::VSVersion::VS11:
176 case cmGlobalVisualStudioGenerator::VSVersion::VS12:
178 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
180 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
182 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
184 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
190 static const char* VSVersionToAndroidToolset(
191 cmGlobalVisualStudioGenerator::VSVersion v)
194 case cmGlobalVisualStudioGenerator::VSVersion::VS9:
195 case cmGlobalVisualStudioGenerator::VSVersion::VS11:
196 case cmGlobalVisualStudioGenerator::VSVersion::VS12:
198 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
200 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
201 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
202 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
208 static const char vs15generatorName[] = "Visual Studio 15 2017";
210 // Map generator name without year to name with year.
211 static const char* cmVS15GenName(const std::string& name, std::string& genName)
213 if (strncmp(name.c_str(), vs15generatorName,
214 sizeof(vs15generatorName) - 6) != 0) {
217 const char* p = name.c_str() + sizeof(vs15generatorName) - 6;
218 if (cmHasLiteralPrefix(p, " 2017")) {
221 genName = std::string(vs15generatorName) + p;
225 class cmGlobalVisualStudioVersionedGenerator::Factory15
226 : public cmGlobalGeneratorFactory
229 std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
230 const std::string& name, bool allowArch, cmake* cm) const override
233 const char* p = cmVS15GenName(name, genName);
235 return std::unique_ptr<cmGlobalGenerator>();
238 return std::unique_ptr<cmGlobalGenerator>(
239 new cmGlobalVisualStudioVersionedGenerator(
240 cmGlobalVisualStudioGenerator::VSVersion::VS15, cm, genName, ""));
242 if (!allowArch || *p++ != ' ') {
243 return std::unique_ptr<cmGlobalGenerator>();
245 if (strcmp(p, "Win64") == 0) {
246 return std::unique_ptr<cmGlobalGenerator>(
247 new cmGlobalVisualStudioVersionedGenerator(
248 cmGlobalVisualStudioGenerator::VSVersion::VS15, cm, genName, "x64"));
250 if (strcmp(p, "ARM") == 0) {
251 return std::unique_ptr<cmGlobalGenerator>(
252 new cmGlobalVisualStudioVersionedGenerator(
253 cmGlobalVisualStudioGenerator::VSVersion::VS15, cm, genName, "ARM"));
255 return std::unique_ptr<cmGlobalGenerator>();
258 void GetDocumentation(cmDocumentationEntry& entry) const override
260 entry.Name = std::string(vs15generatorName) + " [arch]";
261 entry.Brief = "Generates Visual Studio 2017 project files. "
262 "Optional [arch] can be \"Win64\" or \"ARM\".";
265 std::vector<std::string> GetGeneratorNames() const override
267 std::vector<std::string> names;
268 names.push_back(vs15generatorName);
272 std::vector<std::string> GetGeneratorNamesWithPlatform() const override
274 std::vector<std::string> names;
275 names.push_back(vs15generatorName + std::string(" ARM"));
276 names.push_back(vs15generatorName + std::string(" Win64"));
280 bool SupportsToolset() const override { return true; }
281 bool SupportsPlatform() const override { return true; }
283 std::vector<std::string> GetKnownPlatforms() const override
285 std::vector<std::string> platforms;
286 platforms.emplace_back("x64");
287 platforms.emplace_back("Win32");
288 platforms.emplace_back("ARM");
289 platforms.emplace_back("ARM64");
293 std::string GetDefaultPlatformName() const override { return "Win32"; }
296 std::unique_ptr<cmGlobalGeneratorFactory>
297 cmGlobalVisualStudioVersionedGenerator::NewFactory15()
299 return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory15);
302 static const char vs16generatorName[] = "Visual Studio 16 2019";
303 static const char vs17generatorName[] = "Visual Studio 17 2022";
305 // Map generator name without year to name with year.
306 static const char* cmVS16GenName(const std::string& name, std::string& genName)
308 if (strncmp(name.c_str(), vs16generatorName,
309 sizeof(vs16generatorName) - 6) != 0) {
312 const char* p = name.c_str() + sizeof(vs16generatorName) - 6;
313 if (cmHasLiteralPrefix(p, " 2019")) {
316 genName = std::string(vs16generatorName) + p;
320 static const char* cmVS17GenName(const std::string& name, std::string& genName)
322 if (strncmp(name.c_str(), vs17generatorName,
323 sizeof(vs17generatorName) - 6) != 0) {
326 const char* p = name.c_str() + sizeof(vs17generatorName) - 6;
327 if (cmHasLiteralPrefix(p, " 2022")) {
330 genName = std::string(vs17generatorName) + p;
334 class cmGlobalVisualStudioVersionedGenerator::Factory16
335 : public cmGlobalGeneratorFactory
338 std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
339 const std::string& name, bool /*allowArch*/, cmake* cm) const override
342 const char* p = cmVS16GenName(name, genName);
344 return std::unique_ptr<cmGlobalGenerator>();
347 return std::unique_ptr<cmGlobalGenerator>(
348 new cmGlobalVisualStudioVersionedGenerator(
349 cmGlobalVisualStudioGenerator::VSVersion::VS16, cm, genName, ""));
351 return std::unique_ptr<cmGlobalGenerator>();
354 void GetDocumentation(cmDocumentationEntry& entry) const override
356 entry.Name = std::string(vs16generatorName);
357 entry.Brief = "Generates Visual Studio 2019 project files. "
358 "Use -A option to specify architecture.";
361 std::vector<std::string> GetGeneratorNames() const override
363 std::vector<std::string> names;
364 names.push_back(vs16generatorName);
368 std::vector<std::string> GetGeneratorNamesWithPlatform() const override
370 return std::vector<std::string>();
373 bool SupportsToolset() const override { return true; }
374 bool SupportsPlatform() const override { return true; }
376 std::vector<std::string> GetKnownPlatforms() const override
378 std::vector<std::string> platforms;
379 platforms.emplace_back("x64");
380 platforms.emplace_back("Win32");
381 platforms.emplace_back("ARM");
382 platforms.emplace_back("ARM64");
383 platforms.emplace_back("ARM64EC");
387 std::string GetDefaultPlatformName() const override
389 return VSHostPlatformName();
393 std::unique_ptr<cmGlobalGeneratorFactory>
394 cmGlobalVisualStudioVersionedGenerator::NewFactory16()
396 return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory16);
399 class cmGlobalVisualStudioVersionedGenerator::Factory17
400 : public cmGlobalGeneratorFactory
403 std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
404 const std::string& name, bool /*allowArch*/, cmake* cm) const override
407 const char* p = cmVS17GenName(name, genName);
409 return std::unique_ptr<cmGlobalGenerator>();
412 return std::unique_ptr<cmGlobalGenerator>(
413 new cmGlobalVisualStudioVersionedGenerator(
414 cmGlobalVisualStudioGenerator::VSVersion::VS17, cm, genName, ""));
416 return std::unique_ptr<cmGlobalGenerator>();
419 void GetDocumentation(cmDocumentationEntry& entry) const override
421 entry.Name = std::string(vs17generatorName);
422 entry.Brief = "Generates Visual Studio 2022 project files. "
423 "Use -A option to specify architecture.";
426 std::vector<std::string> GetGeneratorNames() const override
428 std::vector<std::string> names;
429 names.push_back(vs17generatorName);
433 std::vector<std::string> GetGeneratorNamesWithPlatform() const override
435 return std::vector<std::string>();
438 bool SupportsToolset() const override { return true; }
439 bool SupportsPlatform() const override { return true; }
441 std::vector<std::string> GetKnownPlatforms() const override
443 std::vector<std::string> platforms;
444 platforms.emplace_back("x64");
445 platforms.emplace_back("Win32");
446 platforms.emplace_back("ARM");
447 platforms.emplace_back("ARM64");
448 platforms.emplace_back("ARM64EC");
452 std::string GetDefaultPlatformName() const override
454 return VSHostPlatformName();
458 std::unique_ptr<cmGlobalGeneratorFactory>
459 cmGlobalVisualStudioVersionedGenerator::NewFactory17()
461 return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory17);
464 cmGlobalVisualStudioVersionedGenerator::cmGlobalVisualStudioVersionedGenerator(
465 VSVersion version, cmake* cm, const std::string& name,
466 std::string const& platformInGeneratorName)
467 : cmGlobalVisualStudio14Generator(cm, name, platformInGeneratorName)
468 , vsSetupAPIHelper(VSVersionToMajor(version))
470 this->Version = version;
471 this->ExpressEdition = false;
472 this->DefaultPlatformToolset = VSVersionToToolset(this->Version);
473 this->DefaultAndroidToolset = VSVersionToAndroidToolset(this->Version);
474 this->DefaultCLFlagTableName = VSVersionToToolset(this->Version);
475 this->DefaultCSharpFlagTableName = VSVersionToToolset(this->Version);
476 this->DefaultLinkFlagTableName = VSVersionToToolset(this->Version);
477 if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS16) {
478 this->DefaultPlatformName = VSHostPlatformName();
479 this->DefaultPlatformToolsetHostArchitecture =
480 VSHostArchitecture(this->Version);
482 if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS17) {
483 // FIXME: Search for an existing framework? Under '%ProgramFiles(x86)%',
484 // see 'Reference Assemblies\Microsoft\Framework\.NETFramework'.
485 // Use a version installed by VS 2022 without a separate component.
486 this->DefaultTargetFrameworkVersion = "v4.7.2";
490 bool cmGlobalVisualStudioVersionedGenerator::MatchesGeneratorName(
491 const std::string& name) const
494 switch (this->Version) {
495 case cmGlobalVisualStudioGenerator::VSVersion::VS9:
496 case cmGlobalVisualStudioGenerator::VSVersion::VS11:
497 case cmGlobalVisualStudioGenerator::VSVersion::VS12:
498 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
500 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
501 if (cmVS15GenName(name, genName)) {
502 return genName == this->GetName();
505 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
506 if (cmVS16GenName(name, genName)) {
507 return genName == this->GetName();
510 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
511 if (cmVS17GenName(name, genName)) {
512 return genName == this->GetName();
519 bool cmGlobalVisualStudioVersionedGenerator::SetGeneratorInstance(
520 std::string const& i, cmMakefile* mf)
522 if (this->LastGeneratorInstanceString &&
523 i == *(this->LastGeneratorInstanceString)) {
527 if (!this->ParseGeneratorInstance(i, mf)) {
531 if (!this->GeneratorInstanceVersion.empty()) {
532 std::string const majorStr = VSVersionToMajorString(this->Version);
533 cmsys::RegularExpression versionRegex(
534 cmStrCat("^", majorStr, "\\.[0-9]+\\.[0-9]+\\.[0-9]+$"));
535 if (!versionRegex.find(this->GeneratorInstanceVersion)) {
536 std::ostringstream e;
537 /* clang-format off */
540 " " << this->GetName() << "\n"
541 "given instance specification\n"
543 "but the version field is not 4 integer components"
544 " starting in " << majorStr << "."
546 /* clang-format on */
547 mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
552 std::string vsInstance;
555 if (!this->vsSetupAPIHelper.SetVSInstance(
556 this->GeneratorInstance, this->GeneratorInstanceVersion)) {
557 std::ostringstream e;
558 /* clang-format off */
561 " " << this->GetName() << "\n"
562 "could not find specified instance of Visual Studio:\n"
564 /* clang-format on */
565 if (!this->GeneratorInstance.empty() &&
566 this->GeneratorInstanceVersion.empty() &&
567 cmSystemTools::FileIsDirectory(this->GeneratorInstance)) {
569 "The directory exists, but the instance is not known to the "
570 "Visual Studio Installer, and no 'version=' field was given.";
572 mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
575 } else if (!this->vsSetupAPIHelper.GetVSInstanceInfo(vsInstance)) {
576 std::ostringstream e;
577 /* clang-format off */
580 " " << this->GetName() << "\n"
581 "could not find any instance of Visual Studio.\n";
582 /* clang-format on */
583 mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
587 // Save the selected instance persistently.
588 std::string genInstance = mf->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
589 if (vsInstance != genInstance) {
590 this->CMakeInstance->AddCacheEntry("CMAKE_GENERATOR_INSTANCE", vsInstance,
591 "Generator instance identifier.",
592 cmStateEnums::INTERNAL);
595 // The selected instance may have a different MSBuild than previously found.
596 this->MSBuildCommandInitialized = false;
598 this->LastGeneratorInstanceString = i;
603 bool cmGlobalVisualStudioVersionedGenerator::ParseGeneratorInstance(
604 std::string const& is, cmMakefile* mf)
606 this->GeneratorInstance.clear();
607 this->GeneratorInstanceVersion.clear();
609 std::vector<std::string> const fields = cmTokenize(is, ",");
610 std::vector<std::string>::const_iterator fi = fields.begin();
611 if (fi == fields.end()) {
615 // The first field may be the VS instance.
616 if (fi->find('=') == fi->npos) {
617 this->GeneratorInstance = *fi;
621 std::set<std::string> handled;
623 // The rest of the fields must be key=value pairs.
624 for (; fi != fields.end(); ++fi) {
625 std::string::size_type pos = fi->find('=');
626 if (pos == fi->npos) {
627 std::ostringstream e;
628 /* clang-format off */
631 " " << this->GetName() << "\n"
632 "given instance specification\n"
634 "that contains a field after the first ',' with no '='."
636 /* clang-format on */
637 mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
640 std::string const key = fi->substr(0, pos);
641 std::string const value = fi->substr(pos + 1);
642 if (!handled.insert(key).second) {
643 std::ostringstream e;
644 /* clang-format off */
647 " " << this->GetName() << "\n"
648 "given instance specification\n"
650 "that contains duplicate field key '" << key << "'."
652 /* clang-format on */
653 mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
656 if (!this->ProcessGeneratorInstanceField(key, value)) {
657 std::ostringstream e;
658 /* clang-format off */
661 " " << this->GetName() << "\n"
662 "given instance specification\n"
664 "that contains invalid field '" << *fi << "'."
666 /* clang-format on */
667 mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
675 bool cmGlobalVisualStudioVersionedGenerator::ProcessGeneratorInstanceField(
676 std::string const& key, std::string const& value)
678 if (key == "version") {
679 this->GeneratorInstanceVersion = value;
685 bool cmGlobalVisualStudioVersionedGenerator::GetVSInstance(
686 std::string& dir) const
688 return vsSetupAPIHelper.GetVSInstanceInfo(dir);
691 cm::optional<std::string>
692 cmGlobalVisualStudioVersionedGenerator::GetVSInstanceVersion() const
694 cm::optional<std::string> result;
695 std::string vsInstanceVersion;
696 if (vsSetupAPIHelper.GetVSInstanceVersion(vsInstanceVersion)) {
697 result = vsInstanceVersion;
702 bool cmGlobalVisualStudioVersionedGenerator::IsStdOutEncodingSupported() const
704 // Supported from Visual Studio 16.7 Preview 3.
705 if (this->Version > cmGlobalVisualStudioGenerator::VSVersion::VS16) {
708 if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS16) {
711 static std::string const vsVer16_7_P2 = "16.7.30128.36";
712 cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
714 cmSystemTools::VersionCompareGreaterEq(*vsVer, vsVer16_7_P2));
717 bool cmGlobalVisualStudioVersionedGenerator::IsUtf8EncodingSupported() const
719 // Supported from Visual Studio 16.10 Preview 2.
720 if (this->Version > cmGlobalVisualStudioGenerator::VSVersion::VS16) {
723 if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS16) {
726 static std::string const vsVer16_10_P2 = "16.10.31213.239";
727 cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
729 cmSystemTools::VersionCompareGreaterEq(*vsVer, vsVer16_10_P2));
733 cmGlobalVisualStudioVersionedGenerator::GetAndroidApplicationTypeRevision()
736 switch (this->Version) {
737 case cmGlobalVisualStudioGenerator::VSVersion::VS9:
738 case cmGlobalVisualStudioGenerator::VSVersion::VS11:
739 case cmGlobalVisualStudioGenerator::VSVersion::VS12:
741 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
743 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
744 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
745 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
751 cmGlobalVisualStudioVersionedGenerator::AuxToolset
752 cmGlobalVisualStudioVersionedGenerator::FindAuxToolset(
753 std::string& version, std::string& props) const
755 if (version.empty()) {
756 return AuxToolset::None;
759 std::string instancePath;
760 this->GetVSInstance(instancePath);
761 cmSystemTools::ConvertToUnixSlashes(instancePath);
763 // Translate three-component format accepted by "vcvarsall -vcvars_ver=".
764 cmsys::RegularExpression threeComponentRegex(
765 "^([0-9]+\\.[0-9]+)\\.[0-9][0-9][0-9][0-9][0-9]$");
766 // The two-component format represents the two major components of the
767 // three-component format
768 cmsys::RegularExpression twoComponentRegex("^([0-9]+\\.[0-9]+)$");
769 if (threeComponentRegex.find(version)) {
770 // Load "VC/Auxiliary/Build/*/Microsoft.VCToolsVersion.*.txt" files
771 // with two matching components to check their three-component version.
772 std::string const& twoComponent = threeComponentRegex.match(1);
773 std::string pattern =
774 cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s, twoComponent,
775 "*/Microsoft.VCToolsVersion."_s, twoComponent, "*.txt"_s);
777 glob.SetRecurseThroughSymlinks(false);
778 if (glob.FindFiles(pattern)) {
779 for (std::string const& txt : glob.GetFiles()) {
781 cmsys::ifstream fin(txt.c_str());
782 if (fin && std::getline(fin, ver)) {
783 // Strip trailing whitespace.
784 ver = ver.substr(0, ver.find_first_not_of("0123456789."));
785 // If the three-component version matches, translate it to
786 // that used by the "Microsoft.VCToolsVersion.*.txt" file name.
787 if (ver == version) {
788 cmsys::RegularExpression extractVersion(
789 "VCToolsVersion\\.([0-9.]+)\\.txt$");
790 if (extractVersion.find(txt)) {
791 version = extractVersion.match(1);
798 } else if (twoComponentRegex.find(version)) {
799 std::string const& twoComponent = twoComponentRegex.match(1);
800 std::string pattern =
801 cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s, twoComponent,
802 "*/Microsoft.VCToolsVersion."_s, twoComponent, "*.txt"_s);
804 glob.SetRecurseThroughSymlinks(false);
805 if (glob.FindFiles(pattern) && !glob.GetFiles().empty()) {
806 // Since we are only using the first two components of the
807 // toolset version, we require a single match.
808 if (glob.GetFiles().size() == 1) {
809 std::string const& txt = glob.GetFiles()[0];
811 cmsys::ifstream fin(txt.c_str());
812 if (fin && std::getline(fin, ver)) {
813 // Strip trailing whitespace.
814 ver = ver.substr(0, ver.find_first_not_of("0123456789."));
815 // We assume the version is correct, since it is the only one that
817 cmsys::RegularExpression extractVersion(
818 "VCToolsVersion\\.([0-9.]+)\\.txt$");
819 if (extractVersion.find(txt)) {
820 version = extractVersion.match(1);
824 props = cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s);
825 return AuxToolset::PropsIndeterminate;
830 if (cmSystemTools::VersionCompareGreaterEq(version, "14.20")) {
831 props = cmStrCat(instancePath, "/VC/Auxiliary/Build."_s, version,
832 "/Microsoft.VCToolsVersion."_s, version, ".props"_s);
833 if (cmSystemTools::PathExists(props)) {
834 return AuxToolset::PropsExist;
837 props = cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s, version,
838 "/Microsoft.VCToolsVersion."_s, version, ".props"_s);
839 if (cmSystemTools::PathExists(props)) {
840 return AuxToolset::PropsExist;
843 // Accept the toolset version that is default in the current VS version
844 // by matching the name later VS versions will use for the SxS props files.
845 std::string vcToolsetVersion;
846 if (this->vsSetupAPIHelper.GetVCToolsetVersion(vcToolsetVersion)) {
847 // Accept an exact-match (three-component version).
848 if (version == vcToolsetVersion) {
849 return AuxToolset::Default;
852 // Accept known SxS props file names using four version components
853 // in VS versions later than the current.
854 if (version == "14.28.16.9" && vcToolsetVersion == "14.28.29910") {
855 return AuxToolset::Default;
857 if (version == "14.29.16.10" && vcToolsetVersion == "14.29.30037") {
858 return AuxToolset::Default;
860 if (version == "14.29.16.11" && vcToolsetVersion == "14.29.30133") {
861 return AuxToolset::Default;
864 // The first two components of the default toolset version typically
865 // match the name used by later VS versions for the SxS props files.
866 cmsys::RegularExpression twoComponent("^([0-9]+\\.[0-9]+)");
867 if (twoComponent.find(version)) {
868 std::string const versionPrefix = cmStrCat(twoComponent.match(1), '.');
869 if (cmHasPrefix(vcToolsetVersion, versionPrefix)) {
870 return AuxToolset::Default;
875 return AuxToolset::PropsMissing;
878 bool cmGlobalVisualStudioVersionedGenerator::InitializeWindows(cmMakefile* mf)
880 // If the Win 8.1 SDK is installed then we can select a SDK matching
881 // the target Windows version.
882 if (this->IsWin81SDKInstalled()) {
883 // VS 2019 does not default to 8.1 so specify it explicitly when needed.
884 if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS16 &&
885 !cmSystemTools::VersionCompareGreater(this->SystemVersion, "8.1")) {
886 this->SetWindowsTargetPlatformVersion("8.1", mf);
889 return cmGlobalVisualStudio14Generator::InitializeWindows(mf);
891 // Otherwise we must choose a Win 10 SDK even if we are not targeting
893 return this->SelectWindows10SDK(mf, false);
896 bool cmGlobalVisualStudioVersionedGenerator::SelectWindowsStoreToolset(
897 std::string& toolset) const
899 if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
900 if (this->IsWindowsStoreToolsetInstalled() &&
901 this->IsWindowsDesktopToolsetInstalled()) {
902 toolset = VSVersionToToolset(this->Version);
908 return this->cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
912 bool cmGlobalVisualStudioVersionedGenerator::IsWindowsDesktopToolsetInstalled()
915 return vsSetupAPIHelper.IsVSInstalled();
918 bool cmGlobalVisualStudioVersionedGenerator::IsWindowsStoreToolsetInstalled()
921 return vsSetupAPIHelper.IsWin10SDKInstalled();
924 bool cmGlobalVisualStudioVersionedGenerator::IsWin81SDKInstalled() const
926 // Does the VS installer tool know about one?
927 if (vsSetupAPIHelper.IsWin81SDKInstalled()) {
931 // Does the registry know about one (e.g. from VS 2015)?
932 std::string win81Root;
933 if (cmSystemTools::ReadRegistryValue(
934 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
935 "Windows Kits\\Installed Roots;KitsRoot81",
936 win81Root, cmSystemTools::KeyWOW64_32) ||
937 cmSystemTools::ReadRegistryValue(
938 "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
939 "Windows Kits\\Installed Roots;KitsRoot81",
940 win81Root, cmSystemTools::KeyWOW64_32)) {
941 return cmSystemTools::FileExists(win81Root + "/include/um/windows.h",
948 cmGlobalVisualStudioVersionedGenerator::GetWindows10SDKMaxVersionDefault(
951 return std::string();
954 cm::optional<std::string>
955 cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommandEarly(cmMakefile* mf)
957 std::string instance = mf->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
958 if (!this->SetGeneratorInstance(instance, mf)) {
959 cmSystemTools::SetFatalErrorOccurred();
962 return this->cmGlobalVisualStudio14Generator::FindMSBuildCommandEarly(mf);
965 std::string cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommand()
969 // Ask Visual Studio Installer tool.
971 if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
972 if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS17) {
973 if (VSIsArm64Host()) {
974 if (VSHasDotNETFrameworkArm64()) {
975 msbuild = vs + "/MSBuild/Current/Bin/arm64/MSBuild.exe";
976 if (cmSystemTools::FileExists(msbuild)) {
980 if (VSIsWindows11OrGreater()) {
981 msbuild = vs + "/MSBuild/Current/Bin/amd64/MSBuild.exe";
982 if (cmSystemTools::FileExists(msbuild)) {
987 msbuild = vs + "/MSBuild/Current/Bin/amd64/MSBuild.exe";
988 if (cmSystemTools::FileExists(msbuild)) {
993 msbuild = vs + "/MSBuild/Current/Bin/MSBuild.exe";
994 if (cmSystemTools::FileExists(msbuild)) {
997 msbuild = vs + "/MSBuild/15.0/Bin/MSBuild.exe";
998 if (cmSystemTools::FileExists(msbuild)) {
1003 msbuild = "MSBuild.exe";
1007 std::string cmGlobalVisualStudioVersionedGenerator::FindDevEnvCommand()
1011 // Ask Visual Studio Installer tool.
1013 if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
1014 devenv = vs + "/Common7/IDE/devenv.com";
1015 if (cmSystemTools::FileExists(devenv)) {
1020 devenv = "devenv.com";