cmGlobalVisualStudioGenerator.cxx 29 KB

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