cmGlobalVisualStudioGenerator.cxx 34 KB

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