cmGlobalVisualStudioGenerator.cxx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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(linked.insert(target).second &&
  318. target->GetType() == cmTarget::STATIC_LIBRARY)
  319. {
  320. // Static library targets do not list their link dependencies so
  321. // we must follow them transitively now.
  322. TargetDependSet const& depends = this->GetTargetDirectDepends(*target);
  323. for(TargetDependSet::const_iterator di = depends.begin();
  324. di != depends.end(); ++di)
  325. {
  326. if(di->IsLink())
  327. {
  328. this->FollowLinkDepends(*di, linked);
  329. }
  330. }
  331. }
  332. }
  333. //----------------------------------------------------------------------------
  334. bool cmGlobalVisualStudioGenerator::ComputeTargetDepends()
  335. {
  336. if(!this->cmGlobalGenerator::ComputeTargetDepends())
  337. {
  338. return false;
  339. }
  340. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  341. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  342. {
  343. std::vector<cmLocalGenerator*>& gen = it->second;
  344. for(std::vector<cmLocalGenerator*>::iterator i = gen.begin();
  345. i != gen.end(); ++i)
  346. {
  347. cmTargets& targets = (*i)->GetMakefile()->GetTargets();
  348. for(cmTargets::iterator ti = targets.begin();
  349. ti != targets.end(); ++ti)
  350. {
  351. this->ComputeVSTargetDepends(ti->second);
  352. }
  353. }
  354. }
  355. return true;
  356. }
  357. //----------------------------------------------------------------------------
  358. static bool VSLinkable(cmTarget* t)
  359. {
  360. return t->IsLinkable() || t->GetType() == cmTarget::OBJECT_LIBRARY;
  361. }
  362. //----------------------------------------------------------------------------
  363. void cmGlobalVisualStudioGenerator::ComputeVSTargetDepends(cmTarget& target)
  364. {
  365. if(this->VSTargetDepends.find(&target) != this->VSTargetDepends.end())
  366. {
  367. return;
  368. }
  369. VSDependSet& vsTargetDepend = this->VSTargetDepends[&target];
  370. // VS <= 7.1 has two behaviors that affect solution dependencies.
  371. //
  372. // (1) Solution-level dependencies between a linkable target and a
  373. // library cause that library to be linked. We use an intermedite
  374. // empty utility target to express the dependency. (VS 8 and above
  375. // provide a project file "LinkLibraryDependencies" setting to
  376. // choose whether to activate this behavior. We disable it except
  377. // when linking external project files.)
  378. //
  379. // (2) We cannot let static libraries depend directly on targets to
  380. // which they "link" because the librarian tool will copy the
  381. // targets into the static library. While the work-around for
  382. // behavior (1) would also avoid this, it would create a large
  383. // number of extra utility targets for little gain. Instead, use
  384. // the above work-around only for dependencies explicitly added by
  385. // the add_dependencies() command. Approximate link dependencies by
  386. // leaving them out for the static library itself but following them
  387. // transitively for other targets.
  388. bool allowLinkable = (target.GetType() != cmTarget::STATIC_LIBRARY &&
  389. target.GetType() != cmTarget::SHARED_LIBRARY &&
  390. target.GetType() != cmTarget::MODULE_LIBRARY &&
  391. target.GetType() != cmTarget::EXECUTABLE);
  392. TargetDependSet const& depends = this->GetTargetDirectDepends(target);
  393. // Collect implicit link dependencies (target_link_libraries).
  394. // Static libraries cannot depend on their link implementation
  395. // due to behavior (2), but they do not really need to.
  396. std::set<cmTarget*> linkDepends;
  397. if(target.GetType() != cmTarget::STATIC_LIBRARY)
  398. {
  399. for(TargetDependSet::const_iterator di = depends.begin();
  400. di != depends.end(); ++di)
  401. {
  402. cmTargetDepend dep = *di;
  403. if(dep.IsLink())
  404. {
  405. this->FollowLinkDepends(dep, linkDepends);
  406. }
  407. }
  408. }
  409. // Collect explicit util dependencies (add_dependencies).
  410. std::set<cmTarget*> utilDepends;
  411. for(TargetDependSet::const_iterator di = depends.begin();
  412. di != depends.end(); ++di)
  413. {
  414. cmTargetDepend dep = *di;
  415. if(dep.IsUtil())
  416. {
  417. this->FollowLinkDepends(dep, utilDepends);
  418. }
  419. }
  420. // Collect all targets linked by this target so we can avoid
  421. // intermediate targets below.
  422. TargetSet linked;
  423. if(target.GetType() != cmTarget::STATIC_LIBRARY)
  424. {
  425. linked = this->GetTargetLinkClosure(&target);
  426. }
  427. // Emit link dependencies.
  428. for(std::set<cmTarget*>::iterator di = linkDepends.begin();
  429. di != linkDepends.end(); ++di)
  430. {
  431. cmTarget* dep = *di;
  432. vsTargetDepend.insert(dep->GetName());
  433. }
  434. // Emit util dependencies. Possibly use intermediate targets.
  435. for(std::set<cmTarget*>::iterator di = utilDepends.begin();
  436. di != utilDepends.end(); ++di)
  437. {
  438. cmTarget* dep = *di;
  439. if(allowLinkable || !VSLinkable(dep) || linked.count(dep))
  440. {
  441. // Direct dependency allowed.
  442. vsTargetDepend.insert(dep->GetName());
  443. }
  444. else
  445. {
  446. // Direct dependency on linkable target not allowed.
  447. // Use an intermediate utility target.
  448. vsTargetDepend.insert(this->GetUtilityDepend(dep));
  449. }
  450. }
  451. }
  452. //----------------------------------------------------------------------------
  453. void cmGlobalVisualStudioGenerator::AddPlatformDefinitions(cmMakefile* mf)
  454. {
  455. if(this->AdditionalPlatformDefinition)
  456. {
  457. mf->AddDefinition(this->AdditionalPlatformDefinition, "TRUE");
  458. }
  459. }
  460. //----------------------------------------------------------------------------
  461. std::string cmGlobalVisualStudioGenerator::GetUtilityDepend(cmTarget* target)
  462. {
  463. UtilityDependsMap::iterator i = this->UtilityDepends.find(target);
  464. if(i == this->UtilityDepends.end())
  465. {
  466. std::string name = this->WriteUtilityDepend(target);
  467. UtilityDependsMap::value_type entry(target, name);
  468. i = this->UtilityDepends.insert(entry).first;
  469. }
  470. return i->second;
  471. }
  472. //----------------------------------------------------------------------------
  473. #include <windows.h>
  474. //----------------------------------------------------------------------------
  475. bool IsVisualStudioMacrosFileRegistered(const std::string& macrosFile,
  476. const std::string& regKeyBase,
  477. std::string& nextAvailableSubKeyName)
  478. {
  479. bool macrosRegistered = false;
  480. std::string s1;
  481. std::string s2;
  482. // Make lowercase local copies, convert to Unix slashes, and
  483. // see if the resulting strings are the same:
  484. s1 = cmSystemTools::LowerCase(macrosFile);
  485. cmSystemTools::ConvertToUnixSlashes(s1);
  486. std::string keyname;
  487. HKEY hkey = NULL;
  488. LONG result = ERROR_SUCCESS;
  489. DWORD index = 0;
  490. keyname = regKeyBase + "\\OtherProjects7";
  491. hkey = NULL;
  492. result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(),
  493. 0, KEY_READ, &hkey);
  494. if (ERROR_SUCCESS == result)
  495. {
  496. // Iterate the subkeys and look for the values of interest in each subkey:
  497. CHAR subkeyname[256];
  498. DWORD cch_subkeyname = sizeof(subkeyname)/sizeof(subkeyname[0]);
  499. CHAR keyclass[256];
  500. DWORD cch_keyclass = sizeof(keyclass)/sizeof(keyclass[0]);
  501. FILETIME lastWriteTime;
  502. lastWriteTime.dwHighDateTime = 0;
  503. lastWriteTime.dwLowDateTime = 0;
  504. while (ERROR_SUCCESS == RegEnumKeyEx(hkey, index, subkeyname,
  505. &cch_subkeyname,
  506. 0, keyclass, &cch_keyclass, &lastWriteTime))
  507. {
  508. // Open the subkey and query the values of interest:
  509. HKEY hsubkey = NULL;
  510. result = RegOpenKeyEx(hkey, subkeyname, 0, KEY_READ, &hsubkey);
  511. if (ERROR_SUCCESS == result)
  512. {
  513. DWORD valueType = REG_SZ;
  514. CHAR data1[256];
  515. DWORD cch_data1 = sizeof(data1)/sizeof(data1[0]);
  516. RegQueryValueEx(hsubkey, "Path", 0, &valueType,
  517. (LPBYTE) &data1[0], &cch_data1);
  518. DWORD data2 = 0;
  519. DWORD cch_data2 = sizeof(data2);
  520. RegQueryValueEx(hsubkey, "Security", 0, &valueType,
  521. (LPBYTE) &data2, &cch_data2);
  522. DWORD data3 = 0;
  523. DWORD cch_data3 = sizeof(data3);
  524. RegQueryValueEx(hsubkey, "StorageFormat", 0, &valueType,
  525. (LPBYTE) &data3, &cch_data3);
  526. s2 = cmSystemTools::LowerCase(data1);
  527. cmSystemTools::ConvertToUnixSlashes(s2);
  528. if (s2 == s1)
  529. {
  530. macrosRegistered = true;
  531. }
  532. std::string fullname(data1);
  533. std::string filename;
  534. std::string filepath;
  535. std::string filepathname;
  536. std::string filepathpath;
  537. if (cmSystemTools::FileExists(fullname.c_str()))
  538. {
  539. filename = cmSystemTools::GetFilenameName(fullname);
  540. filepath = cmSystemTools::GetFilenamePath(fullname);
  541. filepathname = cmSystemTools::GetFilenameName(filepath);
  542. filepathpath = cmSystemTools::GetFilenamePath(filepath);
  543. }
  544. //std::cout << keyname << "\\" << subkeyname << ":" << std::endl;
  545. //std::cout << " Path: " << data1 << std::endl;
  546. //std::cout << " Security: " << data2 << std::endl;
  547. //std::cout << " StorageFormat: " << data3 << std::endl;
  548. //std::cout << " filename: " << filename << std::endl;
  549. //std::cout << " filepath: " << filepath << std::endl;
  550. //std::cout << " filepathname: " << filepathname << std::endl;
  551. //std::cout << " filepathpath: " << filepathpath << std::endl;
  552. //std::cout << std::endl;
  553. RegCloseKey(hsubkey);
  554. }
  555. else
  556. {
  557. std::cout << "error opening subkey: " << subkeyname << std::endl;
  558. std::cout << std::endl;
  559. }
  560. ++index;
  561. cch_subkeyname = sizeof(subkeyname)/sizeof(subkeyname[0]);
  562. cch_keyclass = sizeof(keyclass)/sizeof(keyclass[0]);
  563. lastWriteTime.dwHighDateTime = 0;
  564. lastWriteTime.dwLowDateTime = 0;
  565. }
  566. RegCloseKey(hkey);
  567. }
  568. else
  569. {
  570. std::cout << "error opening key: " << keyname << std::endl;
  571. std::cout << std::endl;
  572. }
  573. // Pass back next available sub key name, assuming sub keys always
  574. // follow the expected naming scheme. Expected naming scheme is that
  575. // the subkeys of OtherProjects7 is 0 to n-1, so it's ok to use "n"
  576. // as the name of the next subkey.
  577. std::ostringstream ossNext;
  578. ossNext << index;
  579. nextAvailableSubKeyName = ossNext.str();
  580. keyname = regKeyBase + "\\RecordingProject7";
  581. hkey = NULL;
  582. result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(),
  583. 0, KEY_READ, &hkey);
  584. if (ERROR_SUCCESS == result)
  585. {
  586. DWORD valueType = REG_SZ;
  587. CHAR data1[256];
  588. DWORD cch_data1 = sizeof(data1)/sizeof(data1[0]);
  589. RegQueryValueEx(hkey, "Path", 0, &valueType,
  590. (LPBYTE) &data1[0], &cch_data1);
  591. DWORD data2 = 0;
  592. DWORD cch_data2 = sizeof(data2);
  593. RegQueryValueEx(hkey, "Security", 0, &valueType,
  594. (LPBYTE) &data2, &cch_data2);
  595. DWORD data3 = 0;
  596. DWORD cch_data3 = sizeof(data3);
  597. RegQueryValueEx(hkey, "StorageFormat", 0, &valueType,
  598. (LPBYTE) &data3, &cch_data3);
  599. s2 = cmSystemTools::LowerCase(data1);
  600. cmSystemTools::ConvertToUnixSlashes(s2);
  601. if (s2 == s1)
  602. {
  603. macrosRegistered = true;
  604. }
  605. //std::cout << keyname << ":" << std::endl;
  606. //std::cout << " Path: " << data1 << std::endl;
  607. //std::cout << " Security: " << data2 << std::endl;
  608. //std::cout << " StorageFormat: " << data3 << std::endl;
  609. //std::cout << std::endl;
  610. RegCloseKey(hkey);
  611. }
  612. else
  613. {
  614. std::cout << "error opening key: " << keyname << std::endl;
  615. std::cout << std::endl;
  616. }
  617. return macrosRegistered;
  618. }
  619. //----------------------------------------------------------------------------
  620. void WriteVSMacrosFileRegistryEntry(
  621. const std::string& nextAvailableSubKeyName,
  622. const std::string& macrosFile,
  623. const std::string& regKeyBase)
  624. {
  625. std::string keyname = regKeyBase + "\\OtherProjects7";
  626. HKEY hkey = NULL;
  627. LONG result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(), 0,
  628. KEY_READ|KEY_WRITE, &hkey);
  629. if (ERROR_SUCCESS == result)
  630. {
  631. // Create the subkey and set the values of interest:
  632. HKEY hsubkey = NULL;
  633. char lpClass[] = "";
  634. result = RegCreateKeyEx(hkey, nextAvailableSubKeyName.c_str(), 0,
  635. lpClass, 0, KEY_READ|KEY_WRITE, 0, &hsubkey, 0);
  636. if (ERROR_SUCCESS == result)
  637. {
  638. DWORD dw = 0;
  639. std::string s(macrosFile);
  640. cmSystemTools::ReplaceString(s, "/", "\\");
  641. result = RegSetValueEx(hsubkey, "Path", 0, REG_SZ, (LPBYTE) s.c_str(),
  642. static_cast<DWORD>(strlen(s.c_str()) + 1));
  643. if (ERROR_SUCCESS != result)
  644. {
  645. std::cout << "error result 1: " << result << std::endl;
  646. std::cout << std::endl;
  647. }
  648. // Security value is always "1" for sample macros files (seems to be "2"
  649. // if you put the file somewhere outside the standard VSMacros folder)
  650. dw = 1;
  651. result = RegSetValueEx(hsubkey, "Security",
  652. 0, REG_DWORD, (LPBYTE) &dw, sizeof(DWORD));
  653. if (ERROR_SUCCESS != result)
  654. {
  655. std::cout << "error result 2: " << result << std::endl;
  656. std::cout << std::endl;
  657. }
  658. // StorageFormat value is always "0" for sample macros files
  659. dw = 0;
  660. result = RegSetValueEx(hsubkey, "StorageFormat",
  661. 0, REG_DWORD, (LPBYTE) &dw, sizeof(DWORD));
  662. if (ERROR_SUCCESS != result)
  663. {
  664. std::cout << "error result 3: " << result << std::endl;
  665. std::cout << std::endl;
  666. }
  667. RegCloseKey(hsubkey);
  668. }
  669. else
  670. {
  671. std::cout << "error creating subkey: "
  672. << nextAvailableSubKeyName << std::endl;
  673. std::cout << std::endl;
  674. }
  675. RegCloseKey(hkey);
  676. }
  677. else
  678. {
  679. std::cout << "error opening key: " << keyname << std::endl;
  680. std::cout << std::endl;
  681. }
  682. }
  683. //----------------------------------------------------------------------------
  684. void RegisterVisualStudioMacros(const std::string& macrosFile,
  685. const std::string& regKeyBase)
  686. {
  687. bool macrosRegistered;
  688. std::string nextAvailableSubKeyName;
  689. macrosRegistered = IsVisualStudioMacrosFileRegistered(macrosFile,
  690. regKeyBase, nextAvailableSubKeyName);
  691. if (!macrosRegistered)
  692. {
  693. int count = cmCallVisualStudioMacro::
  694. GetNumberOfRunningVisualStudioInstances("ALL");
  695. // Only register the macros file if there are *no* instances of Visual
  696. // Studio running. If we register it while one is running, first, it has
  697. // no effect on the running instance; second, and worse, Visual Studio
  698. // removes our newly added registration entry when it quits. Instead,
  699. // emit a warning asking the user to exit all running Visual Studio
  700. // instances...
  701. //
  702. if (0 != count)
  703. {
  704. std::ostringstream oss;
  705. oss << "Could not register CMake's Visual Studio macros file '"
  706. << CMAKE_VSMACROS_FILENAME "' while Visual Studio is running."
  707. << " Please exit all running instances of Visual Studio before"
  708. << " continuing." << std::endl
  709. << std::endl
  710. << "CMake needs to register Visual Studio macros when its macros"
  711. << " file is updated or when it detects that its current macros file"
  712. << " is no longer registered with Visual Studio."
  713. << std::endl;
  714. cmSystemTools::Message(oss.str().c_str(), "Warning");
  715. // Count them again now that the warning is over. In the case of a GUI
  716. // warning, the user may have gone to close Visual Studio and then come
  717. // back to the CMake GUI and clicked ok on the above warning. If so,
  718. // then register the macros *now* if the count is *now* 0...
  719. //
  720. count = cmCallVisualStudioMacro::
  721. GetNumberOfRunningVisualStudioInstances("ALL");
  722. // Also re-get the nextAvailableSubKeyName in case Visual Studio
  723. // wrote out new registered macros information as it was exiting:
  724. //
  725. if (0 == count)
  726. {
  727. IsVisualStudioMacrosFileRegistered(macrosFile, regKeyBase,
  728. nextAvailableSubKeyName);
  729. }
  730. }
  731. // Do another if check - 'count' may have changed inside the above if:
  732. //
  733. if (0 == count)
  734. {
  735. WriteVSMacrosFileRegistryEntry(nextAvailableSubKeyName, macrosFile,
  736. regKeyBase);
  737. }
  738. }
  739. }
  740. bool cmGlobalVisualStudioGenerator::TargetIsFortranOnly(cmTarget& target)
  741. {
  742. // check to see if this is a fortran build
  743. std::set<cmStdString> languages;
  744. target.GetLanguages(languages);
  745. if(languages.size() == 1)
  746. {
  747. if(*languages.begin() == "Fortran")
  748. {
  749. return true;
  750. }
  751. }
  752. return false;
  753. }
  754. //----------------------------------------------------------------------------
  755. bool
  756. cmGlobalVisualStudioGenerator::TargetCompare
  757. ::operator()(cmTarget const* l, cmTarget const* r) const
  758. {
  759. // Make sure ALL_BUILD is first so it is the default active project.
  760. if(strcmp(r->GetName(), "ALL_BUILD") == 0)
  761. {
  762. return false;
  763. }
  764. if(strcmp(l->GetName(), "ALL_BUILD") == 0)
  765. {
  766. return true;
  767. }
  768. return strcmp(l->GetName(), r->GetName()) < 0;
  769. }
  770. //----------------------------------------------------------------------------
  771. cmGlobalVisualStudioGenerator::OrderedTargetDependSet
  772. ::OrderedTargetDependSet(TargetDependSet const& targets)
  773. {
  774. for(TargetDependSet::const_iterator ti =
  775. targets.begin(); ti != targets.end(); ++ti)
  776. {
  777. this->insert(*ti);
  778. }
  779. }
  780. //----------------------------------------------------------------------------
  781. cmGlobalVisualStudioGenerator::OrderedTargetDependSet
  782. ::OrderedTargetDependSet(TargetSet const& targets)
  783. {
  784. for(TargetSet::const_iterator ti = targets.begin();
  785. ti != targets.end(); ++ti)
  786. {
  787. this->insert(*ti);
  788. }
  789. }