cmGlobalVisualStudio15Generator.cxx 4.5 KB

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