CMake Nightly Date Stamp
[kiteware-cmake.git] / Source / cmGlobalVisualStudioVersionedGenerator.cxx
blobb4683aa055259e7924a7fb86f56b7b8dba08f87d
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"
5 #include <cstring>
6 #include <set>
7 #include <sstream>
8 #include <utility>
9 #include <vector>
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"
25 #include "cmake.h"
27 #ifndef IMAGE_FILE_MACHINE_ARM64
28 # define IMAGE_FILE_MACHINE_ARM64 0xaa64 // ARM64 Little-Endian
29 #endif
31 static bool VSIsWow64()
33 BOOL isWow64 = false;
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"
46 #endif
47 static const CM_ISWOW64PROCESS2 s_IsWow64Process2Impl =
48 (CM_ISWOW64PROCESS2)GetProcAddress(
49 GetModuleHandleW(L"api-ms-win-core-wow64-l1-1-1.dll"),
50 "IsWow64Process2");
51 #ifdef CM_VS_GCC_DIAGNOSTIC_PUSHED
52 # pragma GCC diagnostic pop
53 # undef CM_VS_GCC_DIAGNOSTIC_PUSHED
54 #endif
56 USHORT processMachine;
57 USHORT nativeMachine;
59 return s_IsWow64Process2Impl != nullptr &&
60 s_IsWow64Process2Impl(GetCurrentProcess(), &processMachine,
61 &nativeMachine) &&
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()) {
88 return "ARM64";
90 if (VSIsWow64()) {
91 return "x64";
93 #if defined(_M_ARM)
94 return "ARM";
95 #elif defined(_M_IA64)
96 return "Itanium";
97 #elif defined(_WIN64)
98 return "x64";
99 #else
100 return "Win32";
101 #endif
104 static std::string VSHostArchitecture(
105 cmGlobalVisualStudioGenerator::VSVersion v)
107 if (VSIsArm64Host()) {
108 return v >= cmGlobalVisualStudioGenerator::VSVersion::VS17 ? "ARM64" : "";
110 if (VSIsWow64()) {
111 return "x64";
113 #if defined(_M_ARM)
114 return "";
115 #elif defined(_M_IA64)
116 return "";
117 #elif defined(_WIN64)
118 return "x64";
119 #else
120 return "x86";
121 #endif
124 static unsigned int VSVersionToMajor(
125 cmGlobalVisualStudioGenerator::VSVersion v)
127 switch (v) {
128 case cmGlobalVisualStudioGenerator::VSVersion::VS12:
129 return 12;
130 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
131 return 14;
132 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
133 return 15;
134 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
135 return 16;
136 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
137 return 17;
139 return 0;
142 static const char* VSVersionToToolset(
143 cmGlobalVisualStudioGenerator::VSVersion v)
145 switch (v) {
146 case cmGlobalVisualStudioGenerator::VSVersion::VS12:
147 return "v120";
148 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
149 return "v140";
150 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
151 return "v141";
152 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
153 return "v142";
154 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
155 return "v143";
157 return "";
160 static std::string VSVersionToMajorString(
161 cmGlobalVisualStudioGenerator::VSVersion v)
163 switch (v) {
164 case cmGlobalVisualStudioGenerator::VSVersion::VS12:
165 return "12";
166 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
167 return "14";
168 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
169 return "15";
170 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
171 return "16";
172 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
173 return "17";
175 return "";
178 static const char* VSVersionToAndroidToolset(
179 cmGlobalVisualStudioGenerator::VSVersion v)
181 switch (v) {
182 case cmGlobalVisualStudioGenerator::VSVersion::VS12:
183 return "";
184 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
185 return "Clang_3_8";
186 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
187 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
188 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
189 return "Clang_5_0";
191 return "";
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) {
201 return nullptr;
203 const char* p = name.c_str() + sizeof(vs15generatorName) - 6;
204 if (cmHasLiteralPrefix(p, " 2017")) {
205 p += 5;
207 genName = cmStrCat(vs15generatorName, p);
208 return p;
211 class cmGlobalVisualStudioVersionedGenerator::Factory15
212 : public cmGlobalGeneratorFactory
214 public:
215 std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
216 const std::string& name, bool allowArch, cmake* cm) const override
218 std::string genName;
219 const char* p = cmVS15GenName(name, genName);
220 if (!p) {
221 return std::unique_ptr<cmGlobalGenerator>();
223 if (!*p) {
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);
255 return names;
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"));
263 return names;
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");
276 return platforms;
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) {
296 return nullptr;
298 const char* p = name.c_str() + sizeof(vs16generatorName) - 6;
299 if (cmHasLiteralPrefix(p, " 2019")) {
300 p += 5;
302 genName = cmStrCat(vs16generatorName, p);
303 return 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) {
310 return nullptr;
312 const char* p = name.c_str() + sizeof(vs17generatorName) - 6;
313 if (cmHasLiteralPrefix(p, " 2022")) {
314 p += 5;
316 genName = cmStrCat(vs17generatorName, p);
317 return p;
320 class cmGlobalVisualStudioVersionedGenerator::Factory16
321 : public cmGlobalGeneratorFactory
323 public:
324 std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
325 const std::string& name, bool /*allowArch*/, cmake* cm) const override
327 std::string genName;
328 const char* p = cmVS16GenName(name, genName);
329 if (!p) {
330 return std::unique_ptr<cmGlobalGenerator>();
332 if (!*p) {
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);
351 return names;
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");
370 return platforms;
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
388 public:
389 std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
390 const std::string& name, bool /*allowArch*/, cmake* cm) const override
392 std::string genName;
393 const char* p = cmVS17GenName(name, genName);
394 if (!p) {
395 return std::unique_ptr<cmGlobalGenerator>();
397 if (!*p) {
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);
416 return names;
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");
435 return platforms;
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
479 std::string genName;
480 switch (this->Version) {
481 case cmGlobalVisualStudioGenerator::VSVersion::VS12:
482 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
483 break;
484 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
485 if (cmVS15GenName(name, genName)) {
486 return genName == this->GetName();
488 break;
489 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
490 if (cmVS16GenName(name, genName)) {
491 return genName == this->GetName();
493 break;
494 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
495 if (cmVS17GenName(name, genName)) {
496 return genName == this->GetName();
498 break;
500 return false;
503 bool cmGlobalVisualStudioVersionedGenerator::SetGeneratorInstance(
504 std::string const& i, cmMakefile* mf)
506 if (this->LastGeneratorInstanceString &&
507 i == *(this->LastGeneratorInstanceString)) {
508 this->SetVSVersionVar(mf);
509 return true;
512 if (!this->ParseGeneratorInstance(i, mf)) {
513 return false;
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)) {
521 mf->IssueMessage(
522 MessageType::FATAL_ERROR,
523 cmStrCat("Generator\n"
524 " ",
525 this->GetName(),
526 "\n"
527 "given instance specification\n"
528 " ",
530 "\n"
531 "but the version field is not 4 integer components"
532 " starting in ",
533 majorStr, '.'));
534 return false;
538 std::string vsInstance;
539 if (!i.empty()) {
540 vsInstance = i;
541 if (!this->vsSetupAPIHelper.SetVSInstance(
542 this->GeneratorInstance, this->GeneratorInstanceVersion)) {
543 std::ostringstream e;
544 /* clang-format off */
545 e <<
546 "Generator\n"
547 " " << this->GetName() << "\n"
548 "could not find specified instance of Visual Studio:\n"
549 " " << i;
550 /* clang-format on */
551 if (!this->GeneratorInstance.empty() &&
552 this->GeneratorInstanceVersion.empty() &&
553 cmSystemTools::FileIsDirectory(this->GeneratorInstance)) {
554 e << "\n"
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());
559 return false;
561 } else if (!this->vsSetupAPIHelper.GetVSInstanceInfo(vsInstance)) {
562 mf->IssueMessage(
563 MessageType::FATAL_ERROR,
564 cmStrCat("Generator\n"
565 " ",
566 this->GetName(),
567 "\n"
568 "could not find any instance of Visual Studio.\n"));
569 return false;
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;
587 return true;
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()) {
599 return true;
602 // The first field may be the VS instance.
603 if (fi->find('=') == fi->npos) {
604 this->GeneratorInstance = *fi;
605 ++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) {
614 mf->IssueMessage(
615 MessageType::FATAL_ERROR,
616 cmStrCat("Generator\n"
617 " ",
618 this->GetName(),
619 "\n"
620 "given instance specification\n"
621 " ",
623 "\n"
624 "that contains a field after the first ',' with no '='."));
625 return false;
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"
632 " ",
633 this->GetName(),
634 "\n"
635 "given instance specification\n"
636 " ",
638 "\n"
639 "that contains duplicate field key '",
640 key, "'."));
641 return false;
643 if (!this->ProcessGeneratorInstanceField(key, value)) {
644 mf->IssueMessage(MessageType::FATAL_ERROR,
645 cmStrCat("Generator\n"
646 " ",
647 this->GetName(),
648 "\n"
649 "given instance specification\n"
650 " ",
652 "\n"
653 "that contains invalid field '",
654 *fi, "'."));
655 return false;
659 return true;
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;
674 return true;
676 return false;
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;
693 return result;
696 bool cmGlobalVisualStudioVersionedGenerator::IsStdOutEncodingSupported() const
698 // Supported from Visual Studio 16.7 Preview 3.
699 if (this->Version > cmGlobalVisualStudioGenerator::VSVersion::VS16) {
700 return true;
702 if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS16) {
703 return false;
705 static std::string const vsVer16_7_P2 = "16.7.30128.36";
706 cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
707 return (vsVer &&
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) {
715 return true;
717 if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS16) {
718 return false;
720 static std::string const vsVer16_10_P2 = "16.10.31213.239";
721 cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
722 return (vsVer &&
723 cmSystemTools::VersionCompareGreaterEq(*vsVer, vsVer16_10_P2));
726 bool cmGlobalVisualStudioVersionedGenerator::IsScanDependenciesSupported()
727 const
729 // Supported from Visual Studio 17.6 Preview 7.
730 if (this->Version > cmGlobalVisualStudioGenerator::VSVersion::VS17) {
731 return true;
733 if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS17) {
734 return false;
736 static std::string const vsVer17_6_P7 = "17.6.33706.43";
737 cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
738 return (vsVer &&
739 cmSystemTools::VersionCompareGreaterEq(*vsVer, vsVer17_6_P7));
742 const char*
743 cmGlobalVisualStudioVersionedGenerator::GetAndroidApplicationTypeRevision()
744 const
746 switch (this->Version) {
747 case cmGlobalVisualStudioGenerator::VSVersion::VS12:
748 return "";
749 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
750 return "2.0";
751 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
752 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
753 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
754 return "3.0";
756 return "";
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);
784 cmsys::Glob glob;
785 glob.SetRecurseThroughSymlinks(false);
786 if (glob.FindFiles(pattern)) {
787 for (std::string const& txt : glob.GetFiles()) {
788 std::string ver;
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);
800 break;
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);
811 cmsys::Glob glob;
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];
818 std::string ver;
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
824 // matched.
825 cmsys::RegularExpression extractVersion(
826 "VCToolsVersion\\.([0-9.]+)\\.txt$");
827 if (extractVersion.find(txt)) {
828 version = extractVersion.match(1);
831 } else {
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);
893 return true;
895 return false;
897 return this->cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
898 toolset);
901 bool cmGlobalVisualStudioVersionedGenerator::IsWindowsDesktopToolsetInstalled()
902 const
904 return vsSetupAPIHelper.IsVSInstalled();
907 bool cmGlobalVisualStudioVersionedGenerator::IsWindowsStoreToolsetInstalled()
908 const
910 return vsSetupAPIHelper.IsWin10SDKInstalled();
913 bool cmGlobalVisualStudioVersionedGenerator::IsWin81SDKInstalled() const
915 // Does the VS installer tool know about one?
916 if (vsSetupAPIHelper.IsWin81SDKInstalled()) {
917 return true;
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);
933 return false;
936 std::string
937 cmGlobalVisualStudioVersionedGenerator::GetWindows10SDKMaxVersionDefault(
938 cmMakefile*) const
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();
949 return {};
951 return this->cmGlobalVisualStudio14Generator::FindMSBuildCommandEarly(mf);
954 std::string cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommand()
956 std::string msbuild;
958 // Ask Visual Studio Installer tool.
959 std::string vs;
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)) {
966 return msbuild;
969 if (VSIsWindows11OrGreater()) {
970 msbuild = cmStrCat(vs, "/MSBuild/Current/Bin/amd64/MSBuild.exe");
971 if (cmSystemTools::FileExists(msbuild)) {
972 return msbuild;
975 } else {
976 msbuild = cmStrCat(vs, "/MSBuild/Current/Bin/amd64/MSBuild.exe");
977 if (cmSystemTools::FileExists(msbuild)) {
978 return msbuild;
982 msbuild = cmStrCat(vs, "/MSBuild/Current/Bin/MSBuild.exe");
983 if (cmSystemTools::FileExists(msbuild)) {
984 return msbuild;
986 msbuild = cmStrCat(vs, "/MSBuild/15.0/Bin/MSBuild.exe");
987 if (cmSystemTools::FileExists(msbuild)) {
988 return msbuild;
992 msbuild = "MSBuild.exe";
993 return msbuild;
996 std::string cmGlobalVisualStudioVersionedGenerator::FindDevEnvCommand()
998 std::string devenv;
1000 // Ask Visual Studio Installer tool.
1001 std::string vs;
1002 if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
1003 devenv = cmStrCat(vs, "/Common7/IDE/devenv.com");
1004 if (cmSystemTools::FileExists(devenv)) {
1005 return devenv;
1009 devenv = "devenv.com";
1010 return devenv;