cmGlobalVisualStudioGenerator.cxx 25 KB

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