cmGlobalVisualStudio14Generator.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  129. return this->SelectWindows10SDK(mf);
  130. }
  131. return this->VerifyNoGeneratorPlatformVersion(mf);
  132. }
  133. bool cmGlobalVisualStudio14Generator::VerifyNoGeneratorPlatformVersion(
  134. cmMakefile* mf, cm::optional<std::string> reason) const
  135. {
  136. if (!this->GeneratorPlatformVersion) {
  137. return true;
  138. }
  139. std::ostringstream e;
  140. /* clang-format off */
  141. e <<
  142. "Generator\n"
  143. " " << this->GetName() << "\n"
  144. "given platform specification containing a\n"
  145. " version=" << *this->GeneratorPlatformVersion << "\n"
  146. "field. The version field is not supported when targeting\n"
  147. " " << this->SystemName << " " << this->SystemVersion << "\n"
  148. ;
  149. /* clang-format on */
  150. if (reason) {
  151. e << *reason << ".";
  152. }
  153. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  154. return false;
  155. }
  156. bool cmGlobalVisualStudio14Generator::InitializeWindowsStore(cmMakefile* mf)
  157. {
  158. std::ostringstream e;
  159. if (!this->SelectWindowsStoreToolset(this->DefaultPlatformToolset)) {
  160. if (this->DefaultPlatformToolset.empty()) {
  161. e << this->GetName()
  162. << " supports Windows Store '8.0', '8.1' and "
  163. "'10.0', but not '"
  164. << this->SystemVersion << "'. Check CMAKE_SYSTEM_VERSION.";
  165. } else {
  166. e << "A Windows Store component with CMake requires both the Windows "
  167. << "Desktop SDK as well as the Windows Store '" << this->SystemVersion
  168. << "' SDK. Please make sure that you have both installed";
  169. }
  170. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  171. return false;
  172. }
  173. return true;
  174. }
  175. bool cmGlobalVisualStudio14Generator::InitializeAndroid(cmMakefile*)
  176. {
  177. return true;
  178. }
  179. bool cmGlobalVisualStudio14Generator::ProcessGeneratorPlatformField(
  180. std::string const& key, std::string const& value)
  181. {
  182. if (key == "version"_s) {
  183. this->GeneratorPlatformVersion = value;
  184. return true;
  185. }
  186. return false;
  187. }
  188. bool cmGlobalVisualStudio14Generator::SelectWindows10SDK(cmMakefile* mf)
  189. {
  190. if (this->GeneratorPlatformVersion &&
  191. this->GeneratorPlatformVersion->empty()) {
  192. mf->IssueMessage(
  193. MessageType::FATAL_ERROR,
  194. cmStrCat("Generator\n ", this->GetName(),
  195. "\ngiven platform specification with empty\n version=\n"
  196. "field."));
  197. return false;
  198. }
  199. // Find the default version of the Windows 10 SDK.
  200. std::string const version = this->GetWindows10SDKVersion(mf);
  201. if (version.empty()) {
  202. if (this->GeneratorPlatformVersion) {
  203. mf->IssueMessage(
  204. MessageType::FATAL_ERROR,
  205. cmStrCat("Generator\n ", this->GetName(),
  206. "\ngiven platform specification with\n version=",
  207. *this->GeneratorPlatformVersion,
  208. "\nfield, but no Windows SDK with that version was found."));
  209. return false;
  210. }
  211. if (this->SystemName == "WindowsStore"_s) {
  212. mf->IssueMessage(
  213. MessageType::FATAL_ERROR,
  214. "Could not find an appropriate version of the Windows 10 SDK"
  215. " installed on this machine");
  216. return false;
  217. }
  218. }
  219. this->SetWindowsTargetPlatformVersion(version, mf);
  220. return true;
  221. }
  222. void cmGlobalVisualStudio14Generator::SetWindowsTargetPlatformVersion(
  223. std::string const& version, cmMakefile* mf)
  224. {
  225. this->WindowsTargetPlatformVersion = version;
  226. if (!cmSystemTools::VersionCompareEqual(this->WindowsTargetPlatformVersion,
  227. this->SystemVersion)) {
  228. std::ostringstream e;
  229. e << "Selecting Windows SDK version " << this->WindowsTargetPlatformVersion
  230. << " to target Windows " << this->SystemVersion << ".";
  231. mf->DisplayStatus(e.str(), -1);
  232. }
  233. mf->AddDefinition("CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION",
  234. this->WindowsTargetPlatformVersion);
  235. }
  236. bool cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
  237. std::string& toolset) const
  238. {
  239. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  240. if (this->IsWindowsStoreToolsetInstalled() &&
  241. this->IsWindowsDesktopToolsetInstalled()) {
  242. toolset = "v140";
  243. return true;
  244. }
  245. return false;
  246. }
  247. return this->cmGlobalVisualStudio12Generator::SelectWindowsStoreToolset(
  248. toolset);
  249. }
  250. bool cmGlobalVisualStudio14Generator::IsWindowsDesktopToolsetInstalled() const
  251. {
  252. const char desktop10Key[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  253. "VisualStudio\\14.0\\VC\\Runtimes";
  254. std::vector<std::string> vc14;
  255. return cmSystemTools::GetRegistrySubKeys(desktop10Key, vc14,
  256. cmSystemTools::KeyWOW64_32);
  257. }
  258. bool cmGlobalVisualStudio14Generator::IsWindowsStoreToolsetInstalled() const
  259. {
  260. const char universal10Key[] =
  261. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  262. "VisualStudio\\14.0\\Setup\\Build Tools for Windows 10;SrcPath";
  263. std::string win10SDK;
  264. return cmSystemTools::ReadRegistryValue(universal10Key, win10SDK,
  265. cmSystemTools::KeyWOW64_32);
  266. }
  267. std::string cmGlobalVisualStudio14Generator::GetWindows10SDKMaxVersion(
  268. cmMakefile* mf) const
  269. {
  270. // if the given value is set, it can either be OFF/FALSE or a valid SDK
  271. // string
  272. if (cmValue value = mf->GetDefinition(
  273. "CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION_MAXIMUM")) {
  274. // If the value is some off/false value, then there is NO maximum set.
  275. if (cmIsOff(value)) {
  276. return std::string();
  277. }
  278. // If the value is something else, trust that it is a valid SDK value.
  279. if (value) {
  280. return *value;
  281. }
  282. // If value is an invalid pointer, leave result unchanged.
  283. }
  284. return this->GetWindows10SDKMaxVersionDefault(mf);
  285. }
  286. std::string cmGlobalVisualStudio14Generator::GetWindows10SDKMaxVersionDefault(
  287. cmMakefile*) const
  288. {
  289. // The last Windows 10 SDK version that VS 2015 can target is 10.0.14393.0.
  290. //
  291. // "VS 2015 Users: The Windows 10 SDK (15063, 16299, 17134, 17763) is
  292. // officially only supported for VS 2017." From:
  293. // https://blogs.msdn.microsoft.com/chuckw/2018/10/02/windows-10-october-2018-update/
  294. return "10.0.14393.0";
  295. }
  296. #if defined(_WIN32) && !defined(__CYGWIN__)
  297. struct NoWindowsH
  298. {
  299. bool operator()(std::string const& p)
  300. {
  301. return !cmSystemTools::FileExists(p + "/um/windows.h", true);
  302. }
  303. };
  304. class WindowsSDKTooRecent
  305. {
  306. std::string const& MaxVersion;
  307. public:
  308. WindowsSDKTooRecent(std::string const& maxVersion)
  309. : MaxVersion(maxVersion)
  310. {
  311. }
  312. bool operator()(std::string const& v)
  313. {
  314. return cmSystemTools::VersionCompareGreater(v, MaxVersion);
  315. }
  316. };
  317. #endif
  318. std::string cmGlobalVisualStudio14Generator::GetWindows10SDKVersion(
  319. cmMakefile* mf)
  320. {
  321. #if defined(_WIN32) && !defined(__CYGWIN__)
  322. // Accept specific version requests as-is.
  323. if (this->GeneratorPlatformVersion) {
  324. std::string const& ver = *this->GeneratorPlatformVersion;
  325. // VS 2019 and above support specifying plain "10.0".
  326. if (this->Version >= VSVersion::VS16 && ver == "10.0"_s) {
  327. return ver;
  328. }
  329. }
  330. std::vector<std::string> win10Roots;
  331. {
  332. std::string win10Root;
  333. if (cmSystemTools::GetEnv("CMAKE_WINDOWS_KITS_10_DIR", win10Root)) {
  334. cmSystemTools::ConvertToUnixSlashes(win10Root);
  335. win10Roots.push_back(win10Root);
  336. }
  337. }
  338. {
  339. // This logic is taken from the vcvarsqueryregistry.bat file from VS2015
  340. // Try HKLM and then HKCU.
  341. std::string win10Root;
  342. if (cmSystemTools::ReadRegistryValue(
  343. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  344. "Windows Kits\\Installed Roots;KitsRoot10",
  345. win10Root, cmSystemTools::KeyWOW64_32) ||
  346. cmSystemTools::ReadRegistryValue(
  347. "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
  348. "Windows Kits\\Installed Roots;KitsRoot10",
  349. win10Root, cmSystemTools::KeyWOW64_32)) {
  350. cmSystemTools::ConvertToUnixSlashes(win10Root);
  351. win10Roots.push_back(win10Root);
  352. }
  353. }
  354. if (win10Roots.empty()) {
  355. return std::string();
  356. }
  357. std::vector<std::string> sdks;
  358. // Grab the paths of the different SDKs that are installed
  359. for (std::string const& i : win10Roots) {
  360. std::string path = i + "/Include/*";
  361. cmSystemTools::GlobDirs(path, sdks);
  362. }
  363. // Skip SDKs that do not contain <um/windows.h> because that indicates that
  364. // only the UCRT MSIs were installed for them.
  365. cm::erase_if(sdks, NoWindowsH());
  366. // Only use the filename, which will be the SDK version.
  367. for (std::string& i : sdks) {
  368. i = cmSystemTools::GetFilenameName(i);
  369. }
  370. // Skip SDKs that cannot be used with our toolset, unless the user does not
  371. // want to limit the highest supported SDK according to the Microsoft
  372. // documentation.
  373. std::string maxVersion = this->GetWindows10SDKMaxVersion(mf);
  374. if (!maxVersion.empty()) {
  375. cm::erase_if(sdks, WindowsSDKTooRecent(maxVersion));
  376. }
  377. // Sort the results to make sure we select the most recent one.
  378. std::sort(sdks.begin(), sdks.end(), cmSystemTools::VersionCompareGreater);
  379. // Look for a SDK exactly matching the requested version, if any.
  380. if (this->GeneratorPlatformVersion) {
  381. for (std::string const& i : sdks) {
  382. if (cmSystemTools::VersionCompareEqual(
  383. i, *this->GeneratorPlatformVersion)) {
  384. return i;
  385. }
  386. }
  387. // An exact version was requested but not found.
  388. // Our caller will issue the error message.
  389. return std::string();
  390. }
  391. if (mf->GetPolicyStatus(cmPolicies::CMP0149) == cmPolicies::NEW) {
  392. if (cm::optional<std::string> const envVer =
  393. cmSystemTools::GetEnvVar("WindowsSDKVersion")) {
  394. // Look for a SDK exactly matching the environment variable.
  395. for (std::string const& i : sdks) {
  396. if (cmSystemTools::VersionCompareEqual(i, *envVer)) {
  397. return i;
  398. }
  399. }
  400. }
  401. } else {
  402. // Look for a SDK exactly matching the target Windows version.
  403. for (std::string const& i : sdks) {
  404. if (cmSystemTools::VersionCompareEqual(i, this->SystemVersion)) {
  405. return i;
  406. }
  407. }
  408. }
  409. if (!sdks.empty()) {
  410. // Use the latest Windows 10 SDK since the exact version is not available.
  411. return sdks.at(0);
  412. }
  413. #endif
  414. (void)mf;
  415. // Return an empty string
  416. return std::string();
  417. }