cmGlobalVisualStudioVersionedGenerator.cxx 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmGlobalVisualStudioVersionedGenerator.h"
  4. #include <cstring>
  5. #include <set>
  6. #include <sstream>
  7. #include <utility>
  8. #include <vector>
  9. #include <cmext/string_view>
  10. #include "cmsys/FStream.hxx"
  11. #include "cmsys/Glob.hxx"
  12. #include "cmsys/RegularExpression.hxx"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmGlobalGeneratorFactory.h"
  15. #include "cmMakefile.h"
  16. #include "cmMessageType.h"
  17. #include "cmStateTypes.h"
  18. #include "cmStringAlgorithms.h"
  19. #include "cmSystemTools.h"
  20. #include "cmVSSetupHelper.h"
  21. #include "cmake.h"
  22. #ifndef IMAGE_FILE_MACHINE_ARM64
  23. # define IMAGE_FILE_MACHINE_ARM64 0xaa64 // ARM64 Little-Endian
  24. #endif
  25. static bool VSIsWow64()
  26. {
  27. BOOL isWow64 = false;
  28. return IsWow64Process(GetCurrentProcess(), &isWow64) && isWow64;
  29. }
  30. static bool VSIsArm64Host()
  31. {
  32. typedef BOOL(WINAPI * CM_ISWOW64PROCESS2)(
  33. HANDLE hProcess, USHORT * pProcessMachine, USHORT * pNativeMachine);
  34. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
  35. # define CM_VS_GCC_DIAGNOSTIC_PUSHED
  36. # pragma GCC diagnostic push
  37. # pragma GCC diagnostic ignored "-Wcast-function-type"
  38. #endif
  39. static const CM_ISWOW64PROCESS2 s_IsWow64Process2Impl =
  40. (CM_ISWOW64PROCESS2)GetProcAddress(
  41. GetModuleHandleW(L"api-ms-win-core-wow64-l1-1-1.dll"),
  42. "IsWow64Process2");
  43. #ifdef CM_VS_GCC_DIAGNOSTIC_PUSHED
  44. # pragma GCC diagnostic pop
  45. # undef CM_VS_GCC_DIAGNOSTIC_PUSHED
  46. #endif
  47. USHORT processMachine;
  48. USHORT nativeMachine;
  49. return s_IsWow64Process2Impl &&
  50. s_IsWow64Process2Impl(GetCurrentProcess(), &processMachine,
  51. &nativeMachine) &&
  52. nativeMachine == IMAGE_FILE_MACHINE_ARM64;
  53. }
  54. static bool VSHasDotNETFrameworkArm64()
  55. {
  56. std::string dotNetArm64;
  57. return cmSystemTools::ReadRegistryValue(
  58. R"(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework;InstallRootArm64)",
  59. dotNetArm64, cmSystemTools::KeyWOW64_64);
  60. }
  61. static bool VSIsWindows11OrGreater()
  62. {
  63. cmSystemTools::WindowsVersion const windowsVersion =
  64. cmSystemTools::GetWindowsVersion();
  65. return (windowsVersion.dwMajorVersion > 10 ||
  66. (windowsVersion.dwMajorVersion == 10 &&
  67. windowsVersion.dwMinorVersion > 0) ||
  68. (windowsVersion.dwMajorVersion == 10 &&
  69. windowsVersion.dwMinorVersion == 0 &&
  70. windowsVersion.dwBuildNumber >= 22000));
  71. }
  72. static std::string VSHostPlatformName()
  73. {
  74. if (VSIsArm64Host()) {
  75. return "ARM64";
  76. }
  77. if (VSIsWow64()) {
  78. return "x64";
  79. }
  80. #if defined(_M_ARM)
  81. return "ARM";
  82. #elif defined(_M_IA64)
  83. return "Itanium";
  84. #elif defined(_WIN64)
  85. return "x64";
  86. #else
  87. return "Win32";
  88. #endif
  89. }
  90. static std::string VSHostArchitecture(
  91. cmGlobalVisualStudioGenerator::VSVersion v)
  92. {
  93. if (VSIsArm64Host()) {
  94. return v >= cmGlobalVisualStudioGenerator::VSVersion::VS17 ? "ARM64" : "";
  95. }
  96. if (VSIsWow64()) {
  97. return "x64";
  98. }
  99. #if defined(_M_ARM)
  100. return "";
  101. #elif defined(_M_IA64)
  102. return "";
  103. #elif defined(_WIN64)
  104. return "x64";
  105. #else
  106. return "x86";
  107. #endif
  108. }
  109. static unsigned int VSVersionToMajor(
  110. cmGlobalVisualStudioGenerator::VSVersion v)
  111. {
  112. switch (v) {
  113. case cmGlobalVisualStudioGenerator::VSVersion::VS14:
  114. return 14;
  115. case cmGlobalVisualStudioGenerator::VSVersion::VS15:
  116. return 15;
  117. case cmGlobalVisualStudioGenerator::VSVersion::VS16:
  118. return 16;
  119. case cmGlobalVisualStudioGenerator::VSVersion::VS17:
  120. return 17;
  121. case cmGlobalVisualStudioGenerator::VSVersion::VS18:
  122. return 18;
  123. }
  124. return 0;
  125. }
  126. static char const* VSVersionToToolset(
  127. cmGlobalVisualStudioGenerator::VSVersion v)
  128. {
  129. switch (v) {
  130. case cmGlobalVisualStudioGenerator::VSVersion::VS14:
  131. return "v140";
  132. case cmGlobalVisualStudioGenerator::VSVersion::VS15:
  133. return "v141";
  134. case cmGlobalVisualStudioGenerator::VSVersion::VS16:
  135. return "v142";
  136. case cmGlobalVisualStudioGenerator::VSVersion::VS17:
  137. return "v143";
  138. case cmGlobalVisualStudioGenerator::VSVersion::VS18:
  139. return "v145";
  140. }
  141. return "";
  142. }
  143. static std::string VSVersionToMajorString(
  144. cmGlobalVisualStudioGenerator::VSVersion v)
  145. {
  146. switch (v) {
  147. case cmGlobalVisualStudioGenerator::VSVersion::VS14:
  148. return "14";
  149. case cmGlobalVisualStudioGenerator::VSVersion::VS15:
  150. return "15";
  151. case cmGlobalVisualStudioGenerator::VSVersion::VS16:
  152. return "16";
  153. case cmGlobalVisualStudioGenerator::VSVersion::VS17:
  154. return "17";
  155. case cmGlobalVisualStudioGenerator::VSVersion::VS18:
  156. return "18";
  157. }
  158. return "";
  159. }
  160. static char const* VSVersionToAndroidToolset(
  161. cmGlobalVisualStudioGenerator::VSVersion v)
  162. {
  163. switch (v) {
  164. case cmGlobalVisualStudioGenerator::VSVersion::VS14:
  165. return "Clang_3_8";
  166. case cmGlobalVisualStudioGenerator::VSVersion::VS15:
  167. case cmGlobalVisualStudioGenerator::VSVersion::VS16:
  168. case cmGlobalVisualStudioGenerator::VSVersion::VS17:
  169. case cmGlobalVisualStudioGenerator::VSVersion::VS18:
  170. return "Clang_5_0";
  171. }
  172. return "";
  173. }
  174. static char const vs15generatorName[] = "Visual Studio 15 2017";
  175. // Map generator name without year to name with year.
  176. static char const* cmVS15GenName(std::string const& name, std::string& genName)
  177. {
  178. if (strncmp(name.c_str(), vs15generatorName,
  179. sizeof(vs15generatorName) - 6) != 0) {
  180. return nullptr;
  181. }
  182. char const* p = name.c_str() + sizeof(vs15generatorName) - 6;
  183. if (cmHasLiteralPrefix(p, " 2017")) {
  184. p += 5;
  185. }
  186. genName = cmStrCat(vs15generatorName, p);
  187. return p;
  188. }
  189. class cmGlobalVisualStudioVersionedGenerator::Factory15
  190. : public cmGlobalGeneratorFactory
  191. {
  192. public:
  193. std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
  194. std::string const& name, cmake* cm) const override
  195. {
  196. std::string genName;
  197. char const* p = cmVS15GenName(name, genName);
  198. if (!p) {
  199. return std::unique_ptr<cmGlobalGenerator>();
  200. }
  201. if (!*p) {
  202. return std::unique_ptr<cmGlobalGenerator>(
  203. new cmGlobalVisualStudioVersionedGenerator(
  204. cmGlobalVisualStudioGenerator::VSVersion::VS15, cm, genName));
  205. }
  206. return std::unique_ptr<cmGlobalGenerator>();
  207. }
  208. cmDocumentationEntry GetDocumentation() const override
  209. {
  210. return { std::string(vs15generatorName),
  211. "Generates Visual Studio 2017 project files. "
  212. "Use -A option to specify architecture." };
  213. }
  214. std::vector<std::string> GetGeneratorNames() const override
  215. {
  216. std::vector<std::string> names;
  217. names.push_back(vs15generatorName);
  218. return names;
  219. }
  220. bool SupportsToolset() const override { return true; }
  221. bool SupportsPlatform() const override { return true; }
  222. std::vector<std::string> GetKnownPlatforms() const override
  223. {
  224. std::vector<std::string> platforms;
  225. platforms.emplace_back("x64");
  226. platforms.emplace_back("Win32");
  227. platforms.emplace_back("ARM");
  228. platforms.emplace_back("ARM64");
  229. return platforms;
  230. }
  231. std::string GetDefaultPlatformName() const override { return "Win32"; }
  232. };
  233. std::unique_ptr<cmGlobalGeneratorFactory>
  234. cmGlobalVisualStudioVersionedGenerator::NewFactory15()
  235. {
  236. return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory15);
  237. }
  238. static char const vs16generatorName[] = "Visual Studio 16 2019";
  239. static char const vs17generatorName[] = "Visual Studio 17 2022";
  240. static char const vs18generatorName[] = "Visual Studio 18 2026";
  241. // Map generator name without year to name with year.
  242. static char const* cmVS16GenName(std::string const& name, std::string& genName)
  243. {
  244. if (strncmp(name.c_str(), vs16generatorName,
  245. sizeof(vs16generatorName) - 6) != 0) {
  246. return nullptr;
  247. }
  248. char const* p = name.c_str() + sizeof(vs16generatorName) - 6;
  249. if (cmHasLiteralPrefix(p, " 2019")) {
  250. p += 5;
  251. }
  252. genName = cmStrCat(vs16generatorName, p);
  253. return p;
  254. }
  255. static char const* cmVS17GenName(std::string const& name, std::string& genName)
  256. {
  257. if (strncmp(name.c_str(), vs17generatorName,
  258. sizeof(vs17generatorName) - 6) != 0) {
  259. return nullptr;
  260. }
  261. char const* p = name.c_str() + sizeof(vs17generatorName) - 6;
  262. if (cmHasLiteralPrefix(p, " 2022")) {
  263. p += 5;
  264. }
  265. genName = cmStrCat(vs17generatorName, p);
  266. return p;
  267. }
  268. static char const* cmVS18GenName(std::string const& name, std::string& genName)
  269. {
  270. if (strncmp(name.c_str(), vs18generatorName,
  271. sizeof(vs18generatorName) - 6) != 0) {
  272. return nullptr;
  273. }
  274. char const* p = name.c_str() + sizeof(vs18generatorName) - 6;
  275. if (cmHasLiteralPrefix(p, " 2026")) {
  276. p += 5;
  277. }
  278. genName = cmStrCat(vs18generatorName, p);
  279. return p;
  280. }
  281. class cmGlobalVisualStudioVersionedGenerator::Factory16
  282. : public cmGlobalGeneratorFactory
  283. {
  284. public:
  285. std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
  286. std::string const& name, cmake* cm) const override
  287. {
  288. std::string genName;
  289. char const* p = cmVS16GenName(name, genName);
  290. if (!p) {
  291. return std::unique_ptr<cmGlobalGenerator>();
  292. }
  293. if (!*p) {
  294. return std::unique_ptr<cmGlobalGenerator>(
  295. new cmGlobalVisualStudioVersionedGenerator(
  296. cmGlobalVisualStudioGenerator::VSVersion::VS16, cm, genName));
  297. }
  298. return std::unique_ptr<cmGlobalGenerator>();
  299. }
  300. cmDocumentationEntry GetDocumentation() const override
  301. {
  302. return { std::string(vs16generatorName),
  303. "Generates Visual Studio 2019 project files. "
  304. "Use -A option to specify architecture." };
  305. }
  306. std::vector<std::string> GetGeneratorNames() const override
  307. {
  308. std::vector<std::string> names;
  309. names.push_back(vs16generatorName);
  310. return names;
  311. }
  312. bool SupportsToolset() const override { return true; }
  313. bool SupportsPlatform() const override { return true; }
  314. std::vector<std::string> GetKnownPlatforms() const override
  315. {
  316. std::vector<std::string> platforms;
  317. platforms.emplace_back("x64");
  318. platforms.emplace_back("Win32");
  319. platforms.emplace_back("ARM");
  320. platforms.emplace_back("ARM64");
  321. platforms.emplace_back("ARM64EC");
  322. return platforms;
  323. }
  324. std::string GetDefaultPlatformName() const override
  325. {
  326. return VSHostPlatformName();
  327. }
  328. };
  329. std::unique_ptr<cmGlobalGeneratorFactory>
  330. cmGlobalVisualStudioVersionedGenerator::NewFactory16()
  331. {
  332. return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory16);
  333. }
  334. class cmGlobalVisualStudioVersionedGenerator::Factory17
  335. : public cmGlobalGeneratorFactory
  336. {
  337. public:
  338. std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
  339. std::string const& name, cmake* cm) const override
  340. {
  341. std::string genName;
  342. char const* p = cmVS17GenName(name, genName);
  343. if (!p) {
  344. return std::unique_ptr<cmGlobalGenerator>();
  345. }
  346. if (!*p) {
  347. return std::unique_ptr<cmGlobalGenerator>(
  348. new cmGlobalVisualStudioVersionedGenerator(
  349. cmGlobalVisualStudioGenerator::VSVersion::VS17, cm, genName));
  350. }
  351. return std::unique_ptr<cmGlobalGenerator>();
  352. }
  353. cmDocumentationEntry GetDocumentation() const override
  354. {
  355. return { std::string(vs17generatorName),
  356. "Generates Visual Studio 2022 project files. "
  357. "Use -A option to specify architecture." };
  358. }
  359. std::vector<std::string> GetGeneratorNames() const override
  360. {
  361. std::vector<std::string> names;
  362. names.push_back(vs17generatorName);
  363. return names;
  364. }
  365. bool SupportsToolset() const override { return true; }
  366. bool SupportsPlatform() const override { return true; }
  367. std::vector<std::string> GetKnownPlatforms() const override
  368. {
  369. std::vector<std::string> platforms;
  370. platforms.emplace_back("x64");
  371. platforms.emplace_back("Win32");
  372. platforms.emplace_back("ARM");
  373. platforms.emplace_back("ARM64");
  374. platforms.emplace_back("ARM64EC");
  375. return platforms;
  376. }
  377. std::string GetDefaultPlatformName() const override
  378. {
  379. return VSHostPlatformName();
  380. }
  381. };
  382. std::unique_ptr<cmGlobalGeneratorFactory>
  383. cmGlobalVisualStudioVersionedGenerator::NewFactory17()
  384. {
  385. return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory17);
  386. }
  387. class cmGlobalVisualStudioVersionedGenerator::Factory18
  388. : public cmGlobalGeneratorFactory
  389. {
  390. public:
  391. std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
  392. std::string const& name, cmake* cm) const override
  393. {
  394. std::string genName;
  395. char const* p = cmVS18GenName(name, genName);
  396. if (!p) {
  397. return std::unique_ptr<cmGlobalGenerator>();
  398. }
  399. if (!*p) {
  400. return std::unique_ptr<cmGlobalGenerator>(
  401. new cmGlobalVisualStudioVersionedGenerator(
  402. cmGlobalVisualStudioGenerator::VSVersion::VS18, cm, genName));
  403. }
  404. return std::unique_ptr<cmGlobalGenerator>();
  405. }
  406. cmDocumentationEntry GetDocumentation() const override
  407. {
  408. return { std::string(vs18generatorName),
  409. "Generates Visual Studio 2026 project files. "
  410. "Use -A option to specify architecture." };
  411. }
  412. std::vector<std::string> GetGeneratorNames() const override
  413. {
  414. std::vector<std::string> names;
  415. names.push_back(vs18generatorName);
  416. return names;
  417. }
  418. bool SupportsToolset() const override { return true; }
  419. bool SupportsPlatform() const override { return true; }
  420. std::vector<std::string> GetKnownPlatforms() const override
  421. {
  422. std::vector<std::string> platforms;
  423. platforms.emplace_back("x64");
  424. platforms.emplace_back("Win32");
  425. platforms.emplace_back("ARM");
  426. platforms.emplace_back("ARM64");
  427. platforms.emplace_back("ARM64EC");
  428. return platforms;
  429. }
  430. std::string GetDefaultPlatformName() const override
  431. {
  432. return VSHostPlatformName();
  433. }
  434. };
  435. std::unique_ptr<cmGlobalGeneratorFactory>
  436. cmGlobalVisualStudioVersionedGenerator::NewFactory18()
  437. {
  438. return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory18);
  439. }
  440. cmGlobalVisualStudioVersionedGenerator::cmGlobalVisualStudioVersionedGenerator(
  441. VSVersion version, cmake* cm, std::string const& name)
  442. : cmGlobalVisualStudio14Generator(cm, name)
  443. , vsSetupAPIHelper(VSVersionToMajor(version))
  444. {
  445. this->Version = version;
  446. this->ExpressEdition = false;
  447. this->DefaultPlatformToolset = VSVersionToToolset(this->Version);
  448. this->DefaultAndroidToolset = VSVersionToAndroidToolset(this->Version);
  449. this->DefaultCLFlagTableName = VSVersionToToolset(this->Version);
  450. this->DefaultCSharpFlagTableName = VSVersionToToolset(this->Version);
  451. this->DefaultLinkFlagTableName = VSVersionToToolset(this->Version);
  452. if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS16) {
  453. this->DefaultPlatformName = VSHostPlatformName();
  454. this->DefaultPlatformToolsetHostArchitecture =
  455. VSHostArchitecture(this->Version);
  456. }
  457. if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS17) {
  458. // FIXME: Search for an existing framework? Under '%ProgramFiles(x86)%',
  459. // see 'Reference Assemblies\Microsoft\Framework\.NETFramework'.
  460. // Use a version installed by VS 2022 without a separate component.
  461. this->DefaultTargetFrameworkVersion = "v4.7.2";
  462. }
  463. if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS18) {
  464. this->DefaultToolsetFortran = "ifx";
  465. }
  466. }
  467. bool cmGlobalVisualStudioVersionedGenerator::MatchesGeneratorName(
  468. std::string const& name) const
  469. {
  470. std::string genName;
  471. switch (this->Version) {
  472. case cmGlobalVisualStudioGenerator::VSVersion::VS14:
  473. break;
  474. case cmGlobalVisualStudioGenerator::VSVersion::VS15:
  475. if (cmVS15GenName(name, genName)) {
  476. return genName == this->GetName();
  477. }
  478. break;
  479. case cmGlobalVisualStudioGenerator::VSVersion::VS16:
  480. if (cmVS16GenName(name, genName)) {
  481. return genName == this->GetName();
  482. }
  483. break;
  484. case cmGlobalVisualStudioGenerator::VSVersion::VS17:
  485. if (cmVS17GenName(name, genName)) {
  486. return genName == this->GetName();
  487. }
  488. break;
  489. case cmGlobalVisualStudioGenerator::VSVersion::VS18:
  490. if (cmVS18GenName(name, genName)) {
  491. return genName == this->GetName();
  492. }
  493. break;
  494. }
  495. return false;
  496. }
  497. bool cmGlobalVisualStudioVersionedGenerator::SetGeneratorInstance(
  498. std::string const& i, cmMakefile* mf)
  499. {
  500. if (this->LastGeneratorInstanceString &&
  501. i == *(this->LastGeneratorInstanceString)) {
  502. this->SetVSVersionVar(mf);
  503. return true;
  504. }
  505. if (!this->ParseGeneratorInstance(i, mf)) {
  506. return false;
  507. }
  508. if (!this->GeneratorInstanceVersion.empty()) {
  509. std::string const majorStr = VSVersionToMajorString(this->Version);
  510. cmsys::RegularExpression versionRegex(
  511. cmStrCat('^', majorStr, R"(\.[0-9]+\.[0-9]+\.[0-9]+$)"));
  512. if (!versionRegex.find(this->GeneratorInstanceVersion)) {
  513. mf->IssueMessage(
  514. MessageType::FATAL_ERROR,
  515. cmStrCat("Generator\n"
  516. " ",
  517. this->GetName(),
  518. "\n"
  519. "given instance specification\n"
  520. " ",
  521. i,
  522. "\n"
  523. "but the version field is not 4 integer components"
  524. " starting in ",
  525. majorStr, '.'));
  526. return false;
  527. }
  528. }
  529. std::string vsInstance;
  530. if (!i.empty()) {
  531. vsInstance = i;
  532. if (!this->vsSetupAPIHelper.SetVSInstance(
  533. this->GeneratorInstance, this->GeneratorInstanceVersion)) {
  534. std::ostringstream e;
  535. /* clang-format off */
  536. e <<
  537. "Generator\n"
  538. " " << this->GetName() << "\n"
  539. "could not find specified instance of Visual Studio:\n"
  540. " " << i;
  541. /* clang-format on */
  542. if (!this->GeneratorInstance.empty() &&
  543. this->GeneratorInstanceVersion.empty() &&
  544. cmSystemTools::FileIsDirectory(this->GeneratorInstance)) {
  545. e << "\n"
  546. "The directory exists, but the instance is not known to the "
  547. "Visual Studio Installer, and no 'version=' field was given.";
  548. }
  549. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  550. return false;
  551. }
  552. } else if (!this->vsSetupAPIHelper.GetVSInstanceInfo(vsInstance)) {
  553. mf->IssueMessage(
  554. MessageType::FATAL_ERROR,
  555. cmStrCat("Generator\n"
  556. " ",
  557. this->GetName(),
  558. "\n"
  559. "could not find any instance of Visual Studio.\n"));
  560. return false;
  561. }
  562. // Save the selected instance persistently.
  563. std::string genInstance = mf->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
  564. if (vsInstance != genInstance) {
  565. this->CMakeInstance->AddCacheEntry("CMAKE_GENERATOR_INSTANCE", vsInstance,
  566. "Generator instance identifier.",
  567. cmStateEnums::INTERNAL);
  568. }
  569. this->SetVSVersionVar(mf);
  570. // The selected instance may have a different MSBuild than previously found.
  571. this->MSBuildCommandInitialized = false;
  572. this->LastGeneratorInstanceString = i;
  573. return true;
  574. }
  575. bool cmGlobalVisualStudioVersionedGenerator::ParseGeneratorInstance(
  576. std::string const& is, cmMakefile* mf)
  577. {
  578. this->GeneratorInstance.clear();
  579. this->GeneratorInstanceVersion.clear();
  580. std::vector<std::string> const fields =
  581. cmTokenize(is, ',', cmTokenizerMode::New);
  582. if (fields.empty()) {
  583. return true;
  584. }
  585. auto fi = fields.begin();
  586. // The first field may be the VS instance.
  587. if (fi->find('=') == fi->npos) {
  588. this->GeneratorInstance = *fi;
  589. ++fi;
  590. }
  591. std::set<std::string> handled;
  592. // The rest of the fields must be key=value pairs.
  593. for (; fi != fields.end(); ++fi) {
  594. std::string::size_type pos = fi->find('=');
  595. if (pos == fi->npos) {
  596. mf->IssueMessage(
  597. MessageType::FATAL_ERROR,
  598. cmStrCat("Generator\n"
  599. " ",
  600. this->GetName(),
  601. "\n"
  602. "given instance specification\n"
  603. " ",
  604. is,
  605. "\n"
  606. "that contains a field after the first ',' with no '='."));
  607. return false;
  608. }
  609. std::string const key = fi->substr(0, pos);
  610. std::string const value = fi->substr(pos + 1);
  611. if (!handled.insert(key).second) {
  612. mf->IssueMessage(MessageType::FATAL_ERROR,
  613. cmStrCat("Generator\n"
  614. " ",
  615. this->GetName(),
  616. "\n"
  617. "given instance specification\n"
  618. " ",
  619. is,
  620. "\n"
  621. "that contains duplicate field key '",
  622. key, "'."));
  623. return false;
  624. }
  625. if (!this->ProcessGeneratorInstanceField(key, value)) {
  626. mf->IssueMessage(MessageType::FATAL_ERROR,
  627. cmStrCat("Generator\n"
  628. " ",
  629. this->GetName(),
  630. "\n"
  631. "given instance specification\n"
  632. " ",
  633. is,
  634. "\n"
  635. "that contains invalid field '",
  636. *fi, "'."));
  637. return false;
  638. }
  639. }
  640. return true;
  641. }
  642. void cmGlobalVisualStudioVersionedGenerator::SetVSVersionVar(cmMakefile* mf)
  643. {
  644. if (cm::optional<std::string> vsVer = this->GetVSInstanceVersion()) {
  645. mf->AddDefinition("CMAKE_VS_VERSION_BUILD_NUMBER", *vsVer);
  646. }
  647. }
  648. bool cmGlobalVisualStudioVersionedGenerator::ProcessGeneratorInstanceField(
  649. std::string const& key, std::string const& value)
  650. {
  651. if (key == "version"_s) {
  652. this->GeneratorInstanceVersion = value;
  653. return true;
  654. }
  655. return false;
  656. }
  657. bool cmGlobalVisualStudioVersionedGenerator::GetVSInstance(
  658. std::string& dir) const
  659. {
  660. return vsSetupAPIHelper.GetVSInstanceInfo(dir);
  661. }
  662. cm::optional<std::string>
  663. cmGlobalVisualStudioVersionedGenerator::GetVSInstanceVersion() const
  664. {
  665. cm::optional<std::string> result;
  666. std::string vsInstanceVersion;
  667. if (vsSetupAPIHelper.GetVSInstanceVersion(vsInstanceVersion)) {
  668. result = vsInstanceVersion;
  669. }
  670. return result;
  671. }
  672. bool cmGlobalVisualStudioVersionedGenerator::IsStdOutEncodingSupported() const
  673. {
  674. // Supported from Visual Studio 16.7 Preview 3.
  675. if (this->Version > cmGlobalVisualStudioGenerator::VSVersion::VS16) {
  676. return true;
  677. }
  678. if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS16) {
  679. return false;
  680. }
  681. static std::string const vsVer16_7_P2 = "16.7.30128.36";
  682. cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
  683. return (vsVer &&
  684. cmSystemTools::VersionCompareGreaterEq(*vsVer, vsVer16_7_P2));
  685. }
  686. bool cmGlobalVisualStudioVersionedGenerator::IsUtf8EncodingSupported() const
  687. {
  688. // Supported from Visual Studio 16.10 Preview 2.
  689. if (this->Version > cmGlobalVisualStudioGenerator::VSVersion::VS16) {
  690. return true;
  691. }
  692. if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS16) {
  693. return false;
  694. }
  695. static std::string const vsVer16_10_P2 = "16.10.31213.239";
  696. cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
  697. return (vsVer &&
  698. cmSystemTools::VersionCompareGreaterEq(*vsVer, vsVer16_10_P2));
  699. }
  700. bool cmGlobalVisualStudioVersionedGenerator::IsScanDependenciesSupported()
  701. const
  702. {
  703. // Supported from Visual Studio 17.6 Preview 7.
  704. if (this->Version > cmGlobalVisualStudioGenerator::VSVersion::VS17) {
  705. return true;
  706. }
  707. if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS17) {
  708. return false;
  709. }
  710. static std::string const vsVer17_6_P7 = "17.6.33706.43";
  711. cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
  712. return (vsVer &&
  713. cmSystemTools::VersionCompareGreaterEq(*vsVer, vsVer17_6_P7));
  714. }
  715. char const*
  716. cmGlobalVisualStudioVersionedGenerator::GetAndroidApplicationTypeRevision()
  717. const
  718. {
  719. switch (this->Version) {
  720. case cmGlobalVisualStudioGenerator::VSVersion::VS14:
  721. return "2.0";
  722. case cmGlobalVisualStudioGenerator::VSVersion::VS15:
  723. case cmGlobalVisualStudioGenerator::VSVersion::VS16:
  724. case cmGlobalVisualStudioGenerator::VSVersion::VS17:
  725. case cmGlobalVisualStudioGenerator::VSVersion::VS18:
  726. return "3.0";
  727. }
  728. return "";
  729. }
  730. cmGlobalVisualStudioVersionedGenerator::AuxToolset
  731. cmGlobalVisualStudioVersionedGenerator::FindAuxToolset(
  732. std::string& version, std::string& props) const
  733. {
  734. if (version.empty()) {
  735. return AuxToolset::None;
  736. }
  737. std::string instancePath;
  738. this->GetVSInstance(instancePath);
  739. cmSystemTools::ConvertToUnixSlashes(instancePath);
  740. // Translate three-component format accepted by "vcvarsall -vcvars_ver=".
  741. cmsys::RegularExpression threeComponentRegex(
  742. "^([0-9]+\\.[0-9]+)\\.[0-9][0-9][0-9][0-9][0-9]$");
  743. // The two-component format represents the two major components of the
  744. // three-component format
  745. cmsys::RegularExpression twoComponentRegex("^([0-9]+\\.[0-9]+)$");
  746. if (threeComponentRegex.find(version)) {
  747. // Load "VC/Auxiliary/Build/*/Microsoft.VCToolsVersion.*.txt" files
  748. // with two matching components to check their three-component version.
  749. std::string const& twoComponent = threeComponentRegex.match(1);
  750. std::string pattern =
  751. cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s, twoComponent,
  752. "*/Microsoft.VCToolsVersion."_s, twoComponent, "*.txt"_s);
  753. cmsys::Glob glob;
  754. glob.SetRecurseThroughSymlinks(false);
  755. if (glob.FindFiles(pattern)) {
  756. for (std::string const& txt : glob.GetFiles()) {
  757. std::string ver;
  758. cmsys::ifstream fin(txt.c_str());
  759. if (fin && std::getline(fin, ver)) {
  760. // Strip trailing whitespace.
  761. ver = ver.substr(0, ver.find_first_not_of("0123456789."));
  762. // If the three-component version matches, translate it to
  763. // that used by the "Microsoft.VCToolsVersion.*.txt" file name.
  764. if (ver == version) {
  765. cmsys::RegularExpression extractVersion(
  766. "VCToolsVersion\\.([0-9.]+)\\.txt$");
  767. if (extractVersion.find(txt)) {
  768. version = extractVersion.match(1);
  769. break;
  770. }
  771. }
  772. }
  773. }
  774. }
  775. } else if (twoComponentRegex.find(version)) {
  776. std::string const& twoComponent = twoComponentRegex.match(1);
  777. std::string pattern =
  778. cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s, twoComponent,
  779. "*/Microsoft.VCToolsVersion."_s, twoComponent, "*.txt"_s);
  780. cmsys::Glob glob;
  781. glob.SetRecurseThroughSymlinks(false);
  782. if (glob.FindFiles(pattern) && !glob.GetFiles().empty()) {
  783. // Since we are only using the first two components of the
  784. // toolset version, we require a single match.
  785. if (glob.GetFiles().size() == 1) {
  786. std::string const& txt = glob.GetFiles()[0];
  787. std::string ver;
  788. cmsys::ifstream fin(txt.c_str());
  789. if (fin && std::getline(fin, ver)) {
  790. // Strip trailing whitespace.
  791. ver = ver.substr(0, ver.find_first_not_of("0123456789."));
  792. // We assume the version is correct, since it is the only one that
  793. // matched.
  794. cmsys::RegularExpression extractVersion(
  795. "VCToolsVersion\\.([0-9.]+)\\.txt$");
  796. if (extractVersion.find(txt)) {
  797. version = extractVersion.match(1);
  798. }
  799. }
  800. } else {
  801. props = cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s);
  802. return AuxToolset::PropsIndeterminate;
  803. }
  804. }
  805. }
  806. if (cmSystemTools::VersionCompareGreaterEq(version, "14.20")) {
  807. props = cmStrCat(instancePath, "/VC/Auxiliary/Build."_s, version,
  808. "/Microsoft.VCToolsVersion."_s, version, ".props"_s);
  809. if (cmSystemTools::PathExists(props)) {
  810. return AuxToolset::PropsExist;
  811. }
  812. }
  813. props = cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s, version,
  814. "/Microsoft.VCToolsVersion."_s, version, ".props"_s);
  815. if (cmSystemTools::PathExists(props)) {
  816. return AuxToolset::PropsExist;
  817. }
  818. // Accept the toolset version that is default in the current VS version
  819. // by matching the name later VS versions will use for the SxS props files.
  820. std::string vcToolsetVersion;
  821. if (this->vsSetupAPIHelper.GetVCToolsetVersion(vcToolsetVersion)) {
  822. // Accept an exact-match (three-component version).
  823. if (version == vcToolsetVersion) {
  824. return AuxToolset::Default;
  825. }
  826. // Accept known SxS props file names using four version components
  827. // in VS versions later than the current.
  828. if (version == "14.28.16.9"_s && vcToolsetVersion == "14.28.29910"_s) {
  829. return AuxToolset::Default;
  830. }
  831. if (version == "14.29.16.10"_s && vcToolsetVersion == "14.29.30037"_s) {
  832. return AuxToolset::Default;
  833. }
  834. if (version == "14.29.16.11"_s && vcToolsetVersion == "14.29.30133"_s) {
  835. return AuxToolset::Default;
  836. }
  837. // The first two components of the default toolset version typically
  838. // match the name used by later VS versions for the SxS props files.
  839. cmsys::RegularExpression twoComponent("^([0-9]+\\.[0-9]+)");
  840. if (twoComponent.find(version)) {
  841. std::string const versionPrefix = cmStrCat(twoComponent.match(1), '.');
  842. if (cmHasPrefix(vcToolsetVersion, versionPrefix)) {
  843. return AuxToolset::Default;
  844. }
  845. }
  846. }
  847. return AuxToolset::PropsMissing;
  848. }
  849. bool cmGlobalVisualStudioVersionedGenerator::SelectWindowsStoreToolset(
  850. std::string& toolset) const
  851. {
  852. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  853. if (this->IsWindowsStoreToolsetInstalled() &&
  854. this->IsWindowsDesktopToolsetInstalled()) {
  855. toolset = VSVersionToToolset(this->Version);
  856. return true;
  857. }
  858. return false;
  859. }
  860. return this->cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
  861. toolset);
  862. }
  863. bool cmGlobalVisualStudioVersionedGenerator::IsWindowsDesktopToolsetInstalled()
  864. const
  865. {
  866. return vsSetupAPIHelper.IsVSInstalled();
  867. }
  868. bool cmGlobalVisualStudioVersionedGenerator::IsWindowsStoreToolsetInstalled()
  869. const
  870. {
  871. return vsSetupAPIHelper.IsWin10SDKInstalled();
  872. }
  873. bool cmGlobalVisualStudioVersionedGenerator::IsWin81SDKInstalled() const
  874. {
  875. // Does the VS installer tool know about one?
  876. if (vsSetupAPIHelper.IsWin81SDKInstalled()) {
  877. return true;
  878. }
  879. // Does the registry know about one (e.g. from VS 2015)?
  880. std::string win81Root;
  881. if (cmSystemTools::ReadRegistryValue(
  882. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  883. "Windows Kits\\Installed Roots;KitsRoot81",
  884. win81Root, cmSystemTools::KeyWOW64_32) ||
  885. cmSystemTools::ReadRegistryValue(
  886. "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
  887. "Windows Kits\\Installed Roots;KitsRoot81",
  888. win81Root, cmSystemTools::KeyWOW64_32)) {
  889. return cmSystemTools::FileExists(
  890. cmStrCat(win81Root, "/include/um/windows.h"), true);
  891. }
  892. return false;
  893. }
  894. std::string
  895. cmGlobalVisualStudioVersionedGenerator::GetWindows10SDKMaxVersionDefault(
  896. cmMakefile*) const
  897. {
  898. return std::string();
  899. }
  900. cm::optional<std::string>
  901. cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommandEarly(cmMakefile* mf)
  902. {
  903. std::string instance = mf->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
  904. if (!this->SetGeneratorInstance(instance, mf)) {
  905. cmSystemTools::SetFatalErrorOccurred();
  906. return {};
  907. }
  908. return this->cmGlobalVisualStudio14Generator::FindMSBuildCommandEarly(mf);
  909. }
  910. std::string cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommand()
  911. {
  912. std::string msbuild;
  913. // Ask Visual Studio Installer tool.
  914. std::string vs;
  915. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  916. if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS17) {
  917. if (VSIsArm64Host()) {
  918. if (VSHasDotNETFrameworkArm64()) {
  919. msbuild = cmStrCat(vs, "/MSBuild/Current/Bin/arm64/MSBuild.exe");
  920. if (cmSystemTools::FileExists(msbuild)) {
  921. return msbuild;
  922. }
  923. }
  924. if (VSIsWindows11OrGreater()) {
  925. msbuild = cmStrCat(vs, "/MSBuild/Current/Bin/amd64/MSBuild.exe");
  926. if (cmSystemTools::FileExists(msbuild)) {
  927. return msbuild;
  928. }
  929. }
  930. } else {
  931. msbuild = cmStrCat(vs, "/MSBuild/Current/Bin/amd64/MSBuild.exe");
  932. if (cmSystemTools::FileExists(msbuild)) {
  933. return msbuild;
  934. }
  935. }
  936. }
  937. msbuild = cmStrCat(vs, "/MSBuild/Current/Bin/MSBuild.exe");
  938. if (cmSystemTools::FileExists(msbuild)) {
  939. return msbuild;
  940. }
  941. msbuild = cmStrCat(vs, "/MSBuild/15.0/Bin/MSBuild.exe");
  942. if (cmSystemTools::FileExists(msbuild)) {
  943. return msbuild;
  944. }
  945. }
  946. msbuild = "MSBuild.exe";
  947. return msbuild;
  948. }
  949. std::string cmGlobalVisualStudioVersionedGenerator::FindDevEnvCommand()
  950. {
  951. std::string devenv;
  952. // Ask Visual Studio Installer tool.
  953. std::string vs;
  954. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  955. devenv = cmStrCat(vs, "/Common7/IDE/devenv.com");
  956. if (cmSystemTools::FileExists(devenv)) {
  957. return devenv;
  958. }
  959. }
  960. devenv = "devenv.com";
  961. return devenv;
  962. }