cmGlobalVisualStudioGenerator.cxx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmGlobalVisualStudioGenerator.h"
  14. #include "cmCallVisualStudioMacro.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmTarget.h"
  18. //----------------------------------------------------------------------------
  19. cmGlobalVisualStudioGenerator::cmGlobalVisualStudioGenerator()
  20. {
  21. }
  22. //----------------------------------------------------------------------------
  23. cmGlobalVisualStudioGenerator::~cmGlobalVisualStudioGenerator()
  24. {
  25. }
  26. //----------------------------------------------------------------------------
  27. void cmGlobalVisualStudioGenerator::Generate()
  28. {
  29. // Add a special target that depends on ALL projects for easy build
  30. // of one configuration only.
  31. const char* no_working_dir = 0;
  32. std::vector<std::string> no_depends;
  33. cmCustomCommandLines no_commands;
  34. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  35. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  36. {
  37. std::vector<cmLocalGenerator*>& gen = it->second;
  38. // add the ALL_BUILD to the first local generator of each project
  39. if(gen.size())
  40. {
  41. // Use no actual command lines so that the target itself is not
  42. // considered always out of date.
  43. gen[0]->GetMakefile()->
  44. AddUtilityCommand("ALL_BUILD", true, no_working_dir,
  45. no_depends, no_commands, false,
  46. "Build all projects");
  47. }
  48. }
  49. // Fix utility dependencies to avoid linking to libraries.
  50. this->FixUtilityDepends();
  51. // Configure CMake Visual Studio macros, for this user on this version
  52. // of Visual Studio.
  53. this->ConfigureCMakeVisualStudioMacros();
  54. // Run all the local generators.
  55. this->cmGlobalGenerator::Generate();
  56. }
  57. //----------------------------------------------------------------------------
  58. void RegisterVisualStudioMacros(const std::string& macrosFile);
  59. //----------------------------------------------------------------------------
  60. #define CMAKE_VSMACROS_FILENAME \
  61. "CMakeVSMacros1.vsmacros"
  62. #define CMAKE_VSMACROS_RELOAD_MACRONAME \
  63. "Macros.CMakeVSMacros1.Macros.ReloadProjects"
  64. //----------------------------------------------------------------------------
  65. void cmGlobalVisualStudioGenerator::ConfigureCMakeVisualStudioMacros()
  66. {
  67. cmMakefile* mf = this->LocalGenerators[0]->GetMakefile();
  68. std::string dir = this->GetUserMacrosDirectory();
  69. if (mf != 0 && dir != "")
  70. {
  71. std::string src = mf->GetRequiredDefinition("CMAKE_ROOT");
  72. src += "/Templates/" CMAKE_VSMACROS_FILENAME;
  73. std::string dst = dir + "/CMakeMacros/" CMAKE_VSMACROS_FILENAME;
  74. // Only copy if dst does not already exist. Write this file initially,
  75. // but never overwrite local mods.
  76. if (!cmSystemTools::FileExists(dst.c_str()))
  77. {
  78. if (!cmSystemTools::CopyFileAlways(src.c_str(), dst.c_str()))
  79. {
  80. std::ostringstream oss;
  81. oss << "Could not copy from: " << src << std::endl;
  82. oss << " to: " << dst << std::endl;
  83. cmSystemTools::Message(oss.str().c_str(), "Warning");
  84. }
  85. }
  86. RegisterVisualStudioMacros(dst);
  87. }
  88. }
  89. //----------------------------------------------------------------------------
  90. void cmGlobalVisualStudioGenerator::CallVisualStudioReloadMacro()
  91. {
  92. // If any solution or project files changed during the generation,
  93. // tell Visual Studio to reload them...
  94. cmMakefile* mf = this->LocalGenerators[0]->GetMakefile();
  95. std::string dir = this->GetUserMacrosDirectory();
  96. if (mf != 0 && dir != "")
  97. {
  98. std::vector<std::string> filenames;
  99. this->GetFilesReplacedDuringGenerate(filenames);
  100. if (filenames.size() > 0)
  101. {
  102. // Convert vector to semi-colon delimited string of filenames:
  103. std::string projects;
  104. std::vector<std::string>::iterator it = filenames.begin();
  105. if (it != filenames.end())
  106. {
  107. projects = *it;
  108. ++it;
  109. }
  110. for (; it != filenames.end(); ++it)
  111. {
  112. projects += ";";
  113. projects += *it;
  114. }
  115. std::string topLevelSlnName = mf->GetStartOutputDirectory();
  116. topLevelSlnName += "/";
  117. topLevelSlnName += mf->GetProjectName();
  118. topLevelSlnName += ".sln";
  119. cmCallVisualStudioMacro::CallMacro(topLevelSlnName,
  120. CMAKE_VSMACROS_RELOAD_MACRONAME, projects);
  121. }
  122. }
  123. }
  124. //----------------------------------------------------------------------------
  125. std::string cmGlobalVisualStudioGenerator::GetUserMacrosDirectory()
  126. {
  127. return "";
  128. }
  129. //----------------------------------------------------------------------------
  130. void cmGlobalVisualStudioGenerator::FixUtilityDepends()
  131. {
  132. // For VS versions before 8:
  133. //
  134. // When a target that links contains a project-level dependency on a
  135. // library target that library is automatically linked. In order to
  136. // allow utility-style project-level dependencies that do not
  137. // actually link we need to automatically insert an intermediate
  138. // custom target.
  139. //
  140. // Here we edit the utility dependencies of a target to add the
  141. // intermediate custom target when necessary.
  142. for(unsigned i = 0; i < this->LocalGenerators.size(); ++i)
  143. {
  144. cmTargets* targets =
  145. &(this->LocalGenerators[i]->GetMakefile()->GetTargets());
  146. for(cmTargets::iterator tarIt = targets->begin();
  147. tarIt != targets->end(); ++tarIt)
  148. {
  149. this->FixUtilityDependsForTarget(tarIt->second);
  150. }
  151. }
  152. }
  153. //----------------------------------------------------------------------------
  154. void
  155. cmGlobalVisualStudioGenerator::FixUtilityDependsForTarget(cmTarget& target)
  156. {
  157. // Only targets that link need to be fixed.
  158. if(target.GetType() != cmTarget::STATIC_LIBRARY &&
  159. target.GetType() != cmTarget::SHARED_LIBRARY &&
  160. target.GetType() != cmTarget::MODULE_LIBRARY &&
  161. target.GetType() != cmTarget::EXECUTABLE)
  162. {
  163. return;
  164. }
  165. // Look at each utility dependency.
  166. for(std::set<cmStdString>::const_iterator ui =
  167. target.GetUtilities().begin();
  168. ui != target.GetUtilities().end(); ++ui)
  169. {
  170. if(cmTarget* depTarget = this->FindTarget(0, ui->c_str(), false))
  171. {
  172. if(depTarget->GetType() == cmTarget::STATIC_LIBRARY ||
  173. depTarget->GetType() == cmTarget::SHARED_LIBRARY ||
  174. depTarget->GetType() == cmTarget::MODULE_LIBRARY)
  175. {
  176. // This utility dependency will cause an attempt to link. If
  177. // the depender does not already link the dependee we need an
  178. // intermediate target.
  179. if(!this->CheckTargetLinks(target, ui->c_str()))
  180. {
  181. this->CreateUtilityDependTarget(*depTarget);
  182. }
  183. }
  184. }
  185. }
  186. }
  187. //----------------------------------------------------------------------------
  188. void
  189. cmGlobalVisualStudioGenerator::CreateUtilityDependTarget(cmTarget& target)
  190. {
  191. // This target is a library on which a utility dependency exists.
  192. // We need to create an intermediate custom target to hook up the
  193. // dependency without causing a link.
  194. const char* altName = target.GetProperty("ALTERNATIVE_DEPENDENCY_NAME");
  195. if(!altName)
  196. {
  197. // Create the intermediate utility target.
  198. std::string altNameStr = target.GetName();
  199. altNameStr += "_UTILITY";
  200. const std::vector<std::string> no_depends;
  201. cmCustomCommandLines no_commands;
  202. const char* no_working_dir = 0;
  203. const char* no_comment = 0;
  204. target.GetMakefile()->AddUtilityCommand(altNameStr.c_str(), true,
  205. no_working_dir, no_depends,
  206. no_commands, false, no_comment);
  207. target.SetProperty("ALTERNATIVE_DEPENDENCY_NAME", altNameStr.c_str());
  208. // Most targets have a GUID created in ConfigureFinalPass. Since
  209. // that has already been called, create one for this target now.
  210. this->CreateGUID(altNameStr.c_str());
  211. // The intermediate target should depend on the original target.
  212. if(cmTarget* alt = this->FindTarget(0, altNameStr.c_str(), false))
  213. {
  214. alt->AddUtility(target.GetName());
  215. }
  216. }
  217. }
  218. //----------------------------------------------------------------------------
  219. bool cmGlobalVisualStudioGenerator::CheckTargetLinks(cmTarget& target,
  220. const char* name)
  221. {
  222. // Return whether the given target links to a target with the given name.
  223. if(target.GetType() == cmTarget::STATIC_LIBRARY)
  224. {
  225. // Static libraries never link to anything.
  226. return false;
  227. }
  228. cmTarget::LinkLibraryVectorType const& libs = target.GetLinkLibraries();
  229. for(cmTarget::LinkLibraryVectorType::const_iterator i = libs.begin();
  230. i != libs.end(); ++i)
  231. {
  232. if(i->first == name)
  233. {
  234. return true;
  235. }
  236. }
  237. return false;
  238. }
  239. //----------------------------------------------------------------------------
  240. const char*
  241. cmGlobalVisualStudioGenerator::GetUtilityForTarget(cmTarget& target,
  242. const char* name)
  243. {
  244. // Handle the external MS project special case.
  245. if(strncmp(name, "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  246. {
  247. // Note from Ken:
  248. // kind of weird removing the first 27 letters. my
  249. // recommendatsions: use cmCustomCommand::GetCommand() to get the
  250. // project name or get rid of the target name starting with
  251. // "INCLUDE_EXTERNAL_MSPROJECT_" and use another indicator/flag
  252. // somewhere. These external project names shouldn't conflict
  253. // with cmake target names anyways.
  254. return name+27;
  255. }
  256. // Possibly depend on an intermediate utility target to avoid
  257. // linking.
  258. if(target.GetType() == cmTarget::STATIC_LIBRARY ||
  259. target.GetType() == cmTarget::SHARED_LIBRARY ||
  260. target.GetType() == cmTarget::MODULE_LIBRARY ||
  261. target.GetType() == cmTarget::EXECUTABLE)
  262. {
  263. // The depender is a target that links. Lookup the dependee to
  264. // see if it provides an alternative dependency name.
  265. if(cmTarget* depTarget = this->FindTarget(0, name, false))
  266. {
  267. // Check for an alternative name created by FixUtilityDepends.
  268. if(const char* altName =
  269. depTarget->GetProperty("ALTERNATIVE_DEPENDENCY_NAME"))
  270. {
  271. // The alternative name is needed only if the depender does
  272. // not really link to the dependee.
  273. if(!this->CheckTargetLinks(target, name))
  274. {
  275. return altName;
  276. }
  277. }
  278. }
  279. }
  280. // No special case. Just use the original dependency name.
  281. return name;
  282. }
  283. //----------------------------------------------------------------------------
  284. #include <windows.h>
  285. //----------------------------------------------------------------------------
  286. bool IsVisualStudioMacrosFileRegistered(const std::string& macrosFile,
  287. std::string& nextAvailableSubKeyName)
  288. {
  289. bool macrosRegistered = false;
  290. std::string s1;
  291. std::string s2;
  292. // Make lowercase local copies, convert to Unix slashes, and
  293. // see if the resulting strings are the same:
  294. s1 = cmSystemTools::LowerCase(macrosFile);
  295. cmSystemTools::ConvertToUnixSlashes(s1);
  296. std::string keyname;
  297. HKEY hkey = NULL;
  298. LONG result = ERROR_SUCCESS;
  299. DWORD index = 0;
  300. keyname = "Software\\Microsoft\\VisualStudio\\8.0\\vsmacros\\OtherProjects7";
  301. hkey = NULL;
  302. result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(), 0, KEY_READ, &hkey);
  303. if (ERROR_SUCCESS == result)
  304. {
  305. // Iterate the subkeys and look for the values of interest in each subkey:
  306. CHAR subkeyname[256];
  307. DWORD cch_subkeyname = sizeof(subkeyname)/sizeof(subkeyname[0]);
  308. CHAR keyclass[256];
  309. DWORD cch_keyclass = sizeof(keyclass)/sizeof(keyclass[0]);
  310. FILETIME lastWriteTime;
  311. lastWriteTime.dwHighDateTime = 0;
  312. lastWriteTime.dwLowDateTime = 0;
  313. while (ERROR_SUCCESS == RegEnumKeyEx(hkey, index, subkeyname, &cch_subkeyname,
  314. 0, keyclass, &cch_keyclass, &lastWriteTime))
  315. {
  316. // Open the subkey and query the values of interest:
  317. HKEY hsubkey = NULL;
  318. result = RegOpenKeyEx(hkey, subkeyname, 0, KEY_READ, &hsubkey);
  319. if (ERROR_SUCCESS == result)
  320. {
  321. DWORD valueType = REG_SZ;
  322. CHAR data1[256];
  323. DWORD cch_data1 = sizeof(data1)/sizeof(data1[0]);
  324. RegQueryValueEx(hsubkey, "Path", 0, &valueType, (LPBYTE) &data1[0], &cch_data1);
  325. DWORD data2 = 0;
  326. DWORD cch_data2 = sizeof(data2);
  327. RegQueryValueEx(hsubkey, "Security", 0, &valueType, (LPBYTE) &data2, &cch_data2);
  328. DWORD data3 = 0;
  329. DWORD cch_data3 = sizeof(data3);
  330. RegQueryValueEx(hsubkey, "StorageFormat", 0, &valueType, (LPBYTE) &data3, &cch_data3);
  331. s2 = cmSystemTools::LowerCase(data1);
  332. cmSystemTools::ConvertToUnixSlashes(s2);
  333. if (s2 == s1)
  334. {
  335. macrosRegistered = true;
  336. }
  337. std::string fullname(data1);
  338. std::string filename;
  339. std::string filepath;
  340. std::string filepathname;
  341. std::string filepathpath;
  342. if (cmSystemTools::FileExists(fullname.c_str()))
  343. {
  344. filename = cmSystemTools::GetFilenameName(fullname);
  345. filepath = cmSystemTools::GetFilenamePath(fullname);
  346. filepathname = cmSystemTools::GetFilenameName(filepath);
  347. filepathpath = cmSystemTools::GetFilenamePath(filepath);
  348. }
  349. //std::cout << keyname << "\\" << subkeyname << ":" << std::endl;
  350. //std::cout << " Path: " << data1 << std::endl;
  351. //std::cout << " Security: " << data2 << std::endl;
  352. //std::cout << " StorageFormat: " << data3 << std::endl;
  353. //std::cout << " filename: " << filename << std::endl;
  354. //std::cout << " filepath: " << filepath << std::endl;
  355. //std::cout << " filepathname: " << filepathname << std::endl;
  356. //std::cout << " filepathpath: " << filepathpath << std::endl;
  357. //std::cout << std::endl;
  358. RegCloseKey(hsubkey);
  359. }
  360. else
  361. {
  362. std::cout << "error opening subkey: " << subkeyname << std::endl;
  363. std::cout << std::endl;
  364. }
  365. ++index;
  366. cch_subkeyname = sizeof(subkeyname)/sizeof(subkeyname[0]);
  367. cch_keyclass = sizeof(keyclass)/sizeof(keyclass[0]);
  368. lastWriteTime.dwHighDateTime = 0;
  369. lastWriteTime.dwLowDateTime = 0;
  370. }
  371. RegCloseKey(hkey);
  372. }
  373. else
  374. {
  375. std::cout << "error opening key: " << keyname << std::endl;
  376. std::cout << std::endl;
  377. }
  378. // Pass back next available sub key name, assuming sub keys always
  379. // follow the expected naming scheme. Expected naming scheme is that
  380. // the subkeys of OtherProjects7 is 0 to n-1, so it's ok to use "n"
  381. // as the name of the next subkey.
  382. std::ostringstream ossNext;
  383. ossNext << index;
  384. nextAvailableSubKeyName = ossNext.str();
  385. keyname = "Software\\Microsoft\\VisualStudio\\8.0\\vsmacros\\RecordingProject7";
  386. hkey = NULL;
  387. result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(), 0, KEY_READ, &hkey);
  388. if (ERROR_SUCCESS == result)
  389. {
  390. DWORD valueType = REG_SZ;
  391. CHAR data1[256];
  392. DWORD cch_data1 = sizeof(data1)/sizeof(data1[0]);
  393. RegQueryValueEx(hkey, "Path", 0, &valueType, (LPBYTE) &data1[0], &cch_data1);
  394. DWORD data2 = 0;
  395. DWORD cch_data2 = sizeof(data2);
  396. RegQueryValueEx(hkey, "Security", 0, &valueType, (LPBYTE) &data2, &cch_data2);
  397. DWORD data3 = 0;
  398. DWORD cch_data3 = sizeof(data3);
  399. RegQueryValueEx(hkey, "StorageFormat", 0, &valueType, (LPBYTE) &data3, &cch_data3);
  400. s2 = cmSystemTools::LowerCase(data1);
  401. cmSystemTools::ConvertToUnixSlashes(s2);
  402. if (s2 == s1)
  403. {
  404. macrosRegistered = true;
  405. }
  406. //std::cout << keyname << ":" << std::endl;
  407. //std::cout << " Path: " << data1 << std::endl;
  408. //std::cout << " Security: " << data2 << std::endl;
  409. //std::cout << " StorageFormat: " << data3 << std::endl;
  410. //std::cout << std::endl;
  411. RegCloseKey(hkey);
  412. }
  413. else
  414. {
  415. std::cout << "error opening key: " << keyname << std::endl;
  416. std::cout << std::endl;
  417. }
  418. return macrosRegistered;
  419. }
  420. //----------------------------------------------------------------------------
  421. void WriteVSMacrosFileRegistryEntry(
  422. const std::string& nextAvailableSubKeyName,
  423. const std::string& macrosFile)
  424. {
  425. std::string keyname = "Software\\Microsoft\\VisualStudio\\8.0\\vsmacros\\OtherProjects7";
  426. HKEY hkey = NULL;
  427. LONG result = RegOpenKeyEx(HKEY_CURRENT_USER, keyname.c_str(), 0,
  428. KEY_READ|KEY_WRITE, &hkey);
  429. if (ERROR_SUCCESS == result)
  430. {
  431. // Create the subkey and set the values of interest:
  432. HKEY hsubkey = NULL;
  433. result = RegCreateKeyEx(hkey, nextAvailableSubKeyName.c_str(), 0, "", 0,
  434. KEY_READ|KEY_WRITE, 0, &hsubkey, 0);
  435. if (ERROR_SUCCESS == result)
  436. {
  437. DWORD dw = 0;
  438. std::string s(macrosFile);
  439. cmSystemTools::ReplaceString(s, "/", "\\");
  440. result = RegSetValueEx(hsubkey, "Path", 0, REG_SZ, (LPBYTE) s.c_str(),
  441. strlen(s.c_str()) + 1);
  442. if (ERROR_SUCCESS != result)
  443. {
  444. std::cout << "error result 1: " << result << std::endl;
  445. std::cout << std::endl;
  446. }
  447. // Security value is always "1" for sample macros files (seems to be "2"
  448. // if you put the file somewhere outside the standard VSMacros folder)
  449. dw = 1;
  450. result = RegSetValueEx(hsubkey, "Security", 0, REG_DWORD, (LPBYTE) &dw, sizeof(DWORD));
  451. if (ERROR_SUCCESS != result)
  452. {
  453. std::cout << "error result 2: " << result << std::endl;
  454. std::cout << std::endl;
  455. }
  456. // StorageFormat value is always "0" for sample macros files
  457. dw = 0;
  458. result = RegSetValueEx(hsubkey, "StorageFormat", 0, REG_DWORD, (LPBYTE) &dw, sizeof(DWORD));
  459. if (ERROR_SUCCESS != result)
  460. {
  461. std::cout << "error result 3: " << result << std::endl;
  462. std::cout << std::endl;
  463. }
  464. RegCloseKey(hsubkey);
  465. }
  466. else
  467. {
  468. std::cout << "error creating subkey: " << nextAvailableSubKeyName << std::endl;
  469. std::cout << std::endl;
  470. }
  471. RegCloseKey(hkey);
  472. }
  473. else
  474. {
  475. std::cout << "error opening key: " << keyname << std::endl;
  476. std::cout << std::endl;
  477. }
  478. }
  479. //----------------------------------------------------------------------------
  480. void RegisterVisualStudioMacros(const std::string& macrosFile)
  481. {
  482. bool macrosRegistered;
  483. std::string nextAvailableSubKeyName;
  484. macrosRegistered = IsVisualStudioMacrosFileRegistered(macrosFile,
  485. nextAvailableSubKeyName);
  486. if (!macrosRegistered)
  487. {
  488. int count = cmCallVisualStudioMacro::
  489. GetNumberOfRunningVisualStudioInstances("ALL");
  490. // Only register the macros file if there are *no* instances of Visual
  491. // Studio running. If we register it while one is running, first, it has
  492. // no effect on the running instance; second, and worse, Visual Studio
  493. // removes our newly added registration entry when it quits. Instead,
  494. // emit a warning instructing the user to re-run the CMake configure step
  495. // after exiting all running Visual Studio instances...
  496. //
  497. if (0 == count)
  498. {
  499. WriteVSMacrosFileRegistryEntry(nextAvailableSubKeyName, macrosFile);
  500. }
  501. else
  502. {
  503. std::ostringstream oss;
  504. oss << "Could not register Visual Studio macros file '" << macrosFile
  505. << "' with instances of Visual Studio running. Please exit all"
  506. << " running instances of Visual Studio and rerun this CMake"
  507. << " configure to register CMake's Visual Studio macros file."
  508. << std::endl;
  509. cmSystemTools::Message(oss.str().c_str(), "Warning");
  510. }
  511. }
  512. }