Merge topic 'ci-script-cmake-version'
[kiteware-cmake.git] / Source / cmGlobalVisualStudioVersionedGenerator.cxx
blob14460fd765562c487aa7565732d176cd7c08d1b0
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 &&
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::VS14:
129 return 14;
130 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
131 return 15;
132 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
133 return 16;
134 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
135 return 17;
137 return 0;
140 static const char* VSVersionToToolset(
141 cmGlobalVisualStudioGenerator::VSVersion v)
143 switch (v) {
144 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
145 return "v140";
146 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
147 return "v141";
148 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
149 return "v142";
150 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
151 return "v143";
153 return "";
156 static std::string VSVersionToMajorString(
157 cmGlobalVisualStudioGenerator::VSVersion v)
159 switch (v) {
160 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
161 return "14";
162 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
163 return "15";
164 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
165 return "16";
166 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
167 return "17";
169 return "";
172 static const char* VSVersionToAndroidToolset(
173 cmGlobalVisualStudioGenerator::VSVersion v)
175 switch (v) {
176 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
177 return "Clang_3_8";
178 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
179 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
180 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
181 return "Clang_5_0";
183 return "";
186 static const char vs15generatorName[] = "Visual Studio 15 2017";
188 // Map generator name without year to name with year.
189 static const char* cmVS15GenName(const std::string& name, std::string& genName)
191 if (strncmp(name.c_str(), vs15generatorName,
192 sizeof(vs15generatorName) - 6) != 0) {
193 return nullptr;
195 const char* p = name.c_str() + sizeof(vs15generatorName) - 6;
196 if (cmHasLiteralPrefix(p, " 2017")) {
197 p += 5;
199 genName = cmStrCat(vs15generatorName, p);
200 return p;
203 class cmGlobalVisualStudioVersionedGenerator::Factory15
204 : public cmGlobalGeneratorFactory
206 public:
207 std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
208 const std::string& name, bool allowArch, cmake* cm) const override
210 std::string genName;
211 const char* p = cmVS15GenName(name, genName);
212 if (!p) {
213 return std::unique_ptr<cmGlobalGenerator>();
215 if (!*p) {
216 return std::unique_ptr<cmGlobalGenerator>(
217 new cmGlobalVisualStudioVersionedGenerator(
218 cmGlobalVisualStudioGenerator::VSVersion::VS15, cm, genName, ""));
220 if (!allowArch || *p++ != ' ') {
221 return std::unique_ptr<cmGlobalGenerator>();
223 if (strcmp(p, "Win64") == 0) {
224 return std::unique_ptr<cmGlobalGenerator>(
225 new cmGlobalVisualStudioVersionedGenerator(
226 cmGlobalVisualStudioGenerator::VSVersion::VS15, cm, genName, "x64"));
228 if (strcmp(p, "ARM") == 0) {
229 return std::unique_ptr<cmGlobalGenerator>(
230 new cmGlobalVisualStudioVersionedGenerator(
231 cmGlobalVisualStudioGenerator::VSVersion::VS15, cm, genName, "ARM"));
233 return std::unique_ptr<cmGlobalGenerator>();
236 cmDocumentationEntry GetDocumentation() const override
238 return { cmStrCat(vs15generatorName, " [arch]"),
239 "Generates Visual Studio 2017 project files. "
240 "Optional [arch] can be \"Win64\" or \"ARM\"." };
243 std::vector<std::string> GetGeneratorNames() const override
245 std::vector<std::string> names;
246 names.push_back(vs15generatorName);
247 return names;
250 std::vector<std::string> GetGeneratorNamesWithPlatform() const override
252 std::vector<std::string> names;
253 names.emplace_back(cmStrCat(vs15generatorName, " ARM"));
254 names.emplace_back(cmStrCat(vs15generatorName, " Win64"));
255 return names;
258 bool SupportsToolset() const override { return true; }
259 bool SupportsPlatform() const override { return true; }
261 std::vector<std::string> GetKnownPlatforms() const override
263 std::vector<std::string> platforms;
264 platforms.emplace_back("x64");
265 platforms.emplace_back("Win32");
266 platforms.emplace_back("ARM");
267 platforms.emplace_back("ARM64");
268 return platforms;
271 std::string GetDefaultPlatformName() const override { return "Win32"; }
274 std::unique_ptr<cmGlobalGeneratorFactory>
275 cmGlobalVisualStudioVersionedGenerator::NewFactory15()
277 return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory15);
280 static const char vs16generatorName[] = "Visual Studio 16 2019";
281 static const char vs17generatorName[] = "Visual Studio 17 2022";
283 // Map generator name without year to name with year.
284 static const char* cmVS16GenName(const std::string& name, std::string& genName)
286 if (strncmp(name.c_str(), vs16generatorName,
287 sizeof(vs16generatorName) - 6) != 0) {
288 return nullptr;
290 const char* p = name.c_str() + sizeof(vs16generatorName) - 6;
291 if (cmHasLiteralPrefix(p, " 2019")) {
292 p += 5;
294 genName = cmStrCat(vs16generatorName, p);
295 return p;
298 static const char* cmVS17GenName(const std::string& name, std::string& genName)
300 if (strncmp(name.c_str(), vs17generatorName,
301 sizeof(vs17generatorName) - 6) != 0) {
302 return nullptr;
304 const char* p = name.c_str() + sizeof(vs17generatorName) - 6;
305 if (cmHasLiteralPrefix(p, " 2022")) {
306 p += 5;
308 genName = cmStrCat(vs17generatorName, p);
309 return p;
312 class cmGlobalVisualStudioVersionedGenerator::Factory16
313 : public cmGlobalGeneratorFactory
315 public:
316 std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
317 const std::string& name, bool /*allowArch*/, cmake* cm) const override
319 std::string genName;
320 const char* p = cmVS16GenName(name, genName);
321 if (!p) {
322 return std::unique_ptr<cmGlobalGenerator>();
324 if (!*p) {
325 return std::unique_ptr<cmGlobalGenerator>(
326 new cmGlobalVisualStudioVersionedGenerator(
327 cmGlobalVisualStudioGenerator::VSVersion::VS16, cm, genName, ""));
329 return std::unique_ptr<cmGlobalGenerator>();
332 cmDocumentationEntry GetDocumentation() const override
334 return { std::string(vs16generatorName),
335 "Generates Visual Studio 2019 project files. "
336 "Use -A option to specify architecture." };
339 std::vector<std::string> GetGeneratorNames() const override
341 std::vector<std::string> names;
342 names.push_back(vs16generatorName);
343 return names;
346 std::vector<std::string> GetGeneratorNamesWithPlatform() const override
348 return std::vector<std::string>();
351 bool SupportsToolset() const override { return true; }
352 bool SupportsPlatform() const override { return true; }
354 std::vector<std::string> GetKnownPlatforms() const override
356 std::vector<std::string> platforms;
357 platforms.emplace_back("x64");
358 platforms.emplace_back("Win32");
359 platforms.emplace_back("ARM");
360 platforms.emplace_back("ARM64");
361 platforms.emplace_back("ARM64EC");
362 return platforms;
365 std::string GetDefaultPlatformName() const override
367 return VSHostPlatformName();
371 std::unique_ptr<cmGlobalGeneratorFactory>
372 cmGlobalVisualStudioVersionedGenerator::NewFactory16()
374 return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory16);
377 class cmGlobalVisualStudioVersionedGenerator::Factory17
378 : public cmGlobalGeneratorFactory
380 public:
381 std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
382 const std::string& name, bool /*allowArch*/, cmake* cm) const override
384 std::string genName;
385 const char* p = cmVS17GenName(name, genName);
386 if (!p) {
387 return std::unique_ptr<cmGlobalGenerator>();
389 if (!*p) {
390 return std::unique_ptr<cmGlobalGenerator>(
391 new cmGlobalVisualStudioVersionedGenerator(
392 cmGlobalVisualStudioGenerator::VSVersion::VS17, cm, genName, ""));
394 return std::unique_ptr<cmGlobalGenerator>();
397 cmDocumentationEntry GetDocumentation() const override
399 return { std::string(vs17generatorName),
400 "Generates Visual Studio 2022 project files. "
401 "Use -A option to specify architecture." };
404 std::vector<std::string> GetGeneratorNames() const override
406 std::vector<std::string> names;
407 names.push_back(vs17generatorName);
408 return names;
411 std::vector<std::string> GetGeneratorNamesWithPlatform() const override
413 return std::vector<std::string>();
416 bool SupportsToolset() const override { return true; }
417 bool SupportsPlatform() const override { return true; }
419 std::vector<std::string> GetKnownPlatforms() const override
421 std::vector<std::string> platforms;
422 platforms.emplace_back("x64");
423 platforms.emplace_back("Win32");
424 platforms.emplace_back("ARM");
425 platforms.emplace_back("ARM64");
426 platforms.emplace_back("ARM64EC");
427 return platforms;
430 std::string GetDefaultPlatformName() const override
432 return VSHostPlatformName();
436 std::unique_ptr<cmGlobalGeneratorFactory>
437 cmGlobalVisualStudioVersionedGenerator::NewFactory17()
439 return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory17);
442 cmGlobalVisualStudioVersionedGenerator::cmGlobalVisualStudioVersionedGenerator(
443 VSVersion version, cmake* cm, const std::string& name,
444 std::string const& platformInGeneratorName)
445 : cmGlobalVisualStudio14Generator(cm, name, platformInGeneratorName)
446 , vsSetupAPIHelper(VSVersionToMajor(version))
448 this->Version = version;
449 this->ExpressEdition = false;
450 this->DefaultPlatformToolset = VSVersionToToolset(this->Version);
451 this->DefaultAndroidToolset = VSVersionToAndroidToolset(this->Version);
452 this->DefaultCLFlagTableName = VSVersionToToolset(this->Version);
453 this->DefaultCSharpFlagTableName = VSVersionToToolset(this->Version);
454 this->DefaultLinkFlagTableName = VSVersionToToolset(this->Version);
455 if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS16) {
456 this->DefaultPlatformName = VSHostPlatformName();
457 this->DefaultPlatformToolsetHostArchitecture =
458 VSHostArchitecture(this->Version);
460 if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS17) {
461 // FIXME: Search for an existing framework? Under '%ProgramFiles(x86)%',
462 // see 'Reference Assemblies\Microsoft\Framework\.NETFramework'.
463 // Use a version installed by VS 2022 without a separate component.
464 this->DefaultTargetFrameworkVersion = "v4.7.2";
468 bool cmGlobalVisualStudioVersionedGenerator::MatchesGeneratorName(
469 const std::string& name) const
471 std::string genName;
472 switch (this->Version) {
473 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
474 break;
475 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
476 if (cmVS15GenName(name, genName)) {
477 return genName == this->GetName();
479 break;
480 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
481 if (cmVS16GenName(name, genName)) {
482 return genName == this->GetName();
484 break;
485 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
486 if (cmVS17GenName(name, genName)) {
487 return genName == this->GetName();
489 break;
491 return false;
494 bool cmGlobalVisualStudioVersionedGenerator::SetGeneratorInstance(
495 std::string const& i, cmMakefile* mf)
497 if (this->LastGeneratorInstanceString &&
498 i == *(this->LastGeneratorInstanceString)) {
499 this->SetVSVersionVar(mf);
500 return true;
503 if (!this->ParseGeneratorInstance(i, mf)) {
504 return false;
507 if (!this->GeneratorInstanceVersion.empty()) {
508 std::string const majorStr = VSVersionToMajorString(this->Version);
509 cmsys::RegularExpression versionRegex(
510 cmStrCat('^', majorStr, R"(\.[0-9]+\.[0-9]+\.[0-9]+$)"));
511 if (!versionRegex.find(this->GeneratorInstanceVersion)) {
512 mf->IssueMessage(
513 MessageType::FATAL_ERROR,
514 cmStrCat("Generator\n"
515 " ",
516 this->GetName(),
517 "\n"
518 "given instance specification\n"
519 " ",
521 "\n"
522 "but the version field is not 4 integer components"
523 " starting in ",
524 majorStr, '.'));
525 return false;
529 std::string vsInstance;
530 if (!i.empty()) {
531 vsInstance = i;
532 if (!this->vsSetupAPIHelper.SetVSInstance(
533 this->GeneratorInstance, this->GeneratorInstanceVersion)) {
534 std::ostringstream e;
535 /* clang-format off */
536 e <<
537 "Generator\n"
538 " " << this->GetName() << "\n"
539 "could not find specified instance of Visual Studio:\n"
540 " " << i;
541 /* clang-format on */
542 if (!this->GeneratorInstance.empty() &&
543 this->GeneratorInstanceVersion.empty() &&
544 cmSystemTools::FileIsDirectory(this->GeneratorInstance)) {
545 e << "\n"
546 "The directory exists, but the instance is not known to the "
547 "Visual Studio Installer, and no 'version=' field was given.";
549 mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
550 return false;
552 } else if (!this->vsSetupAPIHelper.GetVSInstanceInfo(vsInstance)) {
553 mf->IssueMessage(
554 MessageType::FATAL_ERROR,
555 cmStrCat("Generator\n"
556 " ",
557 this->GetName(),
558 "\n"
559 "could not find any instance of Visual Studio.\n"));
560 return false;
563 // Save the selected instance persistently.
564 std::string genInstance = mf->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
565 if (vsInstance != genInstance) {
566 this->CMakeInstance->AddCacheEntry("CMAKE_GENERATOR_INSTANCE", vsInstance,
567 "Generator instance identifier.",
568 cmStateEnums::INTERNAL);
571 this->SetVSVersionVar(mf);
573 // The selected instance may have a different MSBuild than previously found.
574 this->MSBuildCommandInitialized = false;
576 this->LastGeneratorInstanceString = i;
578 return true;
581 bool cmGlobalVisualStudioVersionedGenerator::ParseGeneratorInstance(
582 std::string const& is, cmMakefile* mf)
584 this->GeneratorInstance.clear();
585 this->GeneratorInstanceVersion.clear();
587 std::vector<std::string> const fields = cmTokenize(is, ",");
588 auto fi = fields.begin();
589 if (fi == fields.end()) {
590 return true;
593 // The first field may be the VS instance.
594 if (fi->find('=') == fi->npos) {
595 this->GeneratorInstance = *fi;
596 ++fi;
599 std::set<std::string> handled;
601 // The rest of the fields must be key=value pairs.
602 for (; fi != fields.end(); ++fi) {
603 std::string::size_type pos = fi->find('=');
604 if (pos == fi->npos) {
605 mf->IssueMessage(
606 MessageType::FATAL_ERROR,
607 cmStrCat("Generator\n"
608 " ",
609 this->GetName(),
610 "\n"
611 "given instance specification\n"
612 " ",
614 "\n"
615 "that contains a field after the first ',' with no '='."));
616 return false;
618 std::string const key = fi->substr(0, pos);
619 std::string const value = fi->substr(pos + 1);
620 if (!handled.insert(key).second) {
621 mf->IssueMessage(MessageType::FATAL_ERROR,
622 cmStrCat("Generator\n"
623 " ",
624 this->GetName(),
625 "\n"
626 "given instance specification\n"
627 " ",
629 "\n"
630 "that contains duplicate field key '",
631 key, "'."));
632 return false;
634 if (!this->ProcessGeneratorInstanceField(key, value)) {
635 mf->IssueMessage(MessageType::FATAL_ERROR,
636 cmStrCat("Generator\n"
637 " ",
638 this->GetName(),
639 "\n"
640 "given instance specification\n"
641 " ",
643 "\n"
644 "that contains invalid field '",
645 *fi, "'."));
646 return false;
650 return true;
653 void cmGlobalVisualStudioVersionedGenerator::SetVSVersionVar(cmMakefile* mf)
655 if (cm::optional<std::string> vsVer = this->GetVSInstanceVersion()) {
656 mf->AddDefinition("CMAKE_VS_VERSION_BUILD_NUMBER", *vsVer);
660 bool cmGlobalVisualStudioVersionedGenerator::ProcessGeneratorInstanceField(
661 std::string const& key, std::string const& value)
663 if (key == "version"_s) {
664 this->GeneratorInstanceVersion = value;
665 return true;
667 return false;
670 bool cmGlobalVisualStudioVersionedGenerator::GetVSInstance(
671 std::string& dir) const
673 return vsSetupAPIHelper.GetVSInstanceInfo(dir);
676 cm::optional<std::string>
677 cmGlobalVisualStudioVersionedGenerator::GetVSInstanceVersion() const
679 cm::optional<std::string> result;
680 std::string vsInstanceVersion;
681 if (vsSetupAPIHelper.GetVSInstanceVersion(vsInstanceVersion)) {
682 result = vsInstanceVersion;
684 return result;
687 bool cmGlobalVisualStudioVersionedGenerator::IsStdOutEncodingSupported() const
689 // Supported from Visual Studio 16.7 Preview 3.
690 if (this->Version > cmGlobalVisualStudioGenerator::VSVersion::VS16) {
691 return true;
693 if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS16) {
694 return false;
696 static std::string const vsVer16_7_P2 = "16.7.30128.36";
697 cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
698 return (vsVer &&
699 cmSystemTools::VersionCompareGreaterEq(*vsVer, vsVer16_7_P2));
702 bool cmGlobalVisualStudioVersionedGenerator::IsUtf8EncodingSupported() const
704 // Supported from Visual Studio 16.10 Preview 2.
705 if (this->Version > cmGlobalVisualStudioGenerator::VSVersion::VS16) {
706 return true;
708 if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS16) {
709 return false;
711 static std::string const vsVer16_10_P2 = "16.10.31213.239";
712 cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
713 return (vsVer &&
714 cmSystemTools::VersionCompareGreaterEq(*vsVer, vsVer16_10_P2));
717 bool cmGlobalVisualStudioVersionedGenerator::IsScanDependenciesSupported()
718 const
720 // Supported from Visual Studio 17.6 Preview 7.
721 if (this->Version > cmGlobalVisualStudioGenerator::VSVersion::VS17) {
722 return true;
724 if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS17) {
725 return false;
727 static std::string const vsVer17_6_P7 = "17.6.33706.43";
728 cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
729 return (vsVer &&
730 cmSystemTools::VersionCompareGreaterEq(*vsVer, vsVer17_6_P7));
733 const char*
734 cmGlobalVisualStudioVersionedGenerator::GetAndroidApplicationTypeRevision()
735 const
737 switch (this->Version) {
738 case cmGlobalVisualStudioGenerator::VSVersion::VS14:
739 return "2.0";
740 case cmGlobalVisualStudioGenerator::VSVersion::VS15:
741 case cmGlobalVisualStudioGenerator::VSVersion::VS16:
742 case cmGlobalVisualStudioGenerator::VSVersion::VS17:
743 return "3.0";
745 return "";
748 cmGlobalVisualStudioVersionedGenerator::AuxToolset
749 cmGlobalVisualStudioVersionedGenerator::FindAuxToolset(
750 std::string& version, std::string& props) const
752 if (version.empty()) {
753 return AuxToolset::None;
756 std::string instancePath;
757 this->GetVSInstance(instancePath);
758 cmSystemTools::ConvertToUnixSlashes(instancePath);
760 // Translate three-component format accepted by "vcvarsall -vcvars_ver=".
761 cmsys::RegularExpression threeComponentRegex(
762 "^([0-9]+\\.[0-9]+)\\.[0-9][0-9][0-9][0-9][0-9]$");
763 // The two-component format represents the two major components of the
764 // three-component format
765 cmsys::RegularExpression twoComponentRegex("^([0-9]+\\.[0-9]+)$");
766 if (threeComponentRegex.find(version)) {
767 // Load "VC/Auxiliary/Build/*/Microsoft.VCToolsVersion.*.txt" files
768 // with two matching components to check their three-component version.
769 std::string const& twoComponent = threeComponentRegex.match(1);
770 std::string pattern =
771 cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s, twoComponent,
772 "*/Microsoft.VCToolsVersion."_s, twoComponent, "*.txt"_s);
773 cmsys::Glob glob;
774 glob.SetRecurseThroughSymlinks(false);
775 if (glob.FindFiles(pattern)) {
776 for (std::string const& txt : glob.GetFiles()) {
777 std::string ver;
778 cmsys::ifstream fin(txt.c_str());
779 if (fin && std::getline(fin, ver)) {
780 // Strip trailing whitespace.
781 ver = ver.substr(0, ver.find_first_not_of("0123456789."));
782 // If the three-component version matches, translate it to
783 // that used by the "Microsoft.VCToolsVersion.*.txt" file name.
784 if (ver == version) {
785 cmsys::RegularExpression extractVersion(
786 "VCToolsVersion\\.([0-9.]+)\\.txt$");
787 if (extractVersion.find(txt)) {
788 version = extractVersion.match(1);
789 break;
795 } else if (twoComponentRegex.find(version)) {
796 std::string const& twoComponent = twoComponentRegex.match(1);
797 std::string pattern =
798 cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s, twoComponent,
799 "*/Microsoft.VCToolsVersion."_s, twoComponent, "*.txt"_s);
800 cmsys::Glob glob;
801 glob.SetRecurseThroughSymlinks(false);
802 if (glob.FindFiles(pattern) && !glob.GetFiles().empty()) {
803 // Since we are only using the first two components of the
804 // toolset version, we require a single match.
805 if (glob.GetFiles().size() == 1) {
806 std::string const& txt = glob.GetFiles()[0];
807 std::string ver;
808 cmsys::ifstream fin(txt.c_str());
809 if (fin && std::getline(fin, ver)) {
810 // Strip trailing whitespace.
811 ver = ver.substr(0, ver.find_first_not_of("0123456789."));
812 // We assume the version is correct, since it is the only one that
813 // matched.
814 cmsys::RegularExpression extractVersion(
815 "VCToolsVersion\\.([0-9.]+)\\.txt$");
816 if (extractVersion.find(txt)) {
817 version = extractVersion.match(1);
820 } else {
821 props = cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s);
822 return AuxToolset::PropsIndeterminate;
827 if (cmSystemTools::VersionCompareGreaterEq(version, "14.20")) {
828 props = cmStrCat(instancePath, "/VC/Auxiliary/Build."_s, version,
829 "/Microsoft.VCToolsVersion."_s, version, ".props"_s);
830 if (cmSystemTools::PathExists(props)) {
831 return AuxToolset::PropsExist;
834 props = cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s, version,
835 "/Microsoft.VCToolsVersion."_s, version, ".props"_s);
836 if (cmSystemTools::PathExists(props)) {
837 return AuxToolset::PropsExist;
840 // Accept the toolset version that is default in the current VS version
841 // by matching the name later VS versions will use for the SxS props files.
842 std::string vcToolsetVersion;
843 if (this->vsSetupAPIHelper.GetVCToolsetVersion(vcToolsetVersion)) {
844 // Accept an exact-match (three-component version).
845 if (version == vcToolsetVersion) {
846 return AuxToolset::Default;
849 // Accept known SxS props file names using four version components
850 // in VS versions later than the current.
851 if (version == "14.28.16.9"_s && vcToolsetVersion == "14.28.29910"_s) {
852 return AuxToolset::Default;
854 if (version == "14.29.16.10"_s && vcToolsetVersion == "14.29.30037"_s) {
855 return AuxToolset::Default;
857 if (version == "14.29.16.11"_s && vcToolsetVersion == "14.29.30133"_s) {
858 return AuxToolset::Default;
861 // The first two components of the default toolset version typically
862 // match the name used by later VS versions for the SxS props files.
863 cmsys::RegularExpression twoComponent("^([0-9]+\\.[0-9]+)");
864 if (twoComponent.find(version)) {
865 std::string const versionPrefix = cmStrCat(twoComponent.match(1), '.');
866 if (cmHasPrefix(vcToolsetVersion, versionPrefix)) {
867 return AuxToolset::Default;
872 return AuxToolset::PropsMissing;
875 bool cmGlobalVisualStudioVersionedGenerator::SelectWindowsStoreToolset(
876 std::string& toolset) const
878 if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
879 if (this->IsWindowsStoreToolsetInstalled() &&
880 this->IsWindowsDesktopToolsetInstalled()) {
881 toolset = VSVersionToToolset(this->Version);
882 return true;
884 return false;
886 return this->cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
887 toolset);
890 bool cmGlobalVisualStudioVersionedGenerator::IsWindowsDesktopToolsetInstalled()
891 const
893 return vsSetupAPIHelper.IsVSInstalled();
896 bool cmGlobalVisualStudioVersionedGenerator::IsWindowsStoreToolsetInstalled()
897 const
899 return vsSetupAPIHelper.IsWin10SDKInstalled();
902 bool cmGlobalVisualStudioVersionedGenerator::IsWin81SDKInstalled() const
904 // Does the VS installer tool know about one?
905 if (vsSetupAPIHelper.IsWin81SDKInstalled()) {
906 return true;
909 // Does the registry know about one (e.g. from VS 2015)?
910 std::string win81Root;
911 if (cmSystemTools::ReadRegistryValue(
912 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
913 "Windows Kits\\Installed Roots;KitsRoot81",
914 win81Root, cmSystemTools::KeyWOW64_32) ||
915 cmSystemTools::ReadRegistryValue(
916 "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
917 "Windows Kits\\Installed Roots;KitsRoot81",
918 win81Root, cmSystemTools::KeyWOW64_32)) {
919 return cmSystemTools::FileExists(
920 cmStrCat(win81Root, "/include/um/windows.h"), true);
922 return false;
925 std::string
926 cmGlobalVisualStudioVersionedGenerator::GetWindows10SDKMaxVersionDefault(
927 cmMakefile*) const
929 return std::string();
932 cm::optional<std::string>
933 cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommandEarly(cmMakefile* mf)
935 std::string instance = mf->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
936 if (!this->SetGeneratorInstance(instance, mf)) {
937 cmSystemTools::SetFatalErrorOccurred();
938 return {};
940 return this->cmGlobalVisualStudio14Generator::FindMSBuildCommandEarly(mf);
943 std::string cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommand()
945 std::string msbuild;
947 // Ask Visual Studio Installer tool.
948 std::string vs;
949 if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
950 if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS17) {
951 if (VSIsArm64Host()) {
952 if (VSHasDotNETFrameworkArm64()) {
953 msbuild = cmStrCat(vs, "/MSBuild/Current/Bin/arm64/MSBuild.exe");
954 if (cmSystemTools::FileExists(msbuild)) {
955 return msbuild;
958 if (VSIsWindows11OrGreater()) {
959 msbuild = cmStrCat(vs, "/MSBuild/Current/Bin/amd64/MSBuild.exe");
960 if (cmSystemTools::FileExists(msbuild)) {
961 return msbuild;
964 } else {
965 msbuild = cmStrCat(vs, "/MSBuild/Current/Bin/amd64/MSBuild.exe");
966 if (cmSystemTools::FileExists(msbuild)) {
967 return msbuild;
971 msbuild = cmStrCat(vs, "/MSBuild/Current/Bin/MSBuild.exe");
972 if (cmSystemTools::FileExists(msbuild)) {
973 return msbuild;
975 msbuild = cmStrCat(vs, "/MSBuild/15.0/Bin/MSBuild.exe");
976 if (cmSystemTools::FileExists(msbuild)) {
977 return msbuild;
981 msbuild = "MSBuild.exe";
982 return msbuild;
985 std::string cmGlobalVisualStudioVersionedGenerator::FindDevEnvCommand()
987 std::string devenv;
989 // Ask Visual Studio Installer tool.
990 std::string vs;
991 if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
992 devenv = cmStrCat(vs, "/Common7/IDE/devenv.com");
993 if (cmSystemTools::FileExists(devenv)) {
994 return devenv;
998 devenv = "devenv.com";
999 return devenv;