cmGlobalVisualStudioVersionedGenerator.cxx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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 = cmStrCat(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 { cmStrCat(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.emplace_back(cmStrCat(vs15generatorName, " ARM"));
  245. names.emplace_back(cmStrCat(vs15generatorName, " 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 = cmStrCat(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 = cmStrCat(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. mf->IssueMessage(
  478. MessageType::FATAL_ERROR,
  479. cmStrCat("Generator\n"
  480. " ",
  481. this->GetName(),
  482. "\n"
  483. "given instance specification\n"
  484. " ",
  485. i,
  486. "\n"
  487. "but the version field is not 4 integer components"
  488. " starting in ",
  489. majorStr, '.'));
  490. return false;
  491. }
  492. }
  493. std::string vsInstance;
  494. if (!i.empty()) {
  495. vsInstance = i;
  496. if (!this->vsSetupAPIHelper.SetVSInstance(
  497. this->GeneratorInstance, this->GeneratorInstanceVersion)) {
  498. std::ostringstream e;
  499. /* clang-format off */
  500. e <<
  501. "Generator\n"
  502. " " << this->GetName() << "\n"
  503. "could not find specified instance of Visual Studio:\n"
  504. " " << i;
  505. /* clang-format on */
  506. if (!this->GeneratorInstance.empty() &&
  507. this->GeneratorInstanceVersion.empty() &&
  508. cmSystemTools::FileIsDirectory(this->GeneratorInstance)) {
  509. e << "\n"
  510. "The directory exists, but the instance is not known to the "
  511. "Visual Studio Installer, and no 'version=' field was given.";
  512. }
  513. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  514. return false;
  515. }
  516. } else if (!this->vsSetupAPIHelper.GetVSInstanceInfo(vsInstance)) {
  517. mf->IssueMessage(
  518. MessageType::FATAL_ERROR,
  519. cmStrCat("Generator\n"
  520. " ",
  521. this->GetName(),
  522. "\n"
  523. "could not find any instance of Visual Studio.\n"));
  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. mf->IssueMessage(
  560. MessageType::FATAL_ERROR,
  561. cmStrCat("Generator\n"
  562. " ",
  563. this->GetName(),
  564. "\n"
  565. "given instance specification\n"
  566. " ",
  567. is,
  568. "\n"
  569. "that contains a field after the first ',' with no '='."));
  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. mf->IssueMessage(MessageType::FATAL_ERROR,
  576. cmStrCat("Generator\n"
  577. " ",
  578. this->GetName(),
  579. "\n"
  580. "given instance specification\n"
  581. " ",
  582. is,
  583. "\n"
  584. "that contains duplicate field key '",
  585. key, "'."));
  586. return false;
  587. }
  588. if (!this->ProcessGeneratorInstanceField(key, value)) {
  589. mf->IssueMessage(MessageType::FATAL_ERROR,
  590. cmStrCat("Generator\n"
  591. " ",
  592. this->GetName(),
  593. "\n"
  594. "given instance specification\n"
  595. " ",
  596. is,
  597. "\n"
  598. "that contains invalid field '",
  599. *fi, "'."));
  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"_s) {
  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"_s && vcToolsetVersion == "14.28.29910"_s) {
  794. return AuxToolset::Default;
  795. }
  796. if (version == "14.29.16.10"_s && vcToolsetVersion == "14.29.30037"_s) {
  797. return AuxToolset::Default;
  798. }
  799. if (version == "14.29.16.11"_s && vcToolsetVersion == "14.29.30133"_s) {
  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::SelectWindowsStoreToolset(
  815. std::string& toolset) const
  816. {
  817. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  818. if (this->IsWindowsStoreToolsetInstalled() &&
  819. this->IsWindowsDesktopToolsetInstalled()) {
  820. toolset = VSVersionToToolset(this->Version);
  821. return true;
  822. }
  823. return false;
  824. }
  825. return this->cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
  826. toolset);
  827. }
  828. bool cmGlobalVisualStudioVersionedGenerator::IsWindowsDesktopToolsetInstalled()
  829. const
  830. {
  831. return vsSetupAPIHelper.IsVSInstalled();
  832. }
  833. bool cmGlobalVisualStudioVersionedGenerator::IsWindowsStoreToolsetInstalled()
  834. const
  835. {
  836. return vsSetupAPIHelper.IsWin10SDKInstalled();
  837. }
  838. bool cmGlobalVisualStudioVersionedGenerator::IsWin81SDKInstalled() const
  839. {
  840. // Does the VS installer tool know about one?
  841. if (vsSetupAPIHelper.IsWin81SDKInstalled()) {
  842. return true;
  843. }
  844. // Does the registry know about one (e.g. from VS 2015)?
  845. std::string win81Root;
  846. if (cmSystemTools::ReadRegistryValue(
  847. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  848. "Windows Kits\\Installed Roots;KitsRoot81",
  849. win81Root, cmSystemTools::KeyWOW64_32) ||
  850. cmSystemTools::ReadRegistryValue(
  851. "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
  852. "Windows Kits\\Installed Roots;KitsRoot81",
  853. win81Root, cmSystemTools::KeyWOW64_32)) {
  854. return cmSystemTools::FileExists(
  855. cmStrCat(win81Root, "/include/um/windows.h"), true);
  856. }
  857. return false;
  858. }
  859. std::string
  860. cmGlobalVisualStudioVersionedGenerator::GetWindows10SDKMaxVersionDefault(
  861. cmMakefile*) const
  862. {
  863. return std::string();
  864. }
  865. cm::optional<std::string>
  866. cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommandEarly(cmMakefile* mf)
  867. {
  868. std::string instance = mf->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
  869. if (!this->SetGeneratorInstance(instance, mf)) {
  870. cmSystemTools::SetFatalErrorOccurred();
  871. return {};
  872. }
  873. return this->cmGlobalVisualStudio14Generator::FindMSBuildCommandEarly(mf);
  874. }
  875. std::string cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommand()
  876. {
  877. std::string msbuild;
  878. // Ask Visual Studio Installer tool.
  879. std::string vs;
  880. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  881. if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS17) {
  882. if (VSIsArm64Host()) {
  883. if (VSHasDotNETFrameworkArm64()) {
  884. msbuild = cmStrCat(vs, "/MSBuild/Current/Bin/arm64/MSBuild.exe");
  885. if (cmSystemTools::FileExists(msbuild)) {
  886. return msbuild;
  887. }
  888. }
  889. if (VSIsWindows11OrGreater()) {
  890. msbuild = cmStrCat(vs, "/MSBuild/Current/Bin/amd64/MSBuild.exe");
  891. if (cmSystemTools::FileExists(msbuild)) {
  892. return msbuild;
  893. }
  894. }
  895. } else {
  896. msbuild = cmStrCat(vs, "/MSBuild/Current/Bin/amd64/MSBuild.exe");
  897. if (cmSystemTools::FileExists(msbuild)) {
  898. return msbuild;
  899. }
  900. }
  901. }
  902. msbuild = cmStrCat(vs, "/MSBuild/Current/Bin/MSBuild.exe");
  903. if (cmSystemTools::FileExists(msbuild)) {
  904. return msbuild;
  905. }
  906. msbuild = cmStrCat(vs, "/MSBuild/15.0/Bin/MSBuild.exe");
  907. if (cmSystemTools::FileExists(msbuild)) {
  908. return msbuild;
  909. }
  910. }
  911. msbuild = "MSBuild.exe";
  912. return msbuild;
  913. }
  914. std::string cmGlobalVisualStudioVersionedGenerator::FindDevEnvCommand()
  915. {
  916. std::string devenv;
  917. // Ask Visual Studio Installer tool.
  918. std::string vs;
  919. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  920. devenv = cmStrCat(vs, "/Common7/IDE/devenv.com");
  921. if (cmSystemTools::FileExists(devenv)) {
  922. return devenv;
  923. }
  924. }
  925. devenv = "devenv.com";
  926. return devenv;
  927. }