cmGlobalVisualStudioVersionedGenerator.cxx 32 KB

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