cmGlobalVisualStudioGenerator.cxx 32 KB

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