cmGlobalVisualStudioGenerator.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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. // Handle the external MS project special case.
  343. if(strncmp(name, "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  344. {
  345. // Note from Ken:
  346. // kind of weird removing the first 27 letters. my
  347. // recommendatsions: use cmCustomCommand::GetCommand() to get the
  348. // project name or get rid of the target name starting with
  349. // "INCLUDE_EXTERNAL_MSPROJECT_" and use another indicator/flag
  350. // somewhere. These external project names shouldn't conflict
  351. // with cmake target names anyways.
  352. return name+27;
  353. }
  354. // Possibly depend on an intermediate utility target to avoid
  355. // linking.
  356. if(target.GetType() == cmTarget::STATIC_LIBRARY ||
  357. target.GetType() == cmTarget::SHARED_LIBRARY ||
  358. target.GetType() == cmTarget::MODULE_LIBRARY ||
  359. target.GetType() == cmTarget::EXECUTABLE)
  360. {
  361. // The depender is a target that links. Lookup the dependee to
  362. // see if it provides an alternative dependency name.
  363. if(cmTarget* depTarget = this->FindTarget(0, name))
  364. {
  365. // Check for an alternative name created by FixUtilityDepends.
  366. if(const char* altName =
  367. depTarget->GetProperty("ALTERNATIVE_DEPENDENCY_NAME"))
  368. {
  369. // The alternative name is needed only if the depender does
  370. // not really link to the dependee.
  371. if(!this->CheckTargetLinks(target, name))
  372. {
  373. return altName;
  374. }
  375. }
  376. }
  377. }
  378. // No special case. Just use the original dependency name.
  379. return name;
  380. }
  381. //----------------------------------------------------------------------------
  382. #include <windows.h>
  383. //----------------------------------------------------------------------------
  384. bool IsVisualStudioMacrosFileRegistered(const std::string& macrosFile,
  385. const std::string& regKeyBase,
  386. std::string& nextAvailableSubKeyName)
  387. {
  388. bool macrosRegistered = false;
  389. std::string s1;
  390. std::string s2;
  391. // Make lowercase local copies, convert to Unix slashes, and
  392. // see if the resulting strings are the same:
  393. s1 = cmSystemTools::LowerCase(macrosFile);
  394. cmSystemTools::ConvertToUnixSlashes(s1);
  395. std::string keyname;
  396. HKEY hkey = NULL;
  397. LONG result = ERROR_SUCCESS;
  398. DWORD index = 0;
  399. keyname = regKeyBase + "\\OtherProjects7";
  400. hkey = NULL;
  401. result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(),
  402. 0, KEY_READ, &hkey);
  403. if (ERROR_SUCCESS == result)
  404. {
  405. // Iterate the subkeys and look for the values of interest in each subkey:
  406. CHAR subkeyname[256];
  407. DWORD cch_subkeyname = sizeof(subkeyname)/sizeof(subkeyname[0]);
  408. CHAR keyclass[256];
  409. DWORD cch_keyclass = sizeof(keyclass)/sizeof(keyclass[0]);
  410. FILETIME lastWriteTime;
  411. lastWriteTime.dwHighDateTime = 0;
  412. lastWriteTime.dwLowDateTime = 0;
  413. while (ERROR_SUCCESS == RegEnumKeyEx(hkey, index, subkeyname,
  414. &cch_subkeyname,
  415. 0, keyclass, &cch_keyclass, &lastWriteTime))
  416. {
  417. // Open the subkey and query the values of interest:
  418. HKEY hsubkey = NULL;
  419. result = RegOpenKeyEx(hkey, subkeyname, 0, KEY_READ, &hsubkey);
  420. if (ERROR_SUCCESS == result)
  421. {
  422. DWORD valueType = REG_SZ;
  423. CHAR data1[256];
  424. DWORD cch_data1 = sizeof(data1)/sizeof(data1[0]);
  425. RegQueryValueEx(hsubkey, "Path", 0, &valueType,
  426. (LPBYTE) &data1[0], &cch_data1);
  427. DWORD data2 = 0;
  428. DWORD cch_data2 = sizeof(data2);
  429. RegQueryValueEx(hsubkey, "Security", 0, &valueType,
  430. (LPBYTE) &data2, &cch_data2);
  431. DWORD data3 = 0;
  432. DWORD cch_data3 = sizeof(data3);
  433. RegQueryValueEx(hsubkey, "StorageFormat", 0, &valueType,
  434. (LPBYTE) &data3, &cch_data3);
  435. s2 = cmSystemTools::LowerCase(data1);
  436. cmSystemTools::ConvertToUnixSlashes(s2);
  437. if (s2 == s1)
  438. {
  439. macrosRegistered = true;
  440. }
  441. std::string fullname(data1);
  442. std::string filename;
  443. std::string filepath;
  444. std::string filepathname;
  445. std::string filepathpath;
  446. if (cmSystemTools::FileExists(fullname.c_str()))
  447. {
  448. filename = cmSystemTools::GetFilenameName(fullname);
  449. filepath = cmSystemTools::GetFilenamePath(fullname);
  450. filepathname = cmSystemTools::GetFilenameName(filepath);
  451. filepathpath = cmSystemTools::GetFilenamePath(filepath);
  452. }
  453. //std::cout << keyname << "\\" << subkeyname << ":" << std::endl;
  454. //std::cout << " Path: " << data1 << std::endl;
  455. //std::cout << " Security: " << data2 << std::endl;
  456. //std::cout << " StorageFormat: " << data3 << std::endl;
  457. //std::cout << " filename: " << filename << std::endl;
  458. //std::cout << " filepath: " << filepath << std::endl;
  459. //std::cout << " filepathname: " << filepathname << std::endl;
  460. //std::cout << " filepathpath: " << filepathpath << std::endl;
  461. //std::cout << std::endl;
  462. RegCloseKey(hsubkey);
  463. }
  464. else
  465. {
  466. std::cout << "error opening subkey: " << subkeyname << std::endl;
  467. std::cout << std::endl;
  468. }
  469. ++index;
  470. cch_subkeyname = sizeof(subkeyname)/sizeof(subkeyname[0]);
  471. cch_keyclass = sizeof(keyclass)/sizeof(keyclass[0]);
  472. lastWriteTime.dwHighDateTime = 0;
  473. lastWriteTime.dwLowDateTime = 0;
  474. }
  475. RegCloseKey(hkey);
  476. }
  477. else
  478. {
  479. std::cout << "error opening key: " << keyname << std::endl;
  480. std::cout << std::endl;
  481. }
  482. // Pass back next available sub key name, assuming sub keys always
  483. // follow the expected naming scheme. Expected naming scheme is that
  484. // the subkeys of OtherProjects7 is 0 to n-1, so it's ok to use "n"
  485. // as the name of the next subkey.
  486. std::ostringstream ossNext;
  487. ossNext << index;
  488. nextAvailableSubKeyName = ossNext.str();
  489. keyname = regKeyBase + "\\RecordingProject7";
  490. hkey = NULL;
  491. result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(),
  492. 0, KEY_READ, &hkey);
  493. if (ERROR_SUCCESS == result)
  494. {
  495. DWORD valueType = REG_SZ;
  496. CHAR data1[256];
  497. DWORD cch_data1 = sizeof(data1)/sizeof(data1[0]);
  498. RegQueryValueEx(hkey, "Path", 0, &valueType,
  499. (LPBYTE) &data1[0], &cch_data1);
  500. DWORD data2 = 0;
  501. DWORD cch_data2 = sizeof(data2);
  502. RegQueryValueEx(hkey, "Security", 0, &valueType,
  503. (LPBYTE) &data2, &cch_data2);
  504. DWORD data3 = 0;
  505. DWORD cch_data3 = sizeof(data3);
  506. RegQueryValueEx(hkey, "StorageFormat", 0, &valueType,
  507. (LPBYTE) &data3, &cch_data3);
  508. s2 = cmSystemTools::LowerCase(data1);
  509. cmSystemTools::ConvertToUnixSlashes(s2);
  510. if (s2 == s1)
  511. {
  512. macrosRegistered = true;
  513. }
  514. //std::cout << keyname << ":" << std::endl;
  515. //std::cout << " Path: " << data1 << std::endl;
  516. //std::cout << " Security: " << data2 << std::endl;
  517. //std::cout << " StorageFormat: " << data3 << std::endl;
  518. //std::cout << std::endl;
  519. RegCloseKey(hkey);
  520. }
  521. else
  522. {
  523. std::cout << "error opening key: " << keyname << std::endl;
  524. std::cout << std::endl;
  525. }
  526. return macrosRegistered;
  527. }
  528. //----------------------------------------------------------------------------
  529. void WriteVSMacrosFileRegistryEntry(
  530. const std::string& nextAvailableSubKeyName,
  531. const std::string& macrosFile,
  532. const std::string& regKeyBase)
  533. {
  534. std::string keyname = regKeyBase + "\\OtherProjects7";
  535. HKEY hkey = NULL;
  536. LONG result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(), 0,
  537. KEY_READ|KEY_WRITE, &hkey);
  538. if (ERROR_SUCCESS == result)
  539. {
  540. // Create the subkey and set the values of interest:
  541. HKEY hsubkey = NULL;
  542. result = RegCreateKeyEx(hkey, nextAvailableSubKeyName.c_str(), 0, "", 0,
  543. KEY_READ|KEY_WRITE, 0, &hsubkey, 0);
  544. if (ERROR_SUCCESS == result)
  545. {
  546. DWORD dw = 0;
  547. std::string s(macrosFile);
  548. cmSystemTools::ReplaceString(s, "/", "\\");
  549. result = RegSetValueEx(hsubkey, "Path", 0, REG_SZ, (LPBYTE) s.c_str(),
  550. static_cast<DWORD>(strlen(s.c_str()) + 1));
  551. if (ERROR_SUCCESS != result)
  552. {
  553. std::cout << "error result 1: " << result << std::endl;
  554. std::cout << std::endl;
  555. }
  556. // Security value is always "1" for sample macros files (seems to be "2"
  557. // if you put the file somewhere outside the standard VSMacros folder)
  558. dw = 1;
  559. result = RegSetValueEx(hsubkey, "Security",
  560. 0, REG_DWORD, (LPBYTE) &dw, sizeof(DWORD));
  561. if (ERROR_SUCCESS != result)
  562. {
  563. std::cout << "error result 2: " << result << std::endl;
  564. std::cout << std::endl;
  565. }
  566. // StorageFormat value is always "0" for sample macros files
  567. dw = 0;
  568. result = RegSetValueEx(hsubkey, "StorageFormat",
  569. 0, REG_DWORD, (LPBYTE) &dw, sizeof(DWORD));
  570. if (ERROR_SUCCESS != result)
  571. {
  572. std::cout << "error result 3: " << result << std::endl;
  573. std::cout << std::endl;
  574. }
  575. RegCloseKey(hsubkey);
  576. }
  577. else
  578. {
  579. std::cout << "error creating subkey: "
  580. << nextAvailableSubKeyName << std::endl;
  581. std::cout << std::endl;
  582. }
  583. RegCloseKey(hkey);
  584. }
  585. else
  586. {
  587. std::cout << "error opening key: " << keyname << std::endl;
  588. std::cout << std::endl;
  589. }
  590. }
  591. //----------------------------------------------------------------------------
  592. void RegisterVisualStudioMacros(const std::string& macrosFile,
  593. const std::string& regKeyBase)
  594. {
  595. bool macrosRegistered;
  596. std::string nextAvailableSubKeyName;
  597. macrosRegistered = IsVisualStudioMacrosFileRegistered(macrosFile,
  598. regKeyBase, nextAvailableSubKeyName);
  599. if (!macrosRegistered)
  600. {
  601. int count = cmCallVisualStudioMacro::
  602. GetNumberOfRunningVisualStudioInstances("ALL");
  603. // Only register the macros file if there are *no* instances of Visual
  604. // Studio running. If we register it while one is running, first, it has
  605. // no effect on the running instance; second, and worse, Visual Studio
  606. // removes our newly added registration entry when it quits. Instead,
  607. // emit a warning asking the user to exit all running Visual Studio
  608. // instances...
  609. //
  610. if (0 != count)
  611. {
  612. std::ostringstream oss;
  613. oss << "Could not register CMake's Visual Studio macros file '"
  614. << CMAKE_VSMACROS_FILENAME "' while Visual Studio is running."
  615. << " Please exit all running instances of Visual Studio before"
  616. << " continuing." << std::endl
  617. << std::endl
  618. << "CMake needs to register Visual Studio macros when its macros"
  619. << " file is updated or when it detects that its current macros file"
  620. << " is no longer registered with Visual Studio."
  621. << std::endl;
  622. cmSystemTools::Message(oss.str().c_str(), "Warning");
  623. // Count them again now that the warning is over. In the case of a GUI
  624. // warning, the user may have gone to close Visual Studio and then come
  625. // back to the CMake GUI and clicked ok on the above warning. If so,
  626. // then register the macros *now* if the count is *now* 0...
  627. //
  628. count = cmCallVisualStudioMacro::
  629. GetNumberOfRunningVisualStudioInstances("ALL");
  630. // Also re-get the nextAvailableSubKeyName in case Visual Studio
  631. // wrote out new registered macros information as it was exiting:
  632. //
  633. if (0 == count)
  634. {
  635. IsVisualStudioMacrosFileRegistered(macrosFile, regKeyBase,
  636. nextAvailableSubKeyName);
  637. }
  638. }
  639. // Do another if check - 'count' may have changed inside the above if:
  640. //
  641. if (0 == count)
  642. {
  643. WriteVSMacrosFileRegistryEntry(nextAvailableSubKeyName, macrosFile,
  644. regKeyBase);
  645. }
  646. }
  647. }
  648. bool cmGlobalVisualStudioGenerator::TargetIsFortranOnly(cmTarget& target)
  649. {
  650. // check to see if this is a fortran build
  651. std::set<cmStdString> languages;
  652. target.GetLanguages(languages);
  653. if(languages.size() == 1)
  654. {
  655. if(*languages.begin() == "Fortran")
  656. {
  657. return true;
  658. }
  659. }
  660. return false;
  661. }