cmGlobalVisualStudio15Generator.cxx 8.4 KB

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