cmGlobalVisualStudioVersionedGenerator.cxx 29 KB

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