cmGlobalVisualStudio14Generator.cxx 13 KB

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