cmGlobalVisualStudioVersionedGenerator.cxx 31 KB

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