cmGlobalVisualStudio14Generator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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 { std::string(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.push_back(vs14generatorName + std::string(" ARM"));
  77. names.push_back(vs14generatorName + std::string(" 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. std::ostringstream e;
  237. if (!this->SelectWindowsStoreToolset(this->DefaultPlatformToolset)) {
  238. if (this->DefaultPlatformToolset.empty()) {
  239. e << 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 << "A Windows Store component with CMake requires both the Windows "
  245. << "Desktop SDK as well as the Windows Store '" << this->SystemVersion
  246. << "' SDK. Please make sure that you have both installed";
  247. }
  248. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  249. return false;
  250. }
  251. return true;
  252. }
  253. bool cmGlobalVisualStudio14Generator::InitializeAndroid(cmMakefile*)
  254. {
  255. return true;
  256. }
  257. bool cmGlobalVisualStudio14Generator::ProcessGeneratorPlatformField(
  258. std::string const& key, std::string const& value)
  259. {
  260. if (key == "version") {
  261. this->GeneratorPlatformVersion = value;
  262. return true;
  263. }
  264. return false;
  265. }
  266. bool cmGlobalVisualStudio14Generator::SelectWindows10SDK(cmMakefile* mf)
  267. {
  268. // Find the default version of the Windows 10 SDK.
  269. std::string const version = this->GetWindows10SDKVersion(mf);
  270. if (version.empty()) {
  271. if (this->GeneratorPlatformVersion) {
  272. mf->IssueMessage(
  273. MessageType::FATAL_ERROR,
  274. cmStrCat("Generator\n ", this->GetName(),
  275. "\ngiven platform specification with\n version=",
  276. *this->GeneratorPlatformVersion,
  277. "\nfield, but no Windows SDK with that version was found."));
  278. return false;
  279. }
  280. if (this->SystemName == "WindowsStore") {
  281. mf->IssueMessage(
  282. MessageType::FATAL_ERROR,
  283. "Could not find an appropriate version of the Windows 10 SDK"
  284. " installed on this machine");
  285. return false;
  286. }
  287. }
  288. this->SetWindowsTargetPlatformVersion(version, mf);
  289. return true;
  290. }
  291. void cmGlobalVisualStudio14Generator::SetWindowsTargetPlatformVersion(
  292. std::string const& version, cmMakefile* mf)
  293. {
  294. this->WindowsTargetPlatformVersion = version;
  295. if (!this->WindowsTargetPlatformVersion.empty() &&
  296. !cmSystemTools::VersionCompareEqual(this->WindowsTargetPlatformVersion,
  297. this->SystemVersion)) {
  298. std::ostringstream e;
  299. e << "Selecting Windows SDK version " << this->WindowsTargetPlatformVersion
  300. << " to target Windows " << this->SystemVersion << ".";
  301. mf->DisplayStatus(e.str(), -1);
  302. }
  303. mf->AddDefinition("CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION",
  304. this->WindowsTargetPlatformVersion);
  305. }
  306. bool cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
  307. std::string& toolset) const
  308. {
  309. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  310. if (this->IsWindowsStoreToolsetInstalled() &&
  311. this->IsWindowsDesktopToolsetInstalled()) {
  312. toolset = "v140";
  313. return true;
  314. }
  315. return false;
  316. }
  317. return this->cmGlobalVisualStudio12Generator::SelectWindowsStoreToolset(
  318. toolset);
  319. }
  320. bool cmGlobalVisualStudio14Generator::IsWindowsDesktopToolsetInstalled() const
  321. {
  322. const char desktop10Key[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  323. "VisualStudio\\14.0\\VC\\Runtimes";
  324. std::vector<std::string> vc14;
  325. return cmSystemTools::GetRegistrySubKeys(desktop10Key, vc14,
  326. cmSystemTools::KeyWOW64_32);
  327. }
  328. bool cmGlobalVisualStudio14Generator::IsWindowsStoreToolsetInstalled() const
  329. {
  330. const char universal10Key[] =
  331. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  332. "VisualStudio\\14.0\\Setup\\Build Tools for Windows 10;SrcPath";
  333. std::string win10SDK;
  334. return cmSystemTools::ReadRegistryValue(universal10Key, win10SDK,
  335. cmSystemTools::KeyWOW64_32);
  336. }
  337. bool cmGlobalVisualStudio14Generator::IsWin81SDKInstalled() const
  338. {
  339. return true;
  340. }
  341. std::string cmGlobalVisualStudio14Generator::GetWindows10SDKMaxVersion(
  342. cmMakefile* mf) const
  343. {
  344. // if the given value is set, it can either be OFF/FALSE or a valid SDK
  345. // string
  346. if (cmValue value = mf->GetDefinition(
  347. "CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION_MAXIMUM")) {
  348. // If the value is some off/false value, then there is NO maximum set.
  349. if (cmIsOff(value)) {
  350. return std::string();
  351. }
  352. // If the value is something else, trust that it is a valid SDK value.
  353. if (value) {
  354. return *value;
  355. }
  356. // If value is an invalid pointer, leave result unchanged.
  357. }
  358. return this->GetWindows10SDKMaxVersionDefault(mf);
  359. }
  360. std::string cmGlobalVisualStudio14Generator::GetWindows10SDKMaxVersionDefault(
  361. cmMakefile*) const
  362. {
  363. // The last Windows 10 SDK version that VS 2015 can target is 10.0.14393.0.
  364. //
  365. // "VS 2015 Users: The Windows 10 SDK (15063, 16299, 17134, 17763) is
  366. // officially only supported for VS 2017." From:
  367. // https://blogs.msdn.microsoft.com/chuckw/2018/10/02/windows-10-october-2018-update/
  368. return "10.0.14393.0";
  369. }
  370. #if defined(_WIN32) && !defined(__CYGWIN__)
  371. struct NoWindowsH
  372. {
  373. bool operator()(std::string const& p)
  374. {
  375. return !cmSystemTools::FileExists(p + "/um/windows.h", true);
  376. }
  377. };
  378. class WindowsSDKTooRecent
  379. {
  380. std::string const& MaxVersion;
  381. public:
  382. WindowsSDKTooRecent(std::string const& maxVersion)
  383. : MaxVersion(maxVersion)
  384. {
  385. }
  386. bool operator()(std::string const& v)
  387. {
  388. return cmSystemTools::VersionCompareGreater(v, MaxVersion);
  389. }
  390. };
  391. #endif
  392. std::string cmGlobalVisualStudio14Generator::GetWindows10SDKVersion(
  393. cmMakefile* mf)
  394. {
  395. #if defined(_WIN32) && !defined(__CYGWIN__)
  396. std::vector<std::string> win10Roots;
  397. {
  398. std::string win10Root;
  399. if (cmSystemTools::GetEnv("CMAKE_WINDOWS_KITS_10_DIR", win10Root)) {
  400. cmSystemTools::ConvertToUnixSlashes(win10Root);
  401. win10Roots.push_back(win10Root);
  402. }
  403. }
  404. {
  405. // This logic is taken from the vcvarsqueryregistry.bat file from VS2015
  406. // Try HKLM and then HKCU.
  407. std::string win10Root;
  408. if (cmSystemTools::ReadRegistryValue(
  409. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  410. "Windows Kits\\Installed Roots;KitsRoot10",
  411. win10Root, cmSystemTools::KeyWOW64_32) ||
  412. cmSystemTools::ReadRegistryValue(
  413. "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
  414. "Windows Kits\\Installed Roots;KitsRoot10",
  415. win10Root, cmSystemTools::KeyWOW64_32)) {
  416. cmSystemTools::ConvertToUnixSlashes(win10Root);
  417. win10Roots.push_back(win10Root);
  418. }
  419. }
  420. if (win10Roots.empty()) {
  421. return std::string();
  422. }
  423. std::vector<std::string> sdks;
  424. // Grab the paths of the different SDKs that are installed
  425. for (std::string const& i : win10Roots) {
  426. std::string path = i + "/Include/*";
  427. cmSystemTools::GlobDirs(path, sdks);
  428. }
  429. // Skip SDKs that do not contain <um/windows.h> because that indicates that
  430. // only the UCRT MSIs were installed for them.
  431. cm::erase_if(sdks, NoWindowsH());
  432. // Only use the filename, which will be the SDK version.
  433. for (std::string& i : sdks) {
  434. i = cmSystemTools::GetFilenameName(i);
  435. }
  436. // Skip SDKs that cannot be used with our toolset, unless the user does not
  437. // want to limit the highest supported SDK according to the Microsoft
  438. // documentation.
  439. std::string maxVersion = this->GetWindows10SDKMaxVersion(mf);
  440. if (!maxVersion.empty()) {
  441. cm::erase_if(sdks, WindowsSDKTooRecent(maxVersion));
  442. }
  443. // Sort the results to make sure we select the most recent one.
  444. std::sort(sdks.begin(), sdks.end(), cmSystemTools::VersionCompareGreater);
  445. // Look for a SDK exactly matching the requested version, if any.
  446. if (this->GeneratorPlatformVersion) {
  447. for (std::string const& i : sdks) {
  448. if (cmSystemTools::VersionCompareEqual(
  449. i, *this->GeneratorPlatformVersion)) {
  450. return i;
  451. }
  452. }
  453. // An exact version was requested but not found.
  454. // Our caller will issue the error message.
  455. return std::string();
  456. }
  457. if (mf->GetPolicyStatus(cmPolicies::CMP0149) == cmPolicies::NEW) {
  458. if (cm::optional<std::string> const envVer =
  459. cmSystemTools::GetEnvVar("WindowsSDKVersion")) {
  460. // Look for a SDK exactly matching the environment variable.
  461. for (std::string const& i : sdks) {
  462. if (cmSystemTools::VersionCompareEqual(i, *envVer)) {
  463. return i;
  464. }
  465. }
  466. }
  467. } else {
  468. // Look for a SDK exactly matching the target Windows version.
  469. for (std::string const& i : sdks) {
  470. if (cmSystemTools::VersionCompareEqual(i, this->SystemVersion)) {
  471. return i;
  472. }
  473. }
  474. }
  475. if (!sdks.empty()) {
  476. // Use the latest Windows 10 SDK since the exact version is not available.
  477. return sdks.at(0);
  478. }
  479. #endif
  480. (void)mf;
  481. // Return an empty string
  482. return std::string();
  483. }