cmGlobalVisualStudio12Generator.cxx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 "cmAlgorithms.h"
  5. #include "cmLocalVisualStudio10Generator.h"
  6. #include "cmMakefile.h"
  7. static const char vs12generatorName[] = "Visual Studio 12 2013";
  8. // Map generator name without year to name with year.
  9. static const char* cmVS12GenName(const std::string& name, std::string& genName)
  10. {
  11. if (strncmp(name.c_str(), vs12generatorName,
  12. sizeof(vs12generatorName) - 6) != 0) {
  13. return 0;
  14. }
  15. const char* p = name.c_str() + sizeof(vs12generatorName) - 6;
  16. if (cmHasLiteralPrefix(p, " 2013")) {
  17. p += 5;
  18. }
  19. genName = std::string(vs12generatorName) + p;
  20. return p;
  21. }
  22. class cmGlobalVisualStudio12Generator::Factory
  23. : public cmGlobalGeneratorFactory
  24. {
  25. public:
  26. cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
  27. cmake* cm) const CM_OVERRIDE
  28. {
  29. std::string genName;
  30. const char* p = cmVS12GenName(name, genName);
  31. if (!p) {
  32. return 0;
  33. }
  34. if (!*p) {
  35. return new cmGlobalVisualStudio12Generator(cm, genName, "");
  36. }
  37. if (*p++ != ' ') {
  38. return 0;
  39. }
  40. if (strcmp(p, "Win64") == 0) {
  41. return new cmGlobalVisualStudio12Generator(cm, genName, "x64");
  42. }
  43. if (strcmp(p, "ARM") == 0) {
  44. return new cmGlobalVisualStudio12Generator(cm, genName, "ARM");
  45. }
  46. return 0;
  47. }
  48. void GetDocumentation(cmDocumentationEntry& entry) const CM_OVERRIDE
  49. {
  50. entry.Name = std::string(vs12generatorName) + " [arch]";
  51. entry.Brief = "Generates Visual Studio 2013 project files. "
  52. "Optional [arch] can be \"Win64\" or \"ARM\".";
  53. }
  54. void GetGenerators(std::vector<std::string>& names) const CM_OVERRIDE
  55. {
  56. names.push_back(vs12generatorName);
  57. names.push_back(vs12generatorName + std::string(" ARM"));
  58. names.push_back(vs12generatorName + std::string(" Win64"));
  59. }
  60. bool SupportsToolset() const CM_OVERRIDE { return true; }
  61. bool SupportsPlatform() const CM_OVERRIDE { return true; }
  62. };
  63. cmGlobalGeneratorFactory* cmGlobalVisualStudio12Generator::NewFactory()
  64. {
  65. return new Factory;
  66. }
  67. cmGlobalVisualStudio12Generator::cmGlobalVisualStudio12Generator(
  68. cmake* cm, const std::string& name, const std::string& platformName)
  69. : cmGlobalVisualStudio11Generator(cm, name, platformName)
  70. {
  71. std::string vc12Express;
  72. this->ExpressEdition = cmSystemTools::ReadRegistryValue(
  73. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\12.0\\Setup\\VC;"
  74. "ProductDir",
  75. vc12Express, cmSystemTools::KeyWOW64_32);
  76. this->DefaultPlatformToolset = "v120";
  77. this->Version = VS12;
  78. }
  79. bool cmGlobalVisualStudio12Generator::MatchesGeneratorName(
  80. const std::string& name) const
  81. {
  82. std::string genName;
  83. if (cmVS12GenName(name, genName)) {
  84. return genName == this->GetName();
  85. }
  86. return false;
  87. }
  88. bool cmGlobalVisualStudio12Generator::InitializeWindowsPhone(cmMakefile* mf)
  89. {
  90. if (!this->SelectWindowsPhoneToolset(this->DefaultPlatformToolset)) {
  91. std::ostringstream e;
  92. if (this->DefaultPlatformToolset.empty()) {
  93. e << this->GetName() << " supports Windows Phone '8.0' and '8.1', but "
  94. "not '"
  95. << this->SystemVersion << "'. Check CMAKE_SYSTEM_VERSION.";
  96. } else {
  97. e << "A Windows Phone component with CMake requires both the Windows "
  98. << "Desktop SDK as well as the Windows Phone '" << this->SystemVersion
  99. << "' SDK. Please make sure that you have both installed";
  100. }
  101. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  102. return false;
  103. }
  104. return true;
  105. }
  106. bool cmGlobalVisualStudio12Generator::InitializeWindowsStore(cmMakefile* mf)
  107. {
  108. if (!this->SelectWindowsStoreToolset(this->DefaultPlatformToolset)) {
  109. std::ostringstream e;
  110. if (this->DefaultPlatformToolset.empty()) {
  111. e << this->GetName() << " supports Windows Store '8.0' and '8.1', but "
  112. "not '"
  113. << this->SystemVersion << "'. Check CMAKE_SYSTEM_VERSION.";
  114. } else {
  115. e << "A Windows Store component with CMake requires both the Windows "
  116. << "Desktop SDK as well as the Windows Store '" << this->SystemVersion
  117. << "' SDK. Please make sure that you have both installed";
  118. }
  119. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  120. return false;
  121. }
  122. return true;
  123. }
  124. bool cmGlobalVisualStudio12Generator::SelectWindowsPhoneToolset(
  125. std::string& toolset) const
  126. {
  127. if (this->SystemVersion == "8.1") {
  128. if (this->IsWindowsPhoneToolsetInstalled() &&
  129. this->IsWindowsDesktopToolsetInstalled()) {
  130. toolset = "v120_wp81";
  131. return true;
  132. } else {
  133. return false;
  134. }
  135. }
  136. return this->cmGlobalVisualStudio11Generator::SelectWindowsPhoneToolset(
  137. toolset);
  138. }
  139. bool cmGlobalVisualStudio12Generator::SelectWindowsStoreToolset(
  140. std::string& toolset) const
  141. {
  142. if (this->SystemVersion == "8.1") {
  143. if (this->IsWindowsStoreToolsetInstalled() &&
  144. this->IsWindowsDesktopToolsetInstalled()) {
  145. toolset = "v120";
  146. return true;
  147. } else {
  148. return false;
  149. }
  150. }
  151. return this->cmGlobalVisualStudio11Generator::SelectWindowsStoreToolset(
  152. toolset);
  153. }
  154. void cmGlobalVisualStudio12Generator::WriteSLNHeader(std::ostream& fout)
  155. {
  156. fout << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  157. if (this->ExpressEdition) {
  158. fout << "# Visual Studio Express 2013 for Windows Desktop\n";
  159. } else {
  160. fout << "# Visual Studio 2013\n";
  161. }
  162. }
  163. bool cmGlobalVisualStudio12Generator::IsWindowsDesktopToolsetInstalled() const
  164. {
  165. const char desktop81Key[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  166. "VisualStudio\\12.0\\VC\\LibraryDesktop";
  167. std::vector<std::string> subkeys;
  168. return cmSystemTools::GetRegistrySubKeys(desktop81Key, subkeys,
  169. cmSystemTools::KeyWOW64_32);
  170. }
  171. bool cmGlobalVisualStudio12Generator::IsWindowsPhoneToolsetInstalled() const
  172. {
  173. const char wp81Key[] =
  174. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  175. "Microsoft SDKs\\WindowsPhone\\v8.1\\Install Path;Install Path";
  176. std::string path;
  177. cmSystemTools::ReadRegistryValue(wp81Key, path, cmSystemTools::KeyWOW64_32);
  178. return !path.empty();
  179. }
  180. bool cmGlobalVisualStudio12Generator::IsWindowsStoreToolsetInstalled() const
  181. {
  182. const char win81Key[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  183. "VisualStudio\\12.0\\VC\\Libraries\\Core\\Arm";
  184. std::vector<std::string> subkeys;
  185. return cmSystemTools::GetRegistrySubKeys(win81Key, subkeys,
  186. cmSystemTools::KeyWOW64_32);
  187. }