cmGlobalVisualStudio15Generator.cxx 4.8 KB

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