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 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::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. }
  195. return false;
  196. }
  197. return this->cmGlobalVisualStudio12Generator::SelectWindowsStoreToolset(
  198. toolset);
  199. }
  200. bool cmGlobalVisualStudio14Generator::IsWindowsDesktopToolsetInstalled() const
  201. {
  202. const char desktop10Key[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  203. "VisualStudio\\14.0\\VC\\Runtimes";
  204. std::vector<std::string> vc14;
  205. return cmSystemTools::GetRegistrySubKeys(desktop10Key, vc14,
  206. cmSystemTools::KeyWOW64_32);
  207. }
  208. bool cmGlobalVisualStudio14Generator::IsWindowsStoreToolsetInstalled() const
  209. {
  210. const char universal10Key[] =
  211. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  212. "VisualStudio\\14.0\\Setup\\Build Tools for Windows 10;SrcPath";
  213. std::string win10SDK;
  214. return cmSystemTools::ReadRegistryValue(universal10Key, win10SDK,
  215. cmSystemTools::KeyWOW64_32);
  216. }
  217. std::string cmGlobalVisualStudio14Generator::GetWindows10SDKMaxVersion(
  218. cmMakefile* mf) const
  219. {
  220. // if the given value is set, it can either be OFF/FALSE or a valid SDK
  221. // string
  222. if (cmValue value = mf->GetDefinition(
  223. "CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION_MAXIMUM")) {
  224. // If the value is some off/false value, then there is NO maximum set.
  225. if (cmIsOff(value)) {
  226. return std::string();
  227. }
  228. // If the value is something else, trust that it is a valid SDK value.
  229. if (value) {
  230. return *value;
  231. }
  232. // If value is an invalid pointer, leave result unchanged.
  233. }
  234. return this->GetWindows10SDKMaxVersionDefault(mf);
  235. }
  236. std::string cmGlobalVisualStudio14Generator::GetWindows10SDKMaxVersionDefault(
  237. cmMakefile*) const
  238. {
  239. // The last Windows 10 SDK version that VS 2015 can target is 10.0.14393.0.
  240. //
  241. // "VS 2015 Users: The Windows 10 SDK (15063, 16299, 17134, 17763) is
  242. // officially only supported for VS 2017." From:
  243. // https://blogs.msdn.microsoft.com/chuckw/2018/10/02/windows-10-october-2018-update/
  244. return "10.0.14393.0";
  245. }
  246. #if defined(_WIN32) && !defined(__CYGWIN__)
  247. struct NoWindowsH
  248. {
  249. bool operator()(std::string const& p)
  250. {
  251. return !cmSystemTools::FileExists(p + "/um/windows.h", true);
  252. }
  253. };
  254. class WindowsSDKTooRecent
  255. {
  256. std::string const& MaxVersion;
  257. public:
  258. WindowsSDKTooRecent(std::string const& maxVersion)
  259. : MaxVersion(maxVersion)
  260. {
  261. }
  262. bool operator()(std::string const& v)
  263. {
  264. return cmSystemTools::VersionCompareGreater(v, MaxVersion);
  265. }
  266. };
  267. #endif
  268. std::string cmGlobalVisualStudio14Generator::GetWindows10SDKVersion(
  269. cmMakefile* mf)
  270. {
  271. #if defined(_WIN32) && !defined(__CYGWIN__)
  272. std::vector<std::string> win10Roots;
  273. {
  274. std::string win10Root;
  275. if (cmSystemTools::GetEnv("CMAKE_WINDOWS_KITS_10_DIR", win10Root)) {
  276. cmSystemTools::ConvertToUnixSlashes(win10Root);
  277. win10Roots.push_back(win10Root);
  278. }
  279. }
  280. {
  281. // This logic is taken from the vcvarsqueryregistry.bat file from VS2015
  282. // Try HKLM and then HKCU.
  283. std::string win10Root;
  284. if (cmSystemTools::ReadRegistryValue(
  285. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  286. "Windows Kits\\Installed Roots;KitsRoot10",
  287. win10Root, cmSystemTools::KeyWOW64_32) ||
  288. cmSystemTools::ReadRegistryValue(
  289. "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
  290. "Windows Kits\\Installed Roots;KitsRoot10",
  291. win10Root, cmSystemTools::KeyWOW64_32)) {
  292. cmSystemTools::ConvertToUnixSlashes(win10Root);
  293. win10Roots.push_back(win10Root);
  294. }
  295. }
  296. if (win10Roots.empty()) {
  297. return std::string();
  298. }
  299. std::vector<std::string> sdks;
  300. // Grab the paths of the different SDKs that are installed
  301. for (std::string const& i : win10Roots) {
  302. std::string path = i + "/Include/*";
  303. cmSystemTools::GlobDirs(path, sdks);
  304. }
  305. // Skip SDKs that do not contain <um/windows.h> because that indicates that
  306. // only the UCRT MSIs were installed for them.
  307. cm::erase_if(sdks, NoWindowsH());
  308. // Only use the filename, which will be the SDK version.
  309. for (std::string& i : sdks) {
  310. i = cmSystemTools::GetFilenameName(i);
  311. }
  312. // Skip SDKs that cannot be used with our toolset, unless the user does not
  313. // want to limit the highest supported SDK according to the Microsoft
  314. // documentation.
  315. std::string maxVersion = this->GetWindows10SDKMaxVersion(mf);
  316. if (!maxVersion.empty()) {
  317. cm::erase_if(sdks, WindowsSDKTooRecent(maxVersion));
  318. }
  319. // Sort the results to make sure we select the most recent one.
  320. std::sort(sdks.begin(), sdks.end(), cmSystemTools::VersionCompareGreater);
  321. // Look for a SDK exactly matching the requested target version.
  322. for (std::string const& i : sdks) {
  323. if (cmSystemTools::VersionCompareEqual(i, this->SystemVersion)) {
  324. return i;
  325. }
  326. }
  327. if (!sdks.empty()) {
  328. // Use the latest Windows 10 SDK since the exact version is not available.
  329. return sdks.at(0);
  330. }
  331. #endif
  332. (void)mf;
  333. // Return an empty string
  334. return std::string();
  335. }