cmGlobalVisualStudio15Generator.cxx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #include "cmVSSetupHelper.h"
  11. static const char vs15generatorName[] = "Visual Studio 15 2017";
  12. // Map generator name without year to name with year.
  13. static const char* cmVS15GenName(const std::string& name, std::string& genName)
  14. {
  15. if (strncmp(name.c_str(), vs15generatorName,
  16. sizeof(vs15generatorName) - 6) != 0) {
  17. return 0;
  18. }
  19. const char* p = name.c_str() + sizeof(vs15generatorName) - 6;
  20. if (cmHasLiteralPrefix(p, " 2017")) {
  21. p += 5;
  22. }
  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 2017 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. this->ExpressEdition = false;
  76. this->DefaultPlatformToolset = "v141";
  77. this->DefaultClFlagTable = cmVS141CLFlagTable;
  78. this->DefaultCSharpFlagTable = cmVS141CSharpFlagTable;
  79. this->Version = VS15;
  80. }
  81. bool cmGlobalVisualStudio15Generator::MatchesGeneratorName(
  82. const std::string& name) const
  83. {
  84. std::string genName;
  85. if (cmVS15GenName(name, genName)) {
  86. return genName == this->GetName();
  87. }
  88. return false;
  89. }
  90. void cmGlobalVisualStudio15Generator::WriteSLNHeader(std::ostream& fout)
  91. {
  92. // Visual Studio 15 writes .sln format 12.00
  93. fout << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  94. if (this->ExpressEdition) {
  95. fout << "# Visual Studio Express 15 for Windows Desktop\n";
  96. } else {
  97. fout << "# Visual Studio 15\n";
  98. }
  99. }
  100. bool cmGlobalVisualStudio15Generator::SelectWindowsStoreToolset(
  101. std::string& toolset) const
  102. {
  103. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  104. if (this->IsWindowsStoreToolsetInstalled() &&
  105. this->IsWindowsDesktopToolsetInstalled()) {
  106. toolset = "v141"; // VS 15 uses v141 toolset
  107. return true;
  108. } else {
  109. return false;
  110. }
  111. }
  112. return this->cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
  113. toolset);
  114. }
  115. bool cmGlobalVisualStudio15Generator::IsWindowsDesktopToolsetInstalled() const
  116. {
  117. return vsSetupAPIHelper.IsVS2017Installed();
  118. }
  119. bool cmGlobalVisualStudio15Generator::IsWindowsStoreToolsetInstalled() const
  120. {
  121. return vsSetupAPIHelper.IsWin10SDKInstalled();
  122. }
  123. std::string cmGlobalVisualStudio15Generator::FindMSBuildCommand()
  124. {
  125. std::string msbuild;
  126. // Ask Visual Studio Installer tool.
  127. std::string vs;
  128. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  129. msbuild = vs + "/MSBuild/15.0/Bin/MSBuild.exe";
  130. if (cmSystemTools::FileExists(msbuild)) {
  131. return msbuild;
  132. }
  133. }
  134. msbuild = "MSBuild.exe";
  135. return msbuild;
  136. }
  137. std::string cmGlobalVisualStudio15Generator::FindDevEnvCommand()
  138. {
  139. std::string devenv;
  140. // Ask Visual Studio Installer tool.
  141. std::string vs;
  142. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  143. devenv = vs + "/Common7/IDE/devenv.com";
  144. if (cmSystemTools::FileExists(devenv)) {
  145. return devenv;
  146. }
  147. }
  148. devenv = "devenv.com";
  149. return devenv;
  150. }