1
0

cmGlobalVisualStudio11Generator.cxx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 "cmGlobalVisualStudio11Generator.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmLocalVisualStudio10Generator.h"
  6. #include "cmMakefile.h"
  7. static const char vs11generatorName[] = "Visual Studio 11 2012";
  8. // Map generator name without year to name with year.
  9. static const char* cmVS11GenName(const std::string& name, std::string& genName)
  10. {
  11. if (strncmp(name.c_str(), vs11generatorName,
  12. sizeof(vs11generatorName) - 6) != 0) {
  13. return 0;
  14. }
  15. const char* p = name.c_str() + sizeof(vs11generatorName) - 6;
  16. if (cmHasLiteralPrefix(p, " 2012")) {
  17. p += 5;
  18. }
  19. genName = std::string(vs11generatorName) + p;
  20. return p;
  21. }
  22. class cmGlobalVisualStudio11Generator::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 = cmVS11GenName(name, genName);
  31. if (!p) {
  32. return 0;
  33. }
  34. if (!*p) {
  35. return new cmGlobalVisualStudio11Generator(cm, genName, "");
  36. }
  37. if (*p++ != ' ') {
  38. return 0;
  39. }
  40. if (strcmp(p, "Win64") == 0) {
  41. return new cmGlobalVisualStudio11Generator(cm, genName, "x64");
  42. }
  43. if (strcmp(p, "ARM") == 0) {
  44. return new cmGlobalVisualStudio11Generator(cm, genName, "ARM");
  45. }
  46. std::set<std::string> installedSDKs =
  47. cmGlobalVisualStudio11Generator::GetInstalledWindowsCESDKs();
  48. if (installedSDKs.find(p) == installedSDKs.end()) {
  49. return 0;
  50. }
  51. cmGlobalVisualStudio11Generator* ret =
  52. new cmGlobalVisualStudio11Generator(cm, name, p);
  53. ret->WindowsCEVersion = "8.00";
  54. return ret;
  55. }
  56. void GetDocumentation(cmDocumentationEntry& entry) const CM_OVERRIDE
  57. {
  58. entry.Name = std::string(vs11generatorName) + " [arch]";
  59. entry.Brief = "Generates Visual Studio 2012 project files. "
  60. "Optional [arch] can be \"Win64\" or \"ARM\".";
  61. }
  62. void GetGenerators(std::vector<std::string>& names) const CM_OVERRIDE
  63. {
  64. names.push_back(vs11generatorName);
  65. names.push_back(vs11generatorName + std::string(" ARM"));
  66. names.push_back(vs11generatorName + std::string(" Win64"));
  67. std::set<std::string> installedSDKs =
  68. cmGlobalVisualStudio11Generator::GetInstalledWindowsCESDKs();
  69. for (std::set<std::string>::const_iterator i = installedSDKs.begin();
  70. i != installedSDKs.end(); ++i) {
  71. names.push_back(std::string(vs11generatorName) + " " + *i);
  72. }
  73. }
  74. bool SupportsToolset() const CM_OVERRIDE { return true; }
  75. bool SupportsPlatform() const CM_OVERRIDE { return true; }
  76. };
  77. cmGlobalGeneratorFactory* cmGlobalVisualStudio11Generator::NewFactory()
  78. {
  79. return new Factory;
  80. }
  81. cmGlobalVisualStudio11Generator::cmGlobalVisualStudio11Generator(
  82. cmake* cm, const std::string& name, const std::string& platformName)
  83. : cmGlobalVisualStudio10Generator(cm, name, platformName)
  84. {
  85. std::string vc11Express;
  86. this->ExpressEdition = cmSystemTools::ReadRegistryValue(
  87. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\11.0\\Setup\\VC;"
  88. "ProductDir",
  89. vc11Express, cmSystemTools::KeyWOW64_32);
  90. this->DefaultPlatformToolset = "v110";
  91. this->Version = VS11;
  92. }
  93. bool cmGlobalVisualStudio11Generator::MatchesGeneratorName(
  94. const std::string& name) const
  95. {
  96. std::string genName;
  97. if (cmVS11GenName(name, genName)) {
  98. return genName == this->GetName();
  99. }
  100. return false;
  101. }
  102. bool cmGlobalVisualStudio11Generator::InitializeWindowsPhone(cmMakefile* mf)
  103. {
  104. if (!this->SelectWindowsPhoneToolset(this->DefaultPlatformToolset)) {
  105. std::ostringstream e;
  106. if (this->DefaultPlatformToolset.empty()) {
  107. e << this->GetName() << " supports Windows Phone '8.0', but not '"
  108. << this->SystemVersion << "'. Check CMAKE_SYSTEM_VERSION.";
  109. } else {
  110. e << "A Windows Phone component with CMake requires both the Windows "
  111. << "Desktop SDK as well as the Windows Phone '" << this->SystemVersion
  112. << "' SDK. Please make sure that you have both installed";
  113. }
  114. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  115. return false;
  116. }
  117. return true;
  118. }
  119. bool cmGlobalVisualStudio11Generator::InitializeWindowsStore(cmMakefile* mf)
  120. {
  121. if (!this->SelectWindowsStoreToolset(this->DefaultPlatformToolset)) {
  122. std::ostringstream e;
  123. if (this->DefaultPlatformToolset.empty()) {
  124. e << this->GetName() << " supports Windows Store '8.0', but not '"
  125. << this->SystemVersion << "'. Check CMAKE_SYSTEM_VERSION.";
  126. } else {
  127. e << "A Windows Store component with CMake requires both the Windows "
  128. << "Desktop SDK as well as the Windows Store '" << this->SystemVersion
  129. << "' SDK. Please make sure that you have both installed";
  130. }
  131. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  132. return false;
  133. }
  134. return true;
  135. }
  136. bool cmGlobalVisualStudio11Generator::SelectWindowsPhoneToolset(
  137. std::string& toolset) const
  138. {
  139. if (this->SystemVersion == "8.0") {
  140. if (this->IsWindowsPhoneToolsetInstalled() &&
  141. this->IsWindowsDesktopToolsetInstalled()) {
  142. toolset = "v110_wp80";
  143. return true;
  144. } else {
  145. return false;
  146. }
  147. }
  148. return this->cmGlobalVisualStudio10Generator::SelectWindowsPhoneToolset(
  149. toolset);
  150. }
  151. bool cmGlobalVisualStudio11Generator::SelectWindowsStoreToolset(
  152. std::string& toolset) const
  153. {
  154. if (this->SystemVersion == "8.0") {
  155. if (this->IsWindowsStoreToolsetInstalled() &&
  156. this->IsWindowsDesktopToolsetInstalled()) {
  157. toolset = "v110";
  158. return true;
  159. } else {
  160. return false;
  161. }
  162. }
  163. return this->cmGlobalVisualStudio10Generator::SelectWindowsStoreToolset(
  164. toolset);
  165. }
  166. void cmGlobalVisualStudio11Generator::WriteSLNHeader(std::ostream& fout)
  167. {
  168. fout << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  169. if (this->ExpressEdition) {
  170. fout << "# Visual Studio Express 2012 for Windows Desktop\n";
  171. } else {
  172. fout << "# Visual Studio 2012\n";
  173. }
  174. }
  175. bool cmGlobalVisualStudio11Generator::UseFolderProperty()
  176. {
  177. // Intentionally skip up to the top-level class implementation.
  178. // Folders are not supported by the Express editions in VS10 and earlier,
  179. // but they are in VS11 Express and above.
  180. return cmGlobalGenerator::UseFolderProperty();
  181. }
  182. std::set<std::string>
  183. cmGlobalVisualStudio11Generator::GetInstalledWindowsCESDKs()
  184. {
  185. const char sdksKey[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  186. "Windows CE Tools\\SDKs";
  187. std::vector<std::string> subkeys;
  188. cmSystemTools::GetRegistrySubKeys(sdksKey, subkeys,
  189. cmSystemTools::KeyWOW64_32);
  190. std::set<std::string> ret;
  191. for (std::vector<std::string>::const_iterator i = subkeys.begin();
  192. i != subkeys.end(); ++i) {
  193. std::string key = sdksKey;
  194. key += '\\';
  195. key += *i;
  196. key += ';';
  197. std::string path;
  198. if (cmSystemTools::ReadRegistryValue(key.c_str(), path,
  199. cmSystemTools::KeyWOW64_32) &&
  200. !path.empty()) {
  201. ret.insert(*i);
  202. }
  203. }
  204. return ret;
  205. }
  206. bool cmGlobalVisualStudio11Generator::NeedsDeploy(
  207. cmState::TargetType type) const
  208. {
  209. if ((type == cmState::EXECUTABLE || type == cmState::SHARED_LIBRARY) &&
  210. (this->SystemIsWindowsPhone || this->SystemIsWindowsStore)) {
  211. return true;
  212. }
  213. return cmGlobalVisualStudio10Generator::NeedsDeploy(type);
  214. }
  215. bool cmGlobalVisualStudio11Generator::IsWindowsDesktopToolsetInstalled() const
  216. {
  217. const char desktop80Key[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  218. "VisualStudio\\11.0\\VC\\Libraries\\Extended";
  219. const char VS2012DesktopExpressKey[] =
  220. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  221. "WDExpress\\11.0;InstallDir";
  222. std::vector<std::string> subkeys;
  223. std::string path;
  224. return cmSystemTools::ReadRegistryValue(VS2012DesktopExpressKey, path,
  225. cmSystemTools::KeyWOW64_32) ||
  226. cmSystemTools::GetRegistrySubKeys(desktop80Key, subkeys,
  227. cmSystemTools::KeyWOW64_32);
  228. }
  229. bool cmGlobalVisualStudio11Generator::IsWindowsPhoneToolsetInstalled() const
  230. {
  231. const char wp80Key[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  232. "Microsoft SDKs\\WindowsPhone\\v8.0\\"
  233. "Install Path;Install Path";
  234. std::string path;
  235. cmSystemTools::ReadRegistryValue(wp80Key, path, cmSystemTools::KeyWOW64_32);
  236. return !path.empty();
  237. }
  238. bool cmGlobalVisualStudio11Generator::IsWindowsStoreToolsetInstalled() const
  239. {
  240. const char win80Key[] = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  241. "VisualStudio\\11.0\\VC\\Libraries\\Core\\Arm";
  242. std::vector<std::string> subkeys;
  243. return cmSystemTools::GetRegistrySubKeys(win80Key, subkeys,
  244. cmSystemTools::KeyWOW64_32);
  245. }