cmGlobalVisualStudioGenerator.cxx 26 KB

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