cmGlobalVisualStudio15Generator.cxx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 override { return true; }
  66. bool SupportsPlatform() const 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::SetGeneratorInstance(
  103. std::string const& i, cmMakefile* mf)
  104. {
  105. if (!i.empty()) {
  106. if (!this->vsSetupAPIHelper.SetVSInstance(i)) {
  107. std::ostringstream e;
  108. /* clang-format off */
  109. e <<
  110. "Generator\n"
  111. " " << this->GetName() << "\n"
  112. "could not find specified instance of Visual Studio:\n"
  113. " " << i;
  114. /* clang-format on */
  115. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  116. return false;
  117. }
  118. }
  119. std::string vsInstance;
  120. if (!this->vsSetupAPIHelper.GetVSInstanceInfo(vsInstance)) {
  121. std::ostringstream e;
  122. /* clang-format off */
  123. e <<
  124. "Generator\n"
  125. " " << this->GetName() << "\n"
  126. "could not find any instance of Visual Studio.\n";
  127. /* clang-format on */
  128. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  129. return false;
  130. }
  131. // Save the selected instance persistently.
  132. std::string genInstance = mf->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
  133. if (vsInstance != genInstance) {
  134. this->CMakeInstance->AddCacheEntry(
  135. "CMAKE_GENERATOR_INSTANCE", vsInstance.c_str(),
  136. "Generator instance identifier.", cmStateEnums::INTERNAL);
  137. }
  138. return true;
  139. }
  140. bool cmGlobalVisualStudio15Generator::GetVSInstance(std::string& dir) const
  141. {
  142. return vsSetupAPIHelper.GetVSInstanceInfo(dir);
  143. }
  144. bool cmGlobalVisualStudio15Generator::InitializeWindows(cmMakefile* mf)
  145. {
  146. // If the Win 8.1 SDK is installed then we can select a SDK matching
  147. // the target Windows version.
  148. if (this->IsWin81SDKInstalled()) {
  149. return cmGlobalVisualStudio14Generator::InitializeWindows(mf);
  150. }
  151. // Otherwise we must choose a Win 10 SDK even if we are not targeting
  152. // Windows 10.
  153. return this->SelectWindows10SDK(mf, false);
  154. }
  155. bool cmGlobalVisualStudio15Generator::SelectWindowsStoreToolset(
  156. std::string& toolset) const
  157. {
  158. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  159. if (this->IsWindowsStoreToolsetInstalled() &&
  160. this->IsWindowsDesktopToolsetInstalled()) {
  161. toolset = "v141"; // VS 15 uses v141 toolset
  162. return true;
  163. } else {
  164. return false;
  165. }
  166. }
  167. return this->cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
  168. toolset);
  169. }
  170. bool cmGlobalVisualStudio15Generator::IsWindowsDesktopToolsetInstalled() const
  171. {
  172. return vsSetupAPIHelper.IsVS2017Installed();
  173. }
  174. bool cmGlobalVisualStudio15Generator::IsWindowsStoreToolsetInstalled() const
  175. {
  176. return vsSetupAPIHelper.IsWin10SDKInstalled();
  177. }
  178. bool cmGlobalVisualStudio15Generator::IsWin81SDKInstalled() const
  179. {
  180. // Does the VS installer tool know about one?
  181. if (vsSetupAPIHelper.IsWin81SDKInstalled()) {
  182. return true;
  183. }
  184. // Does the registry know about one (e.g. from VS 2015)?
  185. std::string win81Root;
  186. if (cmSystemTools::ReadRegistryValue(
  187. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  188. "Windows Kits\\Installed Roots;KitsRoot81",
  189. win81Root, cmSystemTools::KeyWOW64_32) ||
  190. cmSystemTools::ReadRegistryValue(
  191. "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
  192. "Windows Kits\\Installed Roots;KitsRoot81",
  193. win81Root, cmSystemTools::KeyWOW64_32)) {
  194. return cmSystemTools::FileExists(win81Root + "/um/windows.h", true);
  195. }
  196. return false;
  197. }
  198. std::string cmGlobalVisualStudio15Generator::FindMSBuildCommand()
  199. {
  200. std::string msbuild;
  201. // Ask Visual Studio Installer tool.
  202. std::string vs;
  203. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  204. msbuild = vs + "/MSBuild/15.0/Bin/MSBuild.exe";
  205. if (cmSystemTools::FileExists(msbuild)) {
  206. return msbuild;
  207. }
  208. }
  209. msbuild = "MSBuild.exe";
  210. return msbuild;
  211. }
  212. std::string cmGlobalVisualStudio15Generator::FindDevEnvCommand()
  213. {
  214. std::string devenv;
  215. // Ask Visual Studio Installer tool.
  216. std::string vs;
  217. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  218. devenv = vs + "/Common7/IDE/devenv.com";
  219. if (cmSystemTools::FileExists(devenv)) {
  220. return devenv;
  221. }
  222. }
  223. devenv = "devenv.com";
  224. return devenv;
  225. }