cmGlobalVisualStudio14Generator.cxx 10.0 KB

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