cmGlobalVisualStudioGenerator.cxx 30 KB

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