cmGlobalVisualStudioGenerator.cxx 24 KB

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