cmGlobalVisualStudioGenerator.cxx 24 KB

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