cmGlobalVisualStudioGenerator.cxx 29 KB

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