cmGlobalVisualStudio11Generator.cxx 9.2 KB

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