cmGlobalVisualStudioGenerator.cxx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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. }
  22. //----------------------------------------------------------------------------
  23. cmGlobalVisualStudioGenerator::~cmGlobalVisualStudioGenerator()
  24. {
  25. }
  26. //----------------------------------------------------------------------------
  27. std::string cmGlobalVisualStudioGenerator::GetRegistryBase()
  28. {
  29. std::string key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\";
  30. key += this->GetIDEVersion();
  31. return key;
  32. }
  33. //----------------------------------------------------------------------------
  34. void cmGlobalVisualStudioGenerator::Generate()
  35. {
  36. // Add a special target that depends on ALL projects for easy build
  37. // of one configuration only.
  38. const char* no_working_dir = 0;
  39. std::vector<std::string> no_depends;
  40. cmCustomCommandLines no_commands;
  41. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  42. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  43. {
  44. std::vector<cmLocalGenerator*>& gen = it->second;
  45. // add the ALL_BUILD to the first local generator of each project
  46. if(gen.size())
  47. {
  48. // Use no actual command lines so that the target itself is not
  49. // considered always out of date.
  50. cmTarget* allBuild =
  51. gen[0]->GetMakefile()->
  52. AddUtilityCommand("ALL_BUILD", true, no_working_dir,
  53. no_depends, no_commands, false,
  54. "Build all projects");
  55. #if 0
  56. // Can't activate this code because we want ALL_BUILD
  57. // selected as the default "startup project" when first
  58. // opened in Visual Studio... And if it's nested in a
  59. // folder, then that doesn't happen.
  60. //
  61. // Organize in the "predefined targets" folder:
  62. //
  63. if (this->UseFolderProperty())
  64. {
  65. allBuild->SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
  66. }
  67. #endif
  68. // Now make all targets depend on the ALL_BUILD target
  69. cmTargets targets;
  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. }
  453. //----------------------------------------------------------------------------
  454. std::string cmGlobalVisualStudioGenerator::GetUtilityDepend(cmTarget* target)
  455. {
  456. UtilityDependsMap::iterator i = this->UtilityDepends.find(target);
  457. if(i == this->UtilityDepends.end())
  458. {
  459. std::string name = this->WriteUtilityDepend(target);
  460. UtilityDependsMap::value_type entry(target, name);
  461. i = this->UtilityDepends.insert(entry).first;
  462. }
  463. return i->second;
  464. }
  465. //----------------------------------------------------------------------------
  466. #include <windows.h>
  467. //----------------------------------------------------------------------------
  468. bool IsVisualStudioMacrosFileRegistered(const std::string& macrosFile,
  469. const std::string& regKeyBase,
  470. std::string& nextAvailableSubKeyName)
  471. {
  472. bool macrosRegistered = false;
  473. std::string s1;
  474. std::string s2;
  475. // Make lowercase local copies, convert to Unix slashes, and
  476. // see if the resulting strings are the same:
  477. s1 = cmSystemTools::LowerCase(macrosFile);
  478. cmSystemTools::ConvertToUnixSlashes(s1);
  479. std::string keyname;
  480. HKEY hkey = NULL;
  481. LONG result = ERROR_SUCCESS;
  482. DWORD index = 0;
  483. keyname = regKeyBase + "\\OtherProjects7";
  484. hkey = NULL;
  485. result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(),
  486. 0, KEY_READ, &hkey);
  487. if (ERROR_SUCCESS == result)
  488. {
  489. // Iterate the subkeys and look for the values of interest in each subkey:
  490. CHAR subkeyname[256];
  491. DWORD cch_subkeyname = sizeof(subkeyname)/sizeof(subkeyname[0]);
  492. CHAR keyclass[256];
  493. DWORD cch_keyclass = sizeof(keyclass)/sizeof(keyclass[0]);
  494. FILETIME lastWriteTime;
  495. lastWriteTime.dwHighDateTime = 0;
  496. lastWriteTime.dwLowDateTime = 0;
  497. while (ERROR_SUCCESS == RegEnumKeyEx(hkey, index, subkeyname,
  498. &cch_subkeyname,
  499. 0, keyclass, &cch_keyclass, &lastWriteTime))
  500. {
  501. // Open the subkey and query the values of interest:
  502. HKEY hsubkey = NULL;
  503. result = RegOpenKeyEx(hkey, subkeyname, 0, KEY_READ, &hsubkey);
  504. if (ERROR_SUCCESS == result)
  505. {
  506. DWORD valueType = REG_SZ;
  507. CHAR data1[256];
  508. DWORD cch_data1 = sizeof(data1)/sizeof(data1[0]);
  509. RegQueryValueEx(hsubkey, "Path", 0, &valueType,
  510. (LPBYTE) &data1[0], &cch_data1);
  511. DWORD data2 = 0;
  512. DWORD cch_data2 = sizeof(data2);
  513. RegQueryValueEx(hsubkey, "Security", 0, &valueType,
  514. (LPBYTE) &data2, &cch_data2);
  515. DWORD data3 = 0;
  516. DWORD cch_data3 = sizeof(data3);
  517. RegQueryValueEx(hsubkey, "StorageFormat", 0, &valueType,
  518. (LPBYTE) &data3, &cch_data3);
  519. s2 = cmSystemTools::LowerCase(data1);
  520. cmSystemTools::ConvertToUnixSlashes(s2);
  521. if (s2 == s1)
  522. {
  523. macrosRegistered = true;
  524. }
  525. std::string fullname(data1);
  526. std::string filename;
  527. std::string filepath;
  528. std::string filepathname;
  529. std::string filepathpath;
  530. if (cmSystemTools::FileExists(fullname.c_str()))
  531. {
  532. filename = cmSystemTools::GetFilenameName(fullname);
  533. filepath = cmSystemTools::GetFilenamePath(fullname);
  534. filepathname = cmSystemTools::GetFilenameName(filepath);
  535. filepathpath = cmSystemTools::GetFilenamePath(filepath);
  536. }
  537. //std::cout << keyname << "\\" << subkeyname << ":" << std::endl;
  538. //std::cout << " Path: " << data1 << std::endl;
  539. //std::cout << " Security: " << data2 << std::endl;
  540. //std::cout << " StorageFormat: " << data3 << std::endl;
  541. //std::cout << " filename: " << filename << std::endl;
  542. //std::cout << " filepath: " << filepath << std::endl;
  543. //std::cout << " filepathname: " << filepathname << std::endl;
  544. //std::cout << " filepathpath: " << filepathpath << std::endl;
  545. //std::cout << std::endl;
  546. RegCloseKey(hsubkey);
  547. }
  548. else
  549. {
  550. std::cout << "error opening subkey: " << subkeyname << std::endl;
  551. std::cout << std::endl;
  552. }
  553. ++index;
  554. cch_subkeyname = sizeof(subkeyname)/sizeof(subkeyname[0]);
  555. cch_keyclass = sizeof(keyclass)/sizeof(keyclass[0]);
  556. lastWriteTime.dwHighDateTime = 0;
  557. lastWriteTime.dwLowDateTime = 0;
  558. }
  559. RegCloseKey(hkey);
  560. }
  561. else
  562. {
  563. std::cout << "error opening key: " << keyname << std::endl;
  564. std::cout << std::endl;
  565. }
  566. // Pass back next available sub key name, assuming sub keys always
  567. // follow the expected naming scheme. Expected naming scheme is that
  568. // the subkeys of OtherProjects7 is 0 to n-1, so it's ok to use "n"
  569. // as the name of the next subkey.
  570. std::ostringstream ossNext;
  571. ossNext << index;
  572. nextAvailableSubKeyName = ossNext.str();
  573. keyname = regKeyBase + "\\RecordingProject7";
  574. hkey = NULL;
  575. result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(),
  576. 0, KEY_READ, &hkey);
  577. if (ERROR_SUCCESS == result)
  578. {
  579. DWORD valueType = REG_SZ;
  580. CHAR data1[256];
  581. DWORD cch_data1 = sizeof(data1)/sizeof(data1[0]);
  582. RegQueryValueEx(hkey, "Path", 0, &valueType,
  583. (LPBYTE) &data1[0], &cch_data1);
  584. DWORD data2 = 0;
  585. DWORD cch_data2 = sizeof(data2);
  586. RegQueryValueEx(hkey, "Security", 0, &valueType,
  587. (LPBYTE) &data2, &cch_data2);
  588. DWORD data3 = 0;
  589. DWORD cch_data3 = sizeof(data3);
  590. RegQueryValueEx(hkey, "StorageFormat", 0, &valueType,
  591. (LPBYTE) &data3, &cch_data3);
  592. s2 = cmSystemTools::LowerCase(data1);
  593. cmSystemTools::ConvertToUnixSlashes(s2);
  594. if (s2 == s1)
  595. {
  596. macrosRegistered = true;
  597. }
  598. //std::cout << keyname << ":" << std::endl;
  599. //std::cout << " Path: " << data1 << std::endl;
  600. //std::cout << " Security: " << data2 << std::endl;
  601. //std::cout << " StorageFormat: " << data3 << std::endl;
  602. //std::cout << std::endl;
  603. RegCloseKey(hkey);
  604. }
  605. else
  606. {
  607. std::cout << "error opening key: " << keyname << std::endl;
  608. std::cout << std::endl;
  609. }
  610. return macrosRegistered;
  611. }
  612. //----------------------------------------------------------------------------
  613. void WriteVSMacrosFileRegistryEntry(
  614. const std::string& nextAvailableSubKeyName,
  615. const std::string& macrosFile,
  616. const std::string& regKeyBase)
  617. {
  618. std::string keyname = regKeyBase + "\\OtherProjects7";
  619. HKEY hkey = NULL;
  620. LONG result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(), 0,
  621. KEY_READ|KEY_WRITE, &hkey);
  622. if (ERROR_SUCCESS == result)
  623. {
  624. // Create the subkey and set the values of interest:
  625. HKEY hsubkey = NULL;
  626. char lpClass[] = "";
  627. result = RegCreateKeyEx(hkey, nextAvailableSubKeyName.c_str(), 0,
  628. lpClass, 0, KEY_READ|KEY_WRITE, 0, &hsubkey, 0);
  629. if (ERROR_SUCCESS == result)
  630. {
  631. DWORD dw = 0;
  632. std::string s(macrosFile);
  633. cmSystemTools::ReplaceString(s, "/", "\\");
  634. result = RegSetValueEx(hsubkey, "Path", 0, REG_SZ, (LPBYTE) s.c_str(),
  635. static_cast<DWORD>(strlen(s.c_str()) + 1));
  636. if (ERROR_SUCCESS != result)
  637. {
  638. std::cout << "error result 1: " << result << std::endl;
  639. std::cout << std::endl;
  640. }
  641. // Security value is always "1" for sample macros files (seems to be "2"
  642. // if you put the file somewhere outside the standard VSMacros folder)
  643. dw = 1;
  644. result = RegSetValueEx(hsubkey, "Security",
  645. 0, REG_DWORD, (LPBYTE) &dw, sizeof(DWORD));
  646. if (ERROR_SUCCESS != result)
  647. {
  648. std::cout << "error result 2: " << result << std::endl;
  649. std::cout << std::endl;
  650. }
  651. // StorageFormat value is always "0" for sample macros files
  652. dw = 0;
  653. result = RegSetValueEx(hsubkey, "StorageFormat",
  654. 0, REG_DWORD, (LPBYTE) &dw, sizeof(DWORD));
  655. if (ERROR_SUCCESS != result)
  656. {
  657. std::cout << "error result 3: " << result << std::endl;
  658. std::cout << std::endl;
  659. }
  660. RegCloseKey(hsubkey);
  661. }
  662. else
  663. {
  664. std::cout << "error creating subkey: "
  665. << nextAvailableSubKeyName << std::endl;
  666. std::cout << std::endl;
  667. }
  668. RegCloseKey(hkey);
  669. }
  670. else
  671. {
  672. std::cout << "error opening key: " << keyname << std::endl;
  673. std::cout << std::endl;
  674. }
  675. }
  676. //----------------------------------------------------------------------------
  677. void RegisterVisualStudioMacros(const std::string& macrosFile,
  678. const std::string& regKeyBase)
  679. {
  680. bool macrosRegistered;
  681. std::string nextAvailableSubKeyName;
  682. macrosRegistered = IsVisualStudioMacrosFileRegistered(macrosFile,
  683. regKeyBase, nextAvailableSubKeyName);
  684. if (!macrosRegistered)
  685. {
  686. int count = cmCallVisualStudioMacro::
  687. GetNumberOfRunningVisualStudioInstances("ALL");
  688. // Only register the macros file if there are *no* instances of Visual
  689. // Studio running. If we register it while one is running, first, it has
  690. // no effect on the running instance; second, and worse, Visual Studio
  691. // removes our newly added registration entry when it quits. Instead,
  692. // emit a warning asking the user to exit all running Visual Studio
  693. // instances...
  694. //
  695. if (0 != count)
  696. {
  697. std::ostringstream oss;
  698. oss << "Could not register CMake's Visual Studio macros file '"
  699. << CMAKE_VSMACROS_FILENAME "' while Visual Studio is running."
  700. << " Please exit all running instances of Visual Studio before"
  701. << " continuing." << std::endl
  702. << std::endl
  703. << "CMake needs to register Visual Studio macros when its macros"
  704. << " file is updated or when it detects that its current macros file"
  705. << " is no longer registered with Visual Studio."
  706. << std::endl;
  707. cmSystemTools::Message(oss.str().c_str(), "Warning");
  708. // Count them again now that the warning is over. In the case of a GUI
  709. // warning, the user may have gone to close Visual Studio and then come
  710. // back to the CMake GUI and clicked ok on the above warning. If so,
  711. // then register the macros *now* if the count is *now* 0...
  712. //
  713. count = cmCallVisualStudioMacro::
  714. GetNumberOfRunningVisualStudioInstances("ALL");
  715. // Also re-get the nextAvailableSubKeyName in case Visual Studio
  716. // wrote out new registered macros information as it was exiting:
  717. //
  718. if (0 == count)
  719. {
  720. IsVisualStudioMacrosFileRegistered(macrosFile, regKeyBase,
  721. nextAvailableSubKeyName);
  722. }
  723. }
  724. // Do another if check - 'count' may have changed inside the above if:
  725. //
  726. if (0 == count)
  727. {
  728. WriteVSMacrosFileRegistryEntry(nextAvailableSubKeyName, macrosFile,
  729. regKeyBase);
  730. }
  731. }
  732. }
  733. bool cmGlobalVisualStudioGenerator::TargetIsFortranOnly(cmTarget& target)
  734. {
  735. // check to see if this is a fortran build
  736. std::set<cmStdString> languages;
  737. target.GetLanguages(languages);
  738. if(languages.size() == 1)
  739. {
  740. if(*languages.begin() == "Fortran")
  741. {
  742. return true;
  743. }
  744. }
  745. return false;
  746. }
  747. //----------------------------------------------------------------------------
  748. bool
  749. cmGlobalVisualStudioGenerator::TargetCompare
  750. ::operator()(cmTarget const* l, cmTarget const* r) const
  751. {
  752. // Make sure ALL_BUILD is first so it is the default active project.
  753. if(strcmp(r->GetName(), "ALL_BUILD") == 0)
  754. {
  755. return false;
  756. }
  757. if(strcmp(l->GetName(), "ALL_BUILD") == 0)
  758. {
  759. return true;
  760. }
  761. return strcmp(l->GetName(), r->GetName()) < 0;
  762. }
  763. //----------------------------------------------------------------------------
  764. cmGlobalVisualStudioGenerator::OrderedTargetDependSet
  765. ::OrderedTargetDependSet(TargetDependSet const& targets)
  766. {
  767. for(TargetDependSet::const_iterator ti =
  768. targets.begin(); ti != targets.end(); ++ti)
  769. {
  770. this->insert(*ti);
  771. }
  772. }
  773. //----------------------------------------------------------------------------
  774. cmGlobalVisualStudioGenerator::OrderedTargetDependSet
  775. ::OrderedTargetDependSet(TargetSet const& targets)
  776. {
  777. for(TargetSet::const_iterator ti = targets.begin();
  778. ti != targets.end(); ++ti)
  779. {
  780. this->insert(*ti);
  781. }
  782. }