cmGlobalVisualStudio11Generator.cxx 9.9 KB

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