cmGlobalVisualStudio14Generator.cxx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2014 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 "cmGlobalVisualStudio14Generator.h"
  11. #include "cmAlgorithms.h"
  12. #include "cmLocalVisualStudio10Generator.h"
  13. #include "cmMakefile.h"
  14. static const char vs14generatorName[] = "Visual Studio 14 2015";
  15. // Map generator name without year to name with year.
  16. static const char* cmVS14GenName(const std::string& name, std::string& genName)
  17. {
  18. if (strncmp(name.c_str(), vs14generatorName,
  19. sizeof(vs14generatorName) - 6) != 0) {
  20. return 0;
  21. }
  22. const char* p = name.c_str() + sizeof(vs14generatorName) - 6;
  23. if (cmHasLiteralPrefix(p, " 2015")) {
  24. p += 5;
  25. }
  26. genName = std::string(vs14generatorName) + p;
  27. return p;
  28. }
  29. class cmGlobalVisualStudio14Generator::Factory
  30. : public cmGlobalGeneratorFactory
  31. {
  32. public:
  33. cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
  34. cmake* cm) const CM_OVERRIDE
  35. {
  36. std::string genName;
  37. const char* p = cmVS14GenName(name, genName);
  38. if (!p) {
  39. return 0;
  40. }
  41. if (!*p) {
  42. return new cmGlobalVisualStudio14Generator(cm, genName, "");
  43. }
  44. if (*p++ != ' ') {
  45. return 0;
  46. }
  47. if (strcmp(p, "Win64") == 0) {
  48. return new cmGlobalVisualStudio14Generator(cm, genName, "x64");
  49. }
  50. if (strcmp(p, "ARM") == 0) {
  51. return new cmGlobalVisualStudio14Generator(cm, genName, "ARM");
  52. }
  53. return 0;
  54. }
  55. void GetDocumentation(cmDocumentationEntry& entry) const CM_OVERRIDE
  56. {
  57. entry.Name = std::string(vs14generatorName) + " [arch]";
  58. entry.Brief = "Generates Visual Studio 2015 project files. "
  59. "Optional [arch] can be \"Win64\" or \"ARM\".";
  60. }
  61. void GetGenerators(std::vector<std::string>& names) const CM_OVERRIDE
  62. {
  63. names.push_back(vs14generatorName);
  64. names.push_back(vs14generatorName + std::string(" ARM"));
  65. names.push_back(vs14generatorName + std::string(" Win64"));
  66. }
  67. bool SupportsToolset() const CM_OVERRIDE { return true; }
  68. };
  69. cmGlobalGeneratorFactory* cmGlobalVisualStudio14Generator::NewFactory()
  70. {
  71. return new Factory;
  72. }
  73. cmGlobalVisualStudio14Generator::cmGlobalVisualStudio14Generator(
  74. cmake* cm, const std::string& name, const std::string& platformName)
  75. : cmGlobalVisualStudio12Generator(cm, name, platformName)
  76. {
  77. std::string vc14Express;
  78. this->ExpressEdition = cmSystemTools::ReadRegistryValue(
  79. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\14.0\\Setup\\VC;"
  80. "ProductDir",
  81. vc14Express, cmSystemTools::KeyWOW64_32);
  82. this->DefaultPlatformToolset = "v140";
  83. this->Version = VS14;
  84. }
  85. bool cmGlobalVisualStudio14Generator::MatchesGeneratorName(
  86. const std::string& name) const
  87. {
  88. std::string genName;
  89. if (cmVS14GenName(name, genName)) {
  90. return genName == this->GetName();
  91. }
  92. return false;
  93. }
  94. bool cmGlobalVisualStudio14Generator::InitializeWindows(cmMakefile* mf)
  95. {
  96. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  97. return this->SelectWindows10SDK(mf, false);
  98. }
  99. return true;
  100. }
  101. bool cmGlobalVisualStudio14Generator::InitializeWindowsStore(cmMakefile* mf)
  102. {
  103. std::ostringstream e;
  104. if (!this->SelectWindowsStoreToolset(this->DefaultPlatformToolset)) {
  105. if (this->DefaultPlatformToolset.empty()) {
  106. e << this->GetName() << " supports Windows Store '8.0', '8.1' and "
  107. "'10.0', but not '"
  108. << this->SystemVersion << "'. Check CMAKE_SYSTEM_VERSION.";
  109. } else {
  110. e << "A Windows Store component with CMake requires both the Windows "
  111. << "Desktop SDK as well as the Windows Store '" << this->SystemVersion
  112. << "' SDK. Please make sure that you have both installed";
  113. }
  114. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  115. return false;
  116. }
  117. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  118. return this->SelectWindows10SDK(mf, true);
  119. }
  120. return true;
  121. }
  122. bool cmGlobalVisualStudio14Generator::SelectWindows10SDK(cmMakefile* mf,
  123. bool required)
  124. {
  125. // Find the default version of the Windows 10 SDK.
  126. this->WindowsTargetPlatformVersion = this->GetWindows10SDKVersion();
  127. if (required && this->WindowsTargetPlatformVersion.empty()) {
  128. std::ostringstream e;
  129. e << "Could not find an appropriate version of the Windows 10 SDK"
  130. << " installed on this machine";
  131. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  132. return false;
  133. }
  134. mf->AddDefinition("CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION",
  135. this->WindowsTargetPlatformVersion.c_str());
  136. return true;
  137. }
  138. bool cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
  139. std::string& toolset) const
  140. {
  141. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  142. if (this->IsWindowsStoreToolsetInstalled() &&
  143. this->IsWindowsDesktopToolsetInstalled()) {
  144. toolset = "v140";
  145. return true;
  146. } else {
  147. return false;
  148. }
  149. }
  150. return this->cmGlobalVisualStudio12Generator::SelectWindowsStoreToolset(
  151. toolset);
  152. }
  153. void cmGlobalVisualStudio14Generator::WriteSLNHeader(std::ostream& fout)
  154. {
  155. // Visual Studio 14 writes .sln format 12.00
  156. fout << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  157. if (this->ExpressEdition) {
  158. fout << "# Visual Studio Express 14 for Windows Desktop\n";
  159. } else {
  160. fout << "# Visual Studio 14\n";
  161. }
  162. }
  163. bool cmGlobalVisualStudio14Generator::IsWindowsDesktopToolsetInstalled() const
  164. {
  165. const char desktop10Key[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  166. "VisualStudio\\14.0\\VC\\Runtimes";
  167. std::vector<std::string> vc14;
  168. return cmSystemTools::GetRegistrySubKeys(desktop10Key, vc14,
  169. cmSystemTools::KeyWOW64_32);
  170. }
  171. bool cmGlobalVisualStudio14Generator::IsWindowsStoreToolsetInstalled() const
  172. {
  173. const char universal10Key[] =
  174. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  175. "VisualStudio\\14.0\\Setup\\Build Tools for Windows 10;SrcPath";
  176. std::string win10SDK;
  177. return cmSystemTools::ReadRegistryValue(universal10Key, win10SDK,
  178. cmSystemTools::KeyWOW64_32);
  179. }
  180. #if defined(_WIN32) && !defined(__CYGWIN__)
  181. struct NoWindowsH
  182. {
  183. bool operator()(std::string const& p)
  184. {
  185. return !cmSystemTools::FileExists(p + "/um/windows.h", true);
  186. }
  187. };
  188. #endif
  189. std::string cmGlobalVisualStudio14Generator::GetWindows10SDKVersion()
  190. {
  191. #if defined(_WIN32) && !defined(__CYGWIN__)
  192. // This logic is taken from the vcvarsqueryregistry.bat file from VS2015
  193. // Try HKLM and then HKCU.
  194. std::string win10Root;
  195. if (!cmSystemTools::ReadRegistryValue(
  196. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  197. "Windows Kits\\Installed Roots;KitsRoot10",
  198. win10Root, cmSystemTools::KeyWOW64_32) &&
  199. !cmSystemTools::ReadRegistryValue(
  200. "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
  201. "Windows Kits\\Installed Roots;KitsRoot10",
  202. win10Root, cmSystemTools::KeyWOW64_32)) {
  203. return std::string();
  204. }
  205. std::vector<std::string> sdks;
  206. std::string path = win10Root + "Include/*";
  207. // Grab the paths of the different SDKs that are installed
  208. cmSystemTools::GlobDirs(path, sdks);
  209. // Skip SDKs that do not contain <um/windows.h> because that indicates that
  210. // only the UCRT MSIs were installed for them.
  211. sdks.erase(std::remove_if(sdks.begin(), sdks.end(), NoWindowsH()),
  212. sdks.end());
  213. if (!sdks.empty()) {
  214. // Only use the filename, which will be the SDK version.
  215. for (std::vector<std::string>::iterator i = sdks.begin(); i != sdks.end();
  216. ++i) {
  217. *i = cmSystemTools::GetFilenameName(*i);
  218. }
  219. // Sort the results to make sure we select the most recent one.
  220. std::sort(sdks.begin(), sdks.end(), cmSystemTools::VersionCompareGreater);
  221. // Look for a SDK exactly matching the requested target version.
  222. for (std::vector<std::string>::iterator i = sdks.begin(); i != sdks.end();
  223. ++i) {
  224. if (cmSystemTools::VersionCompareEqual(*i, this->SystemVersion)) {
  225. return *i;
  226. }
  227. }
  228. // Use the latest Windows 10 SDK since the exact version is not available.
  229. return sdks.at(0);
  230. }
  231. #endif
  232. // Return an empty string
  233. return std::string();
  234. }