1
0

cmGlobalVisualStudioGenerator.cxx 32 KB

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