cmGlobalVisualStudioGenerator.cxx 33 KB

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