cmGlobalVisualStudioGenerator.cxx 33 KB

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