cmGlobalVisualStudioGenerator.cxx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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. void cmGlobalVisualStudioGenerator::ComputeVSTargetDepends(cmTarget& target)
  344. {
  345. if(this->VSTargetDepends.find(&target) != this->VSTargetDepends.end())
  346. {
  347. return;
  348. }
  349. VSDependSet& vsTargetDepend = this->VSTargetDepends[&target];
  350. // VS <= 7.1 has two behaviors that affect solution dependencies.
  351. //
  352. // (1) Solution-level dependencies between a linkable target and a
  353. // library cause that library to be linked. We use an intermedite
  354. // empty utility target to express the dependency. (VS 8 and above
  355. // provide a project file "LinkLibraryDependencies" setting to
  356. // choose whether to activate this behavior. We disable it except
  357. // when linking external project files.)
  358. //
  359. // (2) We cannot let static libraries depend directly on targets to
  360. // which they "link" because the librarian tool will copy the
  361. // targets into the static library. While the work-around for
  362. // behavior (1) would also avoid this, it would create a large
  363. // number of extra utility targets for little gain. Instead, use
  364. // the above work-around only for dependencies explicitly added by
  365. // the add_dependencies() command. Approximate link dependencies by
  366. // leaving them out for the static library itself but following them
  367. // transitively for other targets.
  368. bool allowLinkable = (target.GetType() != cmTarget::STATIC_LIBRARY &&
  369. target.GetType() != cmTarget::SHARED_LIBRARY &&
  370. target.GetType() != cmTarget::MODULE_LIBRARY &&
  371. target.GetType() != cmTarget::EXECUTABLE);
  372. TargetDependSet const& depends = this->GetTargetDirectDepends(target);
  373. // Collect implicit link dependencies (target_link_libraries).
  374. // Static libraries cannot depend on their link implementation
  375. // due to behavior (2), but they do not really need to.
  376. std::set<cmTarget*> linkDepends;
  377. if(target.GetType() != cmTarget::STATIC_LIBRARY)
  378. {
  379. for(TargetDependSet::const_iterator di = depends.begin();
  380. di != depends.end(); ++di)
  381. {
  382. cmTargetDepend dep = *di;
  383. if(dep.IsLink())
  384. {
  385. this->FollowLinkDepends(dep, linkDepends);
  386. }
  387. }
  388. }
  389. // Collext explicit util dependencies (add_dependencies).
  390. std::set<cmTarget*> utilDepends;
  391. for(TargetDependSet::const_iterator di = depends.begin();
  392. di != depends.end(); ++di)
  393. {
  394. cmTargetDepend dep = *di;
  395. if(dep.IsUtil())
  396. {
  397. this->FollowLinkDepends(dep, utilDepends);
  398. }
  399. }
  400. // Collect all targets linked by this target so we can avoid
  401. // intermediate targets below.
  402. TargetSet linked;
  403. if(target.GetType() != cmTarget::STATIC_LIBRARY)
  404. {
  405. linked = this->GetTargetLinkClosure(&target);
  406. }
  407. // Emit link dependencies.
  408. for(std::set<cmTarget*>::iterator di = linkDepends.begin();
  409. di != linkDepends.end(); ++di)
  410. {
  411. cmTarget* dep = *di;
  412. vsTargetDepend.insert(dep->GetName());
  413. }
  414. // Emit util dependencies. Possibly use intermediate targets.
  415. for(std::set<cmTarget*>::iterator di = utilDepends.begin();
  416. di != utilDepends.end(); ++di)
  417. {
  418. cmTarget* dep = *di;
  419. if(allowLinkable || !dep->IsLinkable() || linked.count(dep))
  420. {
  421. // Direct dependency allowed.
  422. vsTargetDepend.insert(dep->GetName());
  423. }
  424. else
  425. {
  426. // Direct dependency on linkable target not allowed.
  427. // Use an intermediate utility target.
  428. vsTargetDepend.insert(this->GetUtilityDepend(dep));
  429. }
  430. }
  431. }
  432. //----------------------------------------------------------------------------
  433. std::string cmGlobalVisualStudioGenerator::GetUtilityDepend(cmTarget* target)
  434. {
  435. UtilityDependsMap::iterator i = this->UtilityDepends.find(target);
  436. if(i == this->UtilityDepends.end())
  437. {
  438. std::string name = this->WriteUtilityDepend(target);
  439. UtilityDependsMap::value_type entry(target, name);
  440. i = this->UtilityDepends.insert(entry).first;
  441. }
  442. return i->second;
  443. }
  444. //----------------------------------------------------------------------------
  445. #include <windows.h>
  446. //----------------------------------------------------------------------------
  447. bool IsVisualStudioMacrosFileRegistered(const std::string& macrosFile,
  448. const std::string& regKeyBase,
  449. std::string& nextAvailableSubKeyName)
  450. {
  451. bool macrosRegistered = false;
  452. std::string s1;
  453. std::string s2;
  454. // Make lowercase local copies, convert to Unix slashes, and
  455. // see if the resulting strings are the same:
  456. s1 = cmSystemTools::LowerCase(macrosFile);
  457. cmSystemTools::ConvertToUnixSlashes(s1);
  458. std::string keyname;
  459. HKEY hkey = NULL;
  460. LONG result = ERROR_SUCCESS;
  461. DWORD index = 0;
  462. keyname = regKeyBase + "\\OtherProjects7";
  463. hkey = NULL;
  464. result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(),
  465. 0, KEY_READ, &hkey);
  466. if (ERROR_SUCCESS == result)
  467. {
  468. // Iterate the subkeys and look for the values of interest in each subkey:
  469. CHAR subkeyname[256];
  470. DWORD cch_subkeyname = sizeof(subkeyname)/sizeof(subkeyname[0]);
  471. CHAR keyclass[256];
  472. DWORD cch_keyclass = sizeof(keyclass)/sizeof(keyclass[0]);
  473. FILETIME lastWriteTime;
  474. lastWriteTime.dwHighDateTime = 0;
  475. lastWriteTime.dwLowDateTime = 0;
  476. while (ERROR_SUCCESS == RegEnumKeyEx(hkey, index, subkeyname,
  477. &cch_subkeyname,
  478. 0, keyclass, &cch_keyclass, &lastWriteTime))
  479. {
  480. // Open the subkey and query the values of interest:
  481. HKEY hsubkey = NULL;
  482. result = RegOpenKeyEx(hkey, subkeyname, 0, KEY_READ, &hsubkey);
  483. if (ERROR_SUCCESS == result)
  484. {
  485. DWORD valueType = REG_SZ;
  486. CHAR data1[256];
  487. DWORD cch_data1 = sizeof(data1)/sizeof(data1[0]);
  488. RegQueryValueEx(hsubkey, "Path", 0, &valueType,
  489. (LPBYTE) &data1[0], &cch_data1);
  490. DWORD data2 = 0;
  491. DWORD cch_data2 = sizeof(data2);
  492. RegQueryValueEx(hsubkey, "Security", 0, &valueType,
  493. (LPBYTE) &data2, &cch_data2);
  494. DWORD data3 = 0;
  495. DWORD cch_data3 = sizeof(data3);
  496. RegQueryValueEx(hsubkey, "StorageFormat", 0, &valueType,
  497. (LPBYTE) &data3, &cch_data3);
  498. s2 = cmSystemTools::LowerCase(data1);
  499. cmSystemTools::ConvertToUnixSlashes(s2);
  500. if (s2 == s1)
  501. {
  502. macrosRegistered = true;
  503. }
  504. std::string fullname(data1);
  505. std::string filename;
  506. std::string filepath;
  507. std::string filepathname;
  508. std::string filepathpath;
  509. if (cmSystemTools::FileExists(fullname.c_str()))
  510. {
  511. filename = cmSystemTools::GetFilenameName(fullname);
  512. filepath = cmSystemTools::GetFilenamePath(fullname);
  513. filepathname = cmSystemTools::GetFilenameName(filepath);
  514. filepathpath = cmSystemTools::GetFilenamePath(filepath);
  515. }
  516. //std::cout << keyname << "\\" << subkeyname << ":" << std::endl;
  517. //std::cout << " Path: " << data1 << std::endl;
  518. //std::cout << " Security: " << data2 << std::endl;
  519. //std::cout << " StorageFormat: " << data3 << std::endl;
  520. //std::cout << " filename: " << filename << std::endl;
  521. //std::cout << " filepath: " << filepath << std::endl;
  522. //std::cout << " filepathname: " << filepathname << std::endl;
  523. //std::cout << " filepathpath: " << filepathpath << std::endl;
  524. //std::cout << std::endl;
  525. RegCloseKey(hsubkey);
  526. }
  527. else
  528. {
  529. std::cout << "error opening subkey: " << subkeyname << std::endl;
  530. std::cout << std::endl;
  531. }
  532. ++index;
  533. cch_subkeyname = sizeof(subkeyname)/sizeof(subkeyname[0]);
  534. cch_keyclass = sizeof(keyclass)/sizeof(keyclass[0]);
  535. lastWriteTime.dwHighDateTime = 0;
  536. lastWriteTime.dwLowDateTime = 0;
  537. }
  538. RegCloseKey(hkey);
  539. }
  540. else
  541. {
  542. std::cout << "error opening key: " << keyname << std::endl;
  543. std::cout << std::endl;
  544. }
  545. // Pass back next available sub key name, assuming sub keys always
  546. // follow the expected naming scheme. Expected naming scheme is that
  547. // the subkeys of OtherProjects7 is 0 to n-1, so it's ok to use "n"
  548. // as the name of the next subkey.
  549. std::ostringstream ossNext;
  550. ossNext << index;
  551. nextAvailableSubKeyName = ossNext.str();
  552. keyname = regKeyBase + "\\RecordingProject7";
  553. hkey = NULL;
  554. result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(),
  555. 0, KEY_READ, &hkey);
  556. if (ERROR_SUCCESS == result)
  557. {
  558. DWORD valueType = REG_SZ;
  559. CHAR data1[256];
  560. DWORD cch_data1 = sizeof(data1)/sizeof(data1[0]);
  561. RegQueryValueEx(hkey, "Path", 0, &valueType,
  562. (LPBYTE) &data1[0], &cch_data1);
  563. DWORD data2 = 0;
  564. DWORD cch_data2 = sizeof(data2);
  565. RegQueryValueEx(hkey, "Security", 0, &valueType,
  566. (LPBYTE) &data2, &cch_data2);
  567. DWORD data3 = 0;
  568. DWORD cch_data3 = sizeof(data3);
  569. RegQueryValueEx(hkey, "StorageFormat", 0, &valueType,
  570. (LPBYTE) &data3, &cch_data3);
  571. s2 = cmSystemTools::LowerCase(data1);
  572. cmSystemTools::ConvertToUnixSlashes(s2);
  573. if (s2 == s1)
  574. {
  575. macrosRegistered = true;
  576. }
  577. //std::cout << keyname << ":" << std::endl;
  578. //std::cout << " Path: " << data1 << std::endl;
  579. //std::cout << " Security: " << data2 << std::endl;
  580. //std::cout << " StorageFormat: " << data3 << std::endl;
  581. //std::cout << std::endl;
  582. RegCloseKey(hkey);
  583. }
  584. else
  585. {
  586. std::cout << "error opening key: " << keyname << std::endl;
  587. std::cout << std::endl;
  588. }
  589. return macrosRegistered;
  590. }
  591. //----------------------------------------------------------------------------
  592. void WriteVSMacrosFileRegistryEntry(
  593. const std::string& nextAvailableSubKeyName,
  594. const std::string& macrosFile,
  595. const std::string& regKeyBase)
  596. {
  597. std::string keyname = regKeyBase + "\\OtherProjects7";
  598. HKEY hkey = NULL;
  599. LONG result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(), 0,
  600. KEY_READ|KEY_WRITE, &hkey);
  601. if (ERROR_SUCCESS == result)
  602. {
  603. // Create the subkey and set the values of interest:
  604. HKEY hsubkey = NULL;
  605. char lpClass[] = "";
  606. result = RegCreateKeyEx(hkey, nextAvailableSubKeyName.c_str(), 0,
  607. lpClass, 0, KEY_READ|KEY_WRITE, 0, &hsubkey, 0);
  608. if (ERROR_SUCCESS == result)
  609. {
  610. DWORD dw = 0;
  611. std::string s(macrosFile);
  612. cmSystemTools::ReplaceString(s, "/", "\\");
  613. result = RegSetValueEx(hsubkey, "Path", 0, REG_SZ, (LPBYTE) s.c_str(),
  614. static_cast<DWORD>(strlen(s.c_str()) + 1));
  615. if (ERROR_SUCCESS != result)
  616. {
  617. std::cout << "error result 1: " << result << std::endl;
  618. std::cout << std::endl;
  619. }
  620. // Security value is always "1" for sample macros files (seems to be "2"
  621. // if you put the file somewhere outside the standard VSMacros folder)
  622. dw = 1;
  623. result = RegSetValueEx(hsubkey, "Security",
  624. 0, REG_DWORD, (LPBYTE) &dw, sizeof(DWORD));
  625. if (ERROR_SUCCESS != result)
  626. {
  627. std::cout << "error result 2: " << result << std::endl;
  628. std::cout << std::endl;
  629. }
  630. // StorageFormat value is always "0" for sample macros files
  631. dw = 0;
  632. result = RegSetValueEx(hsubkey, "StorageFormat",
  633. 0, REG_DWORD, (LPBYTE) &dw, sizeof(DWORD));
  634. if (ERROR_SUCCESS != result)
  635. {
  636. std::cout << "error result 3: " << result << std::endl;
  637. std::cout << std::endl;
  638. }
  639. RegCloseKey(hsubkey);
  640. }
  641. else
  642. {
  643. std::cout << "error creating subkey: "
  644. << nextAvailableSubKeyName << std::endl;
  645. std::cout << std::endl;
  646. }
  647. RegCloseKey(hkey);
  648. }
  649. else
  650. {
  651. std::cout << "error opening key: " << keyname << std::endl;
  652. std::cout << std::endl;
  653. }
  654. }
  655. //----------------------------------------------------------------------------
  656. void RegisterVisualStudioMacros(const std::string& macrosFile,
  657. const std::string& regKeyBase)
  658. {
  659. bool macrosRegistered;
  660. std::string nextAvailableSubKeyName;
  661. macrosRegistered = IsVisualStudioMacrosFileRegistered(macrosFile,
  662. regKeyBase, nextAvailableSubKeyName);
  663. if (!macrosRegistered)
  664. {
  665. int count = cmCallVisualStudioMacro::
  666. GetNumberOfRunningVisualStudioInstances("ALL");
  667. // Only register the macros file if there are *no* instances of Visual
  668. // Studio running. If we register it while one is running, first, it has
  669. // no effect on the running instance; second, and worse, Visual Studio
  670. // removes our newly added registration entry when it quits. Instead,
  671. // emit a warning asking the user to exit all running Visual Studio
  672. // instances...
  673. //
  674. if (0 != count)
  675. {
  676. std::ostringstream oss;
  677. oss << "Could not register CMake's Visual Studio macros file '"
  678. << CMAKE_VSMACROS_FILENAME "' while Visual Studio is running."
  679. << " Please exit all running instances of Visual Studio before"
  680. << " continuing." << std::endl
  681. << std::endl
  682. << "CMake needs to register Visual Studio macros when its macros"
  683. << " file is updated or when it detects that its current macros file"
  684. << " is no longer registered with Visual Studio."
  685. << std::endl;
  686. cmSystemTools::Message(oss.str().c_str(), "Warning");
  687. // Count them again now that the warning is over. In the case of a GUI
  688. // warning, the user may have gone to close Visual Studio and then come
  689. // back to the CMake GUI and clicked ok on the above warning. If so,
  690. // then register the macros *now* if the count is *now* 0...
  691. //
  692. count = cmCallVisualStudioMacro::
  693. GetNumberOfRunningVisualStudioInstances("ALL");
  694. // Also re-get the nextAvailableSubKeyName in case Visual Studio
  695. // wrote out new registered macros information as it was exiting:
  696. //
  697. if (0 == count)
  698. {
  699. IsVisualStudioMacrosFileRegistered(macrosFile, regKeyBase,
  700. nextAvailableSubKeyName);
  701. }
  702. }
  703. // Do another if check - 'count' may have changed inside the above if:
  704. //
  705. if (0 == count)
  706. {
  707. WriteVSMacrosFileRegistryEntry(nextAvailableSubKeyName, macrosFile,
  708. regKeyBase);
  709. }
  710. }
  711. }
  712. bool cmGlobalVisualStudioGenerator::TargetIsFortranOnly(cmTarget& target)
  713. {
  714. // check to see if this is a fortran build
  715. std::set<cmStdString> languages;
  716. target.GetLanguages(languages);
  717. if(languages.size() == 1)
  718. {
  719. if(*languages.begin() == "Fortran")
  720. {
  721. return true;
  722. }
  723. }
  724. return false;
  725. }
  726. //----------------------------------------------------------------------------
  727. bool
  728. cmGlobalVisualStudioGenerator::TargetCompare
  729. ::operator()(cmTarget const* l, cmTarget const* r) const
  730. {
  731. // Make sure ALL_BUILD is first so it is the default active project.
  732. if(strcmp(r->GetName(), "ALL_BUILD") == 0)
  733. {
  734. return false;
  735. }
  736. if(strcmp(l->GetName(), "ALL_BUILD") == 0)
  737. {
  738. return true;
  739. }
  740. return strcmp(l->GetName(), r->GetName()) < 0;
  741. }
  742. //----------------------------------------------------------------------------
  743. cmGlobalVisualStudioGenerator::OrderedTargetDependSet
  744. ::OrderedTargetDependSet(TargetDependSet const& targets)
  745. {
  746. for(TargetDependSet::const_iterator ti =
  747. targets.begin(); ti != targets.end(); ++ti)
  748. {
  749. this->insert(*ti);
  750. }
  751. }
  752. //----------------------------------------------------------------------------
  753. cmGlobalVisualStudioGenerator::OrderedTargetDependSet
  754. ::OrderedTargetDependSet(TargetSet const& targets)
  755. {
  756. for(TargetSet::const_iterator ti = targets.begin();
  757. ti != targets.end(); ++ti)
  758. {
  759. this->insert(*ti);
  760. }
  761. }