cmGlobalVisualStudio15Generator.cxx 4.4 KB

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