cmGlobalVisualStudioVersionedGenerator.cxx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. // FIXME: VS 2022 Preview 1 uses v142. Will it be v143 later?
  100. return "v142";
  101. }
  102. return "";
  103. }
  104. static const char* VSVersionToAndroidToolset(
  105. cmGlobalVisualStudioGenerator::VSVersion v)
  106. {
  107. switch (v) {
  108. case cmGlobalVisualStudioGenerator::VS9:
  109. case cmGlobalVisualStudioGenerator::VS10:
  110. case cmGlobalVisualStudioGenerator::VS11:
  111. case cmGlobalVisualStudioGenerator::VS12:
  112. return "";
  113. case cmGlobalVisualStudioGenerator::VS14:
  114. return "Clang_3_8";
  115. case cmGlobalVisualStudioGenerator::VS15:
  116. case cmGlobalVisualStudioGenerator::VS16:
  117. case cmGlobalVisualStudioGenerator::VS17:
  118. return "Clang_5_0";
  119. }
  120. return "";
  121. }
  122. static const char vs15generatorName[] = "Visual Studio 15 2017";
  123. // Map generator name without year to name with year.
  124. static const char* cmVS15GenName(const std::string& name, std::string& genName)
  125. {
  126. if (strncmp(name.c_str(), vs15generatorName,
  127. sizeof(vs15generatorName) - 6) != 0) {
  128. return 0;
  129. }
  130. const char* p = name.c_str() + sizeof(vs15generatorName) - 6;
  131. if (cmHasLiteralPrefix(p, " 2017")) {
  132. p += 5;
  133. }
  134. genName = std::string(vs15generatorName) + p;
  135. return p;
  136. }
  137. class cmGlobalVisualStudioVersionedGenerator::Factory15
  138. : public cmGlobalGeneratorFactory
  139. {
  140. public:
  141. std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
  142. const std::string& name, bool allowArch, cmake* cm) const override
  143. {
  144. std::string genName;
  145. const char* p = cmVS15GenName(name, genName);
  146. if (!p) {
  147. return std::unique_ptr<cmGlobalGenerator>();
  148. }
  149. if (!*p) {
  150. return std::unique_ptr<cmGlobalGenerator>(
  151. new cmGlobalVisualStudioVersionedGenerator(
  152. cmGlobalVisualStudioGenerator::VS15, cm, genName, ""));
  153. }
  154. if (!allowArch || *p++ != ' ') {
  155. return std::unique_ptr<cmGlobalGenerator>();
  156. }
  157. if (strcmp(p, "Win64") == 0) {
  158. return std::unique_ptr<cmGlobalGenerator>(
  159. new cmGlobalVisualStudioVersionedGenerator(
  160. cmGlobalVisualStudioGenerator::VS15, cm, genName, "x64"));
  161. }
  162. if (strcmp(p, "ARM") == 0) {
  163. return std::unique_ptr<cmGlobalGenerator>(
  164. new cmGlobalVisualStudioVersionedGenerator(
  165. cmGlobalVisualStudioGenerator::VS15, cm, genName, "ARM"));
  166. }
  167. return std::unique_ptr<cmGlobalGenerator>();
  168. }
  169. void GetDocumentation(cmDocumentationEntry& entry) const override
  170. {
  171. entry.Name = std::string(vs15generatorName) + " [arch]";
  172. entry.Brief = "Generates Visual Studio 2017 project files. "
  173. "Optional [arch] can be \"Win64\" or \"ARM\".";
  174. }
  175. std::vector<std::string> GetGeneratorNames() const override
  176. {
  177. std::vector<std::string> names;
  178. names.push_back(vs15generatorName);
  179. return names;
  180. }
  181. std::vector<std::string> GetGeneratorNamesWithPlatform() const override
  182. {
  183. std::vector<std::string> names;
  184. names.push_back(vs15generatorName + std::string(" ARM"));
  185. names.push_back(vs15generatorName + std::string(" Win64"));
  186. return names;
  187. }
  188. bool SupportsToolset() const override { return true; }
  189. bool SupportsPlatform() const override { return true; }
  190. std::vector<std::string> GetKnownPlatforms() const override
  191. {
  192. std::vector<std::string> platforms;
  193. platforms.emplace_back("x64");
  194. platforms.emplace_back("Win32");
  195. platforms.emplace_back("ARM");
  196. platforms.emplace_back("ARM64");
  197. platforms.emplace_back("ARM64EC");
  198. return platforms;
  199. }
  200. std::string GetDefaultPlatformName() const override { return "Win32"; }
  201. };
  202. std::unique_ptr<cmGlobalGeneratorFactory>
  203. cmGlobalVisualStudioVersionedGenerator::NewFactory15()
  204. {
  205. return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory15);
  206. }
  207. static const char vs16generatorName[] = "Visual Studio 16 2019";
  208. static const char vs17generatorName[] = "Visual Studio 17 2022";
  209. // Map generator name without year to name with year.
  210. static const char* cmVS16GenName(const std::string& name, std::string& genName)
  211. {
  212. if (strncmp(name.c_str(), vs16generatorName,
  213. sizeof(vs16generatorName) - 6) != 0) {
  214. return 0;
  215. }
  216. const char* p = name.c_str() + sizeof(vs16generatorName) - 6;
  217. if (cmHasLiteralPrefix(p, " 2019")) {
  218. p += 5;
  219. }
  220. genName = std::string(vs16generatorName) + p;
  221. return p;
  222. }
  223. static const char* cmVS17GenName(const std::string& name, std::string& genName)
  224. {
  225. if (strncmp(name.c_str(), vs17generatorName,
  226. sizeof(vs17generatorName) - 6) != 0) {
  227. return 0;
  228. }
  229. const char* p = name.c_str() + sizeof(vs17generatorName) - 6;
  230. if (cmHasLiteralPrefix(p, " 2022")) {
  231. p += 5;
  232. }
  233. genName = std::string(vs17generatorName) + p;
  234. return p;
  235. }
  236. class cmGlobalVisualStudioVersionedGenerator::Factory16
  237. : public cmGlobalGeneratorFactory
  238. {
  239. public:
  240. std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
  241. const std::string& name, bool /*allowArch*/, cmake* cm) const override
  242. {
  243. std::string genName;
  244. const char* p = cmVS16GenName(name, genName);
  245. if (!p) {
  246. return std::unique_ptr<cmGlobalGenerator>();
  247. }
  248. if (!*p) {
  249. return std::unique_ptr<cmGlobalGenerator>(
  250. new cmGlobalVisualStudioVersionedGenerator(
  251. cmGlobalVisualStudioGenerator::VS16, cm, genName, ""));
  252. }
  253. return std::unique_ptr<cmGlobalGenerator>();
  254. }
  255. void GetDocumentation(cmDocumentationEntry& entry) const override
  256. {
  257. entry.Name = std::string(vs16generatorName);
  258. entry.Brief = "Generates Visual Studio 2019 project files. "
  259. "Use -A option to specify architecture.";
  260. }
  261. std::vector<std::string> GetGeneratorNames() const override
  262. {
  263. std::vector<std::string> names;
  264. names.push_back(vs16generatorName);
  265. return names;
  266. }
  267. std::vector<std::string> GetGeneratorNamesWithPlatform() const override
  268. {
  269. return std::vector<std::string>();
  270. }
  271. bool SupportsToolset() const override { return true; }
  272. bool SupportsPlatform() const override { return true; }
  273. std::vector<std::string> GetKnownPlatforms() const override
  274. {
  275. std::vector<std::string> platforms;
  276. platforms.emplace_back("x64");
  277. platforms.emplace_back("Win32");
  278. platforms.emplace_back("ARM");
  279. platforms.emplace_back("ARM64");
  280. return platforms;
  281. }
  282. std::string GetDefaultPlatformName() const override
  283. {
  284. return VSHostPlatformName();
  285. }
  286. };
  287. std::unique_ptr<cmGlobalGeneratorFactory>
  288. cmGlobalVisualStudioVersionedGenerator::NewFactory16()
  289. {
  290. return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory16);
  291. }
  292. class cmGlobalVisualStudioVersionedGenerator::Factory17
  293. : public cmGlobalGeneratorFactory
  294. {
  295. public:
  296. std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
  297. const std::string& name, bool /*allowArch*/, cmake* cm) const override
  298. {
  299. std::string genName;
  300. const char* p = cmVS17GenName(name, genName);
  301. if (!p) {
  302. return std::unique_ptr<cmGlobalGenerator>();
  303. }
  304. if (!*p) {
  305. return std::unique_ptr<cmGlobalGenerator>(
  306. new cmGlobalVisualStudioVersionedGenerator(
  307. cmGlobalVisualStudioGenerator::VS17, cm, genName, ""));
  308. }
  309. return std::unique_ptr<cmGlobalGenerator>();
  310. }
  311. void GetDocumentation(cmDocumentationEntry& entry) const override
  312. {
  313. entry.Name = std::string(vs17generatorName);
  314. entry.Brief = "Generates Visual Studio 2022 project files. "
  315. "Use -A option to specify architecture.";
  316. }
  317. std::vector<std::string> GetGeneratorNames() const override
  318. {
  319. std::vector<std::string> names;
  320. names.push_back(vs17generatorName);
  321. return names;
  322. }
  323. std::vector<std::string> GetGeneratorNamesWithPlatform() const override
  324. {
  325. return std::vector<std::string>();
  326. }
  327. bool SupportsToolset() const override { return true; }
  328. bool SupportsPlatform() const override { return true; }
  329. std::vector<std::string> GetKnownPlatforms() const override
  330. {
  331. std::vector<std::string> platforms;
  332. platforms.emplace_back("x64");
  333. platforms.emplace_back("Win32");
  334. platforms.emplace_back("ARM");
  335. platforms.emplace_back("ARM64");
  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(
  428. "CMAKE_GENERATOR_INSTANCE", vsInstance.c_str(),
  429. "Generator instance identifier.", 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. // The first two components of the default toolset version typically
  567. // match the name used by later VS versions for the SxS props files.
  568. cmsys::RegularExpression twoComponent("^([0-9]+\\.[0-9]+)");
  569. if (twoComponent.find(version)) {
  570. std::string const versionPrefix = cmStrCat(twoComponent.match(1), '.');
  571. if (cmHasPrefix(vcToolsetVersion, versionPrefix)) {
  572. return AuxToolset::Default;
  573. }
  574. }
  575. }
  576. return AuxToolset::PropsMissing;
  577. }
  578. bool cmGlobalVisualStudioVersionedGenerator::InitializeWindows(cmMakefile* mf)
  579. {
  580. // If the Win 8.1 SDK is installed then we can select a SDK matching
  581. // the target Windows version.
  582. if (this->IsWin81SDKInstalled()) {
  583. // VS 2019 does not default to 8.1 so specify it explicitly when needed.
  584. if (this->Version >= cmGlobalVisualStudioGenerator::VS16 &&
  585. !cmSystemTools::VersionCompareGreater(this->SystemVersion, "8.1")) {
  586. this->SetWindowsTargetPlatformVersion("8.1", mf);
  587. return true;
  588. }
  589. return cmGlobalVisualStudio14Generator::InitializeWindows(mf);
  590. }
  591. // Otherwise we must choose a Win 10 SDK even if we are not targeting
  592. // Windows 10.
  593. return this->SelectWindows10SDK(mf, false);
  594. }
  595. bool cmGlobalVisualStudioVersionedGenerator::SelectWindowsStoreToolset(
  596. std::string& toolset) const
  597. {
  598. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  599. if (this->IsWindowsStoreToolsetInstalled() &&
  600. this->IsWindowsDesktopToolsetInstalled()) {
  601. toolset = VSVersionToToolset(this->Version);
  602. return true;
  603. } else {
  604. return false;
  605. }
  606. }
  607. return this->cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
  608. toolset);
  609. }
  610. bool cmGlobalVisualStudioVersionedGenerator::IsWindowsDesktopToolsetInstalled()
  611. const
  612. {
  613. return vsSetupAPIHelper.IsVSInstalled();
  614. }
  615. bool cmGlobalVisualStudioVersionedGenerator::IsWindowsStoreToolsetInstalled()
  616. const
  617. {
  618. return vsSetupAPIHelper.IsWin10SDKInstalled();
  619. }
  620. bool cmGlobalVisualStudioVersionedGenerator::IsWin81SDKInstalled() const
  621. {
  622. // Does the VS installer tool know about one?
  623. if (vsSetupAPIHelper.IsWin81SDKInstalled()) {
  624. return true;
  625. }
  626. // Does the registry know about one (e.g. from VS 2015)?
  627. std::string win81Root;
  628. if (cmSystemTools::ReadRegistryValue(
  629. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  630. "Windows Kits\\Installed Roots;KitsRoot81",
  631. win81Root, cmSystemTools::KeyWOW64_32) ||
  632. cmSystemTools::ReadRegistryValue(
  633. "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
  634. "Windows Kits\\Installed Roots;KitsRoot81",
  635. win81Root, cmSystemTools::KeyWOW64_32)) {
  636. return cmSystemTools::FileExists(win81Root + "/include/um/windows.h",
  637. true);
  638. }
  639. return false;
  640. }
  641. std::string
  642. cmGlobalVisualStudioVersionedGenerator::GetWindows10SDKMaxVersionDefault(
  643. cmMakefile*) const
  644. {
  645. return std::string();
  646. }
  647. std::string cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommand()
  648. {
  649. std::string msbuild;
  650. // Ask Visual Studio Installer tool.
  651. std::string vs;
  652. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  653. msbuild = vs + "/MSBuild/Current/Bin/MSBuild.exe";
  654. if (cmSystemTools::FileExists(msbuild)) {
  655. return msbuild;
  656. }
  657. msbuild = vs + "/MSBuild/15.0/Bin/MSBuild.exe";
  658. if (cmSystemTools::FileExists(msbuild)) {
  659. return msbuild;
  660. }
  661. }
  662. msbuild = "MSBuild.exe";
  663. return msbuild;
  664. }
  665. std::string cmGlobalVisualStudioVersionedGenerator::FindDevEnvCommand()
  666. {
  667. std::string devenv;
  668. // Ask Visual Studio Installer tool.
  669. std::string vs;
  670. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  671. devenv = vs + "/Common7/IDE/devenv.com";
  672. if (cmSystemTools::FileExists(devenv)) {
  673. return devenv;
  674. }
  675. }
  676. devenv = "devenv.com";
  677. return devenv;
  678. }