cmGlobalVisualStudio14Generator.cxx 14 KB

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