cmGlobalVisualStudio15Generator.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "cmGlobalVisualStudio15Generator.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmLocalVisualStudio10Generator.h"
  6. #include "cmMakefile.h"
  7. static const char vs15generatorName[] = "Visual Studio 15 2017";
  8. // Map generator name without year to name with year.
  9. static const char* cmVS15GenName(const std::string& name, std::string& genName)
  10. {
  11. if (strncmp(name.c_str(), vs15generatorName,
  12. sizeof(vs15generatorName) - 6) != 0) {
  13. return 0;
  14. }
  15. const char* p = name.c_str() + sizeof(vs15generatorName) - 6;
  16. if (cmHasLiteralPrefix(p, " 2017")) {
  17. p += 5;
  18. }
  19. genName = std::string(vs15generatorName) + p;
  20. return p;
  21. }
  22. class cmGlobalVisualStudio15Generator::Factory
  23. : public cmGlobalGeneratorFactory
  24. {
  25. public:
  26. virtual cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
  27. cmake* cm) const
  28. {
  29. std::string genName;
  30. const char* p = cmVS15GenName(name, genName);
  31. if (!p) {
  32. return 0;
  33. }
  34. if (!*p) {
  35. return new cmGlobalVisualStudio15Generator(cm, genName, "");
  36. }
  37. if (*p++ != ' ') {
  38. return 0;
  39. }
  40. if (strcmp(p, "Win64") == 0) {
  41. return new cmGlobalVisualStudio15Generator(cm, genName, "x64");
  42. }
  43. if (strcmp(p, "ARM") == 0) {
  44. return new cmGlobalVisualStudio15Generator(cm, genName, "ARM");
  45. }
  46. return 0;
  47. }
  48. virtual void GetDocumentation(cmDocumentationEntry& entry) const
  49. {
  50. entry.Name = std::string(vs15generatorName) + " [arch]";
  51. entry.Brief = "Generates Visual Studio 2017 project files. "
  52. "Optional [arch] can be \"Win64\" or \"ARM\".";
  53. }
  54. virtual void GetGenerators(std::vector<std::string>& names) const
  55. {
  56. names.push_back(vs15generatorName);
  57. names.push_back(vs15generatorName + std::string(" ARM"));
  58. names.push_back(vs15generatorName + std::string(" Win64"));
  59. }
  60. bool SupportsToolset() const CM_OVERRIDE { return true; }
  61. bool SupportsPlatform() const CM_OVERRIDE { return true; }
  62. };
  63. cmGlobalGeneratorFactory* cmGlobalVisualStudio15Generator::NewFactory()
  64. {
  65. return new Factory;
  66. }
  67. cmGlobalVisualStudio15Generator::cmGlobalVisualStudio15Generator(
  68. cmake* cm, const std::string& name, const std::string& platformName)
  69. : cmGlobalVisualStudio14Generator(cm, name, platformName)
  70. {
  71. std::string vc15Express;
  72. this->ExpressEdition = cmSystemTools::ReadRegistryValue(
  73. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\15.0\\Setup\\VC;"
  74. "ProductDir",
  75. vc15Express, cmSystemTools::KeyWOW64_32);
  76. this->DefaultPlatformToolset = "v141";
  77. this->Version = VS15;
  78. }
  79. bool cmGlobalVisualStudio15Generator::MatchesGeneratorName(
  80. const std::string& name) const
  81. {
  82. std::string genName;
  83. if (cmVS15GenName(name, genName)) {
  84. return genName == this->GetName();
  85. }
  86. return false;
  87. }
  88. void cmGlobalVisualStudio15Generator::WriteSLNHeader(std::ostream& fout)
  89. {
  90. // Visual Studio 15 writes .sln format 12.00
  91. fout << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  92. if (this->ExpressEdition) {
  93. fout << "# Visual Studio Express 15 for Windows Desktop\n";
  94. } else {
  95. fout << "# Visual Studio 15\n";
  96. }
  97. }
  98. bool cmGlobalVisualStudio15Generator::SelectWindowsStoreToolset(
  99. std::string& toolset) const
  100. {
  101. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  102. if (this->IsWindowsStoreToolsetInstalled() &&
  103. this->IsWindowsDesktopToolsetInstalled()) {
  104. toolset = "v140"; // VS 15 uses v140 toolset
  105. return true;
  106. } else {
  107. return false;
  108. }
  109. }
  110. return this->cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
  111. toolset);
  112. }
  113. bool cmGlobalVisualStudio15Generator::IsWindowsDesktopToolsetInstalled() const
  114. {
  115. const char desktop10Key[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  116. "VisualStudio\\15.0\\VC\\Runtimes";
  117. std::vector<std::string> vc15;
  118. return cmSystemTools::GetRegistrySubKeys(desktop10Key, vc15,
  119. cmSystemTools::KeyWOW64_32);
  120. }
  121. bool cmGlobalVisualStudio15Generator::IsWindowsStoreToolsetInstalled() const
  122. {
  123. const char universal10Key[] =
  124. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  125. "VisualStudio\\15.0\\Setup\\Build Tools for Windows 10;SrcPath";
  126. std::string win10SDK;
  127. return cmSystemTools::ReadRegistryValue(universal10Key, win10SDK,
  128. cmSystemTools::KeyWOW64_32);
  129. }