cmGlobalVisualStudioGenerator.cxx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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 "cmGeneratorTarget.h"
  13. #include "cmLocalVisualStudioGenerator.h"
  14. #include "cmMakefile.h"
  15. #include "cmSourceFile.h"
  16. #include "cmTarget.h"
  17. //----------------------------------------------------------------------------
  18. cmGlobalVisualStudioGenerator::cmGlobalVisualStudioGenerator()
  19. {
  20. this->AdditionalPlatformDefinition = NULL;
  21. }
  22. //----------------------------------------------------------------------------
  23. cmGlobalVisualStudioGenerator::~cmGlobalVisualStudioGenerator()
  24. {
  25. }
  26. //----------------------------------------------------------------------------
  27. std::string cmGlobalVisualStudioGenerator::GetRegistryBase()
  28. {
  29. return cmGlobalVisualStudioGenerator::GetRegistryBase(
  30. this->GetIDEVersion());
  31. }
  32. //----------------------------------------------------------------------------
  33. std::string cmGlobalVisualStudioGenerator::GetRegistryBase(
  34. const char* version)
  35. {
  36. std::string key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\";
  37. return key + version;
  38. }
  39. //----------------------------------------------------------------------------
  40. void cmGlobalVisualStudioGenerator::Generate()
  41. {
  42. // Add a special target that depends on ALL projects for easy build
  43. // of one configuration only.
  44. const char* no_working_dir = 0;
  45. std::vector<std::string> no_depends;
  46. cmCustomCommandLines no_commands;
  47. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  48. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  49. {
  50. std::vector<cmLocalGenerator*>& gen = it->second;
  51. // add the ALL_BUILD to the first local generator of each project
  52. if(gen.size())
  53. {
  54. // Use no actual command lines so that the target itself is not
  55. // considered always out of date.
  56. cmTarget* allBuild =
  57. gen[0]->GetMakefile()->
  58. AddUtilityCommand("ALL_BUILD", true, no_working_dir,
  59. no_depends, no_commands, false,
  60. "Build all projects");
  61. #if 0
  62. // Can't activate this code because we want ALL_BUILD
  63. // selected as the default "startup project" when first
  64. // opened in Visual Studio... And if it's nested in a
  65. // folder, then that doesn't happen.
  66. //
  67. // Organize in the "predefined targets" folder:
  68. //
  69. if (this->UseFolderProperty())
  70. {
  71. allBuild->SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
  72. }
  73. #endif
  74. // Now make all targets depend on the ALL_BUILD target
  75. for(std::vector<cmLocalGenerator*>::iterator i = gen.begin();
  76. i != gen.end(); ++i)
  77. {
  78. cmTargets& targets = (*i)->GetMakefile()->GetTargets();
  79. for(cmTargets::iterator t = targets.begin();
  80. t != targets.end(); ++t)
  81. {
  82. if(!this->IsExcluded(gen[0], t->second))
  83. {
  84. allBuild->AddUtility(t->second.GetName());
  85. }
  86. }
  87. }
  88. }
  89. }
  90. // Configure CMake Visual Studio macros, for this user on this version
  91. // of Visual Studio.
  92. this->ConfigureCMakeVisualStudioMacros();
  93. // Add CMakeLists.txt with custom command to rerun CMake.
  94. for(std::vector<cmLocalGenerator*>::const_iterator
  95. lgi = this->LocalGenerators.begin();
  96. lgi != this->LocalGenerators.end(); ++lgi)
  97. {
  98. cmLocalVisualStudioGenerator* lg =
  99. static_cast<cmLocalVisualStudioGenerator*>(*lgi);
  100. lg->AddCMakeListsRules();
  101. }
  102. // Run all the local generators.
  103. this->cmGlobalGenerator::Generate();
  104. }
  105. //----------------------------------------------------------------------------
  106. void
  107. cmGlobalVisualStudioGenerator
  108. ::ComputeTargetObjects(cmGeneratorTarget* gt) const
  109. {
  110. cmLocalVisualStudioGenerator* lg =
  111. static_cast<cmLocalVisualStudioGenerator*>(gt->LocalGenerator);
  112. std::string dir_max = lg->ComputeLongestObjectDirectory(*gt->Target);
  113. // Count the number of object files with each name. Note that
  114. // windows file names are not case sensitive.
  115. std::map<cmStdString, int> counts;
  116. for(std::vector<cmSourceFile*>::const_iterator
  117. si = gt->ObjectSources.begin();
  118. si != gt->ObjectSources.end(); ++si)
  119. {
  120. cmSourceFile* sf = *si;
  121. std::string objectNameLower = cmSystemTools::LowerCase(
  122. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath()));
  123. objectNameLower += ".obj";
  124. counts[objectNameLower] += 1;
  125. }
  126. // For all source files producing duplicate names we need unique
  127. // object name computation.
  128. for(std::vector<cmSourceFile*>::const_iterator
  129. si = gt->ObjectSources.begin();
  130. si != gt->ObjectSources.end(); ++si)
  131. {
  132. cmSourceFile* sf = *si;
  133. std::string objectName =
  134. cmSystemTools::GetFilenameWithoutLastExtension(sf->GetFullPath());
  135. objectName += ".obj";
  136. if(counts[cmSystemTools::LowerCase(objectName)] > 1)
  137. {
  138. gt->ExplicitObjectName.insert(sf);
  139. objectName = lg->GetObjectFileNameWithoutTarget(*sf, dir_max);
  140. }
  141. gt->Objects[sf] = objectName;
  142. }
  143. std::string dir = gt->Makefile->GetCurrentOutputDirectory();
  144. dir += "/";
  145. std::string tgtDir = lg->GetTargetDirectory(*gt->Target);
  146. if(!tgtDir.empty())
  147. {
  148. dir += tgtDir;
  149. dir += "/";
  150. }
  151. const char* cd = this->GetCMakeCFGIntDir();
  152. if(cd && *cd)
  153. {
  154. dir += cd;
  155. dir += "/";
  156. }
  157. gt->ObjectDirectory = dir;
  158. }
  159. //----------------------------------------------------------------------------
  160. bool IsVisualStudioMacrosFileRegistered(const std::string& macrosFile,
  161. const std::string& regKeyBase,
  162. std::string& nextAvailableSubKeyName);
  163. void RegisterVisualStudioMacros(const std::string& macrosFile,
  164. const std::string& regKeyBase);
  165. //----------------------------------------------------------------------------
  166. #define CMAKE_VSMACROS_FILENAME \
  167. "CMakeVSMacros2.vsmacros"
  168. #define CMAKE_VSMACROS_RELOAD_MACRONAME \
  169. "Macros.CMakeVSMacros2.Macros.ReloadProjects"
  170. #define CMAKE_VSMACROS_STOP_MACRONAME \
  171. "Macros.CMakeVSMacros2.Macros.StopBuild"
  172. //----------------------------------------------------------------------------
  173. void cmGlobalVisualStudioGenerator::ConfigureCMakeVisualStudioMacros()
  174. {
  175. cmMakefile* mf = this->LocalGenerators[0]->GetMakefile();
  176. std::string dir = this->GetUserMacrosDirectory();
  177. if (mf != 0 && dir != "")
  178. {
  179. std::string src = mf->GetRequiredDefinition("CMAKE_ROOT");
  180. src += "/Templates/" CMAKE_VSMACROS_FILENAME;
  181. std::string dst = dir + "/CMakeMacros/" CMAKE_VSMACROS_FILENAME;
  182. // Copy the macros file to the user directory only if the
  183. // destination does not exist or the source location is newer.
  184. // This will allow the user to edit the macros for development
  185. // purposes but newer versions distributed with CMake will replace
  186. // older versions in user directories.
  187. int res;
  188. if(!cmSystemTools::FileTimeCompare(src.c_str(), dst.c_str(), &res) ||
  189. res > 0)
  190. {
  191. if (!cmSystemTools::CopyFileAlways(src.c_str(), dst.c_str()))
  192. {
  193. std::ostringstream oss;
  194. oss << "Could not copy from: " << src << std::endl;
  195. oss << " to: " << dst << std::endl;
  196. cmSystemTools::Message(oss.str().c_str(), "Warning");
  197. }
  198. }
  199. RegisterVisualStudioMacros(dst, this->GetUserMacrosRegKeyBase());
  200. }
  201. }
  202. //----------------------------------------------------------------------------
  203. void
  204. cmGlobalVisualStudioGenerator
  205. ::CallVisualStudioMacro(MacroName m,
  206. const char* vsSolutionFile)
  207. {
  208. // If any solution or project files changed during the generation,
  209. // tell Visual Studio to reload them...
  210. cmMakefile* mf = this->LocalGenerators[0]->GetMakefile();
  211. std::string dir = this->GetUserMacrosDirectory();
  212. // Only really try to call the macro if:
  213. // - mf is non-NULL
  214. // - there is a UserMacrosDirectory
  215. // - the CMake vsmacros file exists
  216. // - the CMake vsmacros file is registered
  217. // - there were .sln/.vcproj files changed during generation
  218. //
  219. if (mf != 0 && dir != "")
  220. {
  221. std::string macrosFile = dir + "/CMakeMacros/" CMAKE_VSMACROS_FILENAME;
  222. std::string nextSubkeyName;
  223. if (cmSystemTools::FileExists(macrosFile.c_str()) &&
  224. IsVisualStudioMacrosFileRegistered(macrosFile,
  225. this->GetUserMacrosRegKeyBase(), nextSubkeyName)
  226. )
  227. {
  228. std::string topLevelSlnName;
  229. if(vsSolutionFile)
  230. {
  231. topLevelSlnName = vsSolutionFile;
  232. }
  233. else
  234. {
  235. topLevelSlnName = mf->GetStartOutputDirectory();
  236. topLevelSlnName += "/";
  237. topLevelSlnName += mf->GetProjectName();
  238. topLevelSlnName += ".sln";
  239. }
  240. if(m == MacroReload)
  241. {
  242. std::vector<std::string> filenames;
  243. this->GetFilesReplacedDuringGenerate(filenames);
  244. if (filenames.size() > 0)
  245. {
  246. // Convert vector to semi-colon delimited string of filenames:
  247. std::string projects;
  248. std::vector<std::string>::iterator it = filenames.begin();
  249. if (it != filenames.end())
  250. {
  251. projects = *it;
  252. ++it;
  253. }
  254. for (; it != filenames.end(); ++it)
  255. {
  256. projects += ";";
  257. projects += *it;
  258. }
  259. cmCallVisualStudioMacro::CallMacro(topLevelSlnName,
  260. CMAKE_VSMACROS_RELOAD_MACRONAME, projects,
  261. this->GetCMakeInstance()->GetDebugOutput());
  262. }
  263. }
  264. else if(m == MacroStop)
  265. {
  266. cmCallVisualStudioMacro::CallMacro(topLevelSlnName,
  267. CMAKE_VSMACROS_STOP_MACRONAME, "",
  268. this->GetCMakeInstance()->GetDebugOutput());
  269. }
  270. }
  271. }
  272. }
  273. //----------------------------------------------------------------------------
  274. std::string cmGlobalVisualStudioGenerator::GetUserMacrosDirectory()
  275. {
  276. return "";
  277. }
  278. //----------------------------------------------------------------------------
  279. std::string cmGlobalVisualStudioGenerator::GetUserMacrosRegKeyBase()
  280. {
  281. return "";
  282. }
  283. //----------------------------------------------------------------------------
  284. void cmGlobalVisualStudioGenerator::FillLinkClosure(cmTarget* target,
  285. TargetSet& linked)
  286. {
  287. if(linked.insert(target).second)
  288. {
  289. TargetDependSet const& depends = this->GetTargetDirectDepends(*target);
  290. for(TargetDependSet::const_iterator di = depends.begin();
  291. di != depends.end(); ++di)
  292. {
  293. if(di->IsLink())
  294. {
  295. this->FillLinkClosure(*di, linked);
  296. }
  297. }
  298. }
  299. }
  300. //----------------------------------------------------------------------------
  301. cmGlobalVisualStudioGenerator::TargetSet const&
  302. cmGlobalVisualStudioGenerator::GetTargetLinkClosure(cmTarget* target)
  303. {
  304. TargetSetMap::iterator i = this->TargetLinkClosure.find(target);
  305. if(i == this->TargetLinkClosure.end())
  306. {
  307. TargetSetMap::value_type entry(target, TargetSet());
  308. i = this->TargetLinkClosure.insert(entry).first;
  309. this->FillLinkClosure(target, i->second);
  310. }
  311. return i->second;
  312. }
  313. //----------------------------------------------------------------------------
  314. void cmGlobalVisualStudioGenerator::FollowLinkDepends(
  315. cmTarget* target, std::set<cmTarget*>& linked)
  316. {
  317. if(target->GetType() == cmTarget::INTERFACE_LIBRARY)
  318. {
  319. return;
  320. }
  321. if(linked.insert(target).second &&
  322. target->GetType() == cmTarget::STATIC_LIBRARY)
  323. {
  324. // Static library targets do not list their link dependencies so
  325. // we must follow them transitively now.
  326. TargetDependSet const& depends = this->GetTargetDirectDepends(*target);
  327. for(TargetDependSet::const_iterator di = depends.begin();
  328. di != depends.end(); ++di)
  329. {
  330. if(di->IsLink())
  331. {
  332. this->FollowLinkDepends(*di, linked);
  333. }
  334. }
  335. }
  336. }
  337. //----------------------------------------------------------------------------
  338. bool cmGlobalVisualStudioGenerator::ComputeTargetDepends()
  339. {
  340. if(!this->cmGlobalGenerator::ComputeTargetDepends())
  341. {
  342. return false;
  343. }
  344. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  345. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  346. {
  347. std::vector<cmLocalGenerator*>& gen = it->second;
  348. for(std::vector<cmLocalGenerator*>::iterator i = gen.begin();
  349. i != gen.end(); ++i)
  350. {
  351. cmTargets& targets = (*i)->GetMakefile()->GetTargets();
  352. for(cmTargets::iterator ti = targets.begin();
  353. ti != targets.end(); ++ti)
  354. {
  355. this->ComputeVSTargetDepends(ti->second);
  356. }
  357. }
  358. }
  359. return true;
  360. }
  361. //----------------------------------------------------------------------------
  362. static bool VSLinkable(cmTarget* t)
  363. {
  364. return t->IsLinkable() || t->GetType() == cmTarget::OBJECT_LIBRARY;
  365. }
  366. //----------------------------------------------------------------------------
  367. void cmGlobalVisualStudioGenerator::ComputeVSTargetDepends(cmTarget& target)
  368. {
  369. if(this->VSTargetDepends.find(&target) != this->VSTargetDepends.end())
  370. {
  371. return;
  372. }
  373. VSDependSet& vsTargetDepend = this->VSTargetDepends[&target];
  374. // VS <= 7.1 has two behaviors that affect solution dependencies.
  375. //
  376. // (1) Solution-level dependencies between a linkable target and a
  377. // library cause that library to be linked. We use an intermedite
  378. // empty utility target to express the dependency. (VS 8 and above
  379. // provide a project file "LinkLibraryDependencies" setting to
  380. // choose whether to activate this behavior. We disable it except
  381. // when linking external project files.)
  382. //
  383. // (2) We cannot let static libraries depend directly on targets to
  384. // which they "link" because the librarian tool will copy the
  385. // targets into the static library. While the work-around for
  386. // behavior (1) would also avoid this, it would create a large
  387. // number of extra utility targets for little gain. Instead, use
  388. // the above work-around only for dependencies explicitly added by
  389. // the add_dependencies() command. Approximate link dependencies by
  390. // leaving them out for the static library itself but following them
  391. // transitively for other targets.
  392. bool allowLinkable = (target.GetType() != cmTarget::STATIC_LIBRARY &&
  393. target.GetType() != cmTarget::SHARED_LIBRARY &&
  394. target.GetType() != cmTarget::MODULE_LIBRARY &&
  395. target.GetType() != cmTarget::EXECUTABLE);
  396. TargetDependSet const& depends = this->GetTargetDirectDepends(target);
  397. // Collect implicit link dependencies (target_link_libraries).
  398. // Static libraries cannot depend on their link implementation
  399. // due to behavior (2), but they do not really need to.
  400. std::set<cmTarget*> linkDepends;
  401. if(target.GetType() != cmTarget::STATIC_LIBRARY)
  402. {
  403. for(TargetDependSet::const_iterator di = depends.begin();
  404. di != depends.end(); ++di)
  405. {
  406. cmTargetDepend dep = *di;
  407. if(dep.IsLink())
  408. {
  409. this->FollowLinkDepends(dep, linkDepends);
  410. }
  411. }
  412. }
  413. // Collect explicit util dependencies (add_dependencies).
  414. std::set<cmTarget*> utilDepends;
  415. for(TargetDependSet::const_iterator di = depends.begin();
  416. di != depends.end(); ++di)
  417. {
  418. cmTargetDepend dep = *di;
  419. if(dep.IsUtil())
  420. {
  421. this->FollowLinkDepends(dep, utilDepends);
  422. }
  423. }
  424. // Collect all targets linked by this target so we can avoid
  425. // intermediate targets below.
  426. TargetSet linked;
  427. if(target.GetType() != cmTarget::STATIC_LIBRARY)
  428. {
  429. linked = this->GetTargetLinkClosure(&target);
  430. }
  431. // Emit link dependencies.
  432. for(std::set<cmTarget*>::iterator di = linkDepends.begin();
  433. di != linkDepends.end(); ++di)
  434. {
  435. cmTarget* dep = *di;
  436. vsTargetDepend.insert(dep->GetName());
  437. }
  438. // Emit util dependencies. Possibly use intermediate targets.
  439. for(std::set<cmTarget*>::iterator di = utilDepends.begin();
  440. di != utilDepends.end(); ++di)
  441. {
  442. cmTarget* dep = *di;
  443. if(allowLinkable || !VSLinkable(dep) || linked.count(dep))
  444. {
  445. // Direct dependency allowed.
  446. vsTargetDepend.insert(dep->GetName());
  447. }
  448. else
  449. {
  450. // Direct dependency on linkable target not allowed.
  451. // Use an intermediate utility target.
  452. vsTargetDepend.insert(this->GetUtilityDepend(dep));
  453. }
  454. }
  455. }
  456. //----------------------------------------------------------------------------
  457. void cmGlobalVisualStudioGenerator::FindMakeProgram(cmMakefile* mf)
  458. {
  459. // Visual Studio generators know how to lookup their build tool
  460. // directly instead of needing a helper module to do it, so we
  461. // do not actually need to put CMAKE_MAKE_PROGRAM into the cache.
  462. if(cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM")))
  463. {
  464. mf->AddDefinition("CMAKE_MAKE_PROGRAM",
  465. this->GetVSMakeProgram().c_str());
  466. }
  467. }
  468. //----------------------------------------------------------------------------
  469. void cmGlobalVisualStudioGenerator::AddPlatformDefinitions(cmMakefile* mf)
  470. {
  471. if(this->AdditionalPlatformDefinition)
  472. {
  473. mf->AddDefinition(this->AdditionalPlatformDefinition, "TRUE");
  474. }
  475. }
  476. //----------------------------------------------------------------------------
  477. std::string cmGlobalVisualStudioGenerator::GetUtilityDepend(cmTarget* target)
  478. {
  479. UtilityDependsMap::iterator i = this->UtilityDepends.find(target);
  480. if(i == this->UtilityDepends.end())
  481. {
  482. std::string name = this->WriteUtilityDepend(target);
  483. UtilityDependsMap::value_type entry(target, name);
  484. i = this->UtilityDepends.insert(entry).first;
  485. }
  486. return i->second;
  487. }
  488. //----------------------------------------------------------------------------
  489. #include <windows.h>
  490. //----------------------------------------------------------------------------
  491. bool IsVisualStudioMacrosFileRegistered(const std::string& macrosFile,
  492. const std::string& regKeyBase,
  493. std::string& nextAvailableSubKeyName)
  494. {
  495. bool macrosRegistered = false;
  496. std::string s1;
  497. std::string s2;
  498. // Make lowercase local copies, convert to Unix slashes, and
  499. // see if the resulting strings are the same:
  500. s1 = cmSystemTools::LowerCase(macrosFile);
  501. cmSystemTools::ConvertToUnixSlashes(s1);
  502. std::string keyname;
  503. HKEY hkey = NULL;
  504. LONG result = ERROR_SUCCESS;
  505. DWORD index = 0;
  506. keyname = regKeyBase + "\\OtherProjects7";
  507. hkey = NULL;
  508. result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(),
  509. 0, KEY_READ, &hkey);
  510. if (ERROR_SUCCESS == result)
  511. {
  512. // Iterate the subkeys and look for the values of interest in each subkey:
  513. CHAR subkeyname[256];
  514. DWORD cch_subkeyname = sizeof(subkeyname)/sizeof(subkeyname[0]);
  515. CHAR keyclass[256];
  516. DWORD cch_keyclass = sizeof(keyclass)/sizeof(keyclass[0]);
  517. FILETIME lastWriteTime;
  518. lastWriteTime.dwHighDateTime = 0;
  519. lastWriteTime.dwLowDateTime = 0;
  520. while (ERROR_SUCCESS == RegEnumKeyEx(hkey, index, subkeyname,
  521. &cch_subkeyname,
  522. 0, keyclass, &cch_keyclass, &lastWriteTime))
  523. {
  524. // Open the subkey and query the values of interest:
  525. HKEY hsubkey = NULL;
  526. result = RegOpenKeyEx(hkey, subkeyname, 0, KEY_READ, &hsubkey);
  527. if (ERROR_SUCCESS == result)
  528. {
  529. DWORD valueType = REG_SZ;
  530. CHAR data1[256];
  531. DWORD cch_data1 = sizeof(data1)/sizeof(data1[0]);
  532. RegQueryValueEx(hsubkey, "Path", 0, &valueType,
  533. (LPBYTE) &data1[0], &cch_data1);
  534. DWORD data2 = 0;
  535. DWORD cch_data2 = sizeof(data2);
  536. RegQueryValueEx(hsubkey, "Security", 0, &valueType,
  537. (LPBYTE) &data2, &cch_data2);
  538. DWORD data3 = 0;
  539. DWORD cch_data3 = sizeof(data3);
  540. RegQueryValueEx(hsubkey, "StorageFormat", 0, &valueType,
  541. (LPBYTE) &data3, &cch_data3);
  542. s2 = cmSystemTools::LowerCase(data1);
  543. cmSystemTools::ConvertToUnixSlashes(s2);
  544. if (s2 == s1)
  545. {
  546. macrosRegistered = true;
  547. }
  548. std::string fullname(data1);
  549. std::string filename;
  550. std::string filepath;
  551. std::string filepathname;
  552. std::string filepathpath;
  553. if (cmSystemTools::FileExists(fullname.c_str()))
  554. {
  555. filename = cmSystemTools::GetFilenameName(fullname);
  556. filepath = cmSystemTools::GetFilenamePath(fullname);
  557. filepathname = cmSystemTools::GetFilenameName(filepath);
  558. filepathpath = cmSystemTools::GetFilenamePath(filepath);
  559. }
  560. //std::cout << keyname << "\\" << subkeyname << ":" << std::endl;
  561. //std::cout << " Path: " << data1 << std::endl;
  562. //std::cout << " Security: " << data2 << std::endl;
  563. //std::cout << " StorageFormat: " << data3 << std::endl;
  564. //std::cout << " filename: " << filename << std::endl;
  565. //std::cout << " filepath: " << filepath << std::endl;
  566. //std::cout << " filepathname: " << filepathname << std::endl;
  567. //std::cout << " filepathpath: " << filepathpath << std::endl;
  568. //std::cout << std::endl;
  569. RegCloseKey(hsubkey);
  570. }
  571. else
  572. {
  573. std::cout << "error opening subkey: " << subkeyname << std::endl;
  574. std::cout << std::endl;
  575. }
  576. ++index;
  577. cch_subkeyname = sizeof(subkeyname)/sizeof(subkeyname[0]);
  578. cch_keyclass = sizeof(keyclass)/sizeof(keyclass[0]);
  579. lastWriteTime.dwHighDateTime = 0;
  580. lastWriteTime.dwLowDateTime = 0;
  581. }
  582. RegCloseKey(hkey);
  583. }
  584. else
  585. {
  586. std::cout << "error opening key: " << keyname << std::endl;
  587. std::cout << std::endl;
  588. }
  589. // Pass back next available sub key name, assuming sub keys always
  590. // follow the expected naming scheme. Expected naming scheme is that
  591. // the subkeys of OtherProjects7 is 0 to n-1, so it's ok to use "n"
  592. // as the name of the next subkey.
  593. std::ostringstream ossNext;
  594. ossNext << index;
  595. nextAvailableSubKeyName = ossNext.str();
  596. keyname = regKeyBase + "\\RecordingProject7";
  597. hkey = NULL;
  598. result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(),
  599. 0, KEY_READ, &hkey);
  600. if (ERROR_SUCCESS == result)
  601. {
  602. DWORD valueType = REG_SZ;
  603. CHAR data1[256];
  604. DWORD cch_data1 = sizeof(data1)/sizeof(data1[0]);
  605. RegQueryValueEx(hkey, "Path", 0, &valueType,
  606. (LPBYTE) &data1[0], &cch_data1);
  607. DWORD data2 = 0;
  608. DWORD cch_data2 = sizeof(data2);
  609. RegQueryValueEx(hkey, "Security", 0, &valueType,
  610. (LPBYTE) &data2, &cch_data2);
  611. DWORD data3 = 0;
  612. DWORD cch_data3 = sizeof(data3);
  613. RegQueryValueEx(hkey, "StorageFormat", 0, &valueType,
  614. (LPBYTE) &data3, &cch_data3);
  615. s2 = cmSystemTools::LowerCase(data1);
  616. cmSystemTools::ConvertToUnixSlashes(s2);
  617. if (s2 == s1)
  618. {
  619. macrosRegistered = true;
  620. }
  621. //std::cout << keyname << ":" << std::endl;
  622. //std::cout << " Path: " << data1 << std::endl;
  623. //std::cout << " Security: " << data2 << std::endl;
  624. //std::cout << " StorageFormat: " << data3 << std::endl;
  625. //std::cout << std::endl;
  626. RegCloseKey(hkey);
  627. }
  628. else
  629. {
  630. std::cout << "error opening key: " << keyname << std::endl;
  631. std::cout << std::endl;
  632. }
  633. return macrosRegistered;
  634. }
  635. //----------------------------------------------------------------------------
  636. void WriteVSMacrosFileRegistryEntry(
  637. const std::string& nextAvailableSubKeyName,
  638. const std::string& macrosFile,
  639. const std::string& regKeyBase)
  640. {
  641. std::string keyname = regKeyBase + "\\OtherProjects7";
  642. HKEY hkey = NULL;
  643. LONG result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(), 0,
  644. KEY_READ|KEY_WRITE, &hkey);
  645. if (ERROR_SUCCESS == result)
  646. {
  647. // Create the subkey and set the values of interest:
  648. HKEY hsubkey = NULL;
  649. char lpClass[] = "";
  650. result = RegCreateKeyEx(hkey, nextAvailableSubKeyName.c_str(), 0,
  651. lpClass, 0, KEY_READ|KEY_WRITE, 0, &hsubkey, 0);
  652. if (ERROR_SUCCESS == result)
  653. {
  654. DWORD dw = 0;
  655. std::string s(macrosFile);
  656. cmSystemTools::ReplaceString(s, "/", "\\");
  657. result = RegSetValueEx(hsubkey, "Path", 0, REG_SZ, (LPBYTE) s.c_str(),
  658. static_cast<DWORD>(strlen(s.c_str()) + 1));
  659. if (ERROR_SUCCESS != result)
  660. {
  661. std::cout << "error result 1: " << result << std::endl;
  662. std::cout << std::endl;
  663. }
  664. // Security value is always "1" for sample macros files (seems to be "2"
  665. // if you put the file somewhere outside the standard VSMacros folder)
  666. dw = 1;
  667. result = RegSetValueEx(hsubkey, "Security",
  668. 0, REG_DWORD, (LPBYTE) &dw, sizeof(DWORD));
  669. if (ERROR_SUCCESS != result)
  670. {
  671. std::cout << "error result 2: " << result << std::endl;
  672. std::cout << std::endl;
  673. }
  674. // StorageFormat value is always "0" for sample macros files
  675. dw = 0;
  676. result = RegSetValueEx(hsubkey, "StorageFormat",
  677. 0, REG_DWORD, (LPBYTE) &dw, sizeof(DWORD));
  678. if (ERROR_SUCCESS != result)
  679. {
  680. std::cout << "error result 3: " << result << std::endl;
  681. std::cout << std::endl;
  682. }
  683. RegCloseKey(hsubkey);
  684. }
  685. else
  686. {
  687. std::cout << "error creating subkey: "
  688. << nextAvailableSubKeyName << std::endl;
  689. std::cout << std::endl;
  690. }
  691. RegCloseKey(hkey);
  692. }
  693. else
  694. {
  695. std::cout << "error opening key: " << keyname << std::endl;
  696. std::cout << std::endl;
  697. }
  698. }
  699. //----------------------------------------------------------------------------
  700. void RegisterVisualStudioMacros(const std::string& macrosFile,
  701. const std::string& regKeyBase)
  702. {
  703. bool macrosRegistered;
  704. std::string nextAvailableSubKeyName;
  705. macrosRegistered = IsVisualStudioMacrosFileRegistered(macrosFile,
  706. regKeyBase, nextAvailableSubKeyName);
  707. if (!macrosRegistered)
  708. {
  709. int count = cmCallVisualStudioMacro::
  710. GetNumberOfRunningVisualStudioInstances("ALL");
  711. // Only register the macros file if there are *no* instances of Visual
  712. // Studio running. If we register it while one is running, first, it has
  713. // no effect on the running instance; second, and worse, Visual Studio
  714. // removes our newly added registration entry when it quits. Instead,
  715. // emit a warning asking the user to exit all running Visual Studio
  716. // instances...
  717. //
  718. if (0 != count)
  719. {
  720. std::ostringstream oss;
  721. oss << "Could not register CMake's Visual Studio macros file '"
  722. << CMAKE_VSMACROS_FILENAME "' while Visual Studio is running."
  723. << " Please exit all running instances of Visual Studio before"
  724. << " continuing." << std::endl
  725. << std::endl
  726. << "CMake needs to register Visual Studio macros when its macros"
  727. << " file is updated or when it detects that its current macros file"
  728. << " is no longer registered with Visual Studio."
  729. << std::endl;
  730. cmSystemTools::Message(oss.str().c_str(), "Warning");
  731. // Count them again now that the warning is over. In the case of a GUI
  732. // warning, the user may have gone to close Visual Studio and then come
  733. // back to the CMake GUI and clicked ok on the above warning. If so,
  734. // then register the macros *now* if the count is *now* 0...
  735. //
  736. count = cmCallVisualStudioMacro::
  737. GetNumberOfRunningVisualStudioInstances("ALL");
  738. // Also re-get the nextAvailableSubKeyName in case Visual Studio
  739. // wrote out new registered macros information as it was exiting:
  740. //
  741. if (0 == count)
  742. {
  743. IsVisualStudioMacrosFileRegistered(macrosFile, regKeyBase,
  744. nextAvailableSubKeyName);
  745. }
  746. }
  747. // Do another if check - 'count' may have changed inside the above if:
  748. //
  749. if (0 == count)
  750. {
  751. WriteVSMacrosFileRegistryEntry(nextAvailableSubKeyName, macrosFile,
  752. regKeyBase);
  753. }
  754. }
  755. }
  756. bool cmGlobalVisualStudioGenerator::TargetIsFortranOnly(cmTarget& target)
  757. {
  758. // check to see if this is a fortran build
  759. std::set<cmStdString> languages;
  760. target.GetLanguages(languages);
  761. if(languages.size() == 1)
  762. {
  763. if(*languages.begin() == "Fortran")
  764. {
  765. return true;
  766. }
  767. }
  768. return false;
  769. }
  770. //----------------------------------------------------------------------------
  771. bool
  772. cmGlobalVisualStudioGenerator::TargetCompare
  773. ::operator()(cmTarget const* l, cmTarget const* r) const
  774. {
  775. // Make sure ALL_BUILD is first so it is the default active project.
  776. if(strcmp(r->GetName(), "ALL_BUILD") == 0)
  777. {
  778. return false;
  779. }
  780. if(strcmp(l->GetName(), "ALL_BUILD") == 0)
  781. {
  782. return true;
  783. }
  784. return strcmp(l->GetName(), r->GetName()) < 0;
  785. }
  786. //----------------------------------------------------------------------------
  787. cmGlobalVisualStudioGenerator::OrderedTargetDependSet
  788. ::OrderedTargetDependSet(TargetDependSet const& targets)
  789. {
  790. for(TargetDependSet::const_iterator ti =
  791. targets.begin(); ti != targets.end(); ++ti)
  792. {
  793. this->insert(*ti);
  794. }
  795. }
  796. //----------------------------------------------------------------------------
  797. cmGlobalVisualStudioGenerator::OrderedTargetDependSet
  798. ::OrderedTargetDependSet(TargetSet const& targets)
  799. {
  800. for(TargetSet::const_iterator ti = targets.begin();
  801. ti != targets.end(); ++ti)
  802. {
  803. this->insert(*ti);
  804. }
  805. }