cmGlobalVisualStudio15Generator.cxx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
  29. cmake* cm) const override
  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. void GetDocumentation(cmDocumentationEntry& entry) const override
  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. void GetGenerators(std::vector<std::string>& names) const override
  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. bool cmGlobalVisualStudio15Generator::SetGeneratorInstance(
  90. std::string const& i, cmMakefile* mf)
  91. {
  92. if (!i.empty()) {
  93. if (!this->vsSetupAPIHelper.SetVSInstance(i)) {
  94. std::ostringstream e;
  95. /* clang-format off */
  96. e <<
  97. "Generator\n"
  98. " " << this->GetName() << "\n"
  99. "could not find specified instance of Visual Studio:\n"
  100. " " << i;
  101. /* clang-format on */
  102. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  103. return false;
  104. }
  105. }
  106. std::string vsInstance;
  107. if (!this->vsSetupAPIHelper.GetVSInstanceInfo(vsInstance)) {
  108. std::ostringstream e;
  109. /* clang-format off */
  110. e <<
  111. "Generator\n"
  112. " " << this->GetName() << "\n"
  113. "could not find any instance of Visual Studio.\n";
  114. /* clang-format on */
  115. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  116. return false;
  117. }
  118. // Save the selected instance persistently.
  119. std::string genInstance = mf->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
  120. if (vsInstance != genInstance) {
  121. this->CMakeInstance->AddCacheEntry(
  122. "CMAKE_GENERATOR_INSTANCE", vsInstance.c_str(),
  123. "Generator instance identifier.", cmStateEnums::INTERNAL);
  124. }
  125. return true;
  126. }
  127. bool cmGlobalVisualStudio15Generator::GetVSInstance(std::string& dir) const
  128. {
  129. return vsSetupAPIHelper.GetVSInstanceInfo(dir);
  130. }
  131. bool cmGlobalVisualStudio15Generator::IsDefaultToolset(
  132. const std::string& version) const
  133. {
  134. if (version.empty()) {
  135. return true;
  136. }
  137. std::string vcToolsetVersion;
  138. if (this->vsSetupAPIHelper.GetVCToolsetVersion(vcToolsetVersion)) {
  139. cmsys::RegularExpression regex("[0-9][0-9]\\.[0-9]+");
  140. if (regex.find(version) && regex.find(vcToolsetVersion)) {
  141. const auto majorMinorEnd = vcToolsetVersion.find('.', 3);
  142. const auto majorMinor = vcToolsetVersion.substr(0, majorMinorEnd);
  143. return version == majorMinor;
  144. }
  145. }
  146. return false;
  147. }
  148. std::string cmGlobalVisualStudio15Generator::GetAuxiliaryToolset() const
  149. {
  150. const char* version = this->GetPlatformToolsetVersion();
  151. if (version) {
  152. std::string instancePath;
  153. GetVSInstance(instancePath);
  154. std::stringstream path;
  155. path << instancePath;
  156. path << "/VC/Auxiliary/Build/";
  157. path << version;
  158. path << "/Microsoft.VCToolsVersion." << version << ".props";
  159. std::string toolsetPath = path.str();
  160. cmSystemTools::ConvertToUnixSlashes(toolsetPath);
  161. return toolsetPath;
  162. }
  163. return {};
  164. }
  165. bool cmGlobalVisualStudio15Generator::InitializeWindows(cmMakefile* mf)
  166. {
  167. // If the Win 8.1 SDK is installed then we can select a SDK matching
  168. // the target Windows version.
  169. if (this->IsWin81SDKInstalled()) {
  170. return cmGlobalVisualStudio14Generator::InitializeWindows(mf);
  171. }
  172. // Otherwise we must choose a Win 10 SDK even if we are not targeting
  173. // Windows 10.
  174. return this->SelectWindows10SDK(mf, false);
  175. }
  176. bool cmGlobalVisualStudio15Generator::SelectWindowsStoreToolset(
  177. std::string& toolset) const
  178. {
  179. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  180. if (this->IsWindowsStoreToolsetInstalled() &&
  181. this->IsWindowsDesktopToolsetInstalled()) {
  182. toolset = "v141"; // VS 15 uses v141 toolset
  183. return true;
  184. } else {
  185. return false;
  186. }
  187. }
  188. return this->cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
  189. toolset);
  190. }
  191. bool cmGlobalVisualStudio15Generator::IsWindowsDesktopToolsetInstalled() const
  192. {
  193. return vsSetupAPIHelper.IsVS2017Installed();
  194. }
  195. bool cmGlobalVisualStudio15Generator::IsWindowsStoreToolsetInstalled() const
  196. {
  197. return vsSetupAPIHelper.IsWin10SDKInstalled();
  198. }
  199. bool cmGlobalVisualStudio15Generator::IsWin81SDKInstalled() const
  200. {
  201. // Does the VS installer tool know about one?
  202. if (vsSetupAPIHelper.IsWin81SDKInstalled()) {
  203. return true;
  204. }
  205. // Does the registry know about one (e.g. from VS 2015)?
  206. std::string win81Root;
  207. if (cmSystemTools::ReadRegistryValue(
  208. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  209. "Windows Kits\\Installed Roots;KitsRoot81",
  210. win81Root, cmSystemTools::KeyWOW64_32) ||
  211. cmSystemTools::ReadRegistryValue(
  212. "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
  213. "Windows Kits\\Installed Roots;KitsRoot81",
  214. win81Root, cmSystemTools::KeyWOW64_32)) {
  215. return cmSystemTools::FileExists(win81Root + "/um/windows.h", true);
  216. }
  217. return false;
  218. }
  219. std::string cmGlobalVisualStudio15Generator::GetWindows10SDKMaxVersion() const
  220. {
  221. return std::string();
  222. }
  223. std::string cmGlobalVisualStudio15Generator::FindMSBuildCommand()
  224. {
  225. std::string msbuild;
  226. // Ask Visual Studio Installer tool.
  227. std::string vs;
  228. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  229. msbuild = vs + "/MSBuild/15.0/Bin/MSBuild.exe";
  230. if (cmSystemTools::FileExists(msbuild)) {
  231. return msbuild;
  232. }
  233. }
  234. msbuild = "MSBuild.exe";
  235. return msbuild;
  236. }
  237. std::string cmGlobalVisualStudio15Generator::FindDevEnvCommand()
  238. {
  239. std::string devenv;
  240. // Ask Visual Studio Installer tool.
  241. std::string vs;
  242. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  243. devenv = vs + "/Common7/IDE/devenv.com";
  244. if (cmSystemTools::FileExists(devenv)) {
  245. return devenv;
  246. }
  247. }
  248. devenv = "devenv.com";
  249. return devenv;
  250. }