cmGlobalVisualStudioGenerator.cxx 33 KB

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