cmGlobalVisualStudioGenerator.cxx 30 KB

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