cmGlobalVisualStudioVersionedGenerator.cxx 32 KB

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