cmGlobalVisualStudioVersionedGenerator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 <cmext/string_view>
  5. #include "cmAlgorithms.h"
  6. #include "cmDocumentationEntry.h"
  7. #include "cmLocalVisualStudio10Generator.h"
  8. #include "cmMakefile.h"
  9. #include "cmStringAlgorithms.h"
  10. #include "cmVSSetupHelper.h"
  11. #include "cmake.h"
  12. #if defined(_M_ARM64)
  13. # define HOST_PLATFORM_NAME "ARM64"
  14. # define HOST_TOOLS_ARCH ""
  15. #elif defined(_M_ARM)
  16. # define HOST_PLATFORM_NAME "ARM"
  17. # define HOST_TOOLS_ARCH ""
  18. #elif defined(_M_IA64)
  19. # define HOST_PLATFORM_NAME "Itanium"
  20. # define HOST_TOOLS_ARCH ""
  21. #elif defined(_WIN64)
  22. # define HOST_PLATFORM_NAME "x64"
  23. # define HOST_TOOLS_ARCH "x64"
  24. #else
  25. static bool VSIsWow64()
  26. {
  27. BOOL isWow64 = false;
  28. return IsWow64Process(GetCurrentProcess(), &isWow64) && isWow64;
  29. }
  30. #endif
  31. static std::string VSHostPlatformName()
  32. {
  33. #ifdef HOST_PLATFORM_NAME
  34. return HOST_PLATFORM_NAME;
  35. #else
  36. if (VSIsWow64()) {
  37. return "x64";
  38. } else {
  39. return "Win32";
  40. }
  41. #endif
  42. }
  43. static std::string VSHostArchitecture()
  44. {
  45. #ifdef HOST_TOOLS_ARCH
  46. return HOST_TOOLS_ARCH;
  47. #else
  48. if (VSIsWow64()) {
  49. return "x64";
  50. } else {
  51. return "x86";
  52. }
  53. #endif
  54. }
  55. static unsigned int VSVersionToMajor(
  56. cmGlobalVisualStudioGenerator::VSVersion v)
  57. {
  58. switch (v) {
  59. case cmGlobalVisualStudioGenerator::VS9:
  60. return 9;
  61. case cmGlobalVisualStudioGenerator::VS10:
  62. return 10;
  63. case cmGlobalVisualStudioGenerator::VS11:
  64. return 11;
  65. case cmGlobalVisualStudioGenerator::VS12:
  66. return 12;
  67. case cmGlobalVisualStudioGenerator::VS14:
  68. return 14;
  69. case cmGlobalVisualStudioGenerator::VS15:
  70. return 15;
  71. case cmGlobalVisualStudioGenerator::VS16:
  72. return 16;
  73. }
  74. return 0;
  75. }
  76. static const char* VSVersionToToolset(
  77. cmGlobalVisualStudioGenerator::VSVersion v)
  78. {
  79. switch (v) {
  80. case cmGlobalVisualStudioGenerator::VS9:
  81. return "v90";
  82. case cmGlobalVisualStudioGenerator::VS10:
  83. return "v100";
  84. case cmGlobalVisualStudioGenerator::VS11:
  85. return "v110";
  86. case cmGlobalVisualStudioGenerator::VS12:
  87. return "v120";
  88. case cmGlobalVisualStudioGenerator::VS14:
  89. return "v140";
  90. case cmGlobalVisualStudioGenerator::VS15:
  91. return "v141";
  92. case cmGlobalVisualStudioGenerator::VS16:
  93. return "v142";
  94. }
  95. return "";
  96. }
  97. static const char* VSVersionToAndroidToolset(
  98. cmGlobalVisualStudioGenerator::VSVersion v)
  99. {
  100. switch (v) {
  101. case cmGlobalVisualStudioGenerator::VS9:
  102. case cmGlobalVisualStudioGenerator::VS10:
  103. case cmGlobalVisualStudioGenerator::VS11:
  104. case cmGlobalVisualStudioGenerator::VS12:
  105. return "";
  106. case cmGlobalVisualStudioGenerator::VS14:
  107. return "Clang_3_8";
  108. case cmGlobalVisualStudioGenerator::VS15:
  109. case cmGlobalVisualStudioGenerator::VS16:
  110. return "Clang_5_0";
  111. }
  112. return "";
  113. }
  114. static const char vs15generatorName[] = "Visual Studio 15 2017";
  115. // Map generator name without year to name with year.
  116. static const char* cmVS15GenName(const std::string& name, std::string& genName)
  117. {
  118. if (strncmp(name.c_str(), vs15generatorName,
  119. sizeof(vs15generatorName) - 6) != 0) {
  120. return 0;
  121. }
  122. const char* p = name.c_str() + sizeof(vs15generatorName) - 6;
  123. if (cmHasLiteralPrefix(p, " 2017")) {
  124. p += 5;
  125. }
  126. genName = std::string(vs15generatorName) + p;
  127. return p;
  128. }
  129. class cmGlobalVisualStudioVersionedGenerator::Factory15
  130. : public cmGlobalGeneratorFactory
  131. {
  132. public:
  133. std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
  134. const std::string& name, bool allowArch, cmake* cm) const override
  135. {
  136. std::string genName;
  137. const char* p = cmVS15GenName(name, genName);
  138. if (!p) {
  139. return std::unique_ptr<cmGlobalGenerator>();
  140. }
  141. if (!*p) {
  142. return std::unique_ptr<cmGlobalGenerator>(
  143. new cmGlobalVisualStudioVersionedGenerator(
  144. cmGlobalVisualStudioGenerator::VS15, cm, genName, ""));
  145. }
  146. if (!allowArch || *p++ != ' ') {
  147. return std::unique_ptr<cmGlobalGenerator>();
  148. }
  149. if (strcmp(p, "Win64") == 0) {
  150. return std::unique_ptr<cmGlobalGenerator>(
  151. new cmGlobalVisualStudioVersionedGenerator(
  152. cmGlobalVisualStudioGenerator::VS15, cm, genName, "x64"));
  153. }
  154. if (strcmp(p, "ARM") == 0) {
  155. return std::unique_ptr<cmGlobalGenerator>(
  156. new cmGlobalVisualStudioVersionedGenerator(
  157. cmGlobalVisualStudioGenerator::VS15, cm, genName, "ARM"));
  158. }
  159. return std::unique_ptr<cmGlobalGenerator>();
  160. }
  161. void GetDocumentation(cmDocumentationEntry& entry) const override
  162. {
  163. entry.Name = std::string(vs15generatorName) + " [arch]";
  164. entry.Brief = "Generates Visual Studio 2017 project files. "
  165. "Optional [arch] can be \"Win64\" or \"ARM\".";
  166. }
  167. std::vector<std::string> GetGeneratorNames() const override
  168. {
  169. std::vector<std::string> names;
  170. names.push_back(vs15generatorName);
  171. return names;
  172. }
  173. std::vector<std::string> GetGeneratorNamesWithPlatform() const override
  174. {
  175. std::vector<std::string> names;
  176. names.push_back(vs15generatorName + std::string(" ARM"));
  177. names.push_back(vs15generatorName + std::string(" Win64"));
  178. return names;
  179. }
  180. bool SupportsToolset() const override { return true; }
  181. bool SupportsPlatform() const override { return true; }
  182. std::vector<std::string> GetKnownPlatforms() const override
  183. {
  184. std::vector<std::string> platforms;
  185. platforms.emplace_back("x64");
  186. platforms.emplace_back("Win32");
  187. platforms.emplace_back("ARM");
  188. platforms.emplace_back("ARM64");
  189. return platforms;
  190. }
  191. std::string GetDefaultPlatformName() const override { return "Win32"; }
  192. };
  193. std::unique_ptr<cmGlobalGeneratorFactory>
  194. cmGlobalVisualStudioVersionedGenerator::NewFactory15()
  195. {
  196. return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory15);
  197. }
  198. static const char vs16generatorName[] = "Visual Studio 16 2019";
  199. // Map generator name without year to name with year.
  200. static const char* cmVS16GenName(const std::string& name, std::string& genName)
  201. {
  202. if (strncmp(name.c_str(), vs16generatorName,
  203. sizeof(vs16generatorName) - 6) != 0) {
  204. return 0;
  205. }
  206. const char* p = name.c_str() + sizeof(vs16generatorName) - 6;
  207. if (cmHasLiteralPrefix(p, " 2019")) {
  208. p += 5;
  209. }
  210. genName = std::string(vs16generatorName) + p;
  211. return p;
  212. }
  213. class cmGlobalVisualStudioVersionedGenerator::Factory16
  214. : public cmGlobalGeneratorFactory
  215. {
  216. public:
  217. std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
  218. const std::string& name, bool /*allowArch*/, cmake* cm) const override
  219. {
  220. std::string genName;
  221. const char* p = cmVS16GenName(name, genName);
  222. if (!p) {
  223. return std::unique_ptr<cmGlobalGenerator>();
  224. }
  225. if (!*p) {
  226. return std::unique_ptr<cmGlobalGenerator>(
  227. new cmGlobalVisualStudioVersionedGenerator(
  228. cmGlobalVisualStudioGenerator::VS16, cm, genName, ""));
  229. }
  230. return std::unique_ptr<cmGlobalGenerator>();
  231. }
  232. void GetDocumentation(cmDocumentationEntry& entry) const override
  233. {
  234. entry.Name = std::string(vs16generatorName);
  235. entry.Brief = "Generates Visual Studio 2019 project files. "
  236. "Use -A option to specify architecture.";
  237. }
  238. std::vector<std::string> GetGeneratorNames() const override
  239. {
  240. std::vector<std::string> names;
  241. names.push_back(vs16generatorName);
  242. return names;
  243. }
  244. std::vector<std::string> GetGeneratorNamesWithPlatform() const override
  245. {
  246. return std::vector<std::string>();
  247. }
  248. bool SupportsToolset() const override { return true; }
  249. bool SupportsPlatform() const override { return true; }
  250. std::vector<std::string> GetKnownPlatforms() const override
  251. {
  252. std::vector<std::string> platforms;
  253. platforms.emplace_back("x64");
  254. platforms.emplace_back("Win32");
  255. platforms.emplace_back("ARM");
  256. platforms.emplace_back("ARM64");
  257. return platforms;
  258. }
  259. std::string GetDefaultPlatformName() const override
  260. {
  261. return VSHostPlatformName();
  262. }
  263. };
  264. std::unique_ptr<cmGlobalGeneratorFactory>
  265. cmGlobalVisualStudioVersionedGenerator::NewFactory16()
  266. {
  267. return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory16);
  268. }
  269. cmGlobalVisualStudioVersionedGenerator::cmGlobalVisualStudioVersionedGenerator(
  270. VSVersion version, cmake* cm, const std::string& name,
  271. std::string const& platformInGeneratorName)
  272. : cmGlobalVisualStudio14Generator(cm, name, platformInGeneratorName)
  273. , vsSetupAPIHelper(VSVersionToMajor(version))
  274. {
  275. this->Version = version;
  276. this->ExpressEdition = false;
  277. this->DefaultPlatformToolset = VSVersionToToolset(this->Version);
  278. this->DefaultAndroidToolset = VSVersionToAndroidToolset(this->Version);
  279. this->DefaultCLFlagTableName = VSVersionToToolset(this->Version);
  280. this->DefaultCSharpFlagTableName = VSVersionToToolset(this->Version);
  281. this->DefaultLinkFlagTableName = VSVersionToToolset(this->Version);
  282. if (this->Version >= cmGlobalVisualStudioGenerator::VS16) {
  283. this->DefaultPlatformName = VSHostPlatformName();
  284. this->DefaultPlatformToolsetHostArchitecture = VSHostArchitecture();
  285. }
  286. }
  287. bool cmGlobalVisualStudioVersionedGenerator::MatchesGeneratorName(
  288. const std::string& name) const
  289. {
  290. std::string genName;
  291. switch (this->Version) {
  292. case cmGlobalVisualStudioGenerator::VS9:
  293. case cmGlobalVisualStudioGenerator::VS10:
  294. case cmGlobalVisualStudioGenerator::VS11:
  295. case cmGlobalVisualStudioGenerator::VS12:
  296. case cmGlobalVisualStudioGenerator::VS14:
  297. break;
  298. case cmGlobalVisualStudioGenerator::VS15:
  299. if (cmVS15GenName(name, genName)) {
  300. return genName == this->GetName();
  301. }
  302. break;
  303. case cmGlobalVisualStudioGenerator::VS16:
  304. if (cmVS16GenName(name, genName)) {
  305. return genName == this->GetName();
  306. }
  307. break;
  308. }
  309. return false;
  310. }
  311. bool cmGlobalVisualStudioVersionedGenerator::SetGeneratorInstance(
  312. std::string const& i, cmMakefile* mf)
  313. {
  314. if (!i.empty()) {
  315. if (!this->vsSetupAPIHelper.SetVSInstance(i)) {
  316. std::ostringstream e;
  317. /* clang-format off */
  318. e <<
  319. "Generator\n"
  320. " " << this->GetName() << "\n"
  321. "could not find specified instance of Visual Studio:\n"
  322. " " << i;
  323. /* clang-format on */
  324. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  325. return false;
  326. }
  327. }
  328. std::string vsInstance;
  329. if (!this->vsSetupAPIHelper.GetVSInstanceInfo(vsInstance)) {
  330. std::ostringstream e;
  331. /* clang-format off */
  332. e <<
  333. "Generator\n"
  334. " " << this->GetName() << "\n"
  335. "could not find any instance of Visual Studio.\n";
  336. /* clang-format on */
  337. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  338. return false;
  339. }
  340. // Save the selected instance persistently.
  341. std::string genInstance = mf->GetSafeDefinition("CMAKE_GENERATOR_INSTANCE");
  342. if (vsInstance != genInstance) {
  343. this->CMakeInstance->AddCacheEntry(
  344. "CMAKE_GENERATOR_INSTANCE", vsInstance.c_str(),
  345. "Generator instance identifier.", cmStateEnums::INTERNAL);
  346. }
  347. return true;
  348. }
  349. bool cmGlobalVisualStudioVersionedGenerator::GetVSInstance(
  350. std::string& dir) const
  351. {
  352. return vsSetupAPIHelper.GetVSInstanceInfo(dir);
  353. }
  354. bool cmGlobalVisualStudioVersionedGenerator::GetVSInstanceVersion(
  355. unsigned long long& vsInstanceVersion) const
  356. {
  357. return vsSetupAPIHelper.GetVSInstanceVersion(vsInstanceVersion);
  358. }
  359. bool cmGlobalVisualStudioVersionedGenerator::IsStdOutEncodingSupported() const
  360. {
  361. // Supported from Visual Studio 16.7 Preview 3.
  362. if (this->Version > cmGlobalVisualStudioGenerator::VSVersion::VS16) {
  363. return true;
  364. }
  365. if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS16) {
  366. return false;
  367. }
  368. unsigned long long const vsInstanceVersion16_7_P2 = 4503631666610212;
  369. unsigned long long vsInstanceVersion;
  370. return (this->GetVSInstanceVersion(vsInstanceVersion) &&
  371. vsInstanceVersion > vsInstanceVersion16_7_P2);
  372. }
  373. const char*
  374. cmGlobalVisualStudioVersionedGenerator::GetAndroidApplicationTypeRevision()
  375. const
  376. {
  377. switch (this->Version) {
  378. case cmGlobalVisualStudioGenerator::VS9:
  379. case cmGlobalVisualStudioGenerator::VS10:
  380. case cmGlobalVisualStudioGenerator::VS11:
  381. case cmGlobalVisualStudioGenerator::VS12:
  382. return "";
  383. case cmGlobalVisualStudioGenerator::VS14:
  384. return "2.0";
  385. case cmGlobalVisualStudioGenerator::VS15:
  386. case cmGlobalVisualStudioGenerator::VS16:
  387. return "3.0";
  388. }
  389. return "";
  390. }
  391. cmGlobalVisualStudioVersionedGenerator::AuxToolset
  392. cmGlobalVisualStudioVersionedGenerator::FindAuxToolset(
  393. std::string& version, std::string& props) const
  394. {
  395. if (version.empty()) {
  396. return AuxToolset::None;
  397. }
  398. std::string instancePath;
  399. this->GetVSInstance(instancePath);
  400. cmSystemTools::ConvertToUnixSlashes(instancePath);
  401. if (cmSystemTools::VersionCompareGreaterEq(version, "14.20")) {
  402. props = cmStrCat(instancePath, "/VC/Auxiliary/Build."_s, version,
  403. "/Microsoft.VCToolsVersion."_s, version, ".props"_s);
  404. if (cmSystemTools::PathExists(props)) {
  405. return AuxToolset::PropsExist;
  406. }
  407. }
  408. props = cmStrCat(instancePath, "/VC/Auxiliary/Build/"_s, version,
  409. "/Microsoft.VCToolsVersion."_s, version, ".props"_s);
  410. if (cmSystemTools::PathExists(props)) {
  411. return AuxToolset::PropsExist;
  412. }
  413. // Accept the toolset version that is default in the current VS version
  414. // by matching the name later VS versions will use for the SxS props files.
  415. std::string vcToolsetVersion;
  416. if (this->vsSetupAPIHelper.GetVCToolsetVersion(vcToolsetVersion)) {
  417. // Accept an exact-match (three-component version).
  418. if (version == vcToolsetVersion) {
  419. return AuxToolset::Default;
  420. }
  421. // Accept known SxS props file names using four version components
  422. // in VS versions later than the current.
  423. if (version == "14.28.16.9" && vcToolsetVersion == "14.28.29910") {
  424. return AuxToolset::Default;
  425. }
  426. // The first two components of the default toolset version typically
  427. // match the name used by later VS versions for the SxS props files.
  428. cmsys::RegularExpression twoComponent("^([0-9]+\\.[0-9]+)");
  429. if (twoComponent.find(version)) {
  430. std::string const versionPrefix = cmStrCat(twoComponent.match(1), '.');
  431. if (cmHasPrefix(vcToolsetVersion, versionPrefix)) {
  432. return AuxToolset::Default;
  433. }
  434. }
  435. }
  436. return AuxToolset::PropsMissing;
  437. }
  438. bool cmGlobalVisualStudioVersionedGenerator::InitializeWindows(cmMakefile* mf)
  439. {
  440. // If the Win 8.1 SDK is installed then we can select a SDK matching
  441. // the target Windows version.
  442. if (this->IsWin81SDKInstalled()) {
  443. // VS 2019 does not default to 8.1 so specify it explicitly when needed.
  444. if (this->Version >= cmGlobalVisualStudioGenerator::VS16 &&
  445. !cmSystemTools::VersionCompareGreater(this->SystemVersion, "8.1")) {
  446. this->SetWindowsTargetPlatformVersion("8.1", mf);
  447. return true;
  448. }
  449. return cmGlobalVisualStudio14Generator::InitializeWindows(mf);
  450. }
  451. // Otherwise we must choose a Win 10 SDK even if we are not targeting
  452. // Windows 10.
  453. return this->SelectWindows10SDK(mf, false);
  454. }
  455. bool cmGlobalVisualStudioVersionedGenerator::SelectWindowsStoreToolset(
  456. std::string& toolset) const
  457. {
  458. if (cmHasLiteralPrefix(this->SystemVersion, "10.0")) {
  459. if (this->IsWindowsStoreToolsetInstalled() &&
  460. this->IsWindowsDesktopToolsetInstalled()) {
  461. toolset = VSVersionToToolset(this->Version);
  462. return true;
  463. } else {
  464. return false;
  465. }
  466. }
  467. return this->cmGlobalVisualStudio14Generator::SelectWindowsStoreToolset(
  468. toolset);
  469. }
  470. bool cmGlobalVisualStudioVersionedGenerator::IsWindowsDesktopToolsetInstalled()
  471. const
  472. {
  473. return vsSetupAPIHelper.IsVSInstalled();
  474. }
  475. bool cmGlobalVisualStudioVersionedGenerator::IsWindowsStoreToolsetInstalled()
  476. const
  477. {
  478. return vsSetupAPIHelper.IsWin10SDKInstalled();
  479. }
  480. bool cmGlobalVisualStudioVersionedGenerator::IsWin81SDKInstalled() const
  481. {
  482. // Does the VS installer tool know about one?
  483. if (vsSetupAPIHelper.IsWin81SDKInstalled()) {
  484. return true;
  485. }
  486. // Does the registry know about one (e.g. from VS 2015)?
  487. std::string win81Root;
  488. if (cmSystemTools::ReadRegistryValue(
  489. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
  490. "Windows Kits\\Installed Roots;KitsRoot81",
  491. win81Root, cmSystemTools::KeyWOW64_32) ||
  492. cmSystemTools::ReadRegistryValue(
  493. "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\"
  494. "Windows Kits\\Installed Roots;KitsRoot81",
  495. win81Root, cmSystemTools::KeyWOW64_32)) {
  496. return cmSystemTools::FileExists(win81Root + "/include/um/windows.h",
  497. true);
  498. }
  499. return false;
  500. }
  501. std::string
  502. cmGlobalVisualStudioVersionedGenerator::GetWindows10SDKMaxVersionDefault(
  503. cmMakefile*) const
  504. {
  505. return std::string();
  506. }
  507. std::string cmGlobalVisualStudioVersionedGenerator::FindMSBuildCommand()
  508. {
  509. std::string msbuild;
  510. // Ask Visual Studio Installer tool.
  511. std::string vs;
  512. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  513. msbuild = vs + "/MSBuild/Current/Bin/MSBuild.exe";
  514. if (cmSystemTools::FileExists(msbuild)) {
  515. return msbuild;
  516. }
  517. msbuild = vs + "/MSBuild/15.0/Bin/MSBuild.exe";
  518. if (cmSystemTools::FileExists(msbuild)) {
  519. return msbuild;
  520. }
  521. }
  522. msbuild = "MSBuild.exe";
  523. return msbuild;
  524. }
  525. std::string cmGlobalVisualStudioVersionedGenerator::FindDevEnvCommand()
  526. {
  527. std::string devenv;
  528. // Ask Visual Studio Installer tool.
  529. std::string vs;
  530. if (vsSetupAPIHelper.GetVSInstanceInfo(vs)) {
  531. devenv = vs + "/Common7/IDE/devenv.com";
  532. if (cmSystemTools::FileExists(devenv)) {
  533. return devenv;
  534. }
  535. }
  536. devenv = "devenv.com";
  537. return devenv;
  538. }