cmGlobalVisualStudio14Generator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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 <cmext/string_view>
  8. #include "cmGlobalGenerator.h"
  9. #include "cmGlobalGeneratorFactory.h"
  10. #include "cmGlobalVisualStudioGenerator.h"
  11. #include "cmMakefile.h"
  12. #include "cmMessageType.h"
  13. #include "cmPolicies.h"
  14. #include "cmStringAlgorithms.h"
  15. #include "cmSystemTools.h"
  16. #include "cmValue.h"
  17. static const char vs14generatorName[] = "Visual Studio 14 2015";
  18. // Map generator name without year to name with year.
  19. static const char* cmVS14GenName(const std::string& name, std::string& genName)
  20. {
  21. if (strncmp(name.c_str(), vs14generatorName,
  22. sizeof(vs14generatorName) - 6) != 0) {
  23. return nullptr;
  24. }
  25. const char* p = name.c_str() + sizeof(vs14generatorName) - 6;
  26. if (cmHasLiteralPrefix(p, " 2015")) {
  27. p += 5;
  28. }
  29. genName = std::string(vs14generatorName) + p;
  30. return p;
  31. }
  32. class cmGlobalVisualStudio14Generator::Factory
  33. : public cmGlobalGeneratorFactory
  34. {
  35. public:
  36. std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
  37. const std::string& name, bool allowArch, cmake* cm) const override
  38. {
  39. std::string genName;
  40. const char* p = cmVS14GenName(name, genName);
  41. if (!p) {
  42. return std::unique_ptr<cmGlobalGenerator>();
  43. }
  44. if (!*p) {
  45. return std::unique_ptr<cmGlobalGenerator>(
  46. new cmGlobalVisualStudio14Generator(cm, genName, ""));
  47. }
  48. if (!allowArch || *p++ != ' ') {
  49. return std::unique_ptr<cmGlobalGenerator>();
  50. }
  51. if (strcmp(p, "Win64") == 0) {
  52. return std::unique_ptr<cmGlobalGenerator>(
  53. new cmGlobalVisualStudio14Generator(cm, genName, "x64"));
  54. }
  55. if (strcmp(p, "ARM") == 0) {
  56. return std::unique_ptr<cmGlobalGenerator>(
  57. new cmGlobalVisualStudio14Generator(cm, genName, "ARM"));
  58. }
  59. return std::unique_ptr<cmGlobalGenerator>();
  60. }
  61. cmDocumentationEntry GetDocumentation() const override
  62. {
  63. return { cmStrCat(vs14generatorName, " [arch]"),
  64. "Generates Visual Studio 2015 project files. "
  65. "Optional [arch] can be \"Win64\" or \"ARM\"." };
  66. }
  67. std::vector<std::string> GetGeneratorNames() const override
  68. {
  69. std::vector<std::string> names;
  70. names.push_back(vs14generatorName);
  71. return names;
  72. }
  73. std::vector<std::string> GetGeneratorNamesWithPlatform() const override
  74. {
  75. std::vector<std::string> names;
  76. names.emplace_back(cmStrCat(vs14generatorName, " ARM"));
  77. names.emplace_back(cmStrCat(vs14generatorName, " Win64"));
  78. return names;
  79. }
  80. bool SupportsToolset() const override { return true; }
  81. bool SupportsPlatform() const override { return true; }
  82. std::vector<std::string> GetKnownPlatforms() const override
  83. {
  84. std::vector<std::string> platforms;
  85. platforms.emplace_back("x64");
  86. platforms.emplace_back("Win32");
  87. platforms.emplace_back("ARM");
  88. return platforms;
  89. }
  90. std::string GetDefaultPlatformName() const override { return "Win32"; }
  91. };
  92. std::unique_ptr<cmGlobalGeneratorFactory>
  93. cmGlobalVisualStudio14Generator::NewFactory()
  94. {
  95. return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory);
  96. }
  97. cmGlobalVisualStudio14Generator::cmGlobalVisualStudio14Generator(
  98. cmake* cm, const std::string& name,
  99. std::string const& platformInGeneratorName)
  100. : cmGlobalVisualStudio12Generator(cm, name, platformInGeneratorName)
  101. {
  102. std::string vc14Express;
  103. this->ExpressEdition = cmSystemTools::ReadRegistryValue(
  104. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\14.0\\Setup\\VC;"
  105. "ProductDir",
  106. vc14Express, cmSystemTools::KeyWOW64_32);
  107. this->DefaultPlatformToolset = "v140";
  108. this->DefaultAndroidToolset = "Clang_3_8";
  109. this->DefaultCLFlagTableName = "v140";
  110. this->DefaultCSharpFlagTableName = "v140";
  111. this->DefaultLibFlagTableName = "v14";
  112. this->DefaultLinkFlagTableName = "v140";
  113. this->DefaultMasmFlagTableName = "v14";
  114. this->DefaultRCFlagTableName = "v14";
  115. this->Version = VSVersion::VS14;
  116. }
  117. bool cmGlobalVisualStudio14Generator::MatchesGeneratorName(
  118. const std::string& name) const
  119. {
  120. std::string genName;
  121. if (cmVS14GenName(name, genName)) {
  122. return genName == this->GetName();
  123. }
  124. return false;
  125. }
  126. bool cmGlobalVisualStudio14Generator::InitializePlatformWindows(cmMakefile* mf)
  127. {
  128. // If a Windows SDK version is explicitly requested, search for it.
  129. if (this->GeneratorPlatformVersion) {
  130. std::string const& version = *this->GeneratorPlatformVersion;
  131. // VS 2019 and above support specifying plain "10.0".
  132. if (version == "10.0"_s) {
  133. if (this->Version >= VSVersion::VS16) {
  134. this->SetWindowsTargetPlatformVersion("10.0", mf);
  135. return true;
  136. }
  137. /* clang-format off */
  138. mf->IssueMessage(MessageType::FATAL_ERROR, cmStrCat(
  139. "Generator\n"
  140. " ", this->GetName(), "\n"
  141. "given platform specification containing a\n"
  142. " version=10.0\n"
  143. "field. The value 10.0 is only supported by VS 2019 and above.\n"
  144. ));
  145. /* clang-format on */
  146. return false;
  147. }
  148. if (cmHasLiteralPrefix(version, "10.0.")) {
  149. return this->SelectWindows10SDK(mf);
  150. }
  151. if (version == "8.1"_s) {
  152. if (this->IsWin81SDKInstalled()) {
  153. this->SetWindowsTargetPlatformVersion("8.1", mf);
  154. return true;
  155. }
  156. /* clang-format off */
  157. mf->IssueMessage(MessageType::FATAL_ERROR, cmStrCat(
  158. "Generator\n"
  159. " ", this->GetName(), "\n"
  160. "given platform specification containing a\n"
  161. " version=8.1\n"
  162. "field, but the Windows 8.1 SDK is not installed.\n"
  163. ));
  164. /* clang-format on */
  165. return false;
  166. }
  167. if (version.empty()) {
  168. /* clang-format off */
  169. mf->IssueMessage(MessageType::FATAL_ERROR, cmStrCat(
  170. "Generator\n"
  171. " ", this->GetName(), "\n"
  172. "given platform specification with empty\n"
  173. " version=\n"
  174. "field.\n"
  175. ));
  176. /* clang-format on */
  177. return false;
  178. }
  179. /* clang-format off */
  180. mf->IssueMessage(MessageType::FATAL_ERROR, cmStrCat(
  181. "Generator\n"
  182. " ", this->GetName(), "\n"
  183. "given platform specification containing a\n"
  184. " version=", version, "\n"
  185. "field with unsupported value.\n"
  186. ));
  187. /* clang-format on */
  188. return false;
  189. }
  190. // If we are targeting Windows 10+, we select a Windows 10 SDK.
  191. // If no Windows 8.1 SDK is installed, which is possible with VS 2017 and
  192. // higher, then we must choose a Windows 10 SDK anyway.
  193. if (cmHasLiteralPrefix(this->SystemVersion, "10.0") ||
  194. !this->IsWin81SDKInstalled()) {
  195. return this->SelectWindows10SDK(mf);
  196. }
  197. // Under CMP0149 NEW behavior, we search for a Windows 10 SDK even
  198. // when targeting older Windows versions, but it is not required.
  199. if (mf->GetPolicyStatus(cmPolicies::CMP0149) == cmPolicies::NEW) {
  200. std::string const version = this->GetWindows10SDKVersion(mf);
  201. if (!version.empty()) {
  202. this->SetWindowsTargetPlatformVersion(version, mf);
  203. return true;
  204. }
  205. }
  206. // We are not targeting Windows 10+, so fall back to the Windows 8.1 SDK.
  207. // For VS 2019 and above we must explicitly specify it.
  208. if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS16 &&
  209. !cmSystemTools::VersionCompareGreater(this->SystemVersion, "8.1")) {
  210. this->SetWindowsTargetPlatformVersion("8.1", mf);
  211. }
  212. return true;
  213. }
  214. bool cmGlobalVisualStudio14Generator::VerifyNoGeneratorPlatformVersion(
  215. cmMakefile* mf) const
  216. {
  217. if (!this->GeneratorPlatformVersion) {
  218. return true;
  219. }
  220. std::ostringstream e;
  221. /* clang-format off */
  222. e <<
  223. "Generator\n"
  224. " " << this->GetName() << "\n"
  225. "given platform specification containing a\n"
  226. " version=" << *this->GeneratorPlatformVersion << "\n"
  227. "field. The version field is not supported when targeting\n"
  228. " " << this->SystemName << ' ' << this->SystemVersion << '\n'
  229. ;
  230. /* clang-format on */
  231. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  232. return false;
  233. }
  234. bool cmGlobalVisualStudio14Generator::InitializeWindowsStore(cmMakefile* mf)
  235. {
  236. if (!this->SelectWindowsStoreToolset(this->DefaultPlatformToolset)) {
  237. std::string e;
  238. if (this->DefaultPlatformToolset.empty()) {
  239. e = cmStrCat(this->GetName(),
  240. " supports Windows Store '8.0', '8.1' and "
  241. "'10.0', but not '",
  242. this->SystemVersion, "'. Check CMAKE_SYSTEM_VERSION.");
  243. } else {
  244. e = cmStrCat(
  245. "A Windows Store component with CMake requires both the Windows "
  246. "Desktop SDK as well as the Windows Store '",
  247. this->SystemVersion,
  248. "' SDK. Please make sure that you have both installed");
  249. }
  250. mf->IssueMessage(MessageType::FATAL_ERROR, e);
  251. return false;
  252. }
  253. return true;
  254. }
  255. bool cmGlobalVisualStudio14Generator::InitializeAndroid(cmMakefile*)
  256. {
  257. return true;
  258. }
  259. bool cmGlobalVisualStudio14Generator::ProcessGeneratorPlatformField(
  260. std::string const& key, std::string const& value)
  261. {
  262. if (key == "version"_s) {
  263. this->GeneratorPlatformVersion = value;
  264. return true;
  265. }
  266. return false;
  267. }
  268. bool cmGlobalVisualStudio14Generator::SelectWindows10SDK(cmMakefile* mf)
  269. {
  270. // Find the default version of the Windows 10 SDK.
  271. std::string const version = this->GetWindows10SDKVersion(mf);
  272. if (version.empty()) {
  273. if (this->GeneratorPlatformVersion) {
  274. mf->IssueMessage(
  275. MessageType::FATAL_ERROR,
  276. cmStrCat("Generator\n ", this->GetName(),
  277. "\ngiven platform specification with\n version=",
  278. *this->GeneratorPlatformVersion,
  279. "\nfield, but no Windows SDK with that version was found."));
  280. return false;
  281. }
  282. if (this->SystemName == "WindowsStore"_s) {
  283. mf->IssueMessage(
  284. MessageType::FATAL_ERROR,
  285. "Could not find an appropriate version of the Windows 10 SDK"
  286. " installed on this machine");
  287. return false;
  288. }
  289. }
  290. this->SetWindowsTargetPlatformVersion(version, mf);
  291. return true;
  292. }
  293. void cmGlobalVisualStudio14Generator::SetWindowsTargetPlatformVersion(
  294. std::string const& version, cmMakefile* mf)
  295. {
  296. this->WindowsTargetPlatformVersion = version;
  297. if (!this->WindowsTargetPlatformVersion.empty() &&
  298. !cmSystemTools::VersionCompareEqual(this->WindowsTargetPlatformVersion,
  299. this->SystemVersion)) {
  300. mf->DisplayStatus(cmStrCat("Selecting Windows SDK version ",
  301. this->WindowsTargetPlatformVersion,
  302. " to target Windows ", this->SystemVersion,
  303. '.'),
  304. -1);
  305. }
  306. mf->AddDefinition("CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION",
  307. this->WindowsTargetPlatformVersion);
  308. }
  309. bool cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
  310. std::string& toolset) const
  311. {
  312. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  313. if (this->IsWindowsStoreToolsetInstalled() &&
  314. this->IsWindowsDesktopToolsetInstalled()) {
  315. toolset = "v140";
  316. return true;
  317. }
  318. return false;
  319. }
  320. return this->cmGlobalVisualStudio12Generator::SelectWindowsStoreToolset(
  321. toolset);
  322. }
  323. bool cmGlobalVisualStudio14Generator::IsWindowsDesktopToolsetInstalled() const
  324. {
  325. const char desktop10Key[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  326. "VisualStudio\\14.0\\VC\\Runtimes";
  327. std::vector<std::string> vc14;
  328. return cmSystemTools::GetRegistrySubKeys(desktop10Key, vc14,
  329. cmSystemTools::KeyWOW64_32);
  330. }
  331. bool cmGlobalVisualStudio14Generator::IsWindowsStoreToolsetInstalled() const
  332. {
  333. const char universal10Key[] =
  334. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  335. "VisualStudio\\14.0\\Setup\\Build Tools for Windows 10;SrcPath";
  336. std::string win10SDK;
  337. return cmSystemTools::ReadRegistryValue(universal10Key, win10SDK,
  338. cmSystemTools::KeyWOW64_32);
  339. }
  340. bool cmGlobalVisualStudio14Generator::IsWin81SDKInstalled() const
  341. {
  342. return true;
  343. }
  344. std::string cmGlobalVisualStudio14Generator::GetWindows10SDKMaxVersion(
  345. cmMakefile* mf) const
  346. {
  347. // if the given value is set, it can either be OFF/FALSE or a valid SDK
  348. // string
  349. if (cmValue value = mf->GetDefinition(
  350. "CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION_MAXIMUM")) {
  351. // If the value is some off/false value, then there is NO maximum set.
  352. if (value.IsOff()) {
  353. return std::string();
  354. }
  355. // If the value is something else, trust that it is a valid SDK value.
  356. if (value) {
  357. return *value;
  358. }
  359. // If value is an invalid pointer, leave result unchanged.
  360. }
  361. return this->GetWindows10SDKMaxVersionDefault(mf);
  362. }
  363. std::string cmGlobalVisualStudio14Generator::GetWindows10SDKMaxVersionDefault(
  364. cmMakefile*) const
  365. {
  366. // The last Windows 10 SDK version that VS 2015 can target is 10.0.14393.0.
  367. //
  368. // "VS 2015 Users: The Windows 10 SDK (15063, 16299, 17134, 17763) is
  369. // officially only supported for VS 2017." From:
  370. // https://blogs.msdn.microsoft.com/chuckw/2018/10/02/windows-10-october-2018-update/
  371. return "10.0.14393.0";
  372. }
  373. #if defined(_WIN32) && !defined(__CYGWIN__)
  374. struct NoWindowsH
  375. {
  376. bool operator()(std::string const& p)
  377. {
  378. return !cmSystemTools::FileExists(cmStrCat(p, "/um/windows.h"), true);
  379. }
  380. };
  381. class WindowsSDKTooRecent
  382. {
  383. std::string const& MaxVersion;
  384. public:
  385. WindowsSDKTooRecent(std::string const& maxVersion)
  386. : MaxVersion(maxVersion)
  387. {
  388. }
  389. bool operator()(std::string const& v)
  390. {
  391. return cmSystemTools::VersionCompareGreater(v, MaxVersion);
  392. }
  393. };
  394. #endif
  395. std::string cmGlobalVisualStudio14Generator::GetWindows10SDKVersion(
  396. cmMakefile* mf)
  397. {
  398. #if defined(_WIN32) && !defined(__CYGWIN__)
  399. std::vector<std::string> win10Roots;
  400. {
  401. std::string win10Root;
  402. if (cmSystemTools::GetEnv("CMAKE_WINDOWS_KITS_10_DIR", win10Root)) {
  403. cmSystemTools::ConvertToUnixSlashes(win10Root);
  404. win10Roots.push_back(win10Root);
  405. }
  406. }
  407. {
  408. // This logic is taken from the vcvarsqueryregistry.bat file from VS2015
  409. // Try HKLM and then HKCU.
  410. std::string win10Root;
  411. if (cmSystemTools::ReadRegistryValue(
  412. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  413. "Windows Kits\\Installed Roots;KitsRoot10",
  414. win10Root, cmSystemTools::KeyWOW64_32) ||
  415. cmSystemTools::ReadRegistryValue(
  416. "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
  417. "Windows Kits\\Installed Roots;KitsRoot10",
  418. win10Root, cmSystemTools::KeyWOW64_32)) {
  419. cmSystemTools::ConvertToUnixSlashes(win10Root);
  420. win10Roots.push_back(win10Root);
  421. }
  422. }
  423. if (win10Roots.empty()) {
  424. return std::string();
  425. }
  426. std::vector<std::string> sdks;
  427. // Grab the paths of the different SDKs that are installed
  428. for (std::string const& i : win10Roots) {
  429. std::string path = cmStrCat(i, "/Include/*");
  430. cmSystemTools::GlobDirs(path, sdks);
  431. }
  432. // Skip SDKs that do not contain <um/windows.h> because that indicates that
  433. // only the UCRT MSIs were installed for them.
  434. cm::erase_if(sdks, NoWindowsH());
  435. // Only use the filename, which will be the SDK version.
  436. for (std::string& i : sdks) {
  437. i = cmSystemTools::GetFilenameName(i);
  438. }
  439. // Skip SDKs that cannot be used with our toolset, unless the user does not
  440. // want to limit the highest supported SDK according to the Microsoft
  441. // documentation.
  442. std::string maxVersion = this->GetWindows10SDKMaxVersion(mf);
  443. if (!maxVersion.empty()) {
  444. cm::erase_if(sdks, WindowsSDKTooRecent(maxVersion));
  445. }
  446. // Sort the results to make sure we select the most recent one.
  447. std::sort(sdks.begin(), sdks.end(), cmSystemTools::VersionCompareGreater);
  448. // Look for a SDK exactly matching the requested version, if any.
  449. if (this->GeneratorPlatformVersion) {
  450. for (std::string const& i : sdks) {
  451. if (cmSystemTools::VersionCompareEqual(
  452. i, *this->GeneratorPlatformVersion)) {
  453. return i;
  454. }
  455. }
  456. // An exact version was requested but not found.
  457. // Our caller will issue the error message.
  458. return std::string();
  459. }
  460. if (mf->GetPolicyStatus(cmPolicies::CMP0149) == cmPolicies::NEW) {
  461. if (cm::optional<std::string> const envVer =
  462. cmSystemTools::GetEnvVar("WindowsSDKVersion")) {
  463. // Look for a SDK exactly matching the environment variable.
  464. for (std::string const& i : sdks) {
  465. if (cmSystemTools::VersionCompareEqual(i, *envVer)) {
  466. return i;
  467. }
  468. }
  469. }
  470. } else {
  471. // Look for a SDK exactly matching the target Windows version.
  472. for (std::string const& i : sdks) {
  473. if (cmSystemTools::VersionCompareEqual(i, this->SystemVersion)) {
  474. return i;
  475. }
  476. }
  477. }
  478. if (!sdks.empty()) {
  479. // Use the latest Windows 10 SDK since the exact version is not available.
  480. return sdks.at(0);
  481. }
  482. #endif
  483. (void)mf;
  484. // Return an empty string
  485. return std::string();
  486. }