cmGlobalVisualStudioGenerator.cxx 29 KB

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