cmGlobalVisualStudioGenerator.cxx 24 KB

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