cmGlobalVisualStudioGenerator.cxx 27 KB

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