cmGlobalVisualStudio12Generator.cxx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "cmGlobalVisualStudio12Generator.h"
  4. #include <cstring>
  5. #include <sstream>
  6. #include <vector>
  7. #include "cmDocumentationEntry.h"
  8. #include "cmGlobalGenerator.h"
  9. #include "cmGlobalGeneratorFactory.h"
  10. #include "cmGlobalVisualStudioGenerator.h"
  11. #include "cmMakefile.h"
  12. #include "cmMessageType.h"
  13. #include "cmStringAlgorithms.h"
  14. #include "cmSystemTools.h"
  15. static const char vs12generatorName[] = "Visual Studio 12 2013";
  16. // Map generator name without year to name with year.
  17. static const char* cmVS12GenName(const std::string& name, std::string& genName)
  18. {
  19. if (strncmp(name.c_str(), vs12generatorName,
  20. sizeof(vs12generatorName) - 6) != 0) {
  21. return 0;
  22. }
  23. const char* p = name.c_str() + sizeof(vs12generatorName) - 6;
  24. if (cmHasLiteralPrefix(p, " 2013")) {
  25. p += 5;
  26. }
  27. genName = std::string(vs12generatorName) + p;
  28. return p;
  29. }
  30. class cmGlobalVisualStudio12Generator::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 = cmVS12GenName(name, genName);
  39. if (!p) {
  40. return std::unique_ptr<cmGlobalGenerator>();
  41. }
  42. if (!*p) {
  43. return std::unique_ptr<cmGlobalGenerator>(
  44. new cmGlobalVisualStudio12Generator(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 cmGlobalVisualStudio12Generator(cm, genName, "x64"));
  52. }
  53. if (strcmp(p, "ARM") == 0) {
  54. return std::unique_ptr<cmGlobalGenerator>(
  55. new cmGlobalVisualStudio12Generator(cm, genName, "ARM"));
  56. }
  57. return std::unique_ptr<cmGlobalGenerator>();
  58. }
  59. void GetDocumentation(cmDocumentationEntry& entry) const override
  60. {
  61. entry.Name = std::string(vs12generatorName) + " [arch]";
  62. entry.Brief = "Generates Visual Studio 2013 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(vs12generatorName);
  69. return names;
  70. }
  71. std::vector<std::string> GetGeneratorNamesWithPlatform() const override
  72. {
  73. std::vector<std::string> names;
  74. names.push_back(vs12generatorName + std::string(" ARM"));
  75. names.push_back(vs12generatorName + 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. cmGlobalVisualStudio12Generator::NewFactory()
  92. {
  93. return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory);
  94. }
  95. cmGlobalVisualStudio12Generator::cmGlobalVisualStudio12Generator(
  96. cmake* cm, const std::string& name,
  97. std::string const& platformInGeneratorName)
  98. : cmGlobalVisualStudio11Generator(cm, name, platformInGeneratorName)
  99. {
  100. std::string vc12Express;
  101. this->ExpressEdition = cmSystemTools::ReadRegistryValue(
  102. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\12.0\\Setup\\VC;"
  103. "ProductDir",
  104. vc12Express, cmSystemTools::KeyWOW64_32);
  105. this->DefaultPlatformToolset = "v120";
  106. this->DefaultCLFlagTableName = "v12";
  107. this->DefaultCSharpFlagTableName = "v12";
  108. this->DefaultLibFlagTableName = "v12";
  109. this->DefaultLinkFlagTableName = "v12";
  110. this->DefaultMasmFlagTableName = "v12";
  111. this->DefaultRCFlagTableName = "v12";
  112. this->Version = VSVersion::VS12;
  113. }
  114. bool cmGlobalVisualStudio12Generator::MatchesGeneratorName(
  115. const std::string& name) const
  116. {
  117. std::string genName;
  118. if (cmVS12GenName(name, genName)) {
  119. return genName == this->GetName();
  120. }
  121. return false;
  122. }
  123. bool cmGlobalVisualStudio12Generator::ProcessGeneratorToolsetField(
  124. std::string const& key, std::string const& value)
  125. {
  126. if (key == "host" &&
  127. (value == "x64" || value == "x86" || value == "ARM64")) {
  128. this->GeneratorToolsetHostArchitecture = value;
  129. return true;
  130. }
  131. return this->cmGlobalVisualStudio11Generator::ProcessGeneratorToolsetField(
  132. key, value);
  133. }
  134. bool cmGlobalVisualStudio12Generator::InitializeWindowsPhone(cmMakefile* mf)
  135. {
  136. if (!this->SelectWindowsPhoneToolset(this->DefaultPlatformToolset)) {
  137. std::ostringstream e;
  138. if (this->DefaultPlatformToolset.empty()) {
  139. e << this->GetName()
  140. << " supports Windows Phone '8.0' and '8.1', but "
  141. "not '"
  142. << this->SystemVersion << "'. Check CMAKE_SYSTEM_VERSION.";
  143. } else {
  144. e << "A Windows Phone component with CMake requires both the Windows "
  145. << "Desktop SDK as well as the Windows Phone '" << this->SystemVersion
  146. << "' SDK. Please make sure that you have both installed";
  147. }
  148. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  149. return false;
  150. }
  151. return true;
  152. }
  153. bool cmGlobalVisualStudio12Generator::InitializeWindowsStore(cmMakefile* mf)
  154. {
  155. if (!this->SelectWindowsStoreToolset(this->DefaultPlatformToolset)) {
  156. std::ostringstream e;
  157. if (this->DefaultPlatformToolset.empty()) {
  158. e << this->GetName()
  159. << " supports Windows Store '8.0' and '8.1', but "
  160. "not '"
  161. << this->SystemVersion << "'. Check CMAKE_SYSTEM_VERSION.";
  162. } else {
  163. e << "A Windows Store component with CMake requires both the Windows "
  164. << "Desktop SDK as well as the Windows Store '" << this->SystemVersion
  165. << "' SDK. Please make sure that you have both installed";
  166. }
  167. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  168. return false;
  169. }
  170. return true;
  171. }
  172. bool cmGlobalVisualStudio12Generator::SelectWindowsPhoneToolset(
  173. std::string& toolset) const
  174. {
  175. if (this->SystemVersion == "8.1") {
  176. if (this->IsWindowsPhoneToolsetInstalled() &&
  177. this->IsWindowsDesktopToolsetInstalled()) {
  178. toolset = "v120_wp81";
  179. return true;
  180. } else {
  181. return false;
  182. }
  183. }
  184. return this->cmGlobalVisualStudio11Generator::SelectWindowsPhoneToolset(
  185. toolset);
  186. }
  187. bool cmGlobalVisualStudio12Generator::SelectWindowsStoreToolset(
  188. std::string& toolset) const
  189. {
  190. if (this->SystemVersion == "8.1") {
  191. if (this->IsWindowsStoreToolsetInstalled() &&
  192. this->IsWindowsDesktopToolsetInstalled()) {
  193. toolset = "v120";
  194. return true;
  195. } else {
  196. return false;
  197. }
  198. }
  199. return this->cmGlobalVisualStudio11Generator::SelectWindowsStoreToolset(
  200. toolset);
  201. }
  202. bool cmGlobalVisualStudio12Generator::IsWindowsDesktopToolsetInstalled() const
  203. {
  204. const char desktop81Key[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  205. "VisualStudio\\12.0\\VC\\LibraryDesktop";
  206. std::vector<std::string> subkeys;
  207. return cmSystemTools::GetRegistrySubKeys(desktop81Key, subkeys,
  208. cmSystemTools::KeyWOW64_32);
  209. }
  210. bool cmGlobalVisualStudio12Generator::IsWindowsPhoneToolsetInstalled() const
  211. {
  212. const char wp81Key[] =
  213. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  214. "Microsoft SDKs\\WindowsPhone\\v8.1\\Install Path;Install Path";
  215. std::string path;
  216. cmSystemTools::ReadRegistryValue(wp81Key, path, cmSystemTools::KeyWOW64_32);
  217. return !path.empty();
  218. }
  219. bool cmGlobalVisualStudio12Generator::IsWindowsStoreToolsetInstalled() const
  220. {
  221. const char win81Key[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  222. "VisualStudio\\12.0\\VC\\Libraries\\Core\\Arm";
  223. std::vector<std::string> subkeys;
  224. return cmSystemTools::GetRegistrySubKeys(win81Key, subkeys,
  225. cmSystemTools::KeyWOW64_32);
  226. }