cmGlobalVisualStudio14Generator.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 0;
  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::InitializeWindows(cmMakefile* mf)
  125. {
  126. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  127. return this->SelectWindows10SDK(mf, false);
  128. }
  129. return true;
  130. }
  131. bool cmGlobalVisualStudio14Generator::InitializeWindowsStore(cmMakefile* mf)
  132. {
  133. std::ostringstream e;
  134. if (!this->SelectWindowsStoreToolset(this->DefaultPlatformToolset)) {
  135. if (this->DefaultPlatformToolset.empty()) {
  136. e << this->GetName()
  137. << " supports Windows Store '8.0', '8.1' and "
  138. "'10.0', but not '"
  139. << this->SystemVersion << "'. Check CMAKE_SYSTEM_VERSION.";
  140. } else {
  141. e << "A Windows Store component with CMake requires both the Windows "
  142. << "Desktop SDK as well as the Windows Store '" << this->SystemVersion
  143. << "' SDK. Please make sure that you have both installed";
  144. }
  145. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  146. return false;
  147. }
  148. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  149. return this->SelectWindows10SDK(mf, true);
  150. }
  151. return true;
  152. }
  153. bool cmGlobalVisualStudio14Generator::InitializeAndroid(cmMakefile*)
  154. {
  155. return true;
  156. }
  157. bool cmGlobalVisualStudio14Generator::SelectWindows10SDK(cmMakefile* mf,
  158. bool required)
  159. {
  160. // Find the default version of the Windows 10 SDK.
  161. std::string const version = this->GetWindows10SDKVersion(mf);
  162. if (required && version.empty()) {
  163. std::ostringstream e;
  164. e << "Could not find an appropriate version of the Windows 10 SDK"
  165. << " installed on this machine";
  166. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  167. return false;
  168. }
  169. this->SetWindowsTargetPlatformVersion(version, mf);
  170. return true;
  171. }
  172. void cmGlobalVisualStudio14Generator::SetWindowsTargetPlatformVersion(
  173. std::string const& version, cmMakefile* mf)
  174. {
  175. this->WindowsTargetPlatformVersion = version;
  176. if (!cmSystemTools::VersionCompareEqual(this->WindowsTargetPlatformVersion,
  177. this->SystemVersion)) {
  178. std::ostringstream e;
  179. e << "Selecting Windows SDK version " << this->WindowsTargetPlatformVersion
  180. << " to target Windows " << this->SystemVersion << ".";
  181. mf->DisplayStatus(e.str(), -1);
  182. }
  183. mf->AddDefinition("CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION",
  184. this->WindowsTargetPlatformVersion);
  185. }
  186. bool cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
  187. std::string& toolset) const
  188. {
  189. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  190. if (this->IsWindowsStoreToolsetInstalled() &&
  191. this->IsWindowsDesktopToolsetInstalled()) {
  192. toolset = "v140";
  193. return true;
  194. } else {
  195. return false;
  196. }
  197. }
  198. return this->cmGlobalVisualStudio12Generator::SelectWindowsStoreToolset(
  199. toolset);
  200. }
  201. bool cmGlobalVisualStudio14Generator::IsWindowsDesktopToolsetInstalled() const
  202. {
  203. const char desktop10Key[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  204. "VisualStudio\\14.0\\VC\\Runtimes";
  205. std::vector<std::string> vc14;
  206. return cmSystemTools::GetRegistrySubKeys(desktop10Key, vc14,
  207. cmSystemTools::KeyWOW64_32);
  208. }
  209. bool cmGlobalVisualStudio14Generator::IsWindowsStoreToolsetInstalled() const
  210. {
  211. const char universal10Key[] =
  212. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  213. "VisualStudio\\14.0\\Setup\\Build Tools for Windows 10;SrcPath";
  214. std::string win10SDK;
  215. return cmSystemTools::ReadRegistryValue(universal10Key, win10SDK,
  216. cmSystemTools::KeyWOW64_32);
  217. }
  218. std::string cmGlobalVisualStudio14Generator::GetWindows10SDKMaxVersion(
  219. cmMakefile* mf) const
  220. {
  221. // if the given value is set, it can either be OFF/FALSE or a valid SDK
  222. // string
  223. if (cmValue value = mf->GetDefinition(
  224. "CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION_MAXIMUM")) {
  225. // If the value is some off/false value, then there is NO maximum set.
  226. if (cmIsOff(value)) {
  227. return std::string();
  228. }
  229. // If the value is something else, trust that it is a valid SDK value.
  230. else if (value) {
  231. return *value;
  232. }
  233. // If value is an invalid pointer, leave result unchanged.
  234. }
  235. return this->GetWindows10SDKMaxVersionDefault(mf);
  236. }
  237. std::string cmGlobalVisualStudio14Generator::GetWindows10SDKMaxVersionDefault(
  238. cmMakefile*) const
  239. {
  240. // The last Windows 10 SDK version that VS 2015 can target is 10.0.14393.0.
  241. //
  242. // "VS 2015 Users: The Windows 10 SDK (15063, 16299, 17134, 17763) is
  243. // officially only supported for VS 2017." From:
  244. // https://blogs.msdn.microsoft.com/chuckw/2018/10/02/windows-10-october-2018-update/
  245. return "10.0.14393.0";
  246. }
  247. #if defined(_WIN32) && !defined(__CYGWIN__)
  248. struct NoWindowsH
  249. {
  250. bool operator()(std::string const& p)
  251. {
  252. return !cmSystemTools::FileExists(p + "/um/windows.h", true);
  253. }
  254. };
  255. class WindowsSDKTooRecent
  256. {
  257. std::string const& MaxVersion;
  258. public:
  259. WindowsSDKTooRecent(std::string const& maxVersion)
  260. : MaxVersion(maxVersion)
  261. {
  262. }
  263. bool operator()(std::string const& v)
  264. {
  265. return cmSystemTools::VersionCompareGreater(v, MaxVersion);
  266. }
  267. };
  268. #endif
  269. std::string cmGlobalVisualStudio14Generator::GetWindows10SDKVersion(
  270. cmMakefile* mf)
  271. {
  272. #if defined(_WIN32) && !defined(__CYGWIN__)
  273. std::vector<std::string> win10Roots;
  274. {
  275. std::string win10Root;
  276. if (cmSystemTools::GetEnv("CMAKE_WINDOWS_KITS_10_DIR", win10Root)) {
  277. cmSystemTools::ConvertToUnixSlashes(win10Root);
  278. win10Roots.push_back(win10Root);
  279. }
  280. }
  281. {
  282. // This logic is taken from the vcvarsqueryregistry.bat file from VS2015
  283. // Try HKLM and then HKCU.
  284. std::string win10Root;
  285. if (cmSystemTools::ReadRegistryValue(
  286. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  287. "Windows Kits\\Installed Roots;KitsRoot10",
  288. win10Root, cmSystemTools::KeyWOW64_32) ||
  289. cmSystemTools::ReadRegistryValue(
  290. "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
  291. "Windows Kits\\Installed Roots;KitsRoot10",
  292. win10Root, cmSystemTools::KeyWOW64_32)) {
  293. cmSystemTools::ConvertToUnixSlashes(win10Root);
  294. win10Roots.push_back(win10Root);
  295. }
  296. }
  297. if (win10Roots.empty()) {
  298. return std::string();
  299. }
  300. std::vector<std::string> sdks;
  301. // Grab the paths of the different SDKs that are installed
  302. for (std::string const& i : win10Roots) {
  303. std::string path = i + "/Include/*";
  304. cmSystemTools::GlobDirs(path, sdks);
  305. }
  306. // Skip SDKs that do not contain <um/windows.h> because that indicates that
  307. // only the UCRT MSIs were installed for them.
  308. cm::erase_if(sdks, NoWindowsH());
  309. // Only use the filename, which will be the SDK version.
  310. for (std::string& i : sdks) {
  311. i = cmSystemTools::GetFilenameName(i);
  312. }
  313. // Skip SDKs that cannot be used with our toolset, unless the user does not
  314. // want to limit the highest supported SDK according to the Microsoft
  315. // documentation.
  316. std::string maxVersion = this->GetWindows10SDKMaxVersion(mf);
  317. if (!maxVersion.empty()) {
  318. cm::erase_if(sdks, WindowsSDKTooRecent(maxVersion));
  319. }
  320. // Sort the results to make sure we select the most recent one.
  321. std::sort(sdks.begin(), sdks.end(), cmSystemTools::VersionCompareGreater);
  322. // Look for a SDK exactly matching the requested target version.
  323. for (std::string const& i : sdks) {
  324. if (cmSystemTools::VersionCompareEqual(i, this->SystemVersion)) {
  325. return i;
  326. }
  327. }
  328. if (!sdks.empty()) {
  329. // Use the latest Windows 10 SDK since the exact version is not available.
  330. return sdks.at(0);
  331. }
  332. #endif
  333. // Return an empty string
  334. return std::string();
  335. }