cmGlobalVisualStudioGenerator.cxx 29 KB

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