cmGlobalVisualStudioGenerator.cxx 23 KB

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