cmGlobalVisualStudioGenerator.cxx 33 KB

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