cmGlobalVisualStudio14Generator.cxx 9.1 KB

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