cmGlobalVisualStudioGenerator.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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 "cmLocalGenerator.h"
  13. #include "cmMakefile.h"
  14. #include "cmTarget.h"
  15. //----------------------------------------------------------------------------
  16. cmGlobalVisualStudioGenerator::cmGlobalVisualStudioGenerator()
  17. {
  18. }
  19. //----------------------------------------------------------------------------
  20. cmGlobalVisualStudioGenerator::~cmGlobalVisualStudioGenerator()
  21. {
  22. }
  23. //----------------------------------------------------------------------------
  24. std::string cmGlobalVisualStudioGenerator::GetRegistryBase()
  25. {
  26. std::string key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\";
  27. key += this->GetIDEVersion();
  28. return key;
  29. }
  30. //----------------------------------------------------------------------------
  31. void cmGlobalVisualStudioGenerator::Generate()
  32. {
  33. // Add a special target that depends on ALL projects for easy build
  34. // of one configuration only.
  35. const char* no_working_dir = 0;
  36. std::vector<std::string> no_depends;
  37. cmCustomCommandLines no_commands;
  38. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  39. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  40. {
  41. std::vector<cmLocalGenerator*>& gen = it->second;
  42. // add the ALL_BUILD to the first local generator of each project
  43. if(gen.size())
  44. {
  45. // Use no actual command lines so that the target itself is not
  46. // considered always out of date.
  47. cmTarget* allBuild =
  48. gen[0]->GetMakefile()->
  49. AddUtilityCommand("ALL_BUILD", true, no_working_dir,
  50. no_depends, no_commands, false,
  51. "Build all projects");
  52. #if 0
  53. // Can't activate this code because we want ALL_BUILD
  54. // selected as the default "startup project" when first
  55. // opened in Visual Studio... And if it's nested in a
  56. // folder, then that doesn't happen.
  57. //
  58. // Organize in the "predefined targets" folder:
  59. //
  60. if (this->UseFolderProperty())
  61. {
  62. allBuild->SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
  63. }
  64. #endif
  65. // Now make all targets depend on the ALL_BUILD target
  66. cmTargets targets;
  67. for(std::vector<cmLocalGenerator*>::iterator i = gen.begin();
  68. i != gen.end(); ++i)
  69. {
  70. cmTargets& targets = (*i)->GetMakefile()->GetTargets();
  71. for(cmTargets::iterator t = targets.begin();
  72. t != targets.end(); ++t)
  73. {
  74. if(!this->IsExcluded(gen[0], t->second))
  75. {
  76. allBuild->AddUtility(t->second.GetName());
  77. }
  78. }
  79. }
  80. }
  81. }
  82. // Fix utility dependencies to avoid linking to libraries.
  83. this->FixUtilityDepends();
  84. // Configure CMake Visual Studio macros, for this user on this version
  85. // of Visual Studio.
  86. this->ConfigureCMakeVisualStudioMacros();
  87. // Run all the local generators.
  88. this->cmGlobalGenerator::Generate();
  89. }
  90. //----------------------------------------------------------------------------
  91. bool IsVisualStudioMacrosFileRegistered(const std::string& macrosFile,
  92. const std::string& regKeyBase,
  93. std::string& nextAvailableSubKeyName);
  94. void RegisterVisualStudioMacros(const std::string& macrosFile,
  95. const std::string& regKeyBase);
  96. //----------------------------------------------------------------------------
  97. #define CMAKE_VSMACROS_FILENAME \
  98. "CMakeVSMacros2.vsmacros"
  99. #define CMAKE_VSMACROS_RELOAD_MACRONAME \
  100. "Macros.CMakeVSMacros2.Macros.ReloadProjects"
  101. #define CMAKE_VSMACROS_STOP_MACRONAME \
  102. "Macros.CMakeVSMacros2.Macros.StopBuild"
  103. //----------------------------------------------------------------------------
  104. void cmGlobalVisualStudioGenerator::ConfigureCMakeVisualStudioMacros()
  105. {
  106. cmMakefile* mf = this->LocalGenerators[0]->GetMakefile();
  107. std::string dir = this->GetUserMacrosDirectory();
  108. if (mf != 0 && dir != "")
  109. {
  110. std::string src = mf->GetRequiredDefinition("CMAKE_ROOT");
  111. src += "/Templates/" CMAKE_VSMACROS_FILENAME;
  112. std::string dst = dir + "/CMakeMacros/" CMAKE_VSMACROS_FILENAME;
  113. // Copy the macros file to the user directory only if the
  114. // destination does not exist or the source location is newer.
  115. // This will allow the user to edit the macros for development
  116. // purposes but newer versions distributed with CMake will replace
  117. // older versions in user directories.
  118. int res;
  119. if(!cmSystemTools::FileTimeCompare(src.c_str(), dst.c_str(), &res) ||
  120. res > 0)
  121. {
  122. if (!cmSystemTools::CopyFileAlways(src.c_str(), dst.c_str()))
  123. {
  124. std::ostringstream oss;
  125. oss << "Could not copy from: " << src << std::endl;
  126. oss << " to: " << dst << std::endl;
  127. cmSystemTools::Message(oss.str().c_str(), "Warning");
  128. }
  129. }
  130. RegisterVisualStudioMacros(dst, this->GetUserMacrosRegKeyBase());
  131. }
  132. }
  133. //----------------------------------------------------------------------------
  134. void
  135. cmGlobalVisualStudioGenerator
  136. ::CallVisualStudioMacro(MacroName m,
  137. const char* vsSolutionFile)
  138. {
  139. // If any solution or project files changed during the generation,
  140. // tell Visual Studio to reload them...
  141. cmMakefile* mf = this->LocalGenerators[0]->GetMakefile();
  142. std::string dir = this->GetUserMacrosDirectory();
  143. // Only really try to call the macro if:
  144. // - mf is non-NULL
  145. // - there is a UserMacrosDirectory
  146. // - the CMake vsmacros file exists
  147. // - the CMake vsmacros file is registered
  148. // - there were .sln/.vcproj files changed during generation
  149. //
  150. if (mf != 0 && dir != "")
  151. {
  152. std::string macrosFile = dir + "/CMakeMacros/" CMAKE_VSMACROS_FILENAME;
  153. std::string nextSubkeyName;
  154. if (cmSystemTools::FileExists(macrosFile.c_str()) &&
  155. IsVisualStudioMacrosFileRegistered(macrosFile,
  156. this->GetUserMacrosRegKeyBase(), nextSubkeyName)
  157. )
  158. {
  159. std::string topLevelSlnName;
  160. if(vsSolutionFile)
  161. {
  162. topLevelSlnName = vsSolutionFile;
  163. }
  164. else
  165. {
  166. topLevelSlnName = mf->GetStartOutputDirectory();
  167. topLevelSlnName += "/";
  168. topLevelSlnName += mf->GetProjectName();
  169. topLevelSlnName += ".sln";
  170. }
  171. if(m == MacroReload)
  172. {
  173. std::vector<std::string> filenames;
  174. this->GetFilesReplacedDuringGenerate(filenames);
  175. if (filenames.size() > 0)
  176. {
  177. // Convert vector to semi-colon delimited string of filenames:
  178. std::string projects;
  179. std::vector<std::string>::iterator it = filenames.begin();
  180. if (it != filenames.end())
  181. {
  182. projects = *it;
  183. ++it;
  184. }
  185. for (; it != filenames.end(); ++it)
  186. {
  187. projects += ";";
  188. projects += *it;
  189. }
  190. cmCallVisualStudioMacro::CallMacro(topLevelSlnName,
  191. CMAKE_VSMACROS_RELOAD_MACRONAME, projects,
  192. this->GetCMakeInstance()->GetDebugOutput());
  193. }
  194. }
  195. else if(m == MacroStop)
  196. {
  197. cmCallVisualStudioMacro::CallMacro(topLevelSlnName,
  198. CMAKE_VSMACROS_STOP_MACRONAME, "",
  199. this->GetCMakeInstance()->GetDebugOutput());
  200. }
  201. }
  202. }
  203. }
  204. //----------------------------------------------------------------------------
  205. std::string cmGlobalVisualStudioGenerator::GetUserMacrosDirectory()
  206. {
  207. return "";
  208. }
  209. //----------------------------------------------------------------------------
  210. std::string cmGlobalVisualStudioGenerator::GetUserMacrosRegKeyBase()
  211. {
  212. return "";
  213. }
  214. //----------------------------------------------------------------------------
  215. void cmGlobalVisualStudioGenerator::FixUtilityDepends()
  216. {
  217. // Skip for VS versions 8 and above.
  218. if(!this->VSLinksDependencies())
  219. {
  220. return;
  221. }
  222. // For VS versions before 8:
  223. //
  224. // When a target that links contains a project-level dependency on a
  225. // library target that library is automatically linked. In order to
  226. // allow utility-style project-level dependencies that do not
  227. // actually link we need to automatically insert an intermediate
  228. // custom target.
  229. //
  230. // Here we edit the utility dependencies of a target to add the
  231. // intermediate custom target when necessary.
  232. for(unsigned i = 0; i < this->LocalGenerators.size(); ++i)
  233. {
  234. cmTargets* targets =
  235. &(this->LocalGenerators[i]->GetMakefile()->GetTargets());
  236. for(cmTargets::iterator tarIt = targets->begin();
  237. tarIt != targets->end(); ++tarIt)
  238. {
  239. this->FixUtilityDependsForTarget(tarIt->second);
  240. }
  241. }
  242. }
  243. //----------------------------------------------------------------------------
  244. void
  245. cmGlobalVisualStudioGenerator::FixUtilityDependsForTarget(cmTarget& target)
  246. {
  247. // Only targets that link need to be fixed.
  248. if(target.GetType() != cmTarget::STATIC_LIBRARY &&
  249. target.GetType() != cmTarget::SHARED_LIBRARY &&
  250. target.GetType() != cmTarget::MODULE_LIBRARY &&
  251. target.GetType() != cmTarget::EXECUTABLE)
  252. {
  253. return;
  254. }
  255. #if 0
  256. // This feature makes a mess in SLN files for VS 7.1 and below. It
  257. // creates an extra target for every target that is "linked" by a
  258. // static library. Without this feature static libraries do not
  259. // wait until their "link" dependencies are built to build. This is
  260. // not a problem 99.9% of the time, and projects that do have the
  261. // problem can enable this work-around by using add_dependencies.
  262. // Static libraries cannot depend directly on the targets to which
  263. // they link because VS will copy those targets into the library
  264. // (for VS < 8). To work around the problem we copy the
  265. // dependencies to be utility dependencies so that the work-around
  266. // below is used.
  267. if(target.GetType() == cmTarget::STATIC_LIBRARY)
  268. {
  269. cmTarget::LinkLibraryVectorType const& libs = target.GetLinkLibraries();
  270. for(cmTarget::LinkLibraryVectorType::const_iterator i = libs.begin();
  271. i != libs.end(); ++i)
  272. {
  273. if(cmTarget* depTarget = this->FindTarget(0, i->first.c_str(), false))
  274. {
  275. target.AddUtility(depTarget->GetName());
  276. }
  277. }
  278. }
  279. #endif
  280. // Look at each utility dependency.
  281. for(std::set<cmStdString>::const_iterator ui =
  282. target.GetUtilities().begin();
  283. ui != target.GetUtilities().end(); ++ui)
  284. {
  285. if(cmTarget* depTarget = this->FindTarget(0, ui->c_str()))
  286. {
  287. if(depTarget->GetType() == cmTarget::STATIC_LIBRARY ||
  288. depTarget->GetType() == cmTarget::SHARED_LIBRARY ||
  289. depTarget->GetType() == cmTarget::MODULE_LIBRARY)
  290. {
  291. // This utility dependency will cause an attempt to link. If
  292. // the depender does not already link the dependee we need an
  293. // intermediate target.
  294. if(!this->CheckTargetLinks(target, ui->c_str()))
  295. {
  296. this->CreateUtilityDependTarget(*depTarget);
  297. }
  298. }
  299. }
  300. }
  301. }
  302. //----------------------------------------------------------------------------
  303. void
  304. cmGlobalVisualStudioGenerator::CreateUtilityDependTarget(cmTarget& target)
  305. {
  306. // This target is a library on which a utility dependency exists.
  307. // We need to create an intermediate custom target to hook up the
  308. // dependency without causing a link.
  309. const char* altName = target.GetProperty("ALTERNATIVE_DEPENDENCY_NAME");
  310. if(!altName)
  311. {
  312. // Create the intermediate utility target.
  313. std::string altNameStr = target.GetName();
  314. altNameStr += "_UTILITY";
  315. const std::vector<std::string> no_depends;
  316. cmCustomCommandLines no_commands;
  317. const char* no_working_dir = 0;
  318. const char* no_comment = 0;
  319. target.GetMakefile()->AddUtilityCommand(altNameStr.c_str(), true,
  320. no_working_dir, no_depends,
  321. no_commands, false, no_comment);
  322. target.SetProperty("ALTERNATIVE_DEPENDENCY_NAME", altNameStr.c_str());
  323. // Most targets have a GUID created in ConfigureFinalPass. Since
  324. // that has already been called, create one for this target now.
  325. this->CreateGUID(altNameStr.c_str());
  326. // The intermediate target should depend on the original target.
  327. if(cmTarget* alt = this->FindTarget(0, altNameStr.c_str()))
  328. {
  329. alt->AddUtility(target.GetName());
  330. }
  331. }
  332. }
  333. //----------------------------------------------------------------------------
  334. bool cmGlobalVisualStudioGenerator::CheckTargetLinks(cmTarget& target,
  335. const char* name)
  336. {
  337. // Return whether the given target links to a target with the given name.
  338. if(target.GetType() == cmTarget::STATIC_LIBRARY)
  339. {
  340. // Static libraries never link to anything.
  341. return false;
  342. }
  343. cmTarget::LinkLibraryVectorType const& libs = target.GetLinkLibraries();
  344. for(cmTarget::LinkLibraryVectorType::const_iterator i = libs.begin();
  345. i != libs.end(); ++i)
  346. {
  347. if(i->first == name)
  348. {
  349. return true;
  350. }
  351. }
  352. return false;
  353. }
  354. //----------------------------------------------------------------------------
  355. const char*
  356. cmGlobalVisualStudioGenerator::GetUtilityForTarget(cmTarget& target,
  357. const char* name)
  358. {
  359. // Possibly depend on an intermediate utility target to avoid
  360. // linking.
  361. if(target.GetType() == cmTarget::STATIC_LIBRARY ||
  362. target.GetType() == cmTarget::SHARED_LIBRARY ||
  363. target.GetType() == cmTarget::MODULE_LIBRARY ||
  364. target.GetType() == cmTarget::EXECUTABLE)
  365. {
  366. // The depender is a target that links. Lookup the dependee to
  367. // see if it provides an alternative dependency name.
  368. if(cmTarget* depTarget = this->FindTarget(0, name))
  369. {
  370. // Check for an alternative name created by FixUtilityDepends.
  371. if(const char* altName =
  372. depTarget->GetProperty("ALTERNATIVE_DEPENDENCY_NAME"))
  373. {
  374. // The alternative name is needed only if the depender does
  375. // not really link to the dependee.
  376. if(!this->CheckTargetLinks(target, name))
  377. {
  378. return altName;
  379. }
  380. }
  381. }
  382. }
  383. // No special case. Just use the original dependency name.
  384. return name;
  385. }
  386. //----------------------------------------------------------------------------
  387. void cmGlobalVisualStudioGenerator::GetTargetSets(
  388. TargetDependSet& projectTargets, TargetDependSet& originalTargets,
  389. cmLocalGenerator* root, GeneratorVector const& generators
  390. )
  391. {
  392. this->cmGlobalGenerator::GetTargetSets(projectTargets, originalTargets,
  393. root, generators);
  394. // Add alternative dependency targets created by FixUtilityDepends.
  395. for(TargetDependSet::iterator ti = projectTargets.begin();
  396. ti != projectTargets.end(); ++ti)
  397. {
  398. cmTarget* tgt = *ti;
  399. if(const char* altName = tgt->GetProperty("ALTERNATIVE_DEPENDENCY_NAME"))
  400. {
  401. if(cmTarget* alt = tgt->GetMakefile()->FindTarget(altName))
  402. {
  403. projectTargets.insert(alt);
  404. }
  405. }
  406. }
  407. }
  408. //----------------------------------------------------------------------------
  409. #include <windows.h>
  410. //----------------------------------------------------------------------------
  411. bool IsVisualStudioMacrosFileRegistered(const std::string& macrosFile,
  412. const std::string& regKeyBase,
  413. std::string& nextAvailableSubKeyName)
  414. {
  415. bool macrosRegistered = false;
  416. std::string s1;
  417. std::string s2;
  418. // Make lowercase local copies, convert to Unix slashes, and
  419. // see if the resulting strings are the same:
  420. s1 = cmSystemTools::LowerCase(macrosFile);
  421. cmSystemTools::ConvertToUnixSlashes(s1);
  422. std::string keyname;
  423. HKEY hkey = NULL;
  424. LONG result = ERROR_SUCCESS;
  425. DWORD index = 0;
  426. keyname = regKeyBase + "\\OtherProjects7";
  427. hkey = NULL;
  428. result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(),
  429. 0, KEY_READ, &hkey);
  430. if (ERROR_SUCCESS == result)
  431. {
  432. // Iterate the subkeys and look for the values of interest in each subkey:
  433. CHAR subkeyname[256];
  434. DWORD cch_subkeyname = sizeof(subkeyname)/sizeof(subkeyname[0]);
  435. CHAR keyclass[256];
  436. DWORD cch_keyclass = sizeof(keyclass)/sizeof(keyclass[0]);
  437. FILETIME lastWriteTime;
  438. lastWriteTime.dwHighDateTime = 0;
  439. lastWriteTime.dwLowDateTime = 0;
  440. while (ERROR_SUCCESS == RegEnumKeyEx(hkey, index, subkeyname,
  441. &cch_subkeyname,
  442. 0, keyclass, &cch_keyclass, &lastWriteTime))
  443. {
  444. // Open the subkey and query the values of interest:
  445. HKEY hsubkey = NULL;
  446. result = RegOpenKeyEx(hkey, subkeyname, 0, KEY_READ, &hsubkey);
  447. if (ERROR_SUCCESS == result)
  448. {
  449. DWORD valueType = REG_SZ;
  450. CHAR data1[256];
  451. DWORD cch_data1 = sizeof(data1)/sizeof(data1[0]);
  452. RegQueryValueEx(hsubkey, "Path", 0, &valueType,
  453. (LPBYTE) &data1[0], &cch_data1);
  454. DWORD data2 = 0;
  455. DWORD cch_data2 = sizeof(data2);
  456. RegQueryValueEx(hsubkey, "Security", 0, &valueType,
  457. (LPBYTE) &data2, &cch_data2);
  458. DWORD data3 = 0;
  459. DWORD cch_data3 = sizeof(data3);
  460. RegQueryValueEx(hsubkey, "StorageFormat", 0, &valueType,
  461. (LPBYTE) &data3, &cch_data3);
  462. s2 = cmSystemTools::LowerCase(data1);
  463. cmSystemTools::ConvertToUnixSlashes(s2);
  464. if (s2 == s1)
  465. {
  466. macrosRegistered = true;
  467. }
  468. std::string fullname(data1);
  469. std::string filename;
  470. std::string filepath;
  471. std::string filepathname;
  472. std::string filepathpath;
  473. if (cmSystemTools::FileExists(fullname.c_str()))
  474. {
  475. filename = cmSystemTools::GetFilenameName(fullname);
  476. filepath = cmSystemTools::GetFilenamePath(fullname);
  477. filepathname = cmSystemTools::GetFilenameName(filepath);
  478. filepathpath = cmSystemTools::GetFilenamePath(filepath);
  479. }
  480. //std::cout << keyname << "\\" << subkeyname << ":" << std::endl;
  481. //std::cout << " Path: " << data1 << std::endl;
  482. //std::cout << " Security: " << data2 << std::endl;
  483. //std::cout << " StorageFormat: " << data3 << std::endl;
  484. //std::cout << " filename: " << filename << std::endl;
  485. //std::cout << " filepath: " << filepath << std::endl;
  486. //std::cout << " filepathname: " << filepathname << std::endl;
  487. //std::cout << " filepathpath: " << filepathpath << std::endl;
  488. //std::cout << std::endl;
  489. RegCloseKey(hsubkey);
  490. }
  491. else
  492. {
  493. std::cout << "error opening subkey: " << subkeyname << std::endl;
  494. std::cout << std::endl;
  495. }
  496. ++index;
  497. cch_subkeyname = sizeof(subkeyname)/sizeof(subkeyname[0]);
  498. cch_keyclass = sizeof(keyclass)/sizeof(keyclass[0]);
  499. lastWriteTime.dwHighDateTime = 0;
  500. lastWriteTime.dwLowDateTime = 0;
  501. }
  502. RegCloseKey(hkey);
  503. }
  504. else
  505. {
  506. std::cout << "error opening key: " << keyname << std::endl;
  507. std::cout << std::endl;
  508. }
  509. // Pass back next available sub key name, assuming sub keys always
  510. // follow the expected naming scheme. Expected naming scheme is that
  511. // the subkeys of OtherProjects7 is 0 to n-1, so it's ok to use "n"
  512. // as the name of the next subkey.
  513. std::ostringstream ossNext;
  514. ossNext << index;
  515. nextAvailableSubKeyName = ossNext.str();
  516. keyname = regKeyBase + "\\RecordingProject7";
  517. hkey = NULL;
  518. result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(),
  519. 0, KEY_READ, &hkey);
  520. if (ERROR_SUCCESS == result)
  521. {
  522. DWORD valueType = REG_SZ;
  523. CHAR data1[256];
  524. DWORD cch_data1 = sizeof(data1)/sizeof(data1[0]);
  525. RegQueryValueEx(hkey, "Path", 0, &valueType,
  526. (LPBYTE) &data1[0], &cch_data1);
  527. DWORD data2 = 0;
  528. DWORD cch_data2 = sizeof(data2);
  529. RegQueryValueEx(hkey, "Security", 0, &valueType,
  530. (LPBYTE) &data2, &cch_data2);
  531. DWORD data3 = 0;
  532. DWORD cch_data3 = sizeof(data3);
  533. RegQueryValueEx(hkey, "StorageFormat", 0, &valueType,
  534. (LPBYTE) &data3, &cch_data3);
  535. s2 = cmSystemTools::LowerCase(data1);
  536. cmSystemTools::ConvertToUnixSlashes(s2);
  537. if (s2 == s1)
  538. {
  539. macrosRegistered = true;
  540. }
  541. //std::cout << keyname << ":" << std::endl;
  542. //std::cout << " Path: " << data1 << std::endl;
  543. //std::cout << " Security: " << data2 << std::endl;
  544. //std::cout << " StorageFormat: " << data3 << std::endl;
  545. //std::cout << std::endl;
  546. RegCloseKey(hkey);
  547. }
  548. else
  549. {
  550. std::cout << "error opening key: " << keyname << std::endl;
  551. std::cout << std::endl;
  552. }
  553. return macrosRegistered;
  554. }
  555. //----------------------------------------------------------------------------
  556. void WriteVSMacrosFileRegistryEntry(
  557. const std::string& nextAvailableSubKeyName,
  558. const std::string& macrosFile,
  559. const std::string& regKeyBase)
  560. {
  561. std::string keyname = regKeyBase + "\\OtherProjects7";
  562. HKEY hkey = NULL;
  563. LONG result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(), 0,
  564. KEY_READ|KEY_WRITE, &hkey);
  565. if (ERROR_SUCCESS == result)
  566. {
  567. // Create the subkey and set the values of interest:
  568. HKEY hsubkey = NULL;
  569. result = RegCreateKeyEx(hkey, nextAvailableSubKeyName.c_str(), 0, "", 0,
  570. KEY_READ|KEY_WRITE, 0, &hsubkey, 0);
  571. if (ERROR_SUCCESS == result)
  572. {
  573. DWORD dw = 0;
  574. std::string s(macrosFile);
  575. cmSystemTools::ReplaceString(s, "/", "\\");
  576. result = RegSetValueEx(hsubkey, "Path", 0, REG_SZ, (LPBYTE) s.c_str(),
  577. static_cast<DWORD>(strlen(s.c_str()) + 1));
  578. if (ERROR_SUCCESS != result)
  579. {
  580. std::cout << "error result 1: " << result << std::endl;
  581. std::cout << std::endl;
  582. }
  583. // Security value is always "1" for sample macros files (seems to be "2"
  584. // if you put the file somewhere outside the standard VSMacros folder)
  585. dw = 1;
  586. result = RegSetValueEx(hsubkey, "Security",
  587. 0, REG_DWORD, (LPBYTE) &dw, sizeof(DWORD));
  588. if (ERROR_SUCCESS != result)
  589. {
  590. std::cout << "error result 2: " << result << std::endl;
  591. std::cout << std::endl;
  592. }
  593. // StorageFormat value is always "0" for sample macros files
  594. dw = 0;
  595. result = RegSetValueEx(hsubkey, "StorageFormat",
  596. 0, REG_DWORD, (LPBYTE) &dw, sizeof(DWORD));
  597. if (ERROR_SUCCESS != result)
  598. {
  599. std::cout << "error result 3: " << result << std::endl;
  600. std::cout << std::endl;
  601. }
  602. RegCloseKey(hsubkey);
  603. }
  604. else
  605. {
  606. std::cout << "error creating subkey: "
  607. << nextAvailableSubKeyName << std::endl;
  608. std::cout << std::endl;
  609. }
  610. RegCloseKey(hkey);
  611. }
  612. else
  613. {
  614. std::cout << "error opening key: " << keyname << std::endl;
  615. std::cout << std::endl;
  616. }
  617. }
  618. //----------------------------------------------------------------------------
  619. void RegisterVisualStudioMacros(const std::string& macrosFile,
  620. const std::string& regKeyBase)
  621. {
  622. bool macrosRegistered;
  623. std::string nextAvailableSubKeyName;
  624. macrosRegistered = IsVisualStudioMacrosFileRegistered(macrosFile,
  625. regKeyBase, nextAvailableSubKeyName);
  626. if (!macrosRegistered)
  627. {
  628. int count = cmCallVisualStudioMacro::
  629. GetNumberOfRunningVisualStudioInstances("ALL");
  630. // Only register the macros file if there are *no* instances of Visual
  631. // Studio running. If we register it while one is running, first, it has
  632. // no effect on the running instance; second, and worse, Visual Studio
  633. // removes our newly added registration entry when it quits. Instead,
  634. // emit a warning asking the user to exit all running Visual Studio
  635. // instances...
  636. //
  637. if (0 != count)
  638. {
  639. std::ostringstream oss;
  640. oss << "Could not register CMake's Visual Studio macros file '"
  641. << CMAKE_VSMACROS_FILENAME "' while Visual Studio is running."
  642. << " Please exit all running instances of Visual Studio before"
  643. << " continuing." << std::endl
  644. << std::endl
  645. << "CMake needs to register Visual Studio macros when its macros"
  646. << " file is updated or when it detects that its current macros file"
  647. << " is no longer registered with Visual Studio."
  648. << std::endl;
  649. cmSystemTools::Message(oss.str().c_str(), "Warning");
  650. // Count them again now that the warning is over. In the case of a GUI
  651. // warning, the user may have gone to close Visual Studio and then come
  652. // back to the CMake GUI and clicked ok on the above warning. If so,
  653. // then register the macros *now* if the count is *now* 0...
  654. //
  655. count = cmCallVisualStudioMacro::
  656. GetNumberOfRunningVisualStudioInstances("ALL");
  657. // Also re-get the nextAvailableSubKeyName in case Visual Studio
  658. // wrote out new registered macros information as it was exiting:
  659. //
  660. if (0 == count)
  661. {
  662. IsVisualStudioMacrosFileRegistered(macrosFile, regKeyBase,
  663. nextAvailableSubKeyName);
  664. }
  665. }
  666. // Do another if check - 'count' may have changed inside the above if:
  667. //
  668. if (0 == count)
  669. {
  670. WriteVSMacrosFileRegistryEntry(nextAvailableSubKeyName, macrosFile,
  671. regKeyBase);
  672. }
  673. }
  674. }
  675. bool cmGlobalVisualStudioGenerator::TargetIsFortranOnly(cmTarget& target)
  676. {
  677. // check to see if this is a fortran build
  678. std::set<cmStdString> languages;
  679. target.GetLanguages(languages);
  680. if(languages.size() == 1)
  681. {
  682. if(*languages.begin() == "Fortran")
  683. {
  684. return true;
  685. }
  686. }
  687. return false;
  688. }
  689. //----------------------------------------------------------------------------
  690. bool
  691. cmGlobalVisualStudioGenerator::TargetCompare
  692. ::operator()(cmTarget const* l, cmTarget const* r) const
  693. {
  694. // Make sure ALL_BUILD is first so it is the default active project.
  695. if(strcmp(r->GetName(), "ALL_BUILD") == 0)
  696. {
  697. return false;
  698. }
  699. if(strcmp(l->GetName(), "ALL_BUILD") == 0)
  700. {
  701. return true;
  702. }
  703. return strcmp(l->GetName(), r->GetName()) < 0;
  704. }
  705. //----------------------------------------------------------------------------
  706. cmGlobalVisualStudioGenerator::OrderedTargetDependSet
  707. ::OrderedTargetDependSet(cmGlobalGenerator::TargetDependSet const& targets)
  708. {
  709. for(cmGlobalGenerator::TargetDependSet::const_iterator ti =
  710. targets.begin(); ti != targets.end(); ++ti)
  711. {
  712. this->insert(*ti);
  713. }
  714. }