cmGlobalVisualStudioVersionedGenerator.cxx 31 KB

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