cmGlobalVisualStudioGenerator.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. // Skip for VS versions 8 and above.
  175. if(!this->VSLinksDependencies())
  176. {
  177. return;
  178. }
  179. // For VS versions before 8:
  180. //
  181. // When a target that links contains a project-level dependency on a
  182. // library target that library is automatically linked. In order to
  183. // allow utility-style project-level dependencies that do not
  184. // actually link we need to automatically insert an intermediate
  185. // custom target.
  186. //
  187. // Here we edit the utility dependencies of a target to add the
  188. // intermediate custom target when necessary.
  189. for(unsigned i = 0; i < this->LocalGenerators.size(); ++i)
  190. {
  191. cmTargets* targets =
  192. &(this->LocalGenerators[i]->GetMakefile()->GetTargets());
  193. for(cmTargets::iterator tarIt = targets->begin();
  194. tarIt != targets->end(); ++tarIt)
  195. {
  196. this->FixUtilityDependsForTarget(tarIt->second);
  197. }
  198. }
  199. }
  200. //----------------------------------------------------------------------------
  201. void
  202. cmGlobalVisualStudioGenerator::FixUtilityDependsForTarget(cmTarget& target)
  203. {
  204. // Only targets that link need to be fixed.
  205. if(target.GetType() != cmTarget::STATIC_LIBRARY &&
  206. target.GetType() != cmTarget::SHARED_LIBRARY &&
  207. target.GetType() != cmTarget::MODULE_LIBRARY &&
  208. target.GetType() != cmTarget::EXECUTABLE)
  209. {
  210. return;
  211. }
  212. #if 0
  213. // This feature makes a mess in SLN files for VS 7.1 and below. It
  214. // creates an extra target for every target that is "linked" by a
  215. // static library. Without this feature static libraries do not
  216. // wait until their "link" dependencies are built to build. This is
  217. // not a problem 99.9% of the time, and projects that do have the
  218. // problem can enable this work-around by using add_dependencies.
  219. // Static libraries cannot depend directly on the targets to which
  220. // they link because VS will copy those targets into the library
  221. // (for VS < 8). To work around the problem we copy the
  222. // dependencies to be utility dependencies so that the work-around
  223. // below is used.
  224. if(target.GetType() == cmTarget::STATIC_LIBRARY)
  225. {
  226. cmTarget::LinkLibraryVectorType const& libs = target.GetLinkLibraries();
  227. for(cmTarget::LinkLibraryVectorType::const_iterator i = libs.begin();
  228. i != libs.end(); ++i)
  229. {
  230. if(cmTarget* depTarget = this->FindTarget(0, i->first.c_str(), false))
  231. {
  232. target.AddUtility(depTarget->GetName());
  233. }
  234. }
  235. }
  236. #endif
  237. // Look at each utility dependency.
  238. for(std::set<cmStdString>::const_iterator ui =
  239. target.GetUtilities().begin();
  240. ui != target.GetUtilities().end(); ++ui)
  241. {
  242. if(cmTarget* depTarget = this->FindTarget(0, ui->c_str(), false))
  243. {
  244. if(depTarget->GetType() == cmTarget::STATIC_LIBRARY ||
  245. depTarget->GetType() == cmTarget::SHARED_LIBRARY ||
  246. depTarget->GetType() == cmTarget::MODULE_LIBRARY)
  247. {
  248. // This utility dependency will cause an attempt to link. If
  249. // the depender does not already link the dependee we need an
  250. // intermediate target.
  251. if(!this->CheckTargetLinks(target, ui->c_str()))
  252. {
  253. this->CreateUtilityDependTarget(*depTarget);
  254. }
  255. }
  256. }
  257. }
  258. }
  259. //----------------------------------------------------------------------------
  260. void
  261. cmGlobalVisualStudioGenerator::CreateUtilityDependTarget(cmTarget& target)
  262. {
  263. // This target is a library on which a utility dependency exists.
  264. // We need to create an intermediate custom target to hook up the
  265. // dependency without causing a link.
  266. const char* altName = target.GetProperty("ALTERNATIVE_DEPENDENCY_NAME");
  267. if(!altName)
  268. {
  269. // Create the intermediate utility target.
  270. std::string altNameStr = target.GetName();
  271. altNameStr += "_UTILITY";
  272. const std::vector<std::string> no_depends;
  273. cmCustomCommandLines no_commands;
  274. const char* no_working_dir = 0;
  275. const char* no_comment = 0;
  276. target.GetMakefile()->AddUtilityCommand(altNameStr.c_str(), true,
  277. no_working_dir, no_depends,
  278. no_commands, false, no_comment);
  279. target.SetProperty("ALTERNATIVE_DEPENDENCY_NAME", altNameStr.c_str());
  280. // Most targets have a GUID created in ConfigureFinalPass. Since
  281. // that has already been called, create one for this target now.
  282. this->CreateGUID(altNameStr.c_str());
  283. // The intermediate target should depend on the original target.
  284. if(cmTarget* alt = this->FindTarget(0, altNameStr.c_str(), false))
  285. {
  286. alt->AddUtility(target.GetName());
  287. }
  288. }
  289. }
  290. //----------------------------------------------------------------------------
  291. bool cmGlobalVisualStudioGenerator::CheckTargetLinks(cmTarget& target,
  292. const char* name)
  293. {
  294. // Return whether the given target links to a target with the given name.
  295. if(target.GetType() == cmTarget::STATIC_LIBRARY)
  296. {
  297. // Static libraries never link to anything.
  298. return false;
  299. }
  300. cmTarget::LinkLibraryVectorType const& libs = target.GetLinkLibraries();
  301. for(cmTarget::LinkLibraryVectorType::const_iterator i = libs.begin();
  302. i != libs.end(); ++i)
  303. {
  304. if(i->first == name)
  305. {
  306. return true;
  307. }
  308. }
  309. return false;
  310. }
  311. //----------------------------------------------------------------------------
  312. const char*
  313. cmGlobalVisualStudioGenerator::GetUtilityForTarget(cmTarget& target,
  314. const char* name)
  315. {
  316. // Handle the external MS project special case.
  317. if(strncmp(name, "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  318. {
  319. // Note from Ken:
  320. // kind of weird removing the first 27 letters. my
  321. // recommendatsions: use cmCustomCommand::GetCommand() to get the
  322. // project name or get rid of the target name starting with
  323. // "INCLUDE_EXTERNAL_MSPROJECT_" and use another indicator/flag
  324. // somewhere. These external project names shouldn't conflict
  325. // with cmake target names anyways.
  326. return name+27;
  327. }
  328. // Possibly depend on an intermediate utility target to avoid
  329. // linking.
  330. if(target.GetType() == cmTarget::STATIC_LIBRARY ||
  331. target.GetType() == cmTarget::SHARED_LIBRARY ||
  332. target.GetType() == cmTarget::MODULE_LIBRARY ||
  333. target.GetType() == cmTarget::EXECUTABLE)
  334. {
  335. // The depender is a target that links. Lookup the dependee to
  336. // see if it provides an alternative dependency name.
  337. if(cmTarget* depTarget = this->FindTarget(0, name, false))
  338. {
  339. // Check for an alternative name created by FixUtilityDepends.
  340. if(const char* altName =
  341. depTarget->GetProperty("ALTERNATIVE_DEPENDENCY_NAME"))
  342. {
  343. // The alternative name is needed only if the depender does
  344. // not really link to the dependee.
  345. if(!this->CheckTargetLinks(target, name))
  346. {
  347. return altName;
  348. }
  349. }
  350. }
  351. }
  352. // No special case. Just use the original dependency name.
  353. return name;
  354. }
  355. //----------------------------------------------------------------------------
  356. #include <windows.h>
  357. //----------------------------------------------------------------------------
  358. bool IsVisualStudioMacrosFileRegistered(const std::string& macrosFile,
  359. std::string& nextAvailableSubKeyName)
  360. {
  361. bool macrosRegistered = false;
  362. std::string s1;
  363. std::string s2;
  364. // Make lowercase local copies, convert to Unix slashes, and
  365. // see if the resulting strings are the same:
  366. s1 = cmSystemTools::LowerCase(macrosFile);
  367. cmSystemTools::ConvertToUnixSlashes(s1);
  368. std::string keyname;
  369. HKEY hkey = NULL;
  370. LONG result = ERROR_SUCCESS;
  371. DWORD index = 0;
  372. keyname =
  373. "Software\\Microsoft\\VisualStudio\\8.0\\vsmacros\\OtherProjects7";
  374. hkey = NULL;
  375. result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(),
  376. 0, KEY_READ, &hkey);
  377. if (ERROR_SUCCESS == result)
  378. {
  379. // Iterate the subkeys and look for the values of interest in each subkey:
  380. CHAR subkeyname[256];
  381. DWORD cch_subkeyname = sizeof(subkeyname)/sizeof(subkeyname[0]);
  382. CHAR keyclass[256];
  383. DWORD cch_keyclass = sizeof(keyclass)/sizeof(keyclass[0]);
  384. FILETIME lastWriteTime;
  385. lastWriteTime.dwHighDateTime = 0;
  386. lastWriteTime.dwLowDateTime = 0;
  387. while (ERROR_SUCCESS == RegEnumKeyEx(hkey, index, subkeyname,
  388. &cch_subkeyname,
  389. 0, keyclass, &cch_keyclass, &lastWriteTime))
  390. {
  391. // Open the subkey and query the values of interest:
  392. HKEY hsubkey = NULL;
  393. result = RegOpenKeyEx(hkey, subkeyname, 0, KEY_READ, &hsubkey);
  394. if (ERROR_SUCCESS == result)
  395. {
  396. DWORD valueType = REG_SZ;
  397. CHAR data1[256];
  398. DWORD cch_data1 = sizeof(data1)/sizeof(data1[0]);
  399. RegQueryValueEx(hsubkey, "Path", 0, &valueType,
  400. (LPBYTE) &data1[0], &cch_data1);
  401. DWORD data2 = 0;
  402. DWORD cch_data2 = sizeof(data2);
  403. RegQueryValueEx(hsubkey, "Security", 0, &valueType,
  404. (LPBYTE) &data2, &cch_data2);
  405. DWORD data3 = 0;
  406. DWORD cch_data3 = sizeof(data3);
  407. RegQueryValueEx(hsubkey, "StorageFormat", 0, &valueType,
  408. (LPBYTE) &data3, &cch_data3);
  409. s2 = cmSystemTools::LowerCase(data1);
  410. cmSystemTools::ConvertToUnixSlashes(s2);
  411. if (s2 == s1)
  412. {
  413. macrosRegistered = true;
  414. }
  415. std::string fullname(data1);
  416. std::string filename;
  417. std::string filepath;
  418. std::string filepathname;
  419. std::string filepathpath;
  420. if (cmSystemTools::FileExists(fullname.c_str()))
  421. {
  422. filename = cmSystemTools::GetFilenameName(fullname);
  423. filepath = cmSystemTools::GetFilenamePath(fullname);
  424. filepathname = cmSystemTools::GetFilenameName(filepath);
  425. filepathpath = cmSystemTools::GetFilenamePath(filepath);
  426. }
  427. //std::cout << keyname << "\\" << subkeyname << ":" << std::endl;
  428. //std::cout << " Path: " << data1 << std::endl;
  429. //std::cout << " Security: " << data2 << std::endl;
  430. //std::cout << " StorageFormat: " << data3 << std::endl;
  431. //std::cout << " filename: " << filename << std::endl;
  432. //std::cout << " filepath: " << filepath << std::endl;
  433. //std::cout << " filepathname: " << filepathname << std::endl;
  434. //std::cout << " filepathpath: " << filepathpath << std::endl;
  435. //std::cout << std::endl;
  436. RegCloseKey(hsubkey);
  437. }
  438. else
  439. {
  440. std::cout << "error opening subkey: " << subkeyname << std::endl;
  441. std::cout << std::endl;
  442. }
  443. ++index;
  444. cch_subkeyname = sizeof(subkeyname)/sizeof(subkeyname[0]);
  445. cch_keyclass = sizeof(keyclass)/sizeof(keyclass[0]);
  446. lastWriteTime.dwHighDateTime = 0;
  447. lastWriteTime.dwLowDateTime = 0;
  448. }
  449. RegCloseKey(hkey);
  450. }
  451. else
  452. {
  453. std::cout << "error opening key: " << keyname << std::endl;
  454. std::cout << std::endl;
  455. }
  456. // Pass back next available sub key name, assuming sub keys always
  457. // follow the expected naming scheme. Expected naming scheme is that
  458. // the subkeys of OtherProjects7 is 0 to n-1, so it's ok to use "n"
  459. // as the name of the next subkey.
  460. std::ostringstream ossNext;
  461. ossNext << index;
  462. nextAvailableSubKeyName = ossNext.str();
  463. keyname =
  464. "Software\\Microsoft\\VisualStudio\\8.0\\vsmacros\\RecordingProject7";
  465. hkey = NULL;
  466. result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(),
  467. 0, KEY_READ, &hkey);
  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(hkey, "Path", 0, &valueType,
  474. (LPBYTE) &data1[0], &cch_data1);
  475. DWORD data2 = 0;
  476. DWORD cch_data2 = sizeof(data2);
  477. RegQueryValueEx(hkey, "Security", 0, &valueType,
  478. (LPBYTE) &data2, &cch_data2);
  479. DWORD data3 = 0;
  480. DWORD cch_data3 = sizeof(data3);
  481. RegQueryValueEx(hkey, "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::cout << keyname << ":" << std::endl;
  490. //std::cout << " Path: " << data1 << std::endl;
  491. //std::cout << " Security: " << data2 << std::endl;
  492. //std::cout << " StorageFormat: " << data3 << std::endl;
  493. //std::cout << std::endl;
  494. RegCloseKey(hkey);
  495. }
  496. else
  497. {
  498. std::cout << "error opening key: " << keyname << std::endl;
  499. std::cout << std::endl;
  500. }
  501. return macrosRegistered;
  502. }
  503. //----------------------------------------------------------------------------
  504. void WriteVSMacrosFileRegistryEntry(
  505. const std::string& nextAvailableSubKeyName,
  506. const std::string& macrosFile)
  507. {
  508. std::string keyname =
  509. "Software\\Microsoft\\VisualStudio\\8.0\\vsmacros\\OtherProjects7";
  510. HKEY hkey = NULL;
  511. LONG result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(), 0,
  512. KEY_READ|KEY_WRITE, &hkey);
  513. if (ERROR_SUCCESS == result)
  514. {
  515. // Create the subkey and set the values of interest:
  516. HKEY hsubkey = NULL;
  517. result = RegCreateKeyEx(hkey, nextAvailableSubKeyName.c_str(), 0, "", 0,
  518. KEY_READ|KEY_WRITE, 0, &hsubkey, 0);
  519. if (ERROR_SUCCESS == result)
  520. {
  521. DWORD dw = 0;
  522. std::string s(macrosFile);
  523. cmSystemTools::ReplaceString(s, "/", "\\");
  524. result = RegSetValueEx(hsubkey, "Path", 0, REG_SZ, (LPBYTE) s.c_str(),
  525. static_cast<DWORD>(strlen(s.c_str()) + 1));
  526. if (ERROR_SUCCESS != result)
  527. {
  528. std::cout << "error result 1: " << result << std::endl;
  529. std::cout << std::endl;
  530. }
  531. // Security value is always "1" for sample macros files (seems to be "2"
  532. // if you put the file somewhere outside the standard VSMacros folder)
  533. dw = 1;
  534. result = RegSetValueEx(hsubkey, "Security",
  535. 0, REG_DWORD, (LPBYTE) &dw, sizeof(DWORD));
  536. if (ERROR_SUCCESS != result)
  537. {
  538. std::cout << "error result 2: " << result << std::endl;
  539. std::cout << std::endl;
  540. }
  541. // StorageFormat value is always "0" for sample macros files
  542. dw = 0;
  543. result = RegSetValueEx(hsubkey, "StorageFormat",
  544. 0, REG_DWORD, (LPBYTE) &dw, sizeof(DWORD));
  545. if (ERROR_SUCCESS != result)
  546. {
  547. std::cout << "error result 3: " << result << std::endl;
  548. std::cout << std::endl;
  549. }
  550. RegCloseKey(hsubkey);
  551. }
  552. else
  553. {
  554. std::cout << "error creating subkey: "
  555. << nextAvailableSubKeyName << std::endl;
  556. std::cout << std::endl;
  557. }
  558. RegCloseKey(hkey);
  559. }
  560. else
  561. {
  562. std::cout << "error opening key: " << keyname << std::endl;
  563. std::cout << std::endl;
  564. }
  565. }
  566. //----------------------------------------------------------------------------
  567. void RegisterVisualStudioMacros(const std::string& macrosFile)
  568. {
  569. bool macrosRegistered;
  570. std::string nextAvailableSubKeyName;
  571. macrosRegistered = IsVisualStudioMacrosFileRegistered(macrosFile,
  572. nextAvailableSubKeyName);
  573. if (!macrosRegistered)
  574. {
  575. int count = cmCallVisualStudioMacro::
  576. GetNumberOfRunningVisualStudioInstances("ALL");
  577. // Only register the macros file if there are *no* instances of Visual
  578. // Studio running. If we register it while one is running, first, it has
  579. // no effect on the running instance; second, and worse, Visual Studio
  580. // removes our newly added registration entry when it quits. Instead,
  581. // emit a warning asking the user to exit all running Visual Studio
  582. // instances...
  583. //
  584. if (0 != count)
  585. {
  586. std::ostringstream oss;
  587. oss << "Could not register CMake's Visual Studio macros file '"
  588. << CMAKE_VSMACROS_FILENAME "' while Visual Studio is running."
  589. << " Please exit all running instances of Visual Studio before"
  590. << " continuing." << std::endl
  591. << std::endl
  592. << "CMake needs to register Visual Studio macros when its macros"
  593. << " file is updated or when it detects that its current macros file"
  594. << " is no longer registered with Visual Studio."
  595. << std::endl;
  596. cmSystemTools::Message(oss.str().c_str(), "Warning");
  597. // Count them again now that the warning is over. In the case of a GUI
  598. // warning, the user may have gone to close Visual Studio and then come
  599. // back to the CMake GUI and clicked ok on the above warning. If so,
  600. // then register the macros *now* if the count is *now* 0...
  601. //
  602. count = cmCallVisualStudioMacro::
  603. GetNumberOfRunningVisualStudioInstances("ALL");
  604. // Also re-get the nextAvailableSubKeyName in case Visual Studio
  605. // wrote out new registered macros information as it was exiting:
  606. //
  607. if (0 == count)
  608. {
  609. IsVisualStudioMacrosFileRegistered(macrosFile,
  610. nextAvailableSubKeyName);
  611. }
  612. }
  613. // Do another if check - 'count' may have changed inside the above if:
  614. //
  615. if (0 == count)
  616. {
  617. WriteVSMacrosFileRegistryEntry(nextAvailableSubKeyName, macrosFile);
  618. }
  619. }
  620. }