cmGlobalVisualStudio15Generator.cxx 8.3 KB

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