cmGlobalVisualStudioGenerator.cxx 32 KB

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