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