cmGlobalVisualStudioVersionedGenerator.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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 "cmGlobalVisualStudioVersionedGenerator.h"
  4. #include <cmext/string_view>
  5. #include "cmsys/FStream.hxx"
  6. #include "cmsys/Glob.hxx"
  7. #include "cmAlgorithms.h"
  8. #include "cmDocumentationEntry.h"
  9. #include "cmLocalVisualStudio10Generator.h"
  10. #include "cmMakefile.h"
  11. #include "cmStringAlgorithms.h"
  12. #include "cmVSSetupHelper.h"
  13. #include "cmake.h"
  14. #if defined(_M_ARM64)
  15. # define HOST_PLATFORM_NAME "ARM64"
  16. # define HOST_TOOLS_ARCH ""
  17. #elif defined(_M_ARM)
  18. # define HOST_PLATFORM_NAME "ARM"
  19. # define HOST_TOOLS_ARCH ""
  20. #elif defined(_M_IA64)
  21. # define HOST_PLATFORM_NAME "Itanium"
  22. # define HOST_TOOLS_ARCH ""
  23. #elif defined(_WIN64)
  24. # define HOST_PLATFORM_NAME "x64"
  25. # define HOST_TOOLS_ARCH "x64"
  26. #else
  27. static bool VSIsWow64()
  28. {
  29. BOOL isWow64 = false;
  30. return IsWow64Process(GetCurrentProcess(), &isWow64) && isWow64;
  31. }
  32. #endif
  33. static std::string VSHostPlatformName()
  34. {
  35. #ifdef HOST_PLATFORM_NAME
  36. return HOST_PLATFORM_NAME;
  37. #else
  38. if (VSIsWow64()) {
  39. return "x64";
  40. } else {
  41. return "Win32";
  42. }
  43. #endif
  44. }
  45. static std::string VSHostArchitecture()
  46. {
  47. #ifdef HOST_TOOLS_ARCH
  48. return HOST_TOOLS_ARCH;
  49. #else
  50. if (VSIsWow64()) {
  51. return "x64";
  52. } else {
  53. return "x86";
  54. }
  55. #endif
  56. }
  57. static unsigned int VSVersionToMajor(
  58. cmGlobalVisualStudioGenerator::VSVersion v)
  59. {
  60. switch (v) {
  61. case cmGlobalVisualStudioGenerator::VS9:
  62. return 9;
  63. case cmGlobalVisualStudioGenerator::VS10:
  64. return 10;
  65. case cmGlobalVisualStudioGenerator::VS11:
  66. return 11;
  67. case cmGlobalVisualStudioGenerator::VS12:
  68. return 12;
  69. case cmGlobalVisualStudioGenerator::VS14:
  70. return 14;
  71. case cmGlobalVisualStudioGenerator::VS15:
  72. return 15;
  73. case cmGlobalVisualStudioGenerator::VS16:
  74. return 16;
  75. case cmGlobalVisualStudioGenerator::VS17:
  76. return 17;
  77. }
  78. return 0;
  79. }
  80. static const char* VSVersionToToolset(
  81. cmGlobalVisualStudioGenerator::VSVersion v)
  82. {
  83. switch (v) {
  84. case cmGlobalVisualStudioGenerator::VS9:
  85. return "v90";
  86. case cmGlobalVisualStudioGenerator::VS10:
  87. return "v100";
  88. case cmGlobalVisualStudioGenerator::VS11:
  89. return "v110";
  90. case cmGlobalVisualStudioGenerator::VS12:
  91. return "v120";
  92. case cmGlobalVisualStudioGenerator::VS14:
  93. return "v140";
  94. case cmGlobalVisualStudioGenerator::VS15:
  95. return "v141";
  96. case cmGlobalVisualStudioGenerator::VS16:
  97. return "v142";
  98. case cmGlobalVisualStudioGenerator::VS17:
  99. return "v143";
  100. }
  101. return "";
  102. }
  103. static const char* VSVersionToAndroidToolset(
  104. cmGlobalVisualStudioGenerator::VSVersion v)
  105. {
  106. switch (v) {
  107. case cmGlobalVisualStudioGenerator::VS9:
  108. case cmGlobalVisualStudioGenerator::VS10:
  109. case cmGlobalVisualStudioGenerator::VS11:
  110. case cmGlobalVisualStudioGenerator::VS12:
  111. return "";
  112. case cmGlobalVisualStudioGenerator::VS14:
  113. return "Clang_3_8";
  114. case cmGlobalVisualStudioGenerator::VS15:
  115. case cmGlobalVisualStudioGenerator::VS16:
  116. case cmGlobalVisualStudioGenerator::VS17:
  117. return "Clang_5_0";
  118. }
  119. return "";
  120. }
  121. static const char vs15generatorName[] = "Visual Studio 15 2017";
  122. // Map generator name without year to name with year.
  123. static const char* cmVS15GenName(const std::string& name, std::string& genName)
  124. {
  125. if (strncmp(name.c_str(), vs15generatorName,
  126. sizeof(vs15generatorName) - 6) != 0) {
  127. return 0;
  128. }
  129. const char* p = name.c_str() + sizeof(vs15generatorName) - 6;
  130. if (cmHasLiteralPrefix(p, " 2017")) {
  131. p += 5;
  132. }
  133. genName = std::string(vs15generatorName) + p;
  134. return p;
  135. }
  136. class cmGlobalVisualStudioVersionedGenerator::Factory15
  137. : public cmGlobalGeneratorFactory
  138. {
  139. public:
  140. std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
  141. const std::string& name, bool allowArch, cmake* cm) const override
  142. {
  143. std::string genName;
  144. const char* p = cmVS15GenName(name, genName);
  145. if (!p) {
  146. return std::unique_ptr<cmGlobalGenerator>();
  147. }
  148. if (!*p) {
  149. return std::unique_ptr<cmGlobalGenerator>(
  150. new cmGlobalVisualStudioVersionedGenerator(
  151. cmGlobalVisualStudioGenerator::VS15, cm, genName, ""));
  152. }
  153. if (!allowArch || *p++ != ' ') {
  154. return std::unique_ptr<cmGlobalGenerator>();
  155. }
  156. if (strcmp(p, "Win64") == 0) {
  157. return std::unique_ptr<cmGlobalGenerator>(
  158. new cmGlobalVisualStudioVersionedGenerator(
  159. cmGlobalVisualStudioGenerator::VS15, cm, genName, "x64"));
  160. }
  161. if (strcmp(p, "ARM") == 0) {
  162. return std::unique_ptr<cmGlobalGenerator>(
  163. new cmGlobalVisualStudioVersionedGenerator(
  164. cmGlobalVisualStudioGenerator::VS15, cm, genName, "ARM"));
  165. }
  166. return std::unique_ptr<cmGlobalGenerator>();
  167. }
  168. void GetDocumentation(cmDocumentationEntry& entry) const override
  169. {
  170. entry.Name = std::string(vs15generatorName) + " [arch]";
  171. entry.Brief = "Generates Visual Studio 2017 project files. "
  172. "Optional [arch] can be \"Win64\" or \"ARM\".";
  173. }
  174. std::vector<std::string> GetGeneratorNames() const override
  175. {
  176. std::vector<std::string> names;
  177. names.push_back(vs15generatorName);
  178. return names;
  179. }
  180. std::vector<std::string> GetGeneratorNamesWithPlatform() const override
  181. {
  182. std::vector<std::string> names;
  183. names.push_back(vs15generatorName + std::string(" ARM"));
  184. names.push_back(vs15generatorName + std::string(" Win64"));
  185. return names;
  186. }
  187. bool SupportsToolset() const override { return true; }
  188. bool SupportsPlatform() const override { return true; }
  189. std::vector<std::string> GetKnownPlatforms() const override
  190. {
  191. std::vector<std::string> platforms;
  192. platforms.emplace_back("x64");
  193. platforms.emplace_back("Win32");
  194. platforms.emplace_back("ARM");
  195. platforms.emplace_back("ARM64");
  196. return platforms;
  197. }
  198. std::string GetDefaultPlatformName() const override { return "Win32"; }
  199. };
  200. std::unique_ptr<cmGlobalGeneratorFactory>
  201. cmGlobalVisualStudioVersionedGenerator::NewFactory15()
  202. {
  203. return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory15);
  204. }
  205. static const char vs16generatorName[] = "Visual Studio 16 2019";
  206. static const char vs17generatorName[] = "Visual Studio 17 2022";
  207. // Map generator name without year to name with year.
  208. static const char* cmVS16GenName(const std::string& name, std::string& genName)
  209. {
  210. if (strncmp(name.c_str(), vs16generatorName,
  211. sizeof(vs16generatorName) - 6) != 0) {
  212. return 0;
  213. }
  214. const char* p = name.c_str() + sizeof(vs16generatorName) - 6;
  215. if (cmHasLiteralPrefix(p, " 2019")) {
  216. p += 5;
  217. }
  218. genName = std::string(vs16generatorName) + p;
  219. return p;
  220. }
  221. static const char* cmVS17GenName(const std::string& name, std::string& genName)
  222. {
  223. if (strncmp(name.c_str(), vs17generatorName,
  224. sizeof(vs17generatorName) - 6) != 0) {
  225. return 0;
  226. }
  227. const char* p = name.c_str() + sizeof(vs17generatorName) - 6;
  228. if (cmHasLiteralPrefix(p, " 2022")) {
  229. p += 5;
  230. }
  231. genName = std::string(vs17generatorName) + p;
  232. return p;
  233. }
  234. class cmGlobalVisualStudioVersionedGenerator::Factory16
  235. : public cmGlobalGeneratorFactory
  236. {
  237. public:
  238. std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
  239. const std::string& name, bool /*allowArch*/, cmake* cm) const override
  240. {
  241. std::string genName;
  242. const char* p = cmVS16GenName(name, genName);
  243. if (!p) {
  244. return std::unique_ptr<cmGlobalGenerator>();
  245. }
  246. if (!*p) {
  247. return std::unique_ptr<cmGlobalGenerator>(
  248. new cmGlobalVisualStudioVersionedGenerator(
  249. cmGlobalVisualStudioGenerator::VS16, cm, genName, ""));
  250. }
  251. return std::unique_ptr<cmGlobalGenerator>();
  252. }
  253. void GetDocumentation(cmDocumentationEntry& entry) const override
  254. {
  255. entry.Name = std::string(vs16generatorName);
  256. entry.Brief = "Generates Visual Studio 2019 project files. "
  257. "Use -A option to specify architecture.";
  258. }
  259. std::vector<std::string> GetGeneratorNames() const override
  260. {
  261. std::vector<std::string> names;
  262. names.push_back(vs16generatorName);
  263. return names;
  264. }
  265. std::vector<std::string> GetGeneratorNamesWithPlatform() const override
  266. {
  267. return std::vector<std::string>();
  268. }
  269. bool SupportsToolset() const override { return true; }
  270. bool SupportsPlatform() const override { return true; }
  271. std::vector<std::string> GetKnownPlatforms() const override
  272. {
  273. std::vector<std::string> platforms;
  274. platforms.emplace_back("x64");
  275. platforms.emplace_back("Win32");
  276. platforms.emplace_back("ARM");
  277. platforms.emplace_back("ARM64");
  278. platforms.emplace_back("ARM64EC");
  279. return platforms;
  280. }
  281. std::string GetDefaultPlatformName() const override
  282. {
  283. return VSHostPlatformName();
  284. }
  285. };
  286. std::unique_ptr<cmGlobalGeneratorFactory>
  287. cmGlobalVisualStudioVersionedGenerator::NewFactory16()
  288. {
  289. return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory16);
  290. }
  291. class cmGlobalVisualStudioVersionedGenerator::Factory17
  292. : public cmGlobalGeneratorFactory
  293. {
  294. public:
  295. std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
  296. const std::string& name, bool /*allowArch*/, cmake* cm) const override
  297. {
  298. std::string genName;
  299. const char* p = cmVS17GenName(name, genName);
  300. if (!p) {
  301. return std::unique_ptr<cmGlobalGenerator>();
  302. }
  303. if (!*p) {
  304. return std::unique_ptr<cmGlobalGenerator>(
  305. new cmGlobalVisualStudioVersionedGenerator(
  306. cmGlobalVisualStudioGenerator::VS17, cm, genName, ""));
  307. }
  308. return std::unique_ptr<cmGlobalGenerator>();
  309. }
  310. void GetDocumentation(cmDocumentationEntry& entry) const override
  311. {
  312. entry.Name = std::string(vs17generatorName);
  313. entry.Brief = "Generates Visual Studio 2022 project files. "
  314. "Use -A option to specify architecture.";
  315. }
  316. std::vector<std::string> GetGeneratorNames() const override
  317. {
  318. std::vector<std::string> names;
  319. names.push_back(vs17generatorName);
  320. return names;
  321. }
  322. std::vector<std::string> GetGeneratorNamesWithPlatform() const override
  323. {
  324. return std::vector<std::string>();
  325. }
  326. bool SupportsToolset() const override { return true; }
  327. bool SupportsPlatform() const override { return true; }
  328. std::vector<std::string> GetKnownPlatforms() const override
  329. {
  330. std::vector<std::string> platforms;
  331. platforms.emplace_back("x64");
  332. platforms.emplace_back("Win32");
  333. platforms.emplace_back("ARM");
  334. platforms.emplace_back("ARM64");
  335. platforms.emplace_back("ARM64EC");
  336. return platforms;
  337. }
  338. std::string GetDefaultPlatformName() const override
  339. {
  340. return VSHostPlatformName();
  341. }
  342. };
  343. std::unique_ptr<cmGlobalGeneratorFactory>
  344. cmGlobalVisualStudioVersionedGenerator::NewFactory17()
  345. {
  346. return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory17);
  347. }
  348. cmGlobalVisualStudioVersionedGenerator::cmGlobalVisualStudioVersionedGenerator(
  349. VSVersion version, cmake* cm, const std::string& name,
  350. std::string const& platformInGeneratorName)
  351. : cmGlobalVisualStudio14Generator(cm, name, platformInGeneratorName)
  352. , vsSetupAPIHelper(VSVersionToMajor(version))
  353. {
  354. this->Version = version;
  355. this->ExpressEdition = false;
  356. this->DefaultPlatformToolset = VSVersionToToolset(this->Version);
  357. this->DefaultAndroidToolset = VSVersionToAndroidToolset(this->Version);
  358. this->DefaultCLFlagTableName = VSVersionToToolset(this->Version);
  359. this->DefaultCSharpFlagTableName = VSVersionToToolset(this->Version);
  360. this->DefaultLinkFlagTableName = VSVersionToToolset(this->Version);
  361. if (this->Version >= cmGlobalVisualStudioGenerator::VS16) {
  362. this->DefaultPlatformName = VSHostPlatformName();
  363. this->DefaultPlatformToolsetHostArchitecture = VSHostArchitecture();
  364. }
  365. }
  366. bool cmGlobalVisualStudioVersionedGenerator::MatchesGeneratorName(
  367. const std::string& name) const
  368. {
  369. std::string genName;
  370. switch (this->Version) {
  371. case cmGlobalVisualStudioGenerator::VS9:
  372. case cmGlobalVisualStudioGenerator::VS10:
  373. case cmGlobalVisualStudioGenerator::VS11:
  374. case cmGlobalVisualStudioGenerator::VS12:
  375. case cmGlobalVisualStudioGenerator::VS14:
  376. break;
  377. case cmGlobalVisualStudioGenerator::VS15:
  378. if (cmVS15GenName(name, genName)) {
  379. return genName == this->GetName();
  380. }
  381. break;
  382. case cmGlobalVisualStudioGenerator::VS16:
  383. if (cmVS16GenName(name, genName)) {
  384. return genName == this->GetName();
  385. }
  386. break;
  387. case cmGlobalVisualStudioGenerator::VS17:
  388. if (cmVS17GenName(name, genName)) {
  389. return genName == this->GetName();
  390. }
  391. break;
  392. }
  393. return false;
  394. }
  395. bool cmGlobalVisualStudioVersionedGenerator::SetGeneratorInstance(
  396. std::string const& i, cmMakefile* mf)
  397. {
  398. if (!i.empty()) {
  399. if (!this->vsSetupAPIHelper.SetVSInstance(i)) {
  400. std::ostringstream e;
  401. /* clang-format off */
  402. e <<
  403. "Generator\n"
  404. " " << this->GetName() << "\n"
  405. "could not find specified instance of Visual Studio:\n"
  406. " " << i;
  407. /* clang-format on */
  408. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  409. return false;
  410. }
  411. }
  412. std::string vsInstance;
  413. if (!this->vsSetupAPIHelper.GetVSInstanceInfo(vsInstance)) {
  414. std::ostringstream e;
  415. /* clang-format off */
  416. e <<
  417. "Generator\n"
  418. " " << this->GetName() << "\n"
  419. "could not find any instance of Visual Studio.\n";
  420. /* clang-format on */
  421. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  422. return false;
  423. }
  424. // Save the selected instance persistently.
  425. std::string genInstance = mf->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
  426. if (vsInstance != genInstance) {
  427. this->CMakeInstance->AddCacheEntry("CMAKE_GENERATOR_INSTANCE", vsInstance,
  428. "Generator instance identifier.",
  429. cmStateEnums::INTERNAL);
  430. }
  431. return true;
  432. }
  433. bool cmGlobalVisualStudioVersionedGenerator::GetVSInstance(
  434. std::string& dir) const
  435. {
  436. return vsSetupAPIHelper.GetVSInstanceInfo(dir);
  437. }
  438. cm::optional<std::string>
  439. cmGlobalVisualStudioVersionedGenerator::GetVSInstanceVersion() const
  440. {
  441. cm::optional<std::string> result;
  442. std::string vsInstanceVersion;
  443. if (vsSetupAPIHelper.GetVSInstanceVersion(vsInstanceVersion)) {
  444. result = vsInstanceVersion;
  445. }
  446. return result;
  447. }
  448. bool cmGlobalVisualStudioVersionedGenerator::IsStdOutEncodingSupported() const
  449. {
  450. // Supported from Visual Studio 16.7 Preview 3.
  451. if (this->Version > cmGlobalVisualStudioGenerator::VSVersion::VS16) {
  452. return true;
  453. }
  454. if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS16) {
  455. return false;
  456. }
  457. static std::string const vsVer16_7_P2 = "16.7.30128.36";
  458. cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
  459. return (vsVer &&
  460. cmSystemTools::VersionCompareGreaterEq(*vsVer, vsVer16_7_P2));
  461. }
  462. bool cmGlobalVisualStudioVersionedGenerator::IsUtf8EncodingSupported() const
  463. {
  464. // Supported from Visual Studio 16.10 Preview 2.
  465. if (this->Version > cmGlobalVisualStudioGenerator::VSVersion::VS16) {
  466. return true;
  467. }
  468. if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS16) {
  469. return false;
  470. }
  471. static std::string const vsVer16_10_P2 = "16.10.31213.239";
  472. cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
  473. return (vsVer &&
  474. cmSystemTools::VersionCompareGreaterEq(*vsVer, vsVer16_10_P2));
  475. }
  476. const char*
  477. cmGlobalVisualStudioVersionedGenerator::GetAndroidApplicationTypeRevision()
  478. const
  479. {
  480. switch (this->Version) {
  481. case cmGlobalVisualStudioGenerator::VS9:
  482. case cmGlobalVisualStudioGenerator::VS10:
  483. case cmGlobalVisualStudioGenerator::VS11:
  484. case cmGlobalVisualStudioGenerator::VS12:
  485. return "";
  486. case cmGlobalVisualStudioGenerator::VS14:
  487. return "2.0";
  488. case cmGlobalVisualStudioGenerator::VS15:
  489. case cmGlobalVisualStudioGenerator::VS16:
  490. case cmGlobalVisualStudioGenerator::VS17:
  491. return "3.0";
  492. }
  493. return "";
  494. }
  495. cmGlobalVisualStudioVersionedGenerator::AuxToolset
  496. cmGlobalVisualStudioVersionedGenerator::FindAuxToolset(
  497. std::string& version, std::string& props) const
  498. {
  499. if (version.empty()) {
  500. return AuxToolset::None;
  501. }
  502. std::string instancePath;
  503. this->GetVSInstance(instancePath);
  504. cmSystemTools::ConvertToUnixSlashes(instancePath);
  505. // Translate three-component format accepted by "vcvarsall -vcvars_ver=".
  506. cmsys::RegularExpression threeComponent(
  507. "^([0-9]+\\.[0-9]+)\\.[0-9][0-9][0-9][0-9][0-9]$");
  508. if (threeComponent.find(version)) {
  509. // Load "VC/Auxiliary/Build/*/Microsoft.VCToolsVersion.*.txt" files
  510. // with two matching components to check their three-component version.
  511. std::string const& twoComponent = threeComponent.match(1);
  512. std::string pattern =
  513. cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s, twoComponent,
  514. "*/Microsoft.VCToolsVersion."_s, twoComponent, "*.txt"_s);
  515. cmsys::Glob glob;
  516. glob.SetRecurseThroughSymlinks(false);
  517. if (glob.FindFiles(pattern)) {
  518. for (std::string const& txt : glob.GetFiles()) {
  519. std::string ver;
  520. cmsys::ifstream fin(txt.c_str());
  521. if (fin && std::getline(fin, ver)) {
  522. // Strip trailing whitespace.
  523. ver = ver.substr(0, ver.find_first_not_of("0123456789."));
  524. // If the three-component version matches, translate it to
  525. // that used by the "Microsoft.VCToolsVersion.*.txt" file name.
  526. if (ver == version) {
  527. cmsys::RegularExpression extractVersion(
  528. "VCToolsVersion\\.([0-9.]+)\\.txt$");
  529. if (extractVersion.find(txt)) {
  530. version = extractVersion.match(1);
  531. break;
  532. }
  533. }
  534. }
  535. }
  536. }
  537. }
  538. if (cmSystemTools::VersionCompareGreaterEq(version, "14.20")) {
  539. props = cmStrCat(instancePath, "/VC/Auxiliary/Build."_s, version,
  540. "/Microsoft.VCToolsVersion."_s, version, ".props"_s);
  541. if (cmSystemTools::PathExists(props)) {
  542. return AuxToolset::PropsExist;
  543. }
  544. }
  545. props = cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s, version,
  546. "/Microsoft.VCToolsVersion."_s, version, ".props"_s);
  547. if (cmSystemTools::PathExists(props)) {
  548. return AuxToolset::PropsExist;
  549. }
  550. // Accept the toolset version that is default in the current VS version
  551. // by matching the name later VS versions will use for the SxS props files.
  552. std::string vcToolsetVersion;
  553. if (this->vsSetupAPIHelper.GetVCToolsetVersion(vcToolsetVersion)) {
  554. // Accept an exact-match (three-component version).
  555. if (version == vcToolsetVersion) {
  556. return AuxToolset::Default;
  557. }
  558. // Accept known SxS props file names using four version components
  559. // in VS versions later than the current.
  560. if (version == "14.28.16.9" && vcToolsetVersion == "14.28.29910") {
  561. return AuxToolset::Default;
  562. }
  563. if (version == "14.29.16.10" && vcToolsetVersion == "14.29.30037") {
  564. return AuxToolset::Default;
  565. }
  566. if (version == "14.29.16.11" && vcToolsetVersion == "14.29.30133") {
  567. return AuxToolset::Default;
  568. }
  569. // The first two components of the default toolset version typically
  570. // match the name used by later VS versions for the SxS props files.
  571. cmsys::RegularExpression twoComponent("^([0-9]+\\.[0-9]+)");
  572. if (twoComponent.find(version)) {
  573. std::string const versionPrefix = cmStrCat(twoComponent.match(1), '.');
  574. if (cmHasPrefix(vcToolsetVersion, versionPrefix)) {
  575. return AuxToolset::Default;
  576. }
  577. }
  578. }
  579. return AuxToolset::PropsMissing;
  580. }
  581. bool cmGlobalVisualStudioVersionedGenerator::InitializeWindows(cmMakefile* mf)
  582. {
  583. // If the Win 8.1 SDK is installed then we can select a SDK matching
  584. // the target Windows version.
  585. if (this->IsWin81SDKInstalled()) {
  586. // VS 2019 does not default to 8.1 so specify it explicitly when needed.
  587. if (this->Version >= cmGlobalVisualStudioGenerator::VS16 &&
  588. !cmSystemTools::VersionCompareGreater(this->SystemVersion, "8.1")) {
  589. this->SetWindowsTargetPlatformVersion("8.1", mf);
  590. return true;
  591. }
  592. return cmGlobalVisualStudio14Generator::InitializeWindows(mf);
  593. }
  594. // Otherwise we must choose a Win 10 SDK even if we are not targeting
  595. // Windows 10.
  596. return this->SelectWindows10SDK(mf, false);
  597. }
  598. bool cmGlobalVisualStudioVersionedGenerator::SelectWindowsStoreToolset(
  599. std::string& toolset) const
  600. {
  601. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  602. if (this->IsWindowsStoreToolsetInstalled() &&
  603. this->IsWindowsDesktopToolsetInstalled()) {
  604. toolset = VSVersionToToolset(this->Version);
  605. return true;
  606. } else {
  607. return false;
  608. }
  609. }
  610. return this->cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
  611. toolset);
  612. }
  613. bool cmGlobalVisualStudioVersionedGenerator::IsWindowsDesktopToolsetInstalled()
  614. const
  615. {
  616. return vsSetupAPIHelper.IsVSInstalled();
  617. }
  618. bool cmGlobalVisualStudioVersionedGenerator::IsWindowsStoreToolsetInstalled()
  619. const
  620. {
  621. return vsSetupAPIHelper.IsWin10SDKInstalled();
  622. }
  623. bool cmGlobalVisualStudioVersionedGenerator::IsWin81SDKInstalled() const
  624. {
  625. // Does the VS installer tool know about one?
  626. if (vsSetupAPIHelper.IsWin81SDKInstalled()) {
  627. return true;
  628. }
  629. // Does the registry know about one (e.g. from VS 2015)?
  630. std::string win81Root;
  631. if (cmSystemTools::ReadRegistryValue(
  632. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  633. "Windows Kits\\Installed Roots;KitsRoot81",
  634. win81Root, cmSystemTools::KeyWOW64_32) ||
  635. cmSystemTools::ReadRegistryValue(
  636. "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
  637. "Windows Kits\\Installed Roots;KitsRoot81",
  638. win81Root, cmSystemTools::KeyWOW64_32)) {
  639. return cmSystemTools::FileExists(win81Root + "/include/um/windows.h",
  640. true);
  641. }
  642. return false;
  643. }
  644. std::string
  645. cmGlobalVisualStudioVersionedGenerator::GetWindows10SDKMaxVersionDefault(
  646. cmMakefile*) const
  647. {
  648. return std::string();
  649. }
  650. std::string cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommand()
  651. {
  652. std::string msbuild;
  653. // Ask Visual Studio Installer tool.
  654. std::string vs;
  655. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  656. if (this->Version >= cmGlobalVisualStudioGenerator::VS17) {
  657. msbuild = vs + "/MSBuild/Current/Bin/amd64/MSBuild.exe";
  658. if (cmSystemTools::FileExists(msbuild)) {
  659. return msbuild;
  660. }
  661. }
  662. msbuild = vs + "/MSBuild/Current/Bin/MSBuild.exe";
  663. if (cmSystemTools::FileExists(msbuild)) {
  664. return msbuild;
  665. }
  666. msbuild = vs + "/MSBuild/15.0/Bin/MSBuild.exe";
  667. if (cmSystemTools::FileExists(msbuild)) {
  668. return msbuild;
  669. }
  670. }
  671. msbuild = "MSBuild.exe";
  672. return msbuild;
  673. }
  674. std::string cmGlobalVisualStudioVersionedGenerator::FindDevEnvCommand()
  675. {
  676. std::string devenv;
  677. // Ask Visual Studio Installer tool.
  678. std::string vs;
  679. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  680. devenv = vs + "/Common7/IDE/devenv.com";
  681. if (cmSystemTools::FileExists(devenv)) {
  682. return devenv;
  683. }
  684. }
  685. devenv = "devenv.com";
  686. return devenv;
  687. }