cmGlobalVisualStudio14Generator.cxx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "cmAlgorithms.h"
  5. #include "cmDocumentationEntry.h"
  6. #include "cmLocalVisualStudio10Generator.h"
  7. #include "cmMakefile.h"
  8. #include "cmVS140CLFlagTable.h"
  9. #include "cmVS140CSharpFlagTable.h"
  10. #include "cmVS140LinkFlagTable.h"
  11. #include "cmVS14LibFlagTable.h"
  12. #include "cmVS14MASMFlagTable.h"
  13. #include "cmVS14RCFlagTable.h"
  14. static const char vs14generatorName[] = "Visual Studio 14 2015";
  15. // Map generator name without year to name with year.
  16. static const char* cmVS14GenName(const std::string& name, std::string& genName)
  17. {
  18. if (strncmp(name.c_str(), vs14generatorName,
  19. sizeof(vs14generatorName) - 6) != 0) {
  20. return 0;
  21. }
  22. const char* p = name.c_str() + sizeof(vs14generatorName) - 6;
  23. if (cmHasLiteralPrefix(p, " 2015")) {
  24. p += 5;
  25. }
  26. genName = std::string(vs14generatorName) + p;
  27. return p;
  28. }
  29. class cmGlobalVisualStudio14Generator::Factory
  30. : public cmGlobalGeneratorFactory
  31. {
  32. public:
  33. cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
  34. cmake* cm) const override
  35. {
  36. std::string genName;
  37. const char* p = cmVS14GenName(name, genName);
  38. if (!p) {
  39. return 0;
  40. }
  41. if (!*p) {
  42. return new cmGlobalVisualStudio14Generator(cm, genName, "");
  43. }
  44. if (*p++ != ' ') {
  45. return 0;
  46. }
  47. if (strcmp(p, "Win64") == 0) {
  48. return new cmGlobalVisualStudio14Generator(cm, genName, "x64");
  49. }
  50. if (strcmp(p, "ARM") == 0) {
  51. return new cmGlobalVisualStudio14Generator(cm, genName, "ARM");
  52. }
  53. return 0;
  54. }
  55. void GetDocumentation(cmDocumentationEntry& entry) const override
  56. {
  57. entry.Name = std::string(vs14generatorName) + " [arch]";
  58. entry.Brief = "Generates Visual Studio 2015 project files. "
  59. "Optional [arch] can be \"Win64\" or \"ARM\".";
  60. }
  61. void GetGenerators(std::vector<std::string>& names) const override
  62. {
  63. names.push_back(vs14generatorName);
  64. names.push_back(vs14generatorName + std::string(" ARM"));
  65. names.push_back(vs14generatorName + std::string(" Win64"));
  66. }
  67. bool SupportsToolset() const override { return true; }
  68. bool SupportsPlatform() const override { return true; }
  69. };
  70. cmGlobalGeneratorFactory* cmGlobalVisualStudio14Generator::NewFactory()
  71. {
  72. return new Factory;
  73. }
  74. cmGlobalVisualStudio14Generator::cmGlobalVisualStudio14Generator(
  75. cmake* cm, const std::string& name, const std::string& platformName)
  76. : cmGlobalVisualStudio12Generator(cm, name, platformName)
  77. {
  78. std::string vc14Express;
  79. this->ExpressEdition = cmSystemTools::ReadRegistryValue(
  80. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\14.0\\Setup\\VC;"
  81. "ProductDir",
  82. vc14Express, cmSystemTools::KeyWOW64_32);
  83. this->DefaultPlatformToolset = "v140";
  84. this->DefaultClFlagTable = cmVS140CLFlagTable;
  85. this->DefaultCSharpFlagTable = cmVS140CSharpFlagTable;
  86. this->DefaultLibFlagTable = cmVS14LibFlagTable;
  87. this->DefaultLinkFlagTable = cmVS140LinkFlagTable;
  88. this->DefaultMasmFlagTable = cmVS14MASMFlagTable;
  89. this->DefaultRcFlagTable = cmVS14RCFlagTable;
  90. this->Version = VS14;
  91. }
  92. bool cmGlobalVisualStudio14Generator::MatchesGeneratorName(
  93. const std::string& name) const
  94. {
  95. std::string genName;
  96. if (cmVS14GenName(name, genName)) {
  97. return genName == this->GetName();
  98. }
  99. return false;
  100. }
  101. bool cmGlobalVisualStudio14Generator::InitializeWindows(cmMakefile* mf)
  102. {
  103. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  104. return this->SelectWindows10SDK(mf, false);
  105. }
  106. return true;
  107. }
  108. bool cmGlobalVisualStudio14Generator::InitializeWindowsStore(cmMakefile* mf)
  109. {
  110. std::ostringstream e;
  111. if (!this->SelectWindowsStoreToolset(this->DefaultPlatformToolset)) {
  112. if (this->DefaultPlatformToolset.empty()) {
  113. e << this->GetName() << " supports Windows Store '8.0', '8.1' and "
  114. "'10.0', but not '"
  115. << this->SystemVersion << "'. Check CMAKE_SYSTEM_VERSION.";
  116. } else {
  117. e << "A Windows Store component with CMake requires both the Windows "
  118. << "Desktop SDK as well as the Windows Store '" << this->SystemVersion
  119. << "' SDK. Please make sure that you have both installed";
  120. }
  121. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  122. return false;
  123. }
  124. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  125. return this->SelectWindows10SDK(mf, true);
  126. }
  127. return true;
  128. }
  129. bool cmGlobalVisualStudio14Generator::SelectWindows10SDK(cmMakefile* mf,
  130. bool required)
  131. {
  132. // Find the default version of the Windows 10 SDK.
  133. this->WindowsTargetPlatformVersion = this->GetWindows10SDKVersion();
  134. if (required && this->WindowsTargetPlatformVersion.empty()) {
  135. std::ostringstream e;
  136. e << "Could not find an appropriate version of the Windows 10 SDK"
  137. << " installed on this machine";
  138. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  139. return false;
  140. }
  141. if (!cmSystemTools::VersionCompareEqual(this->WindowsTargetPlatformVersion,
  142. this->SystemVersion)) {
  143. std::ostringstream e;
  144. e << "Selecting Windows SDK version " << this->WindowsTargetPlatformVersion
  145. << " to target Windows " << this->SystemVersion << ".";
  146. mf->DisplayStatus(e.str().c_str(), -1);
  147. }
  148. mf->AddDefinition("CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION",
  149. this->WindowsTargetPlatformVersion.c_str());
  150. return true;
  151. }
  152. bool cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
  153. std::string& toolset) const
  154. {
  155. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  156. if (this->IsWindowsStoreToolsetInstalled() &&
  157. this->IsWindowsDesktopToolsetInstalled()) {
  158. toolset = "v140";
  159. return true;
  160. } else {
  161. return false;
  162. }
  163. }
  164. return this->cmGlobalVisualStudio12Generator::SelectWindowsStoreToolset(
  165. toolset);
  166. }
  167. void cmGlobalVisualStudio14Generator::WriteSLNHeader(std::ostream& fout)
  168. {
  169. // Visual Studio 14 writes .sln format 12.00
  170. fout << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  171. if (this->ExpressEdition) {
  172. fout << "# Visual Studio Express 14 for Windows Desktop\n";
  173. } else {
  174. fout << "# Visual Studio 14\n";
  175. }
  176. }
  177. bool cmGlobalVisualStudio14Generator::IsWindowsDesktopToolsetInstalled() const
  178. {
  179. const char desktop10Key[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  180. "VisualStudio\\14.0\\VC\\Runtimes";
  181. std::vector<std::string> vc14;
  182. return cmSystemTools::GetRegistrySubKeys(desktop10Key, vc14,
  183. cmSystemTools::KeyWOW64_32);
  184. }
  185. bool cmGlobalVisualStudio14Generator::IsWindowsStoreToolsetInstalled() const
  186. {
  187. const char universal10Key[] =
  188. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  189. "VisualStudio\\14.0\\Setup\\Build Tools for Windows 10;SrcPath";
  190. std::string win10SDK;
  191. return cmSystemTools::ReadRegistryValue(universal10Key, win10SDK,
  192. cmSystemTools::KeyWOW64_32);
  193. }
  194. #if defined(_WIN32) && !defined(__CYGWIN__)
  195. struct NoWindowsH
  196. {
  197. bool operator()(std::string const& p)
  198. {
  199. return !cmSystemTools::FileExists(p + "/um/windows.h", true);
  200. }
  201. };
  202. #endif
  203. std::string cmGlobalVisualStudio14Generator::GetWindows10SDKVersion()
  204. {
  205. #if defined(_WIN32) && !defined(__CYGWIN__)
  206. std::vector<std::string> win10Roots;
  207. {
  208. std::string win10Root;
  209. if (cmSystemTools::GetEnv("CMAKE_WINDOWS_KITS_10_DIR", win10Root)) {
  210. cmSystemTools::ConvertToUnixSlashes(win10Root);
  211. win10Roots.push_back(win10Root);
  212. }
  213. }
  214. {
  215. // This logic is taken from the vcvarsqueryregistry.bat file from VS2015
  216. // Try HKLM and then HKCU.
  217. std::string win10Root;
  218. if (cmSystemTools::ReadRegistryValue(
  219. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  220. "Windows Kits\\Installed Roots;KitsRoot10",
  221. win10Root, cmSystemTools::KeyWOW64_32) ||
  222. cmSystemTools::ReadRegistryValue(
  223. "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
  224. "Windows Kits\\Installed Roots;KitsRoot10",
  225. win10Root, cmSystemTools::KeyWOW64_32)) {
  226. cmSystemTools::ConvertToUnixSlashes(win10Root);
  227. win10Roots.push_back(win10Root);
  228. }
  229. }
  230. if (win10Roots.empty()) {
  231. return std::string();
  232. }
  233. std::vector<std::string> sdks;
  234. // Grab the paths of the different SDKs that are installed
  235. for (std::string const& i : win10Roots) {
  236. std::string path = i + "/Include/*";
  237. cmSystemTools::GlobDirs(path, sdks);
  238. }
  239. // Skip SDKs that do not contain <um/windows.h> because that indicates that
  240. // only the UCRT MSIs were installed for them.
  241. cmEraseIf(sdks, NoWindowsH());
  242. if (!sdks.empty()) {
  243. // Only use the filename, which will be the SDK version.
  244. for (std::string& i : sdks) {
  245. i = cmSystemTools::GetFilenameName(i);
  246. }
  247. // Sort the results to make sure we select the most recent one.
  248. std::sort(sdks.begin(), sdks.end(), cmSystemTools::VersionCompareGreater);
  249. // Look for a SDK exactly matching the requested target version.
  250. for (std::string const& i : sdks) {
  251. if (cmSystemTools::VersionCompareEqual(i, this->SystemVersion)) {
  252. return i;
  253. }
  254. }
  255. // Use the latest Windows 10 SDK since the exact version is not available.
  256. return sdks.at(0);
  257. }
  258. #endif
  259. // Return an empty string
  260. return std::string();
  261. }