cmGlobalVisualStudio14Generator.cxx 18 KB

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