cmGlobalVisualStudioVersionedGenerator.cxx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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. return platforms;
  198. }
  199. std::string GetDefaultPlatformName() const override { return "Win32"; }
  200. };
  201. std::unique_ptr<cmGlobalGeneratorFactory>
  202. cmGlobalVisualStudioVersionedGenerator::NewFactory15()
  203. {
  204. return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory15);
  205. }
  206. static const char vs16generatorName[] = "Visual Studio 16 2019";
  207. static const char vs17generatorName[] = "Visual Studio 17 2022";
  208. // Map generator name without year to name with year.
  209. static const char* cmVS16GenName(const std::string& name, std::string& genName)
  210. {
  211. if (strncmp(name.c_str(), vs16generatorName,
  212. sizeof(vs16generatorName) - 6) != 0) {
  213. return 0;
  214. }
  215. const char* p = name.c_str() + sizeof(vs16generatorName) - 6;
  216. if (cmHasLiteralPrefix(p, " 2019")) {
  217. p += 5;
  218. }
  219. genName = std::string(vs16generatorName) + p;
  220. return p;
  221. }
  222. static const char* cmVS17GenName(const std::string& name, std::string& genName)
  223. {
  224. if (strncmp(name.c_str(), vs17generatorName,
  225. sizeof(vs17generatorName) - 6) != 0) {
  226. return 0;
  227. }
  228. const char* p = name.c_str() + sizeof(vs17generatorName) - 6;
  229. if (cmHasLiteralPrefix(p, " 2022")) {
  230. p += 5;
  231. }
  232. genName = std::string(vs17generatorName) + p;
  233. return p;
  234. }
  235. class cmGlobalVisualStudioVersionedGenerator::Factory16
  236. : public cmGlobalGeneratorFactory
  237. {
  238. public:
  239. std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
  240. const std::string& name, bool /*allowArch*/, cmake* cm) const override
  241. {
  242. std::string genName;
  243. const char* p = cmVS16GenName(name, genName);
  244. if (!p) {
  245. return std::unique_ptr<cmGlobalGenerator>();
  246. }
  247. if (!*p) {
  248. return std::unique_ptr<cmGlobalGenerator>(
  249. new cmGlobalVisualStudioVersionedGenerator(
  250. cmGlobalVisualStudioGenerator::VS16, cm, genName, ""));
  251. }
  252. return std::unique_ptr<cmGlobalGenerator>();
  253. }
  254. void GetDocumentation(cmDocumentationEntry& entry) const override
  255. {
  256. entry.Name = std::string(vs16generatorName);
  257. entry.Brief = "Generates Visual Studio 2019 project files. "
  258. "Use -A option to specify architecture.";
  259. }
  260. std::vector<std::string> GetGeneratorNames() const override
  261. {
  262. std::vector<std::string> names;
  263. names.push_back(vs16generatorName);
  264. return names;
  265. }
  266. std::vector<std::string> GetGeneratorNamesWithPlatform() const override
  267. {
  268. return std::vector<std::string>();
  269. }
  270. bool SupportsToolset() const override { return true; }
  271. bool SupportsPlatform() const override { return true; }
  272. std::vector<std::string> GetKnownPlatforms() const override
  273. {
  274. std::vector<std::string> platforms;
  275. platforms.emplace_back("x64");
  276. platforms.emplace_back("Win32");
  277. platforms.emplace_back("ARM");
  278. platforms.emplace_back("ARM64");
  279. platforms.emplace_back("ARM64EC");
  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. platforms.emplace_back("ARM64EC");
  337. return platforms;
  338. }
  339. std::string GetDefaultPlatformName() const override
  340. {
  341. return VSHostPlatformName();
  342. }
  343. };
  344. std::unique_ptr<cmGlobalGeneratorFactory>
  345. cmGlobalVisualStudioVersionedGenerator::NewFactory17()
  346. {
  347. return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory17);
  348. }
  349. cmGlobalVisualStudioVersionedGenerator::cmGlobalVisualStudioVersionedGenerator(
  350. VSVersion version, cmake* cm, const std::string& name,
  351. std::string const& platformInGeneratorName)
  352. : cmGlobalVisualStudio14Generator(cm, name, platformInGeneratorName)
  353. , vsSetupAPIHelper(VSVersionToMajor(version))
  354. {
  355. this->Version = version;
  356. this->ExpressEdition = false;
  357. this->DefaultPlatformToolset = VSVersionToToolset(this->Version);
  358. this->DefaultAndroidToolset = VSVersionToAndroidToolset(this->Version);
  359. this->DefaultCLFlagTableName = VSVersionToToolset(this->Version);
  360. this->DefaultCSharpFlagTableName = VSVersionToToolset(this->Version);
  361. this->DefaultLinkFlagTableName = VSVersionToToolset(this->Version);
  362. if (this->Version >= cmGlobalVisualStudioGenerator::VS16) {
  363. this->DefaultPlatformName = VSHostPlatformName();
  364. this->DefaultPlatformToolsetHostArchitecture = VSHostArchitecture();
  365. }
  366. }
  367. bool cmGlobalVisualStudioVersionedGenerator::MatchesGeneratorName(
  368. const std::string& name) const
  369. {
  370. std::string genName;
  371. switch (this->Version) {
  372. case cmGlobalVisualStudioGenerator::VS9:
  373. case cmGlobalVisualStudioGenerator::VS10:
  374. case cmGlobalVisualStudioGenerator::VS11:
  375. case cmGlobalVisualStudioGenerator::VS12:
  376. case cmGlobalVisualStudioGenerator::VS14:
  377. break;
  378. case cmGlobalVisualStudioGenerator::VS15:
  379. if (cmVS15GenName(name, genName)) {
  380. return genName == this->GetName();
  381. }
  382. break;
  383. case cmGlobalVisualStudioGenerator::VS16:
  384. if (cmVS16GenName(name, genName)) {
  385. return genName == this->GetName();
  386. }
  387. break;
  388. case cmGlobalVisualStudioGenerator::VS17:
  389. if (cmVS17GenName(name, genName)) {
  390. return genName == this->GetName();
  391. }
  392. break;
  393. }
  394. return false;
  395. }
  396. bool cmGlobalVisualStudioVersionedGenerator::SetGeneratorInstance(
  397. std::string const& i, cmMakefile* mf)
  398. {
  399. if (!i.empty()) {
  400. if (!this->vsSetupAPIHelper.SetVSInstance(i)) {
  401. std::ostringstream e;
  402. /* clang-format off */
  403. e <<
  404. "Generator\n"
  405. " " << this->GetName() << "\n"
  406. "could not find specified instance of Visual Studio:\n"
  407. " " << i;
  408. /* clang-format on */
  409. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  410. return false;
  411. }
  412. }
  413. std::string vsInstance;
  414. if (!this->vsSetupAPIHelper.GetVSInstanceInfo(vsInstance)) {
  415. std::ostringstream e;
  416. /* clang-format off */
  417. e <<
  418. "Generator\n"
  419. " " << this->GetName() << "\n"
  420. "could not find any instance of Visual Studio.\n";
  421. /* clang-format on */
  422. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  423. return false;
  424. }
  425. // Save the selected instance persistently.
  426. std::string genInstance = mf->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
  427. if (vsInstance != genInstance) {
  428. this->CMakeInstance->AddCacheEntry(
  429. "CMAKE_GENERATOR_INSTANCE", vsInstance.c_str(),
  430. "Generator instance identifier.", cmStateEnums::INTERNAL);
  431. }
  432. return true;
  433. }
  434. bool cmGlobalVisualStudioVersionedGenerator::GetVSInstance(
  435. std::string& dir) const
  436. {
  437. return vsSetupAPIHelper.GetVSInstanceInfo(dir);
  438. }
  439. cm::optional<std::string>
  440. cmGlobalVisualStudioVersionedGenerator::GetVSInstanceVersion() const
  441. {
  442. cm::optional<std::string> result;
  443. std::string vsInstanceVersion;
  444. if (vsSetupAPIHelper.GetVSInstanceVersion(vsInstanceVersion)) {
  445. result = vsInstanceVersion;
  446. }
  447. return result;
  448. }
  449. bool cmGlobalVisualStudioVersionedGenerator::IsStdOutEncodingSupported() const
  450. {
  451. // Supported from Visual Studio 16.7 Preview 3.
  452. if (this->Version > cmGlobalVisualStudioGenerator::VSVersion::VS16) {
  453. return true;
  454. }
  455. if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS16) {
  456. return false;
  457. }
  458. static std::string const vsVer16_7_P2 = "16.7.30128.36";
  459. cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
  460. return (vsVer &&
  461. cmSystemTools::VersionCompareGreaterEq(*vsVer, vsVer16_7_P2));
  462. }
  463. bool cmGlobalVisualStudioVersionedGenerator::IsUtf8EncodingSupported() const
  464. {
  465. // Supported from Visual Studio 16.10 Preview 2.
  466. if (this->Version > cmGlobalVisualStudioGenerator::VSVersion::VS16) {
  467. return true;
  468. }
  469. if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS16) {
  470. return false;
  471. }
  472. static std::string const vsVer16_10_P2 = "16.10.31213.239";
  473. cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
  474. return (vsVer &&
  475. cmSystemTools::VersionCompareGreaterEq(*vsVer, vsVer16_10_P2));
  476. }
  477. const char*
  478. cmGlobalVisualStudioVersionedGenerator::GetAndroidApplicationTypeRevision()
  479. const
  480. {
  481. switch (this->Version) {
  482. case cmGlobalVisualStudioGenerator::VS9:
  483. case cmGlobalVisualStudioGenerator::VS10:
  484. case cmGlobalVisualStudioGenerator::VS11:
  485. case cmGlobalVisualStudioGenerator::VS12:
  486. return "";
  487. case cmGlobalVisualStudioGenerator::VS14:
  488. return "2.0";
  489. case cmGlobalVisualStudioGenerator::VS15:
  490. case cmGlobalVisualStudioGenerator::VS16:
  491. case cmGlobalVisualStudioGenerator::VS17:
  492. return "3.0";
  493. }
  494. return "";
  495. }
  496. cmGlobalVisualStudioVersionedGenerator::AuxToolset
  497. cmGlobalVisualStudioVersionedGenerator::FindAuxToolset(
  498. std::string& version, std::string& props) const
  499. {
  500. if (version.empty()) {
  501. return AuxToolset::None;
  502. }
  503. std::string instancePath;
  504. this->GetVSInstance(instancePath);
  505. cmSystemTools::ConvertToUnixSlashes(instancePath);
  506. // Translate three-component format accepted by "vcvarsall -vcvars_ver=".
  507. cmsys::RegularExpression threeComponent(
  508. "^([0-9]+\\.[0-9]+)\\.[0-9][0-9][0-9][0-9][0-9]$");
  509. if (threeComponent.find(version)) {
  510. // Load "VC/Auxiliary/Build/*/Microsoft.VCToolsVersion.*.txt" files
  511. // with two matching components to check their three-component version.
  512. std::string const& twoComponent = threeComponent.match(1);
  513. std::string pattern =
  514. cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s, twoComponent,
  515. "*/Microsoft.VCToolsVersion."_s, twoComponent, "*.txt"_s);
  516. cmsys::Glob glob;
  517. glob.SetRecurseThroughSymlinks(false);
  518. if (glob.FindFiles(pattern)) {
  519. for (std::string const& txt : glob.GetFiles()) {
  520. std::string ver;
  521. cmsys::ifstream fin(txt.c_str());
  522. if (fin && std::getline(fin, ver)) {
  523. // Strip trailing whitespace.
  524. ver = ver.substr(0, ver.find_first_not_of("0123456789."));
  525. // If the three-component version matches, translate it to
  526. // that used by the "Microsoft.VCToolsVersion.*.txt" file name.
  527. if (ver == version) {
  528. cmsys::RegularExpression extractVersion(
  529. "VCToolsVersion\\.([0-9.]+)\\.txt$");
  530. if (extractVersion.find(txt)) {
  531. version = extractVersion.match(1);
  532. break;
  533. }
  534. }
  535. }
  536. }
  537. }
  538. }
  539. if (cmSystemTools::VersionCompareGreaterEq(version, "14.20")) {
  540. props = cmStrCat(instancePath, "/VC/Auxiliary/Build."_s, version,
  541. "/Microsoft.VCToolsVersion."_s, version, ".props"_s);
  542. if (cmSystemTools::PathExists(props)) {
  543. return AuxToolset::PropsExist;
  544. }
  545. }
  546. props = cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s, version,
  547. "/Microsoft.VCToolsVersion."_s, version, ".props"_s);
  548. if (cmSystemTools::PathExists(props)) {
  549. return AuxToolset::PropsExist;
  550. }
  551. // Accept the toolset version that is default in the current VS version
  552. // by matching the name later VS versions will use for the SxS props files.
  553. std::string vcToolsetVersion;
  554. if (this->vsSetupAPIHelper.GetVCToolsetVersion(vcToolsetVersion)) {
  555. // Accept an exact-match (three-component version).
  556. if (version == vcToolsetVersion) {
  557. return AuxToolset::Default;
  558. }
  559. // Accept known SxS props file names using four version components
  560. // in VS versions later than the current.
  561. if (version == "14.28.16.9" && vcToolsetVersion == "14.28.29910") {
  562. return AuxToolset::Default;
  563. }
  564. if (version == "14.29.16.10" && vcToolsetVersion == "14.29.30037") {
  565. return AuxToolset::Default;
  566. }
  567. // The first two components of the default toolset version typically
  568. // match the name used by later VS versions for the SxS props files.
  569. cmsys::RegularExpression twoComponent("^([0-9]+\\.[0-9]+)");
  570. if (twoComponent.find(version)) {
  571. std::string const versionPrefix = cmStrCat(twoComponent.match(1), '.');
  572. if (cmHasPrefix(vcToolsetVersion, versionPrefix)) {
  573. return AuxToolset::Default;
  574. }
  575. }
  576. }
  577. return AuxToolset::PropsMissing;
  578. }
  579. bool cmGlobalVisualStudioVersionedGenerator::InitializeWindows(cmMakefile* mf)
  580. {
  581. // If the Win 8.1 SDK is installed then we can select a SDK matching
  582. // the target Windows version.
  583. if (this->IsWin81SDKInstalled()) {
  584. // VS 2019 does not default to 8.1 so specify it explicitly when needed.
  585. if (this->Version >= cmGlobalVisualStudioGenerator::VS16 &&
  586. !cmSystemTools::VersionCompareGreater(this->SystemVersion, "8.1")) {
  587. this->SetWindowsTargetPlatformVersion("8.1", mf);
  588. return true;
  589. }
  590. return cmGlobalVisualStudio14Generator::InitializeWindows(mf);
  591. }
  592. // Otherwise we must choose a Win 10 SDK even if we are not targeting
  593. // Windows 10.
  594. return this->SelectWindows10SDK(mf, false);
  595. }
  596. bool cmGlobalVisualStudioVersionedGenerator::SelectWindowsStoreToolset(
  597. std::string& toolset) const
  598. {
  599. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  600. if (this->IsWindowsStoreToolsetInstalled() &&
  601. this->IsWindowsDesktopToolsetInstalled()) {
  602. toolset = VSVersionToToolset(this->Version);
  603. return true;
  604. } else {
  605. return false;
  606. }
  607. }
  608. return this->cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
  609. toolset);
  610. }
  611. bool cmGlobalVisualStudioVersionedGenerator::IsWindowsDesktopToolsetInstalled()
  612. const
  613. {
  614. return vsSetupAPIHelper.IsVSInstalled();
  615. }
  616. bool cmGlobalVisualStudioVersionedGenerator::IsWindowsStoreToolsetInstalled()
  617. const
  618. {
  619. return vsSetupAPIHelper.IsWin10SDKInstalled();
  620. }
  621. bool cmGlobalVisualStudioVersionedGenerator::IsWin81SDKInstalled() const
  622. {
  623. // Does the VS installer tool know about one?
  624. if (vsSetupAPIHelper.IsWin81SDKInstalled()) {
  625. return true;
  626. }
  627. // Does the registry know about one (e.g. from VS 2015)?
  628. std::string win81Root;
  629. if (cmSystemTools::ReadRegistryValue(
  630. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  631. "Windows Kits\\Installed Roots;KitsRoot81",
  632. win81Root, cmSystemTools::KeyWOW64_32) ||
  633. cmSystemTools::ReadRegistryValue(
  634. "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
  635. "Windows Kits\\Installed Roots;KitsRoot81",
  636. win81Root, cmSystemTools::KeyWOW64_32)) {
  637. return cmSystemTools::FileExists(win81Root + "/include/um/windows.h",
  638. true);
  639. }
  640. return false;
  641. }
  642. std::string
  643. cmGlobalVisualStudioVersionedGenerator::GetWindows10SDKMaxVersionDefault(
  644. cmMakefile*) const
  645. {
  646. return std::string();
  647. }
  648. std::string cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommand()
  649. {
  650. std::string msbuild;
  651. // Ask Visual Studio Installer tool.
  652. std::string vs;
  653. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  654. if (this->Version >= cmGlobalVisualStudioGenerator::VS17) {
  655. msbuild = vs + "/MSBuild/Current/Bin/amd64/MSBuild.exe";
  656. if (cmSystemTools::FileExists(msbuild)) {
  657. return msbuild;
  658. }
  659. }
  660. msbuild = vs + "/MSBuild/Current/Bin/MSBuild.exe";
  661. if (cmSystemTools::FileExists(msbuild)) {
  662. return msbuild;
  663. }
  664. msbuild = vs + "/MSBuild/15.0/Bin/MSBuild.exe";
  665. if (cmSystemTools::FileExists(msbuild)) {
  666. return msbuild;
  667. }
  668. }
  669. msbuild = "MSBuild.exe";
  670. return msbuild;
  671. }
  672. std::string cmGlobalVisualStudioVersionedGenerator::FindDevEnvCommand()
  673. {
  674. std::string devenv;
  675. // Ask Visual Studio Installer tool.
  676. std::string vs;
  677. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  678. devenv = vs + "/Common7/IDE/devenv.com";
  679. if (cmSystemTools::FileExists(devenv)) {
  680. return devenv;
  681. }
  682. }
  683. devenv = "devenv.com";
  684. return devenv;
  685. }