cmGlobalVisualStudio14Generator.cxx 15 KB

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