cmGlobalVisualStudioGenerator.cxx 32 KB

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