cmGlobalVisualStudio15Generator.cxx 8.0 KB

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