cmGlobalVisualStudioGenerator.cxx 32 KB

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