cmGlobalVisualStudio11Generator.cxx 10 KB

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