cmGlobalVisualStudioVersionedGenerator.cxx 31 KB

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