cmGlobalVisualStudio14Generator.cxx 9.9 KB

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