cmGlobalVisualStudio14Generator.cxx 11 KB

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