cmGlobalVisualStudio15Generator.cxx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "cmVS141LinkFlagTable.h"
  11. #include "cmVSSetupHelper.h"
  12. static const char vs15generatorName[] = "Visual Studio 15 2017";
  13. // Map generator name without year to name with year.
  14. static const char* cmVS15GenName(const std::string& name, std::string& genName)
  15. {
  16. if (strncmp(name.c_str(), vs15generatorName,
  17. sizeof(vs15generatorName) - 6) != 0) {
  18. return 0;
  19. }
  20. const char* p = name.c_str() + sizeof(vs15generatorName) - 6;
  21. if (cmHasLiteralPrefix(p, " 2017")) {
  22. p += 5;
  23. }
  24. genName = std::string(vs15generatorName) + p;
  25. return p;
  26. }
  27. class cmGlobalVisualStudio15Generator::Factory
  28. : public cmGlobalGeneratorFactory
  29. {
  30. public:
  31. virtual cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
  32. cmake* cm) const
  33. {
  34. std::string genName;
  35. const char* p = cmVS15GenName(name, genName);
  36. if (!p) {
  37. return 0;
  38. }
  39. if (!*p) {
  40. return new cmGlobalVisualStudio15Generator(cm, genName, "");
  41. }
  42. if (*p++ != ' ') {
  43. return 0;
  44. }
  45. if (strcmp(p, "Win64") == 0) {
  46. return new cmGlobalVisualStudio15Generator(cm, genName, "x64");
  47. }
  48. if (strcmp(p, "ARM") == 0) {
  49. return new cmGlobalVisualStudio15Generator(cm, genName, "ARM");
  50. }
  51. return 0;
  52. }
  53. virtual void GetDocumentation(cmDocumentationEntry& entry) const
  54. {
  55. entry.Name = std::string(vs15generatorName) + " [arch]";
  56. entry.Brief = "Generates Visual Studio 2017 project files. "
  57. "Optional [arch] can be \"Win64\" or \"ARM\".";
  58. }
  59. virtual void GetGenerators(std::vector<std::string>& names) const
  60. {
  61. names.push_back(vs15generatorName);
  62. names.push_back(vs15generatorName + std::string(" ARM"));
  63. names.push_back(vs15generatorName + std::string(" Win64"));
  64. }
  65. bool SupportsToolset() const CM_OVERRIDE { return true; }
  66. bool SupportsPlatform() const CM_OVERRIDE { return true; }
  67. };
  68. cmGlobalGeneratorFactory* cmGlobalVisualStudio15Generator::NewFactory()
  69. {
  70. return new Factory;
  71. }
  72. cmGlobalVisualStudio15Generator::cmGlobalVisualStudio15Generator(
  73. cmake* cm, const std::string& name, const std::string& platformName)
  74. : cmGlobalVisualStudio14Generator(cm, name, platformName)
  75. {
  76. this->ExpressEdition = false;
  77. this->DefaultPlatformToolset = "v141";
  78. this->DefaultClFlagTable = cmVS141CLFlagTable;
  79. this->DefaultCSharpFlagTable = cmVS141CSharpFlagTable;
  80. this->DefaultLinkFlagTable = cmVS141LinkFlagTable;
  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::InitializeWindows(cmMakefile* mf)
  103. {
  104. // If the Win 8.1 SDK is installed then we can select a SDK matching
  105. // the target Windows version.
  106. if (this->IsWin81SDKInstalled()) {
  107. return cmGlobalVisualStudio14Generator::InitializeWindows(mf);
  108. }
  109. // Otherwise we must choose a Win 10 SDK even if we are not targeting
  110. // Windows 10.
  111. return this->SelectWindows10SDK(mf, false);
  112. }
  113. bool cmGlobalVisualStudio15Generator::SelectWindowsStoreToolset(
  114. std::string& toolset) const
  115. {
  116. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  117. if (this->IsWindowsStoreToolsetInstalled() &&
  118. this->IsWindowsDesktopToolsetInstalled()) {
  119. toolset = "v141"; // VS 15 uses v141 toolset
  120. return true;
  121. } else {
  122. return false;
  123. }
  124. }
  125. return this->cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
  126. toolset);
  127. }
  128. bool cmGlobalVisualStudio15Generator::IsWindowsDesktopToolsetInstalled() const
  129. {
  130. return vsSetupAPIHelper.IsVS2017Installed();
  131. }
  132. bool cmGlobalVisualStudio15Generator::IsWindowsStoreToolsetInstalled() const
  133. {
  134. return vsSetupAPIHelper.IsWin10SDKInstalled();
  135. }
  136. bool cmGlobalVisualStudio15Generator::IsWin81SDKInstalled() const
  137. {
  138. // Does the VS installer tool know about one?
  139. if (vsSetupAPIHelper.IsWin81SDKInstalled()) {
  140. return true;
  141. }
  142. // Does the registry know about one (e.g. from VS 2015)?
  143. std::string win81Root;
  144. if (cmSystemTools::ReadRegistryValue(
  145. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  146. "Windows Kits\\Installed Roots;KitsRoot81",
  147. win81Root, cmSystemTools::KeyWOW64_32) ||
  148. cmSystemTools::ReadRegistryValue(
  149. "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
  150. "Windows Kits\\Installed Roots;KitsRoot81",
  151. win81Root, cmSystemTools::KeyWOW64_32)) {
  152. return cmSystemTools::FileExists(win81Root + "/um/windows.h", true);
  153. }
  154. return false;
  155. }
  156. std::string cmGlobalVisualStudio15Generator::FindMSBuildCommand()
  157. {
  158. std::string msbuild;
  159. // Ask Visual Studio Installer tool.
  160. std::string vs;
  161. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  162. msbuild = vs + "/MSBuild/15.0/Bin/MSBuild.exe";
  163. if (cmSystemTools::FileExists(msbuild)) {
  164. return msbuild;
  165. }
  166. }
  167. msbuild = "MSBuild.exe";
  168. return msbuild;
  169. }
  170. std::string cmGlobalVisualStudio15Generator::FindDevEnvCommand()
  171. {
  172. std::string devenv;
  173. // Ask Visual Studio Installer tool.
  174. std::string vs;
  175. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  176. devenv = vs + "/Common7/IDE/devenv.com";
  177. if (cmSystemTools::FileExists(devenv)) {
  178. return devenv;
  179. }
  180. }
  181. devenv = "devenv.com";
  182. return devenv;
  183. }