cmGlobalVisualStudioGenerator.cxx 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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 "cmGlobalVisualStudioGenerator.h"
  4. #include <cassert>
  5. #include <future>
  6. #include <iostream>
  7. #include <sstream>
  8. #include <system_error>
  9. #include <utility>
  10. #include <cm/iterator>
  11. #include <cm/memory>
  12. #include <windows.h>
  13. #include <objbase.h>
  14. #include <shellapi.h>
  15. #include "cmCallVisualStudioMacro.h"
  16. #include "cmCustomCommand.h"
  17. #include "cmCustomCommandLines.h"
  18. #include "cmGeneratedFileStream.h"
  19. #include "cmGeneratorTarget.h"
  20. #include "cmLocalGenerator.h"
  21. #include "cmMakefile.h"
  22. #include "cmMessageType.h"
  23. #include "cmPolicies.h"
  24. #include "cmSourceFile.h"
  25. #include "cmState.h"
  26. #include "cmStateTypes.h"
  27. #include "cmStringAlgorithms.h"
  28. #include "cmSystemTools.h"
  29. #include "cmTarget.h"
  30. #include "cmake.h"
  31. cmGlobalVisualStudioGenerator::cmGlobalVisualStudioGenerator(
  32. cmake* cm, std::string const& platformInGeneratorName)
  33. : cmGlobalGenerator(cm)
  34. {
  35. cm->GetState()->SetIsGeneratorMultiConfig(true);
  36. cm->GetState()->SetWindowsShell(true);
  37. cm->GetState()->SetWindowsVSIDE(true);
  38. if (platformInGeneratorName.empty()) {
  39. this->DefaultPlatformName = "Win32";
  40. } else {
  41. this->DefaultPlatformName = platformInGeneratorName;
  42. this->PlatformInGeneratorName = true;
  43. }
  44. }
  45. cmGlobalVisualStudioGenerator::~cmGlobalVisualStudioGenerator()
  46. {
  47. }
  48. cmGlobalVisualStudioGenerator::VSVersion
  49. cmGlobalVisualStudioGenerator::GetVersion() const
  50. {
  51. return this->Version;
  52. }
  53. void cmGlobalVisualStudioGenerator::SetVersion(VSVersion v)
  54. {
  55. this->Version = v;
  56. }
  57. void cmGlobalVisualStudioGenerator::EnableLanguage(
  58. std::vector<std::string> const& lang, cmMakefile* mf, bool optional)
  59. {
  60. mf->AddDefinition("CMAKE_VS_PLATFORM_NAME_DEFAULT",
  61. this->DefaultPlatformName);
  62. this->cmGlobalGenerator::EnableLanguage(lang, mf, optional);
  63. }
  64. bool cmGlobalVisualStudioGenerator::SetGeneratorPlatform(std::string const& p,
  65. cmMakefile* mf)
  66. {
  67. if (this->GetPlatformName() == "x64") {
  68. mf->AddDefinition("CMAKE_FORCE_WIN64", "TRUE");
  69. } else if (this->GetPlatformName() == "Itanium") {
  70. mf->AddDefinition("CMAKE_FORCE_IA64", "TRUE");
  71. }
  72. mf->AddDefinition("CMAKE_VS_PLATFORM_NAME", this->GetPlatformName());
  73. return this->cmGlobalGenerator::SetGeneratorPlatform(p, mf);
  74. }
  75. std::string const& cmGlobalVisualStudioGenerator::GetPlatformName() const
  76. {
  77. if (!this->GeneratorPlatform.empty()) {
  78. return this->GeneratorPlatform;
  79. }
  80. return this->DefaultPlatformName;
  81. }
  82. const char* cmGlobalVisualStudioGenerator::GetIDEVersion() const
  83. {
  84. switch (this->Version) {
  85. case cmGlobalVisualStudioGenerator::VSVersion::VS9:
  86. return "9.0";
  87. case cmGlobalVisualStudioGenerator::VSVersion::VS10:
  88. return "10.0";
  89. case cmGlobalVisualStudioGenerator::VSVersion::VS11:
  90. return "11.0";
  91. case cmGlobalVisualStudioGenerator::VSVersion::VS12:
  92. return "12.0";
  93. case cmGlobalVisualStudioGenerator::VSVersion::VS14:
  94. return "14.0";
  95. case cmGlobalVisualStudioGenerator::VSVersion::VS15:
  96. return "15.0";
  97. case cmGlobalVisualStudioGenerator::VSVersion::VS16:
  98. return "16.0";
  99. case cmGlobalVisualStudioGenerator::VSVersion::VS17:
  100. return "17.0";
  101. }
  102. return "";
  103. }
  104. void cmGlobalVisualStudioGenerator::WriteSLNHeader(std::ostream& fout)
  105. {
  106. char utf8bom[] = { char(0xEF), char(0xBB), char(0xBF) };
  107. fout.write(utf8bom, 3);
  108. fout << '\n';
  109. switch (this->Version) {
  110. case cmGlobalVisualStudioGenerator::VSVersion::VS9:
  111. fout << "Microsoft Visual Studio Solution File, Format Version 10.00\n";
  112. fout << "# Visual Studio 2008\n";
  113. break;
  114. case cmGlobalVisualStudioGenerator::VSVersion::VS10:
  115. fout << "Microsoft Visual Studio Solution File, Format Version 11.00\n";
  116. if (this->ExpressEdition) {
  117. fout << "# Visual C++ Express 2010\n";
  118. } else {
  119. fout << "# Visual Studio 2010\n";
  120. }
  121. break;
  122. case cmGlobalVisualStudioGenerator::VSVersion::VS11:
  123. fout << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  124. if (this->ExpressEdition) {
  125. fout << "# Visual Studio Express 2012 for Windows Desktop\n";
  126. } else {
  127. fout << "# Visual Studio 2012\n";
  128. }
  129. break;
  130. case cmGlobalVisualStudioGenerator::VSVersion::VS12:
  131. fout << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  132. if (this->ExpressEdition) {
  133. fout << "# Visual Studio Express 2013 for Windows Desktop\n";
  134. } else {
  135. fout << "# Visual Studio 2013\n";
  136. }
  137. break;
  138. case cmGlobalVisualStudioGenerator::VSVersion::VS14:
  139. // Visual Studio 14 writes .sln format 12.00
  140. fout << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  141. if (this->ExpressEdition) {
  142. fout << "# Visual Studio Express 14 for Windows Desktop\n";
  143. } else {
  144. fout << "# Visual Studio 14\n";
  145. }
  146. break;
  147. case cmGlobalVisualStudioGenerator::VSVersion::VS15:
  148. // Visual Studio 15 writes .sln format 12.00
  149. fout << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  150. if (this->ExpressEdition) {
  151. fout << "# Visual Studio Express 15 for Windows Desktop\n";
  152. } else {
  153. fout << "# Visual Studio 15\n";
  154. }
  155. break;
  156. case cmGlobalVisualStudioGenerator::VSVersion::VS16:
  157. // Visual Studio 16 writes .sln format 12.00
  158. fout << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  159. if (this->ExpressEdition) {
  160. fout << "# Visual Studio Express 16 for Windows Desktop\n";
  161. } else {
  162. fout << "# Visual Studio Version 16\n";
  163. }
  164. break;
  165. case cmGlobalVisualStudioGenerator::VSVersion::VS17:
  166. // Visual Studio 17 writes .sln format 12.00
  167. fout << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  168. if (this->ExpressEdition) {
  169. fout << "# Visual Studio Express 17 for Windows Desktop\n";
  170. } else {
  171. fout << "# Visual Studio Version 17\n";
  172. }
  173. break;
  174. }
  175. }
  176. std::string cmGlobalVisualStudioGenerator::GetRegistryBase()
  177. {
  178. return cmGlobalVisualStudioGenerator::GetRegistryBase(this->GetIDEVersion());
  179. }
  180. std::string cmGlobalVisualStudioGenerator::GetRegistryBase(const char* version)
  181. {
  182. std::string key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\";
  183. return key + version;
  184. }
  185. void cmGlobalVisualStudioGenerator::AddExtraIDETargets()
  186. {
  187. // Add a special target that depends on ALL projects for easy build
  188. // of one configuration only.
  189. for (auto const& it : this->ProjectMap) {
  190. std::vector<cmLocalGenerator*> const& gen = it.second;
  191. // add the ALL_BUILD to the first local generator of each project
  192. if (!gen.empty()) {
  193. // Use no actual command lines so that the target itself is not
  194. // considered always out of date.
  195. auto cc = cm::make_unique<cmCustomCommand>();
  196. cc->SetCMP0116Status(cmPolicies::NEW);
  197. cc->SetEscapeOldStyle(false);
  198. cc->SetComment("Build all projects");
  199. cmTarget* allBuild =
  200. gen[0]->AddUtilityCommand("ALL_BUILD", true, std::move(cc));
  201. gen[0]->AddGeneratorTarget(
  202. cm::make_unique<cmGeneratorTarget>(allBuild, gen[0]));
  203. //
  204. // Organize in the "predefined targets" folder:
  205. //
  206. if (this->UseFolderProperty()) {
  207. allBuild->SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
  208. }
  209. // Now make all targets depend on the ALL_BUILD target
  210. for (cmLocalGenerator const* i : gen) {
  211. for (const auto& tgt : i->GetGeneratorTargets()) {
  212. if (tgt->GetType() == cmStateEnums::GLOBAL_TARGET ||
  213. tgt->IsImported()) {
  214. continue;
  215. }
  216. if (!this->IsExcluded(gen[0], tgt.get())) {
  217. allBuild->AddUtility(tgt->GetName(), false);
  218. }
  219. }
  220. }
  221. }
  222. }
  223. // Configure CMake Visual Studio macros, for this user on this version
  224. // of Visual Studio.
  225. this->ConfigureCMakeVisualStudioMacros();
  226. }
  227. void cmGlobalVisualStudioGenerator::ComputeTargetObjectDirectory(
  228. cmGeneratorTarget* gt) const
  229. {
  230. std::string dir =
  231. cmStrCat(gt->LocalGenerator->GetCurrentBinaryDirectory(), '/');
  232. std::string tgtDir = gt->LocalGenerator->GetTargetDirectory(gt);
  233. if (!tgtDir.empty()) {
  234. dir += tgtDir;
  235. dir += "/";
  236. }
  237. const char* cd = this->GetCMakeCFGIntDir();
  238. if (cd && *cd) {
  239. dir += cd;
  240. dir += "/";
  241. }
  242. gt->ObjectDirectory = dir;
  243. }
  244. bool IsVisualStudioMacrosFileRegistered(const std::string& macrosFile,
  245. const std::string& regKeyBase,
  246. std::string& nextAvailableSubKeyName);
  247. void RegisterVisualStudioMacros(const std::string& macrosFile,
  248. const std::string& regKeyBase);
  249. #define CMAKE_VSMACROS_FILENAME "CMakeVSMacros2.vsmacros"
  250. #define CMAKE_VSMACROS_RELOAD_MACRONAME \
  251. "Macros.CMakeVSMacros2.Macros.ReloadProjects"
  252. #define CMAKE_VSMACROS_STOP_MACRONAME "Macros.CMakeVSMacros2.Macros.StopBuild"
  253. void cmGlobalVisualStudioGenerator::ConfigureCMakeVisualStudioMacros()
  254. {
  255. std::string dir = this->GetUserMacrosDirectory();
  256. if (!dir.empty()) {
  257. std::string src = cmStrCat(cmSystemTools::GetCMakeRoot(),
  258. "/Templates/" CMAKE_VSMACROS_FILENAME);
  259. std::string dst = dir + "/CMakeMacros/" CMAKE_VSMACROS_FILENAME;
  260. // Copy the macros file to the user directory only if the
  261. // destination does not exist or the source location is newer.
  262. // This will allow the user to edit the macros for development
  263. // purposes but newer versions distributed with CMake will replace
  264. // older versions in user directories.
  265. int res;
  266. if (!cmSystemTools::FileTimeCompare(src, dst, &res) || res > 0) {
  267. if (!cmSystemTools::CopyFileAlways(src, dst)) {
  268. std::ostringstream oss;
  269. oss << "Could not copy from: " << src << std::endl;
  270. oss << " to: " << dst << std::endl;
  271. cmSystemTools::Message(oss.str(), "Warning");
  272. }
  273. }
  274. RegisterVisualStudioMacros(dst, this->GetUserMacrosRegKeyBase());
  275. }
  276. }
  277. void cmGlobalVisualStudioGenerator::CallVisualStudioMacro(
  278. MacroName m, const std::string& vsSolutionFile)
  279. {
  280. // If any solution or project files changed during the generation,
  281. // tell Visual Studio to reload them...
  282. std::string dir = this->GetUserMacrosDirectory();
  283. // Only really try to call the macro if:
  284. // - there is a UserMacrosDirectory
  285. // - the CMake vsmacros file exists
  286. // - the CMake vsmacros file is registered
  287. // - there were .sln/.vcproj files changed during generation
  288. //
  289. if (!dir.empty()) {
  290. std::string macrosFile = dir + "/CMakeMacros/" CMAKE_VSMACROS_FILENAME;
  291. std::string nextSubkeyName;
  292. if (cmSystemTools::FileExists(macrosFile) &&
  293. IsVisualStudioMacrosFileRegistered(
  294. macrosFile, this->GetUserMacrosRegKeyBase(), nextSubkeyName)) {
  295. if (m == MacroReload) {
  296. std::vector<std::string> filenames;
  297. this->GetFilesReplacedDuringGenerate(filenames);
  298. if (!filenames.empty()) {
  299. std::string projects = cmJoin(filenames, ";");
  300. cmCallVisualStudioMacro::CallMacro(
  301. vsSolutionFile, CMAKE_VSMACROS_RELOAD_MACRONAME, projects,
  302. this->GetCMakeInstance()->GetDebugOutput());
  303. }
  304. } else if (m == MacroStop) {
  305. cmCallVisualStudioMacro::CallMacro(
  306. vsSolutionFile, CMAKE_VSMACROS_STOP_MACRONAME, "",
  307. this->GetCMakeInstance()->GetDebugOutput());
  308. }
  309. }
  310. }
  311. }
  312. std::string cmGlobalVisualStudioGenerator::GetUserMacrosDirectory()
  313. {
  314. return "";
  315. }
  316. std::string cmGlobalVisualStudioGenerator::GetUserMacrosRegKeyBase()
  317. {
  318. return "";
  319. }
  320. void cmGlobalVisualStudioGenerator::FillLinkClosure(
  321. const cmGeneratorTarget* target, TargetSet& linked)
  322. {
  323. if (linked.insert(target).second) {
  324. TargetDependSet const& depends = this->GetTargetDirectDepends(target);
  325. for (cmTargetDepend const& di : depends) {
  326. if (di.IsLink()) {
  327. this->FillLinkClosure(di, linked);
  328. }
  329. }
  330. }
  331. }
  332. cmGlobalVisualStudioGenerator::TargetSet const&
  333. cmGlobalVisualStudioGenerator::GetTargetLinkClosure(cmGeneratorTarget* target)
  334. {
  335. auto i = this->TargetLinkClosure.find(target);
  336. if (i == this->TargetLinkClosure.end()) {
  337. TargetSetMap::value_type entry(target, TargetSet());
  338. i = this->TargetLinkClosure.insert(entry).first;
  339. this->FillLinkClosure(target, i->second);
  340. }
  341. return i->second;
  342. }
  343. void cmGlobalVisualStudioGenerator::FollowLinkDepends(
  344. const cmGeneratorTarget* target, std::set<const cmGeneratorTarget*>& linked)
  345. {
  346. if (!target->IsInBuildSystem()) {
  347. return;
  348. }
  349. if (linked.insert(target).second &&
  350. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  351. // Static library targets do not list their link dependencies so
  352. // we must follow them transitively now.
  353. TargetDependSet const& depends = this->GetTargetDirectDepends(target);
  354. for (cmTargetDepend const& di : depends) {
  355. if (di.IsLink()) {
  356. this->FollowLinkDepends(di, linked);
  357. }
  358. }
  359. }
  360. }
  361. bool cmGlobalVisualStudioGenerator::ComputeTargetDepends()
  362. {
  363. if (!this->cmGlobalGenerator::ComputeTargetDepends()) {
  364. return false;
  365. }
  366. for (auto const& it : this->ProjectMap) {
  367. for (const cmLocalGenerator* i : it.second) {
  368. for (const auto& ti : i->GetGeneratorTargets()) {
  369. this->ComputeVSTargetDepends(ti.get());
  370. }
  371. }
  372. }
  373. return true;
  374. }
  375. static bool VSLinkable(cmGeneratorTarget const* t)
  376. {
  377. return t->IsLinkable() || t->GetType() == cmStateEnums::OBJECT_LIBRARY;
  378. }
  379. void cmGlobalVisualStudioGenerator::ComputeVSTargetDepends(
  380. cmGeneratorTarget* target)
  381. {
  382. if (this->VSTargetDepends.find(target) != this->VSTargetDepends.end()) {
  383. return;
  384. }
  385. VSDependSet& vsTargetDepend = this->VSTargetDepends[target];
  386. // VS <= 7.1 has two behaviors that affect solution dependencies.
  387. //
  388. // (1) Solution-level dependencies between a linkable target and a
  389. // library cause that library to be linked. We use an intermedite
  390. // empty utility target to express the dependency. (VS 8 and above
  391. // provide a project file "LinkLibraryDependencies" setting to
  392. // choose whether to activate this behavior. We disable it except
  393. // when linking external project files.)
  394. //
  395. // (2) We cannot let static libraries depend directly on targets to
  396. // which they "link" because the librarian tool will copy the
  397. // targets into the static library. While the work-around for
  398. // behavior (1) would also avoid this, it would create a large
  399. // number of extra utility targets for little gain. Instead, use
  400. // the above work-around only for dependencies explicitly added by
  401. // the add_dependencies() command. Approximate link dependencies by
  402. // leaving them out for the static library itself but following them
  403. // transitively for other targets.
  404. bool allowLinkable = (target->GetType() != cmStateEnums::STATIC_LIBRARY &&
  405. target->GetType() != cmStateEnums::SHARED_LIBRARY &&
  406. target->GetType() != cmStateEnums::MODULE_LIBRARY &&
  407. target->GetType() != cmStateEnums::EXECUTABLE);
  408. TargetDependSet const& depends = this->GetTargetDirectDepends(target);
  409. // Collect implicit link dependencies (target_link_libraries).
  410. // Static libraries cannot depend on their link implementation
  411. // due to behavior (2), but they do not really need to.
  412. std::set<cmGeneratorTarget const*> linkDepends;
  413. if (target->GetType() != cmStateEnums::STATIC_LIBRARY) {
  414. for (cmTargetDepend const& di : depends) {
  415. if (di.IsLink()) {
  416. this->FollowLinkDepends(di, linkDepends);
  417. }
  418. }
  419. }
  420. // Collect explicit util dependencies (add_dependencies).
  421. std::set<cmGeneratorTarget const*> utilDepends;
  422. for (cmTargetDepend const& di : depends) {
  423. if (di.IsUtil()) {
  424. this->FollowLinkDepends(di, utilDepends);
  425. }
  426. }
  427. // Collect all targets linked by this target so we can avoid
  428. // intermediate targets below.
  429. TargetSet linked;
  430. if (target->GetType() != cmStateEnums::STATIC_LIBRARY) {
  431. linked = this->GetTargetLinkClosure(target);
  432. }
  433. // Emit link dependencies.
  434. for (cmGeneratorTarget const* dep : linkDepends) {
  435. vsTargetDepend.insert(dep->GetName());
  436. }
  437. // Emit util dependencies. Possibly use intermediate targets.
  438. for (cmGeneratorTarget const* dgt : utilDepends) {
  439. if (allowLinkable || !VSLinkable(dgt) || linked.count(dgt)) {
  440. // Direct dependency allowed.
  441. vsTargetDepend.insert(dgt->GetName());
  442. } else {
  443. // Direct dependency on linkable target not allowed.
  444. // Use an intermediate utility target.
  445. vsTargetDepend.insert(this->GetUtilityDepend(dgt));
  446. }
  447. }
  448. }
  449. bool cmGlobalVisualStudioGenerator::FindMakeProgram(cmMakefile* mf)
  450. {
  451. // Visual Studio generators know how to lookup their build tool
  452. // directly instead of needing a helper module to do it, so we
  453. // do not actually need to put CMAKE_MAKE_PROGRAM into the cache.
  454. if (cmIsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM"))) {
  455. mf->AddDefinition("CMAKE_MAKE_PROGRAM", this->GetVSMakeProgram());
  456. }
  457. return true;
  458. }
  459. std::string cmGlobalVisualStudioGenerator::GetUtilityDepend(
  460. cmGeneratorTarget const* target)
  461. {
  462. auto i = this->UtilityDepends.find(target);
  463. if (i == this->UtilityDepends.end()) {
  464. std::string name = this->WriteUtilityDepend(target);
  465. UtilityDependsMap::value_type entry(target, name);
  466. i = this->UtilityDepends.insert(entry).first;
  467. }
  468. return i->second;
  469. }
  470. std::string cmGlobalVisualStudioGenerator::GetStartupProjectName(
  471. cmLocalGenerator const* root) const
  472. {
  473. cmValue n = root->GetMakefile()->GetProperty("VS_STARTUP_PROJECT");
  474. if (cmNonempty(n)) {
  475. std::string startup = *n;
  476. if (this->FindTarget(startup)) {
  477. return startup;
  478. } else {
  479. root->GetMakefile()->IssueMessage(
  480. MessageType::AUTHOR_WARNING,
  481. "Directory property VS_STARTUP_PROJECT specifies target "
  482. "'" +
  483. startup + "' that does not exist. Ignoring.");
  484. }
  485. }
  486. // default, if not specified
  487. return this->GetAllTargetName();
  488. }
  489. bool IsVisualStudioMacrosFileRegistered(const std::string& macrosFile,
  490. const std::string& regKeyBase,
  491. std::string& nextAvailableSubKeyName)
  492. {
  493. bool macrosRegistered = false;
  494. std::string s1;
  495. std::string s2;
  496. // Make lowercase local copies, convert to Unix slashes, and
  497. // see if the resulting strings are the same:
  498. s1 = cmSystemTools::LowerCase(macrosFile);
  499. cmSystemTools::ConvertToUnixSlashes(s1);
  500. std::string keyname;
  501. HKEY hkey = NULL;
  502. LONG result = ERROR_SUCCESS;
  503. DWORD index = 0;
  504. keyname = regKeyBase + "\\OtherProjects7";
  505. hkey = NULL;
  506. result =
  507. RegOpenKeyExW(HKEY_CURRENT_USER, cmsys::Encoding::ToWide(keyname).c_str(),
  508. 0, KEY_READ, &hkey);
  509. if (ERROR_SUCCESS == result) {
  510. // Iterate the subkeys and look for the values of interest in each subkey:
  511. wchar_t subkeyname[256];
  512. DWORD cch_subkeyname = cm::size(subkeyname);
  513. wchar_t keyclass[256];
  514. DWORD cch_keyclass = cm::size(keyclass);
  515. FILETIME lastWriteTime;
  516. lastWriteTime.dwHighDateTime = 0;
  517. lastWriteTime.dwLowDateTime = 0;
  518. while (ERROR_SUCCESS ==
  519. RegEnumKeyExW(hkey, index, subkeyname, &cch_subkeyname, 0, keyclass,
  520. &cch_keyclass, &lastWriteTime)) {
  521. // Open the subkey and query the values of interest:
  522. HKEY hsubkey = NULL;
  523. result = RegOpenKeyExW(hkey, subkeyname, 0, KEY_READ, &hsubkey);
  524. if (ERROR_SUCCESS == result) {
  525. DWORD valueType = REG_SZ;
  526. wchar_t data1[256];
  527. DWORD cch_data1 = sizeof(data1);
  528. RegQueryValueExW(hsubkey, L"Path", 0, &valueType, (LPBYTE)data1,
  529. &cch_data1);
  530. DWORD data2 = 0;
  531. DWORD cch_data2 = sizeof(data2);
  532. RegQueryValueExW(hsubkey, L"Security", 0, &valueType, (LPBYTE)&data2,
  533. &cch_data2);
  534. DWORD data3 = 0;
  535. DWORD cch_data3 = sizeof(data3);
  536. RegQueryValueExW(hsubkey, L"StorageFormat", 0, &valueType,
  537. (LPBYTE)&data3, &cch_data3);
  538. s2 = cmSystemTools::LowerCase(cmsys::Encoding::ToNarrow(data1));
  539. cmSystemTools::ConvertToUnixSlashes(s2);
  540. if (s2 == s1) {
  541. macrosRegistered = true;
  542. }
  543. std::string fullname = cmsys::Encoding::ToNarrow(data1);
  544. std::string filename;
  545. std::string filepath;
  546. std::string filepathname;
  547. std::string filepathpath;
  548. if (cmSystemTools::FileExists(fullname)) {
  549. filename = cmSystemTools::GetFilenameName(fullname);
  550. filepath = cmSystemTools::GetFilenamePath(fullname);
  551. filepathname = cmSystemTools::GetFilenameName(filepath);
  552. filepathpath = cmSystemTools::GetFilenamePath(filepath);
  553. }
  554. // std::cout << keyname << "\\" << subkeyname << ":" << std::endl;
  555. // std::cout << " Path: " << data1 << std::endl;
  556. // std::cout << " Security: " << data2 << std::endl;
  557. // std::cout << " StorageFormat: " << data3 << std::endl;
  558. // std::cout << " filename: " << filename << std::endl;
  559. // std::cout << " filepath: " << filepath << std::endl;
  560. // std::cout << " filepathname: " << filepathname << std::endl;
  561. // std::cout << " filepathpath: " << filepathpath << std::endl;
  562. // std::cout << std::endl;
  563. RegCloseKey(hsubkey);
  564. } else {
  565. std::cout << "error opening subkey: " << subkeyname << std::endl;
  566. std::cout << std::endl;
  567. }
  568. ++index;
  569. cch_subkeyname = cm::size(subkeyname);
  570. cch_keyclass = cm::size(keyclass);
  571. lastWriteTime.dwHighDateTime = 0;
  572. lastWriteTime.dwLowDateTime = 0;
  573. }
  574. RegCloseKey(hkey);
  575. } else {
  576. std::cout << "error opening key: " << keyname << std::endl;
  577. std::cout << std::endl;
  578. }
  579. // Pass back next available sub key name, assuming sub keys always
  580. // follow the expected naming scheme. Expected naming scheme is that
  581. // the subkeys of OtherProjects7 is 0 to n-1, so it's ok to use "n"
  582. // as the name of the next subkey.
  583. nextAvailableSubKeyName = std::to_string(index);
  584. keyname = regKeyBase + "\\RecordingProject7";
  585. hkey = NULL;
  586. result =
  587. RegOpenKeyExW(HKEY_CURRENT_USER, cmsys::Encoding::ToWide(keyname).c_str(),
  588. 0, KEY_READ, &hkey);
  589. if (ERROR_SUCCESS == result) {
  590. DWORD valueType = REG_SZ;
  591. wchar_t data1[256];
  592. DWORD cch_data1 = sizeof(data1);
  593. RegQueryValueExW(hkey, L"Path", 0, &valueType, (LPBYTE)data1, &cch_data1);
  594. DWORD data2 = 0;
  595. DWORD cch_data2 = sizeof(data2);
  596. RegQueryValueExW(hkey, L"Security", 0, &valueType, (LPBYTE)&data2,
  597. &cch_data2);
  598. DWORD data3 = 0;
  599. DWORD cch_data3 = sizeof(data3);
  600. RegQueryValueExW(hkey, L"StorageFormat", 0, &valueType, (LPBYTE)&data3,
  601. &cch_data3);
  602. s2 = cmSystemTools::LowerCase(cmsys::Encoding::ToNarrow(data1));
  603. cmSystemTools::ConvertToUnixSlashes(s2);
  604. if (s2 == s1) {
  605. macrosRegistered = true;
  606. }
  607. // std::cout << keyname << ":" << std::endl;
  608. // std::cout << " Path: " << data1 << std::endl;
  609. // std::cout << " Security: " << data2 << std::endl;
  610. // std::cout << " StorageFormat: " << data3 << std::endl;
  611. // std::cout << std::endl;
  612. RegCloseKey(hkey);
  613. } else {
  614. std::cout << "error opening key: " << keyname << std::endl;
  615. std::cout << std::endl;
  616. }
  617. return macrosRegistered;
  618. }
  619. void WriteVSMacrosFileRegistryEntry(const std::string& nextAvailableSubKeyName,
  620. const std::string& macrosFile,
  621. const std::string& regKeyBase)
  622. {
  623. std::string keyname = regKeyBase + "\\OtherProjects7";
  624. HKEY hkey = NULL;
  625. LONG result =
  626. RegOpenKeyExW(HKEY_CURRENT_USER, cmsys::Encoding::ToWide(keyname).c_str(),
  627. 0, KEY_READ | KEY_WRITE, &hkey);
  628. if (ERROR_SUCCESS == result) {
  629. // Create the subkey and set the values of interest:
  630. HKEY hsubkey = NULL;
  631. wchar_t lpClass[] = L"";
  632. result = RegCreateKeyExW(
  633. hkey, cmsys::Encoding::ToWide(nextAvailableSubKeyName).c_str(), 0,
  634. lpClass, 0, KEY_READ | KEY_WRITE, 0, &hsubkey, 0);
  635. if (ERROR_SUCCESS == result) {
  636. DWORD dw = 0;
  637. std::string s(macrosFile);
  638. std::replace(s.begin(), s.end(), '/', '\\');
  639. std::wstring ws = cmsys::Encoding::ToWide(s);
  640. result =
  641. RegSetValueExW(hsubkey, L"Path", 0, REG_SZ, (LPBYTE)ws.c_str(),
  642. static_cast<DWORD>(ws.size() + 1) * sizeof(wchar_t));
  643. if (ERROR_SUCCESS != result) {
  644. std::cout << "error result 1: " << result << std::endl;
  645. std::cout << std::endl;
  646. }
  647. // Security value is always "1" for sample macros files (seems to be "2"
  648. // if you put the file somewhere outside the standard VSMacros folder)
  649. dw = 1;
  650. result = RegSetValueExW(hsubkey, L"Security", 0, REG_DWORD, (LPBYTE)&dw,
  651. sizeof(DWORD));
  652. if (ERROR_SUCCESS != result) {
  653. std::cout << "error result 2: " << result << std::endl;
  654. std::cout << std::endl;
  655. }
  656. // StorageFormat value is always "0" for sample macros files
  657. dw = 0;
  658. result = RegSetValueExW(hsubkey, L"StorageFormat", 0, REG_DWORD,
  659. (LPBYTE)&dw, sizeof(DWORD));
  660. if (ERROR_SUCCESS != result) {
  661. std::cout << "error result 3: " << result << std::endl;
  662. std::cout << std::endl;
  663. }
  664. RegCloseKey(hsubkey);
  665. } else {
  666. std::cout << "error creating subkey: " << nextAvailableSubKeyName
  667. << std::endl;
  668. std::cout << std::endl;
  669. }
  670. RegCloseKey(hkey);
  671. } else {
  672. std::cout << "error opening key: " << keyname << std::endl;
  673. std::cout << std::endl;
  674. }
  675. }
  676. void RegisterVisualStudioMacros(const std::string& macrosFile,
  677. const std::string& regKeyBase)
  678. {
  679. bool macrosRegistered;
  680. std::string nextAvailableSubKeyName;
  681. macrosRegistered = IsVisualStudioMacrosFileRegistered(
  682. macrosFile, regKeyBase, nextAvailableSubKeyName);
  683. if (!macrosRegistered) {
  684. int count =
  685. cmCallVisualStudioMacro::GetNumberOfRunningVisualStudioInstances("ALL");
  686. // Only register the macros file if there are *no* instances of Visual
  687. // Studio running. If we register it while one is running, first, it has
  688. // no effect on the running instance; second, and worse, Visual Studio
  689. // removes our newly added registration entry when it quits. Instead,
  690. // emit a warning asking the user to exit all running Visual Studio
  691. // instances...
  692. //
  693. if (0 != count) {
  694. std::ostringstream oss;
  695. oss << "Could not register CMake's Visual Studio macros file '"
  696. << CMAKE_VSMACROS_FILENAME "' while Visual Studio is running."
  697. << " Please exit all running instances of Visual Studio before"
  698. << " continuing." << std::endl
  699. << std::endl
  700. << "CMake needs to register Visual Studio macros when its macros"
  701. << " file is updated or when it detects that its current macros file"
  702. << " is no longer registered with Visual Studio." << std::endl;
  703. cmSystemTools::Message(oss.str(), "Warning");
  704. // Count them again now that the warning is over. In the case of a GUI
  705. // warning, the user may have gone to close Visual Studio and then come
  706. // back to the CMake GUI and clicked ok on the above warning. If so,
  707. // then register the macros *now* if the count is *now* 0...
  708. //
  709. count = cmCallVisualStudioMacro::GetNumberOfRunningVisualStudioInstances(
  710. "ALL");
  711. // Also re-get the nextAvailableSubKeyName in case Visual Studio
  712. // wrote out new registered macros information as it was exiting:
  713. //
  714. if (0 == count) {
  715. IsVisualStudioMacrosFileRegistered(macrosFile, regKeyBase,
  716. nextAvailableSubKeyName);
  717. }
  718. }
  719. // Do another if check - 'count' may have changed inside the above if:
  720. //
  721. if (0 == count) {
  722. WriteVSMacrosFileRegistryEntry(nextAvailableSubKeyName, macrosFile,
  723. regKeyBase);
  724. }
  725. }
  726. }
  727. bool cmGlobalVisualStudioGenerator::TargetIsFortranOnly(
  728. cmGeneratorTarget const* gt)
  729. {
  730. // If there's only one source language, Fortran has to be used
  731. // in order for the sources to compile.
  732. std::set<std::string> languages = gt->GetAllConfigCompileLanguages();
  733. // Consider an explicit linker language property, but *not* the
  734. // computed linker language that may depend on linked targets.
  735. // This allows the project to control the language choice in
  736. // a target with none of its own sources, e.g. when also using
  737. // object libraries.
  738. cmValue linkLang = gt->GetProperty("LINKER_LANGUAGE");
  739. if (cmNonempty(linkLang)) {
  740. languages.insert(*linkLang);
  741. }
  742. // Intel Fortran .vfproj files do support the resource compiler.
  743. languages.erase("RC");
  744. return languages.size() == 1 && *languages.begin() == "Fortran";
  745. }
  746. bool cmGlobalVisualStudioGenerator::TargetCompare::operator()(
  747. cmGeneratorTarget const* l, cmGeneratorTarget const* r) const
  748. {
  749. // Make sure a given named target is ordered first,
  750. // e.g. to set ALL_BUILD as the default active project.
  751. // When the empty string is named this is a no-op.
  752. if (r->GetName() == this->First) {
  753. return false;
  754. }
  755. if (l->GetName() == this->First) {
  756. return true;
  757. }
  758. return l->GetName() < r->GetName();
  759. }
  760. cmGlobalVisualStudioGenerator::OrderedTargetDependSet::OrderedTargetDependSet(
  761. TargetDependSet const& targets, std::string const& first)
  762. : derived(TargetCompare(first))
  763. {
  764. this->insert(targets.begin(), targets.end());
  765. }
  766. cmGlobalVisualStudioGenerator::OrderedTargetDependSet::OrderedTargetDependSet(
  767. TargetSet const& targets, std::string const& first)
  768. : derived(TargetCompare(first))
  769. {
  770. for (cmGeneratorTarget const* it : targets) {
  771. this->insert(it);
  772. }
  773. }
  774. std::string cmGlobalVisualStudioGenerator::ExpandCFGIntDir(
  775. const std::string& str, const std::string& config) const
  776. {
  777. std::string replace = GetCMakeCFGIntDir();
  778. std::string tmp = str;
  779. for (std::string::size_type i = tmp.find(replace); i != std::string::npos;
  780. i = tmp.find(replace, i)) {
  781. tmp.replace(i, replace.size(), config);
  782. i += config.size();
  783. }
  784. return tmp;
  785. }
  786. void cmGlobalVisualStudioGenerator::AddSymbolExportCommand(
  787. cmGeneratorTarget* gt, std::vector<cmCustomCommand>& commands,
  788. std::string const& configName)
  789. {
  790. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  791. gt->GetModuleDefinitionInfo(configName);
  792. if (!mdi || !mdi->DefFileGenerated) {
  793. return;
  794. }
  795. std::vector<std::string> outputs;
  796. outputs.push_back(mdi->DefFile);
  797. std::vector<std::string> empty;
  798. std::vector<cmSourceFile const*> objectSources;
  799. gt->GetObjectSources(objectSources, configName);
  800. std::map<cmSourceFile const*, std::string> mapping;
  801. for (cmSourceFile const* it : objectSources) {
  802. mapping[it];
  803. }
  804. gt->LocalGenerator->ComputeObjectFilenames(mapping, gt);
  805. std::string obj_dir = gt->ObjectDirectory;
  806. std::string cmakeCommand = cmSystemTools::GetCMakeCommand();
  807. std::string obj_dir_expanded = obj_dir;
  808. cmSystemTools::ReplaceString(obj_dir_expanded, this->GetCMakeCFGIntDir(),
  809. configName.c_str());
  810. cmSystemTools::MakeDirectory(obj_dir_expanded);
  811. std::string const objs_file = obj_dir_expanded + "/objects.txt";
  812. cmGeneratedFileStream fout(objs_file.c_str());
  813. if (!fout) {
  814. cmSystemTools::Error("could not open " + objs_file);
  815. return;
  816. }
  817. if (mdi->WindowsExportAllSymbols) {
  818. std::vector<std::string> objs;
  819. for (cmSourceFile const* it : objectSources) {
  820. // Find the object file name corresponding to this source file.
  821. // It must exist because we populated the mapping just above.
  822. const auto& v = mapping[it];
  823. assert(!v.empty());
  824. std::string objFile = obj_dir + v;
  825. objs.push_back(objFile);
  826. }
  827. std::vector<cmSourceFile const*> externalObjectSources;
  828. gt->GetExternalObjects(externalObjectSources, configName);
  829. for (cmSourceFile const* it : externalObjectSources) {
  830. objs.push_back(it->GetFullPath());
  831. }
  832. for (std::string const& it : objs) {
  833. std::string objFile = it;
  834. // replace $(ConfigurationName) in the object names
  835. cmSystemTools::ReplaceString(objFile, this->GetCMakeCFGIntDir(),
  836. configName);
  837. if (cmHasLiteralSuffix(objFile, ".obj")) {
  838. fout << objFile << "\n";
  839. }
  840. }
  841. }
  842. for (cmSourceFile const* i : mdi->Sources) {
  843. fout << i->GetFullPath() << "\n";
  844. }
  845. cmCustomCommandLines commandLines = cmMakeSingleCommandLine(
  846. { cmakeCommand, "-E", "__create_def", mdi->DefFile, objs_file });
  847. cmCustomCommand command;
  848. command.SetOutputs(outputs);
  849. command.SetCommandLines(commandLines);
  850. command.SetComment("Auto build dll exports");
  851. command.SetBacktrace(gt->Target->GetMakefile()->GetBacktrace());
  852. command.SetWorkingDirectory(".");
  853. command.SetStdPipesUTF8(true);
  854. commands.push_back(std::move(command));
  855. }
  856. static bool OpenSolution(std::string sln)
  857. {
  858. HRESULT comInitialized =
  859. CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
  860. if (FAILED(comInitialized)) {
  861. return false;
  862. }
  863. HINSTANCE hi =
  864. ShellExecuteA(NULL, "open", sln.c_str(), NULL, NULL, SW_SHOWNORMAL);
  865. CoUninitialize();
  866. return reinterpret_cast<intptr_t>(hi) > 32;
  867. }
  868. bool cmGlobalVisualStudioGenerator::Open(const std::string& bindir,
  869. const std::string& projectName,
  870. bool dryRun)
  871. {
  872. std::string sln = bindir + "/" + projectName + ".sln";
  873. if (dryRun) {
  874. return cmSystemTools::FileExists(sln, true);
  875. }
  876. sln = cmSystemTools::ConvertToOutputPath(sln);
  877. return std::async(std::launch::async, OpenSolution, sln).get();
  878. }