cmGlobalVisualStudioVersionedGenerator.cxx 32 KB

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