cmGlobalVisualStudio14Generator.cxx 16 KB

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