cmGlobalVisualStudio15Generator.cxx 8.0 KB

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