cmGlobalVisualStudio14Generator.cxx 12 KB

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