cmGlobalVisualStudioVersionedGenerator.cxx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 "cmGlobalVisualStudioVersionedGenerator.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmDocumentationEntry.h"
  6. #include "cmLocalVisualStudio10Generator.h"
  7. #include "cmMakefile.h"
  8. #include "cmVSSetupHelper.h"
  9. static unsigned int VSVersionToMajor(
  10. cmGlobalVisualStudioGenerator::VSVersion v)
  11. {
  12. switch (v) {
  13. case cmGlobalVisualStudioGenerator::VS9:
  14. return 9;
  15. case cmGlobalVisualStudioGenerator::VS10:
  16. return 10;
  17. case cmGlobalVisualStudioGenerator::VS11:
  18. return 11;
  19. case cmGlobalVisualStudioGenerator::VS12:
  20. return 12;
  21. case cmGlobalVisualStudioGenerator::VS14:
  22. return 14;
  23. case cmGlobalVisualStudioGenerator::VS15:
  24. return 15;
  25. }
  26. return 0;
  27. }
  28. static const char vs15generatorName[] = "Visual Studio 15 2017";
  29. // Map generator name without year to name with year.
  30. static const char* cmVS15GenName(const std::string& name, std::string& genName)
  31. {
  32. if (strncmp(name.c_str(), vs15generatorName,
  33. sizeof(vs15generatorName) - 6) != 0) {
  34. return 0;
  35. }
  36. const char* p = name.c_str() + sizeof(vs15generatorName) - 6;
  37. if (cmHasLiteralPrefix(p, " 2017")) {
  38. p += 5;
  39. }
  40. genName = std::string(vs15generatorName) + p;
  41. return p;
  42. }
  43. class cmGlobalVisualStudioVersionedGenerator::Factory15
  44. : public cmGlobalGeneratorFactory
  45. {
  46. public:
  47. cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
  48. cmake* cm) const override
  49. {
  50. std::string genName;
  51. const char* p = cmVS15GenName(name, genName);
  52. if (!p) {
  53. return 0;
  54. }
  55. if (!*p) {
  56. return new cmGlobalVisualStudioVersionedGenerator(
  57. cmGlobalVisualStudioGenerator::VS15, cm, genName, "");
  58. }
  59. if (*p++ != ' ') {
  60. return 0;
  61. }
  62. if (strcmp(p, "Win64") == 0) {
  63. return new cmGlobalVisualStudioVersionedGenerator(
  64. cmGlobalVisualStudioGenerator::VS15, cm, genName, "x64");
  65. }
  66. if (strcmp(p, "ARM") == 0) {
  67. return new cmGlobalVisualStudioVersionedGenerator(
  68. cmGlobalVisualStudioGenerator::VS15, cm, genName, "ARM");
  69. }
  70. return 0;
  71. }
  72. void GetDocumentation(cmDocumentationEntry& entry) const override
  73. {
  74. entry.Name = std::string(vs15generatorName) + " [arch]";
  75. entry.Brief = "Generates Visual Studio 2017 project files. "
  76. "Optional [arch] can be \"Win64\" or \"ARM\".";
  77. }
  78. void GetGenerators(std::vector<std::string>& names) const override
  79. {
  80. names.push_back(vs15generatorName);
  81. names.push_back(vs15generatorName + std::string(" ARM"));
  82. names.push_back(vs15generatorName + std::string(" Win64"));
  83. }
  84. bool SupportsToolset() const override { return true; }
  85. bool SupportsPlatform() const override { return true; }
  86. };
  87. cmGlobalGeneratorFactory*
  88. cmGlobalVisualStudioVersionedGenerator::NewFactory15()
  89. {
  90. return new Factory15;
  91. }
  92. cmGlobalVisualStudioVersionedGenerator::cmGlobalVisualStudioVersionedGenerator(
  93. VSVersion version, cmake* cm, const std::string& name,
  94. std::string const& platformInGeneratorName)
  95. : cmGlobalVisualStudio14Generator(cm, name, platformInGeneratorName)
  96. , vsSetupAPIHelper(VSVersionToMajor(version))
  97. {
  98. this->ExpressEdition = false;
  99. this->DefaultPlatformToolset = "v141";
  100. this->DefaultCLFlagTableName = "v141";
  101. this->DefaultCSharpFlagTableName = "v141";
  102. this->DefaultLinkFlagTableName = "v141";
  103. this->Version = version;
  104. }
  105. bool cmGlobalVisualStudioVersionedGenerator::MatchesGeneratorName(
  106. const std::string& name) const
  107. {
  108. std::string genName;
  109. switch (this->Version) {
  110. case cmGlobalVisualStudioGenerator::VS9:
  111. case cmGlobalVisualStudioGenerator::VS10:
  112. case cmGlobalVisualStudioGenerator::VS11:
  113. case cmGlobalVisualStudioGenerator::VS12:
  114. case cmGlobalVisualStudioGenerator::VS14:
  115. break;
  116. case cmGlobalVisualStudioGenerator::VS15:
  117. if (cmVS15GenName(name, genName)) {
  118. return genName == this->GetName();
  119. }
  120. break;
  121. }
  122. return false;
  123. }
  124. bool cmGlobalVisualStudioVersionedGenerator::SetGeneratorInstance(
  125. std::string const& i, cmMakefile* mf)
  126. {
  127. if (!i.empty()) {
  128. if (!this->vsSetupAPIHelper.SetVSInstance(i)) {
  129. std::ostringstream e;
  130. /* clang-format off */
  131. e <<
  132. "Generator\n"
  133. " " << this->GetName() << "\n"
  134. "could not find specified instance of Visual Studio:\n"
  135. " " << i;
  136. /* clang-format on */
  137. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  138. return false;
  139. }
  140. }
  141. std::string vsInstance;
  142. if (!this->vsSetupAPIHelper.GetVSInstanceInfo(vsInstance)) {
  143. std::ostringstream e;
  144. /* clang-format off */
  145. e <<
  146. "Generator\n"
  147. " " << this->GetName() << "\n"
  148. "could not find any instance of Visual Studio.\n";
  149. /* clang-format on */
  150. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  151. return false;
  152. }
  153. // Save the selected instance persistently.
  154. std::string genInstance = mf->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
  155. if (vsInstance != genInstance) {
  156. this->CMakeInstance->AddCacheEntry(
  157. "CMAKE_GENERATOR_INSTANCE", vsInstance.c_str(),
  158. "Generator instance identifier.", cmStateEnums::INTERNAL);
  159. }
  160. return true;
  161. }
  162. bool cmGlobalVisualStudioVersionedGenerator::GetVSInstance(
  163. std::string& dir) const
  164. {
  165. return vsSetupAPIHelper.GetVSInstanceInfo(dir);
  166. }
  167. bool cmGlobalVisualStudioVersionedGenerator::IsDefaultToolset(
  168. const std::string& version) const
  169. {
  170. if (version.empty()) {
  171. return true;
  172. }
  173. std::string vcToolsetVersion;
  174. if (this->vsSetupAPIHelper.GetVCToolsetVersion(vcToolsetVersion)) {
  175. cmsys::RegularExpression regex("[0-9][0-9]\\.[0-9]+");
  176. if (regex.find(version) && regex.find(vcToolsetVersion)) {
  177. const auto majorMinorEnd = vcToolsetVersion.find('.', 3);
  178. const auto majorMinor = vcToolsetVersion.substr(0, majorMinorEnd);
  179. return version == majorMinor;
  180. }
  181. }
  182. return false;
  183. }
  184. std::string cmGlobalVisualStudioVersionedGenerator::GetAuxiliaryToolset() const
  185. {
  186. const char* version = this->GetPlatformToolsetVersion();
  187. if (version) {
  188. std::string instancePath;
  189. GetVSInstance(instancePath);
  190. std::stringstream path;
  191. path << instancePath;
  192. path << "/VC/Auxiliary/Build/";
  193. path << version;
  194. path << "/Microsoft.VCToolsVersion." << version << ".props";
  195. std::string toolsetPath = path.str();
  196. cmSystemTools::ConvertToUnixSlashes(toolsetPath);
  197. return toolsetPath;
  198. }
  199. return {};
  200. }
  201. bool cmGlobalVisualStudioVersionedGenerator::InitializeWindows(cmMakefile* mf)
  202. {
  203. // If the Win 8.1 SDK is installed then we can select a SDK matching
  204. // the target Windows version.
  205. if (this->IsWin81SDKInstalled()) {
  206. return cmGlobalVisualStudio14Generator::InitializeWindows(mf);
  207. }
  208. // Otherwise we must choose a Win 10 SDK even if we are not targeting
  209. // Windows 10.
  210. return this->SelectWindows10SDK(mf, false);
  211. }
  212. bool cmGlobalVisualStudioVersionedGenerator::SelectWindowsStoreToolset(
  213. std::string& toolset) const
  214. {
  215. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  216. if (this->IsWindowsStoreToolsetInstalled() &&
  217. this->IsWindowsDesktopToolsetInstalled()) {
  218. toolset = "v141"; // VS 15 uses v141 toolset
  219. return true;
  220. } else {
  221. return false;
  222. }
  223. }
  224. return this->cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
  225. toolset);
  226. }
  227. bool cmGlobalVisualStudioVersionedGenerator::IsWindowsDesktopToolsetInstalled()
  228. const
  229. {
  230. return vsSetupAPIHelper.IsVSInstalled();
  231. }
  232. bool cmGlobalVisualStudioVersionedGenerator::IsWindowsStoreToolsetInstalled()
  233. const
  234. {
  235. return vsSetupAPIHelper.IsWin10SDKInstalled();
  236. }
  237. bool cmGlobalVisualStudioVersionedGenerator::IsWin81SDKInstalled() const
  238. {
  239. // Does the VS installer tool know about one?
  240. if (vsSetupAPIHelper.IsWin81SDKInstalled()) {
  241. return true;
  242. }
  243. // Does the registry know about one (e.g. from VS 2015)?
  244. std::string win81Root;
  245. if (cmSystemTools::ReadRegistryValue(
  246. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  247. "Windows Kits\\Installed Roots;KitsRoot81",
  248. win81Root, cmSystemTools::KeyWOW64_32) ||
  249. cmSystemTools::ReadRegistryValue(
  250. "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
  251. "Windows Kits\\Installed Roots;KitsRoot81",
  252. win81Root, cmSystemTools::KeyWOW64_32)) {
  253. return cmSystemTools::FileExists(win81Root + "/um/windows.h", true);
  254. }
  255. return false;
  256. }
  257. std::string cmGlobalVisualStudioVersionedGenerator::GetWindows10SDKMaxVersion()
  258. const
  259. {
  260. return std::string();
  261. }
  262. std::string cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommand()
  263. {
  264. std::string msbuild;
  265. // Ask Visual Studio Installer tool.
  266. std::string vs;
  267. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  268. msbuild = vs + "/MSBuild/15.0/Bin/MSBuild.exe";
  269. if (cmSystemTools::FileExists(msbuild)) {
  270. return msbuild;
  271. }
  272. }
  273. msbuild = "MSBuild.exe";
  274. return msbuild;
  275. }
  276. std::string cmGlobalVisualStudioVersionedGenerator::FindDevEnvCommand()
  277. {
  278. std::string devenv;
  279. // Ask Visual Studio Installer tool.
  280. std::string vs;
  281. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  282. devenv = vs + "/Common7/IDE/devenv.com";
  283. if (cmSystemTools::FileExists(devenv)) {
  284. return devenv;
  285. }
  286. }
  287. devenv = "devenv.com";
  288. return devenv;
  289. }