cmGlobalVisualStudioGenerator.cxx 30 KB

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