cmGlobalVisualStudioGenerator.cxx 29 KB

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