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