cmGlobalVisualStudioGenerator.cxx 30 KB

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