cmGlobalVisualStudioGenerator.cxx 28 KB

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