cmGlobalVisualStudioGenerator.cxx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmGlobalVisualStudioGenerator.h"
  14. #include "cmCallVisualStudioMacro.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmTarget.h"
  18. //----------------------------------------------------------------------------
  19. cmGlobalVisualStudioGenerator::cmGlobalVisualStudioGenerator()
  20. {
  21. }
  22. //----------------------------------------------------------------------------
  23. cmGlobalVisualStudioGenerator::~cmGlobalVisualStudioGenerator()
  24. {
  25. }
  26. //----------------------------------------------------------------------------
  27. void cmGlobalVisualStudioGenerator::Generate()
  28. {
  29. // Add a special target that depends on ALL projects for easy build
  30. // of one configuration only.
  31. const char* no_working_dir = 0;
  32. std::vector<std::string> no_depends;
  33. cmCustomCommandLines no_commands;
  34. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  35. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  36. {
  37. std::vector<cmLocalGenerator*>& gen = it->second;
  38. // add the ALL_BUILD to the first local generator of each project
  39. if(gen.size())
  40. {
  41. // Use no actual command lines so that the target itself is not
  42. // considered always out of date.
  43. gen[0]->GetMakefile()->
  44. AddUtilityCommand("ALL_BUILD", true, no_working_dir,
  45. no_depends, no_commands, false,
  46. "Build all projects");
  47. }
  48. }
  49. // Fix utility dependencies to avoid linking to libraries.
  50. this->FixUtilityDepends();
  51. // Configure CMake Visual Studio macros, for this user on this version
  52. // of Visual Studio.
  53. this->ConfigureCMakeVisualStudioMacros();
  54. // Run all the local generators.
  55. this->cmGlobalGenerator::Generate();
  56. }
  57. //----------------------------------------------------------------------------
  58. bool IsVisualStudioMacrosFileRegistered(const std::string& macrosFile,
  59. std::string& nextAvailableSubKeyName);
  60. void RegisterVisualStudioMacros(const std::string& macrosFile);
  61. //----------------------------------------------------------------------------
  62. #define CMAKE_VSMACROS_FILENAME \
  63. "CMakeVSMacros1.vsmacros"
  64. #define CMAKE_VSMACROS_RELOAD_MACRONAME \
  65. "Macros.CMakeVSMacros1.Macros.ReloadProjects"
  66. #define CMAKE_VSMACROS_STOP_MACRONAME \
  67. "Macros.CMakeVSMacros1.Macros.StopBuild"
  68. //----------------------------------------------------------------------------
  69. void cmGlobalVisualStudioGenerator::ConfigureCMakeVisualStudioMacros()
  70. {
  71. cmMakefile* mf = this->LocalGenerators[0]->GetMakefile();
  72. std::string dir = this->GetUserMacrosDirectory();
  73. if (mf != 0 && dir != "")
  74. {
  75. std::string src = mf->GetRequiredDefinition("CMAKE_ROOT");
  76. src += "/Templates/" CMAKE_VSMACROS_FILENAME;
  77. std::string dst = dir + "/CMakeMacros/" CMAKE_VSMACROS_FILENAME;
  78. // Copy the macros file to the user directory only if the
  79. // destination does not exist or the source location is newer.
  80. // This will allow the user to edit the macros for development
  81. // purposes but newer versions distributed with CMake will replace
  82. // older versions in user directories.
  83. int res;
  84. if(!cmSystemTools::FileTimeCompare(src.c_str(), dst.c_str(), &res) ||
  85. res > 0)
  86. {
  87. if (!cmSystemTools::CopyFileAlways(src.c_str(), dst.c_str()))
  88. {
  89. std::ostringstream oss;
  90. oss << "Could not copy from: " << src << std::endl;
  91. oss << " to: " << dst << std::endl;
  92. cmSystemTools::Message(oss.str().c_str(), "Warning");
  93. }
  94. }
  95. RegisterVisualStudioMacros(dst);
  96. }
  97. }
  98. //----------------------------------------------------------------------------
  99. void
  100. cmGlobalVisualStudioGenerator
  101. ::CallVisualStudioMacro(MacroName m,
  102. const char* vsSolutionFile)
  103. {
  104. // If any solution or project files changed during the generation,
  105. // tell Visual Studio to reload them...
  106. cmMakefile* mf = this->LocalGenerators[0]->GetMakefile();
  107. std::string dir = this->GetUserMacrosDirectory();
  108. // Only really try to call the macro if:
  109. // - mf is non-NULL
  110. // - there is a UserMacrosDirectory
  111. // - the CMake vsmacros file exists
  112. // - the CMake vsmacros file is registered
  113. // - there were .sln/.vcproj files changed during generation
  114. //
  115. if (mf != 0 && dir != "")
  116. {
  117. std::string macrosFile = dir + "/CMakeMacros/" CMAKE_VSMACROS_FILENAME;
  118. std::string nextSubkeyName;
  119. if (cmSystemTools::FileExists(macrosFile.c_str()) &&
  120. IsVisualStudioMacrosFileRegistered(macrosFile, nextSubkeyName)
  121. )
  122. {
  123. std::string topLevelSlnName;
  124. if(vsSolutionFile)
  125. {
  126. topLevelSlnName = vsSolutionFile;
  127. }
  128. else
  129. {
  130. topLevelSlnName = mf->GetStartOutputDirectory();
  131. topLevelSlnName += "/";
  132. topLevelSlnName += mf->GetProjectName();
  133. topLevelSlnName += ".sln";
  134. }
  135. if(m == MacroReload)
  136. {
  137. std::vector<std::string> filenames;
  138. this->GetFilesReplacedDuringGenerate(filenames);
  139. if (filenames.size() > 0)
  140. {
  141. // Convert vector to semi-colon delimited string of filenames:
  142. std::string projects;
  143. std::vector<std::string>::iterator it = filenames.begin();
  144. if (it != filenames.end())
  145. {
  146. projects = *it;
  147. ++it;
  148. }
  149. for (; it != filenames.end(); ++it)
  150. {
  151. projects += ";";
  152. projects += *it;
  153. }
  154. cmCallVisualStudioMacro::CallMacro
  155. (topLevelSlnName, CMAKE_VSMACROS_RELOAD_MACRONAME, projects);
  156. }
  157. }
  158. else if(m == MacroStop)
  159. {
  160. cmCallVisualStudioMacro::CallMacro(topLevelSlnName,
  161. CMAKE_VSMACROS_STOP_MACRONAME, "");
  162. }
  163. }
  164. }
  165. }
  166. //----------------------------------------------------------------------------
  167. std::string cmGlobalVisualStudioGenerator::GetUserMacrosDirectory()
  168. {
  169. return "";
  170. }
  171. //----------------------------------------------------------------------------
  172. void cmGlobalVisualStudioGenerator::FixUtilityDepends()
  173. {
  174. // For VS versions before 8:
  175. //
  176. // When a target that links contains a project-level dependency on a
  177. // library target that library is automatically linked. In order to
  178. // allow utility-style project-level dependencies that do not
  179. // actually link we need to automatically insert an intermediate
  180. // custom target.
  181. //
  182. // Here we edit the utility dependencies of a target to add the
  183. // intermediate custom target when necessary.
  184. for(unsigned i = 0; i < this->LocalGenerators.size(); ++i)
  185. {
  186. cmTargets* targets =
  187. &(this->LocalGenerators[i]->GetMakefile()->GetTargets());
  188. for(cmTargets::iterator tarIt = targets->begin();
  189. tarIt != targets->end(); ++tarIt)
  190. {
  191. this->FixUtilityDependsForTarget(tarIt->second);
  192. }
  193. }
  194. }
  195. //----------------------------------------------------------------------------
  196. void
  197. cmGlobalVisualStudioGenerator::FixUtilityDependsForTarget(cmTarget& target)
  198. {
  199. // Only targets that link need to be fixed.
  200. if(target.GetType() != cmTarget::STATIC_LIBRARY &&
  201. target.GetType() != cmTarget::SHARED_LIBRARY &&
  202. target.GetType() != cmTarget::MODULE_LIBRARY &&
  203. target.GetType() != cmTarget::EXECUTABLE)
  204. {
  205. return;
  206. }
  207. // Look at each utility dependency.
  208. for(std::set<cmStdString>::const_iterator ui =
  209. target.GetUtilities().begin();
  210. ui != target.GetUtilities().end(); ++ui)
  211. {
  212. if(cmTarget* depTarget = this->FindTarget(0, ui->c_str(), false))
  213. {
  214. if(depTarget->GetType() == cmTarget::STATIC_LIBRARY ||
  215. depTarget->GetType() == cmTarget::SHARED_LIBRARY ||
  216. depTarget->GetType() == cmTarget::MODULE_LIBRARY)
  217. {
  218. // This utility dependency will cause an attempt to link. If
  219. // the depender does not already link the dependee we need an
  220. // intermediate target.
  221. if(!this->CheckTargetLinks(target, ui->c_str()))
  222. {
  223. this->CreateUtilityDependTarget(*depTarget);
  224. }
  225. }
  226. }
  227. }
  228. }
  229. //----------------------------------------------------------------------------
  230. void
  231. cmGlobalVisualStudioGenerator::CreateUtilityDependTarget(cmTarget& target)
  232. {
  233. // This target is a library on which a utility dependency exists.
  234. // We need to create an intermediate custom target to hook up the
  235. // dependency without causing a link.
  236. const char* altName = target.GetProperty("ALTERNATIVE_DEPENDENCY_NAME");
  237. if(!altName)
  238. {
  239. // Create the intermediate utility target.
  240. std::string altNameStr = target.GetName();
  241. altNameStr += "_UTILITY";
  242. const std::vector<std::string> no_depends;
  243. cmCustomCommandLines no_commands;
  244. const char* no_working_dir = 0;
  245. const char* no_comment = 0;
  246. target.GetMakefile()->AddUtilityCommand(altNameStr.c_str(), true,
  247. no_working_dir, no_depends,
  248. no_commands, false, no_comment);
  249. target.SetProperty("ALTERNATIVE_DEPENDENCY_NAME", altNameStr.c_str());
  250. // Most targets have a GUID created in ConfigureFinalPass. Since
  251. // that has already been called, create one for this target now.
  252. this->CreateGUID(altNameStr.c_str());
  253. // The intermediate target should depend on the original target.
  254. if(cmTarget* alt = this->FindTarget(0, altNameStr.c_str(), false))
  255. {
  256. alt->AddUtility(target.GetName());
  257. }
  258. }
  259. }
  260. //----------------------------------------------------------------------------
  261. bool cmGlobalVisualStudioGenerator::CheckTargetLinks(cmTarget& target,
  262. const char* name)
  263. {
  264. // Return whether the given target links to a target with the given name.
  265. if(target.GetType() == cmTarget::STATIC_LIBRARY)
  266. {
  267. // Static libraries never link to anything.
  268. return false;
  269. }
  270. cmTarget::LinkLibraryVectorType const& libs = target.GetLinkLibraries();
  271. for(cmTarget::LinkLibraryVectorType::const_iterator i = libs.begin();
  272. i != libs.end(); ++i)
  273. {
  274. if(i->first == name)
  275. {
  276. return true;
  277. }
  278. }
  279. return false;
  280. }
  281. //----------------------------------------------------------------------------
  282. const char*
  283. cmGlobalVisualStudioGenerator::GetUtilityForTarget(cmTarget& target,
  284. const char* name)
  285. {
  286. // Handle the external MS project special case.
  287. if(strncmp(name, "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  288. {
  289. // Note from Ken:
  290. // kind of weird removing the first 27 letters. my
  291. // recommendatsions: use cmCustomCommand::GetCommand() to get the
  292. // project name or get rid of the target name starting with
  293. // "INCLUDE_EXTERNAL_MSPROJECT_" and use another indicator/flag
  294. // somewhere. These external project names shouldn't conflict
  295. // with cmake target names anyways.
  296. return name+27;
  297. }
  298. // Possibly depend on an intermediate utility target to avoid
  299. // linking.
  300. if(target.GetType() == cmTarget::STATIC_LIBRARY ||
  301. target.GetType() == cmTarget::SHARED_LIBRARY ||
  302. target.GetType() == cmTarget::MODULE_LIBRARY ||
  303. target.GetType() == cmTarget::EXECUTABLE)
  304. {
  305. // The depender is a target that links. Lookup the dependee to
  306. // see if it provides an alternative dependency name.
  307. if(cmTarget* depTarget = this->FindTarget(0, name, false))
  308. {
  309. // Check for an alternative name created by FixUtilityDepends.
  310. if(const char* altName =
  311. depTarget->GetProperty("ALTERNATIVE_DEPENDENCY_NAME"))
  312. {
  313. // The alternative name is needed only if the depender does
  314. // not really link to the dependee.
  315. if(!this->CheckTargetLinks(target, name))
  316. {
  317. return altName;
  318. }
  319. }
  320. }
  321. }
  322. // No special case. Just use the original dependency name.
  323. return name;
  324. }
  325. //----------------------------------------------------------------------------
  326. #include <windows.h>
  327. //----------------------------------------------------------------------------
  328. bool IsVisualStudioMacrosFileRegistered(const std::string& macrosFile,
  329. std::string& nextAvailableSubKeyName)
  330. {
  331. bool macrosRegistered = false;
  332. std::string s1;
  333. std::string s2;
  334. // Make lowercase local copies, convert to Unix slashes, and
  335. // see if the resulting strings are the same:
  336. s1 = cmSystemTools::LowerCase(macrosFile);
  337. cmSystemTools::ConvertToUnixSlashes(s1);
  338. std::string keyname;
  339. HKEY hkey = NULL;
  340. LONG result = ERROR_SUCCESS;
  341. DWORD index = 0;
  342. keyname =
  343. "Software\\Microsoft\\VisualStudio\\8.0\\vsmacros\\OtherProjects7";
  344. hkey = NULL;
  345. result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(),
  346. 0, KEY_READ, &hkey);
  347. if (ERROR_SUCCESS == result)
  348. {
  349. // Iterate the subkeys and look for the values of interest in each subkey:
  350. CHAR subkeyname[256];
  351. DWORD cch_subkeyname = sizeof(subkeyname)/sizeof(subkeyname[0]);
  352. CHAR keyclass[256];
  353. DWORD cch_keyclass = sizeof(keyclass)/sizeof(keyclass[0]);
  354. FILETIME lastWriteTime;
  355. lastWriteTime.dwHighDateTime = 0;
  356. lastWriteTime.dwLowDateTime = 0;
  357. while (ERROR_SUCCESS == RegEnumKeyEx(hkey, index, subkeyname,
  358. &cch_subkeyname,
  359. 0, keyclass, &cch_keyclass, &lastWriteTime))
  360. {
  361. // Open the subkey and query the values of interest:
  362. HKEY hsubkey = NULL;
  363. result = RegOpenKeyEx(hkey, subkeyname, 0, KEY_READ, &hsubkey);
  364. if (ERROR_SUCCESS == result)
  365. {
  366. DWORD valueType = REG_SZ;
  367. CHAR data1[256];
  368. DWORD cch_data1 = sizeof(data1)/sizeof(data1[0]);
  369. RegQueryValueEx(hsubkey, "Path", 0, &valueType,
  370. (LPBYTE) &data1[0], &cch_data1);
  371. DWORD data2 = 0;
  372. DWORD cch_data2 = sizeof(data2);
  373. RegQueryValueEx(hsubkey, "Security", 0, &valueType,
  374. (LPBYTE) &data2, &cch_data2);
  375. DWORD data3 = 0;
  376. DWORD cch_data3 = sizeof(data3);
  377. RegQueryValueEx(hsubkey, "StorageFormat", 0, &valueType,
  378. (LPBYTE) &data3, &cch_data3);
  379. s2 = cmSystemTools::LowerCase(data1);
  380. cmSystemTools::ConvertToUnixSlashes(s2);
  381. if (s2 == s1)
  382. {
  383. macrosRegistered = true;
  384. }
  385. std::string fullname(data1);
  386. std::string filename;
  387. std::string filepath;
  388. std::string filepathname;
  389. std::string filepathpath;
  390. if (cmSystemTools::FileExists(fullname.c_str()))
  391. {
  392. filename = cmSystemTools::GetFilenameName(fullname);
  393. filepath = cmSystemTools::GetFilenamePath(fullname);
  394. filepathname = cmSystemTools::GetFilenameName(filepath);
  395. filepathpath = cmSystemTools::GetFilenamePath(filepath);
  396. }
  397. //std::cout << keyname << "\\" << subkeyname << ":" << std::endl;
  398. //std::cout << " Path: " << data1 << std::endl;
  399. //std::cout << " Security: " << data2 << std::endl;
  400. //std::cout << " StorageFormat: " << data3 << std::endl;
  401. //std::cout << " filename: " << filename << std::endl;
  402. //std::cout << " filepath: " << filepath << std::endl;
  403. //std::cout << " filepathname: " << filepathname << std::endl;
  404. //std::cout << " filepathpath: " << filepathpath << std::endl;
  405. //std::cout << std::endl;
  406. RegCloseKey(hsubkey);
  407. }
  408. else
  409. {
  410. std::cout << "error opening subkey: " << subkeyname << std::endl;
  411. std::cout << std::endl;
  412. }
  413. ++index;
  414. cch_subkeyname = sizeof(subkeyname)/sizeof(subkeyname[0]);
  415. cch_keyclass = sizeof(keyclass)/sizeof(keyclass[0]);
  416. lastWriteTime.dwHighDateTime = 0;
  417. lastWriteTime.dwLowDateTime = 0;
  418. }
  419. RegCloseKey(hkey);
  420. }
  421. else
  422. {
  423. std::cout << "error opening key: " << keyname << std::endl;
  424. std::cout << std::endl;
  425. }
  426. // Pass back next available sub key name, assuming sub keys always
  427. // follow the expected naming scheme. Expected naming scheme is that
  428. // the subkeys of OtherProjects7 is 0 to n-1, so it's ok to use "n"
  429. // as the name of the next subkey.
  430. std::ostringstream ossNext;
  431. ossNext << index;
  432. nextAvailableSubKeyName = ossNext.str();
  433. keyname =
  434. "Software\\Microsoft\\VisualStudio\\8.0\\vsmacros\\RecordingProject7";
  435. hkey = NULL;
  436. result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(),
  437. 0, KEY_READ, &hkey);
  438. if (ERROR_SUCCESS == result)
  439. {
  440. DWORD valueType = REG_SZ;
  441. CHAR data1[256];
  442. DWORD cch_data1 = sizeof(data1)/sizeof(data1[0]);
  443. RegQueryValueEx(hkey, "Path", 0, &valueType,
  444. (LPBYTE) &data1[0], &cch_data1);
  445. DWORD data2 = 0;
  446. DWORD cch_data2 = sizeof(data2);
  447. RegQueryValueEx(hkey, "Security", 0, &valueType,
  448. (LPBYTE) &data2, &cch_data2);
  449. DWORD data3 = 0;
  450. DWORD cch_data3 = sizeof(data3);
  451. RegQueryValueEx(hkey, "StorageFormat", 0, &valueType,
  452. (LPBYTE) &data3, &cch_data3);
  453. s2 = cmSystemTools::LowerCase(data1);
  454. cmSystemTools::ConvertToUnixSlashes(s2);
  455. if (s2 == s1)
  456. {
  457. macrosRegistered = true;
  458. }
  459. //std::cout << keyname << ":" << std::endl;
  460. //std::cout << " Path: " << data1 << std::endl;
  461. //std::cout << " Security: " << data2 << std::endl;
  462. //std::cout << " StorageFormat: " << data3 << std::endl;
  463. //std::cout << std::endl;
  464. RegCloseKey(hkey);
  465. }
  466. else
  467. {
  468. std::cout << "error opening key: " << keyname << std::endl;
  469. std::cout << std::endl;
  470. }
  471. return macrosRegistered;
  472. }
  473. //----------------------------------------------------------------------------
  474. void WriteVSMacrosFileRegistryEntry(
  475. const std::string& nextAvailableSubKeyName,
  476. const std::string& macrosFile)
  477. {
  478. std::string keyname =
  479. "Software\\Microsoft\\VisualStudio\\8.0\\vsmacros\\OtherProjects7";
  480. HKEY hkey = NULL;
  481. LONG result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(), 0,
  482. KEY_READ|KEY_WRITE, &hkey);
  483. if (ERROR_SUCCESS == result)
  484. {
  485. // Create the subkey and set the values of interest:
  486. HKEY hsubkey = NULL;
  487. result = RegCreateKeyEx(hkey, nextAvailableSubKeyName.c_str(), 0, "", 0,
  488. KEY_READ|KEY_WRITE, 0, &hsubkey, 0);
  489. if (ERROR_SUCCESS == result)
  490. {
  491. DWORD dw = 0;
  492. std::string s(macrosFile);
  493. cmSystemTools::ReplaceString(s, "/", "\\");
  494. result = RegSetValueEx(hsubkey, "Path", 0, REG_SZ, (LPBYTE) s.c_str(),
  495. static_cast<DWORD>(strlen(s.c_str()) + 1));
  496. if (ERROR_SUCCESS != result)
  497. {
  498. std::cout << "error result 1: " << result << std::endl;
  499. std::cout << std::endl;
  500. }
  501. // Security value is always "1" for sample macros files (seems to be "2"
  502. // if you put the file somewhere outside the standard VSMacros folder)
  503. dw = 1;
  504. result = RegSetValueEx(hsubkey, "Security",
  505. 0, REG_DWORD, (LPBYTE) &dw, sizeof(DWORD));
  506. if (ERROR_SUCCESS != result)
  507. {
  508. std::cout << "error result 2: " << result << std::endl;
  509. std::cout << std::endl;
  510. }
  511. // StorageFormat value is always "0" for sample macros files
  512. dw = 0;
  513. result = RegSetValueEx(hsubkey, "StorageFormat",
  514. 0, REG_DWORD, (LPBYTE) &dw, sizeof(DWORD));
  515. if (ERROR_SUCCESS != result)
  516. {
  517. std::cout << "error result 3: " << result << std::endl;
  518. std::cout << std::endl;
  519. }
  520. RegCloseKey(hsubkey);
  521. }
  522. else
  523. {
  524. std::cout << "error creating subkey: "
  525. << nextAvailableSubKeyName << std::endl;
  526. std::cout << std::endl;
  527. }
  528. RegCloseKey(hkey);
  529. }
  530. else
  531. {
  532. std::cout << "error opening key: " << keyname << std::endl;
  533. std::cout << std::endl;
  534. }
  535. }
  536. //----------------------------------------------------------------------------
  537. void RegisterVisualStudioMacros(const std::string& macrosFile)
  538. {
  539. bool macrosRegistered;
  540. std::string nextAvailableSubKeyName;
  541. macrosRegistered = IsVisualStudioMacrosFileRegistered(macrosFile,
  542. nextAvailableSubKeyName);
  543. if (!macrosRegistered)
  544. {
  545. int count = cmCallVisualStudioMacro::
  546. GetNumberOfRunningVisualStudioInstances("ALL");
  547. // Only register the macros file if there are *no* instances of Visual
  548. // Studio running. If we register it while one is running, first, it has
  549. // no effect on the running instance; second, and worse, Visual Studio
  550. // removes our newly added registration entry when it quits. Instead,
  551. // emit a warning asking the user to exit all running Visual Studio
  552. // instances...
  553. //
  554. if (0 != count)
  555. {
  556. std::ostringstream oss;
  557. oss << "Could not register CMake's Visual Studio macros file '"
  558. << CMAKE_VSMACROS_FILENAME "' while Visual Studio is running."
  559. << " Please exit all running instances of Visual Studio before"
  560. << " continuing." << std::endl
  561. << std::endl
  562. << "CMake needs to register Visual Studio macros when its macros"
  563. << " file is updated or when it detects that its current macros file"
  564. << " is no longer registered with Visual Studio."
  565. << std::endl;
  566. cmSystemTools::Message(oss.str().c_str(), "Warning");
  567. // Count them again now that the warning is over. In the case of a GUI
  568. // warning, the user may have gone to close Visual Studio and then come
  569. // back to the CMake GUI and clicked ok on the above warning. If so,
  570. // then register the macros *now* if the count is *now* 0...
  571. //
  572. count = cmCallVisualStudioMacro::
  573. GetNumberOfRunningVisualStudioInstances("ALL");
  574. // Also re-get the nextAvailableSubKeyName in case Visual Studio
  575. // wrote out new registered macros information as it was exiting:
  576. //
  577. if (0 == count)
  578. {
  579. IsVisualStudioMacrosFileRegistered(macrosFile,
  580. nextAvailableSubKeyName);
  581. }
  582. }
  583. // Do another if check - 'count' may have changed inside the above if:
  584. //
  585. if (0 == count)
  586. {
  587. WriteVSMacrosFileRegistryEntry(nextAvailableSubKeyName, macrosFile);
  588. }
  589. }
  590. }