cmGlobalVisualStudio12Generator.cxx 7.4 KB

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