cmGlobalVisualStudioGenerator.cxx 33 KB

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