cmGlobalVisualStudioGenerator.cxx 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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. const std::vector<cmGeneratorTarget*>& targets =
  73. (*i)->GetGeneratorTargets();
  74. for (std::vector<cmGeneratorTarget*>::const_iterator t =
  75. targets.begin();
  76. t != targets.end(); ++t) {
  77. cmGeneratorTarget* tgt = *t;
  78. if (tgt->GetType() == cmStateEnums::GLOBAL_TARGET ||
  79. 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() == cmStateEnums::INTERFACE_LIBRARY) {
  243. return;
  244. }
  245. if (linked.insert(target).second &&
  246. target->GetType() == cmStateEnums::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. const std::vector<cmGeneratorTarget*>& targets =
  269. (*i)->GetGeneratorTargets();
  270. for (std::vector<cmGeneratorTarget*>::const_iterator ti =
  271. targets.begin();
  272. ti != targets.end(); ++ti) {
  273. this->ComputeVSTargetDepends(*ti);
  274. }
  275. }
  276. }
  277. return true;
  278. }
  279. static bool VSLinkable(cmGeneratorTarget const* t)
  280. {
  281. return t->IsLinkable() || t->GetType() == cmStateEnums::OBJECT_LIBRARY;
  282. }
  283. void cmGlobalVisualStudioGenerator::ComputeVSTargetDepends(
  284. cmGeneratorTarget* target)
  285. {
  286. if (this->VSTargetDepends.find(target) != this->VSTargetDepends.end()) {
  287. return;
  288. }
  289. VSDependSet& vsTargetDepend = this->VSTargetDepends[target];
  290. // VS <= 7.1 has two behaviors that affect solution dependencies.
  291. //
  292. // (1) Solution-level dependencies between a linkable target and a
  293. // library cause that library to be linked. We use an intermedite
  294. // empty utility target to express the dependency. (VS 8 and above
  295. // provide a project file "LinkLibraryDependencies" setting to
  296. // choose whether to activate this behavior. We disable it except
  297. // when linking external project files.)
  298. //
  299. // (2) We cannot let static libraries depend directly on targets to
  300. // which they "link" because the librarian tool will copy the
  301. // targets into the static library. While the work-around for
  302. // behavior (1) would also avoid this, it would create a large
  303. // number of extra utility targets for little gain. Instead, use
  304. // the above work-around only for dependencies explicitly added by
  305. // the add_dependencies() command. Approximate link dependencies by
  306. // leaving them out for the static library itself but following them
  307. // transitively for other targets.
  308. bool allowLinkable = (target->GetType() != cmStateEnums::STATIC_LIBRARY &&
  309. target->GetType() != cmStateEnums::SHARED_LIBRARY &&
  310. target->GetType() != cmStateEnums::MODULE_LIBRARY &&
  311. target->GetType() != cmStateEnums::EXECUTABLE);
  312. TargetDependSet const& depends = this->GetTargetDirectDepends(target);
  313. // Collect implicit link dependencies (target_link_libraries).
  314. // Static libraries cannot depend on their link implementation
  315. // due to behavior (2), but they do not really need to.
  316. std::set<cmGeneratorTarget const*> linkDepends;
  317. if (target->GetType() != cmStateEnums::STATIC_LIBRARY) {
  318. for (TargetDependSet::const_iterator di = depends.begin();
  319. di != depends.end(); ++di) {
  320. cmTargetDepend dep = *di;
  321. if (dep.IsLink()) {
  322. this->FollowLinkDepends(*di, linkDepends);
  323. }
  324. }
  325. }
  326. // Collect explicit util dependencies (add_dependencies).
  327. std::set<cmGeneratorTarget const*> utilDepends;
  328. for (TargetDependSet::const_iterator di = depends.begin();
  329. di != depends.end(); ++di) {
  330. cmTargetDepend dep = *di;
  331. if (dep.IsUtil()) {
  332. this->FollowLinkDepends(*di, utilDepends);
  333. }
  334. }
  335. // Collect all targets linked by this target so we can avoid
  336. // intermediate targets below.
  337. TargetSet linked;
  338. if (target->GetType() != cmStateEnums::STATIC_LIBRARY) {
  339. linked = this->GetTargetLinkClosure(target);
  340. }
  341. // Emit link dependencies.
  342. for (std::set<cmGeneratorTarget const*>::iterator di = linkDepends.begin();
  343. di != linkDepends.end(); ++di) {
  344. cmGeneratorTarget const* dep = *di;
  345. vsTargetDepend.insert(dep->GetName());
  346. }
  347. // Emit util dependencies. Possibly use intermediate targets.
  348. for (std::set<cmGeneratorTarget const*>::iterator di = utilDepends.begin();
  349. di != utilDepends.end(); ++di) {
  350. cmGeneratorTarget const* dgt = *di;
  351. if (allowLinkable || !VSLinkable(dgt) || linked.count(dgt)) {
  352. // Direct dependency allowed.
  353. vsTargetDepend.insert(dgt->GetName());
  354. } else {
  355. // Direct dependency on linkable target not allowed.
  356. // Use an intermediate utility target.
  357. vsTargetDepend.insert(this->GetUtilityDepend(dgt));
  358. }
  359. }
  360. }
  361. bool cmGlobalVisualStudioGenerator::FindMakeProgram(cmMakefile* mf)
  362. {
  363. // Visual Studio generators know how to lookup their build tool
  364. // directly instead of needing a helper module to do it, so we
  365. // do not actually need to put CMAKE_MAKE_PROGRAM into the cache.
  366. if (cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM"))) {
  367. mf->AddDefinition("CMAKE_MAKE_PROGRAM", this->GetVSMakeProgram().c_str());
  368. }
  369. return true;
  370. }
  371. std::string cmGlobalVisualStudioGenerator::GetUtilityDepend(
  372. cmGeneratorTarget const* target)
  373. {
  374. UtilityDependsMap::iterator i = this->UtilityDepends.find(target);
  375. if (i == this->UtilityDepends.end()) {
  376. std::string name = this->WriteUtilityDepend(target);
  377. UtilityDependsMap::value_type entry(target, name);
  378. i = this->UtilityDepends.insert(entry).first;
  379. }
  380. return i->second;
  381. }
  382. std::string cmGlobalVisualStudioGenerator::GetStartupProjectName(
  383. cmLocalGenerator const* root) const
  384. {
  385. const char* n = root->GetMakefile()->GetProperty("VS_STARTUP_PROJECT");
  386. if (n && *n) {
  387. std::string startup = n;
  388. if (this->FindTarget(startup)) {
  389. return startup;
  390. } else {
  391. root->GetMakefile()->IssueMessage(
  392. cmake::AUTHOR_WARNING,
  393. "Directory property VS_STARTUP_PROJECT specifies target "
  394. "'" +
  395. startup + "' that does not exist. Ignoring.");
  396. }
  397. }
  398. // default, if not specified
  399. return this->GetAllTargetName();
  400. }
  401. bool IsVisualStudioMacrosFileRegistered(const std::string& macrosFile,
  402. const std::string& regKeyBase,
  403. std::string& nextAvailableSubKeyName)
  404. {
  405. bool macrosRegistered = false;
  406. std::string s1;
  407. std::string s2;
  408. // Make lowercase local copies, convert to Unix slashes, and
  409. // see if the resulting strings are the same:
  410. s1 = cmSystemTools::LowerCase(macrosFile);
  411. cmSystemTools::ConvertToUnixSlashes(s1);
  412. std::string keyname;
  413. HKEY hkey = NULL;
  414. LONG result = ERROR_SUCCESS;
  415. DWORD index = 0;
  416. keyname = regKeyBase + "\\OtherProjects7";
  417. hkey = NULL;
  418. result =
  419. RegOpenKeyExW(HKEY_CURRENT_USER, cmsys::Encoding::ToWide(keyname).c_str(),
  420. 0, KEY_READ, &hkey);
  421. if (ERROR_SUCCESS == result) {
  422. // Iterate the subkeys and look for the values of interest in each subkey:
  423. wchar_t subkeyname[256];
  424. DWORD cch_subkeyname = sizeof(subkeyname) * sizeof(subkeyname[0]);
  425. wchar_t keyclass[256];
  426. DWORD cch_keyclass = sizeof(keyclass) * sizeof(keyclass[0]);
  427. FILETIME lastWriteTime;
  428. lastWriteTime.dwHighDateTime = 0;
  429. lastWriteTime.dwLowDateTime = 0;
  430. while (ERROR_SUCCESS == RegEnumKeyExW(hkey, index, subkeyname,
  431. &cch_subkeyname, 0, keyclass,
  432. &cch_keyclass, &lastWriteTime)) {
  433. // Open the subkey and query the values of interest:
  434. HKEY hsubkey = NULL;
  435. result = RegOpenKeyExW(hkey, subkeyname, 0, KEY_READ, &hsubkey);
  436. if (ERROR_SUCCESS == result) {
  437. DWORD valueType = REG_SZ;
  438. wchar_t data1[256];
  439. DWORD cch_data1 = sizeof(data1) * sizeof(data1[0]);
  440. RegQueryValueExW(hsubkey, L"Path", 0, &valueType, (LPBYTE)&data1[0],
  441. &cch_data1);
  442. DWORD data2 = 0;
  443. DWORD cch_data2 = sizeof(data2);
  444. RegQueryValueExW(hsubkey, L"Security", 0, &valueType, (LPBYTE)&data2,
  445. &cch_data2);
  446. DWORD data3 = 0;
  447. DWORD cch_data3 = sizeof(data3);
  448. RegQueryValueExW(hsubkey, L"StorageFormat", 0, &valueType,
  449. (LPBYTE)&data3, &cch_data3);
  450. s2 = cmSystemTools::LowerCase(cmsys::Encoding::ToNarrow(data1));
  451. cmSystemTools::ConvertToUnixSlashes(s2);
  452. if (s2 == s1) {
  453. macrosRegistered = true;
  454. }
  455. std::string fullname = cmsys::Encoding::ToNarrow(data1);
  456. std::string filename;
  457. std::string filepath;
  458. std::string filepathname;
  459. std::string filepathpath;
  460. if (cmSystemTools::FileExists(fullname.c_str())) {
  461. filename = cmSystemTools::GetFilenameName(fullname);
  462. filepath = cmSystemTools::GetFilenamePath(fullname);
  463. filepathname = cmSystemTools::GetFilenameName(filepath);
  464. filepathpath = cmSystemTools::GetFilenamePath(filepath);
  465. }
  466. // std::cout << keyname << "\\" << subkeyname << ":" << std::endl;
  467. // std::cout << " Path: " << data1 << std::endl;
  468. // std::cout << " Security: " << data2 << std::endl;
  469. // std::cout << " StorageFormat: " << data3 << std::endl;
  470. // std::cout << " filename: " << filename << std::endl;
  471. // std::cout << " filepath: " << filepath << std::endl;
  472. // std::cout << " filepathname: " << filepathname << std::endl;
  473. // std::cout << " filepathpath: " << filepathpath << std::endl;
  474. // std::cout << std::endl;
  475. RegCloseKey(hsubkey);
  476. } else {
  477. std::cout << "error opening subkey: " << subkeyname << std::endl;
  478. std::cout << std::endl;
  479. }
  480. ++index;
  481. cch_subkeyname = sizeof(subkeyname) * sizeof(subkeyname[0]);
  482. cch_keyclass = sizeof(keyclass) * sizeof(keyclass[0]);
  483. lastWriteTime.dwHighDateTime = 0;
  484. lastWriteTime.dwLowDateTime = 0;
  485. }
  486. RegCloseKey(hkey);
  487. } else {
  488. std::cout << "error opening key: " << keyname << std::endl;
  489. std::cout << std::endl;
  490. }
  491. // Pass back next available sub key name, assuming sub keys always
  492. // follow the expected naming scheme. Expected naming scheme is that
  493. // the subkeys of OtherProjects7 is 0 to n-1, so it's ok to use "n"
  494. // as the name of the next subkey.
  495. std::ostringstream ossNext;
  496. ossNext << index;
  497. nextAvailableSubKeyName = ossNext.str();
  498. keyname = regKeyBase + "\\RecordingProject7";
  499. hkey = NULL;
  500. result =
  501. RegOpenKeyExW(HKEY_CURRENT_USER, cmsys::Encoding::ToWide(keyname).c_str(),
  502. 0, KEY_READ, &hkey);
  503. if (ERROR_SUCCESS == result) {
  504. DWORD valueType = REG_SZ;
  505. wchar_t data1[256];
  506. DWORD cch_data1 = sizeof(data1) * sizeof(data1[0]);
  507. RegQueryValueExW(hkey, L"Path", 0, &valueType, (LPBYTE)&data1[0],
  508. &cch_data1);
  509. DWORD data2 = 0;
  510. DWORD cch_data2 = sizeof(data2);
  511. RegQueryValueExW(hkey, L"Security", 0, &valueType, (LPBYTE)&data2,
  512. &cch_data2);
  513. DWORD data3 = 0;
  514. DWORD cch_data3 = sizeof(data3);
  515. RegQueryValueExW(hkey, L"StorageFormat", 0, &valueType, (LPBYTE)&data3,
  516. &cch_data3);
  517. s2 = cmSystemTools::LowerCase(cmsys::Encoding::ToNarrow(data1));
  518. cmSystemTools::ConvertToUnixSlashes(s2);
  519. if (s2 == s1) {
  520. macrosRegistered = true;
  521. }
  522. // std::cout << keyname << ":" << std::endl;
  523. // std::cout << " Path: " << data1 << std::endl;
  524. // std::cout << " Security: " << data2 << std::endl;
  525. // std::cout << " StorageFormat: " << data3 << std::endl;
  526. // std::cout << std::endl;
  527. RegCloseKey(hkey);
  528. } else {
  529. std::cout << "error opening key: " << keyname << std::endl;
  530. std::cout << std::endl;
  531. }
  532. return macrosRegistered;
  533. }
  534. void WriteVSMacrosFileRegistryEntry(const std::string& nextAvailableSubKeyName,
  535. const std::string& macrosFile,
  536. const std::string& regKeyBase)
  537. {
  538. std::string keyname = regKeyBase + "\\OtherProjects7";
  539. HKEY hkey = NULL;
  540. LONG result =
  541. RegOpenKeyExW(HKEY_CURRENT_USER, cmsys::Encoding::ToWide(keyname).c_str(),
  542. 0, KEY_READ | KEY_WRITE, &hkey);
  543. if (ERROR_SUCCESS == result) {
  544. // Create the subkey and set the values of interest:
  545. HKEY hsubkey = NULL;
  546. wchar_t lpClass[] = L"";
  547. result = RegCreateKeyExW(
  548. hkey, cmsys::Encoding::ToWide(nextAvailableSubKeyName).c_str(), 0,
  549. lpClass, 0, KEY_READ | KEY_WRITE, 0, &hsubkey, 0);
  550. if (ERROR_SUCCESS == result) {
  551. DWORD dw = 0;
  552. std::string s(macrosFile);
  553. std::replace(s.begin(), s.end(), '/', '\\');
  554. std::wstring ws = cmsys::Encoding::ToWide(s);
  555. result =
  556. RegSetValueExW(hsubkey, L"Path", 0, REG_SZ, (LPBYTE)ws.c_str(),
  557. static_cast<DWORD>(ws.size() + 1) * sizeof(wchar_t));
  558. if (ERROR_SUCCESS != result) {
  559. std::cout << "error result 1: " << result << std::endl;
  560. std::cout << std::endl;
  561. }
  562. // Security value is always "1" for sample macros files (seems to be "2"
  563. // if you put the file somewhere outside the standard VSMacros folder)
  564. dw = 1;
  565. result = RegSetValueExW(hsubkey, L"Security", 0, REG_DWORD, (LPBYTE)&dw,
  566. sizeof(DWORD));
  567. if (ERROR_SUCCESS != result) {
  568. std::cout << "error result 2: " << result << std::endl;
  569. std::cout << std::endl;
  570. }
  571. // StorageFormat value is always "0" for sample macros files
  572. dw = 0;
  573. result = RegSetValueExW(hsubkey, L"StorageFormat", 0, REG_DWORD,
  574. (LPBYTE)&dw, sizeof(DWORD));
  575. if (ERROR_SUCCESS != result) {
  576. std::cout << "error result 3: " << result << std::endl;
  577. std::cout << std::endl;
  578. }
  579. RegCloseKey(hsubkey);
  580. } else {
  581. std::cout << "error creating subkey: " << nextAvailableSubKeyName
  582. << std::endl;
  583. std::cout << std::endl;
  584. }
  585. RegCloseKey(hkey);
  586. } else {
  587. std::cout << "error opening key: " << keyname << std::endl;
  588. std::cout << std::endl;
  589. }
  590. }
  591. void RegisterVisualStudioMacros(const std::string& macrosFile,
  592. const std::string& regKeyBase)
  593. {
  594. bool macrosRegistered;
  595. std::string nextAvailableSubKeyName;
  596. macrosRegistered = IsVisualStudioMacrosFileRegistered(
  597. macrosFile, regKeyBase, nextAvailableSubKeyName);
  598. if (!macrosRegistered) {
  599. int count =
  600. cmCallVisualStudioMacro::GetNumberOfRunningVisualStudioInstances("ALL");
  601. // Only register the macros file if there are *no* instances of Visual
  602. // Studio running. If we register it while one is running, first, it has
  603. // no effect on the running instance; second, and worse, Visual Studio
  604. // removes our newly added registration entry when it quits. Instead,
  605. // emit a warning asking the user to exit all running Visual Studio
  606. // instances...
  607. //
  608. if (0 != count) {
  609. std::ostringstream oss;
  610. oss << "Could not register CMake's Visual Studio macros file '"
  611. << CMAKE_VSMACROS_FILENAME "' while Visual Studio is running."
  612. << " Please exit all running instances of Visual Studio before"
  613. << " continuing." << std::endl
  614. << std::endl
  615. << "CMake needs to register Visual Studio macros when its macros"
  616. << " file is updated or when it detects that its current macros file"
  617. << " is no longer registered with Visual Studio." << std::endl;
  618. cmSystemTools::Message(oss.str().c_str(), "Warning");
  619. // Count them again now that the warning is over. In the case of a GUI
  620. // warning, the user may have gone to close Visual Studio and then come
  621. // back to the CMake GUI and clicked ok on the above warning. If so,
  622. // then register the macros *now* if the count is *now* 0...
  623. //
  624. count = cmCallVisualStudioMacro::GetNumberOfRunningVisualStudioInstances(
  625. "ALL");
  626. // Also re-get the nextAvailableSubKeyName in case Visual Studio
  627. // wrote out new registered macros information as it was exiting:
  628. //
  629. if (0 == count) {
  630. IsVisualStudioMacrosFileRegistered(macrosFile, regKeyBase,
  631. nextAvailableSubKeyName);
  632. }
  633. }
  634. // Do another if check - 'count' may have changed inside the above if:
  635. //
  636. if (0 == count) {
  637. WriteVSMacrosFileRegistryEntry(nextAvailableSubKeyName, macrosFile,
  638. regKeyBase);
  639. }
  640. }
  641. }
  642. bool cmGlobalVisualStudioGenerator::TargetIsFortranOnly(
  643. cmGeneratorTarget const* gt)
  644. {
  645. // check to see if this is a fortran build
  646. std::set<std::string> languages;
  647. {
  648. // Issue diagnostic if the source files depend on the config.
  649. std::vector<cmSourceFile*> sources;
  650. if (!gt->GetConfigCommonSourceFiles(sources)) {
  651. return false;
  652. }
  653. }
  654. // If there's only one source language, Fortran has to be used
  655. // in order for the sources to compile.
  656. // Note: Via linker propagation, LINKER_LANGUAGE could become CXX in
  657. // this situation and mismatch from the actual language of the linker.
  658. gt->GetLanguages(languages, "");
  659. if (languages.size() == 1) {
  660. if (*languages.begin() == "Fortran") {
  661. return true;
  662. }
  663. }
  664. // In the case of mixed object files or sources mixed with objects,
  665. // decide the language based on the value of LINKER_LANGUAGE.
  666. // This will not make it possible to mix source files of different
  667. // languages, but object libraries will be linked together in the
  668. // same fashion as other generators do.
  669. if (gt->GetLinkerLanguage("") == "Fortran") {
  670. return true;
  671. }
  672. return false;
  673. }
  674. bool cmGlobalVisualStudioGenerator::TargetIsCSharpOnly(
  675. cmGeneratorTarget const* gt)
  676. {
  677. // check to see if this is a C# build
  678. std::set<std::string> languages;
  679. {
  680. // Issue diagnostic if the source files depend on the config.
  681. std::vector<cmSourceFile*> sources;
  682. if (!gt->GetConfigCommonSourceFiles(sources)) {
  683. return false;
  684. }
  685. // Only "real" targets are allowed to be C# targets.
  686. if (gt->Target->GetType() > cmStateEnums::OBJECT_LIBRARY) {
  687. return false;
  688. }
  689. }
  690. gt->GetLanguages(languages, "");
  691. if (languages.size() == 1) {
  692. if (*languages.begin() == "CSharp") {
  693. return true;
  694. }
  695. }
  696. return false;
  697. }
  698. bool cmGlobalVisualStudioGenerator::TargetCompare::operator()(
  699. cmGeneratorTarget const* l, cmGeneratorTarget const* r) const
  700. {
  701. // Make sure a given named target is ordered first,
  702. // e.g. to set ALL_BUILD as the default active project.
  703. // When the empty string is named this is a no-op.
  704. if (r->GetName() == this->First) {
  705. return false;
  706. }
  707. if (l->GetName() == this->First) {
  708. return true;
  709. }
  710. return l->GetName() < r->GetName();
  711. }
  712. cmGlobalVisualStudioGenerator::OrderedTargetDependSet::OrderedTargetDependSet(
  713. TargetDependSet const& targets, std::string const& first)
  714. : derived(TargetCompare(first))
  715. {
  716. this->insert(targets.begin(), targets.end());
  717. }
  718. cmGlobalVisualStudioGenerator::OrderedTargetDependSet::OrderedTargetDependSet(
  719. TargetSet const& targets, std::string const& first)
  720. : derived(TargetCompare(first))
  721. {
  722. for (TargetSet::const_iterator it = targets.begin(); it != targets.end();
  723. ++it) {
  724. this->insert(*it);
  725. }
  726. }
  727. std::string cmGlobalVisualStudioGenerator::ExpandCFGIntDir(
  728. const std::string& str, const std::string& config) const
  729. {
  730. std::string replace = GetCMakeCFGIntDir();
  731. std::string tmp = str;
  732. for (std::string::size_type i = tmp.find(replace); i != std::string::npos;
  733. i = tmp.find(replace, i)) {
  734. tmp.replace(i, replace.size(), config);
  735. i += config.size();
  736. }
  737. return tmp;
  738. }
  739. void cmGlobalVisualStudioGenerator::AddSymbolExportCommand(
  740. cmGeneratorTarget* gt, std::vector<cmCustomCommand>& commands,
  741. std::string const& configName)
  742. {
  743. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  744. gt->GetModuleDefinitionInfo(configName);
  745. if (!mdi || !mdi->DefFileGenerated) {
  746. return;
  747. }
  748. std::vector<std::string> outputs;
  749. outputs.push_back(mdi->DefFile);
  750. std::vector<std::string> empty;
  751. std::vector<cmSourceFile const*> objectSources;
  752. gt->GetObjectSources(objectSources, configName);
  753. std::map<cmSourceFile const*, std::string> mapping;
  754. for (std::vector<cmSourceFile const*>::const_iterator it =
  755. objectSources.begin();
  756. it != objectSources.end(); ++it) {
  757. mapping[*it];
  758. }
  759. gt->LocalGenerator->ComputeObjectFilenames(mapping, gt);
  760. std::string obj_dir = gt->ObjectDirectory;
  761. std::string cmakeCommand = cmSystemTools::GetCMakeCommand();
  762. cmSystemTools::ConvertToWindowsExtendedPath(cmakeCommand);
  763. cmCustomCommandLine cmdl;
  764. cmdl.push_back(cmakeCommand);
  765. cmdl.push_back("-E");
  766. cmdl.push_back("__create_def");
  767. cmdl.push_back(mdi->DefFile);
  768. std::string obj_dir_expanded = obj_dir;
  769. cmSystemTools::ReplaceString(obj_dir_expanded, this->GetCMakeCFGIntDir(),
  770. configName.c_str());
  771. cmSystemTools::MakeDirectory(obj_dir_expanded);
  772. std::string const objs_file = obj_dir_expanded + "/objects.txt";
  773. cmdl.push_back(objs_file);
  774. cmGeneratedFileStream fout(objs_file.c_str());
  775. if (!fout) {
  776. cmSystemTools::Error("could not open ", objs_file.c_str());
  777. return;
  778. }
  779. if (mdi->WindowsExportAllSymbols) {
  780. std::vector<std::string> objs;
  781. for (std::vector<cmSourceFile const*>::const_iterator it =
  782. objectSources.begin();
  783. it != objectSources.end(); ++it) {
  784. // Find the object file name corresponding to this source file.
  785. std::map<cmSourceFile const*, std::string>::const_iterator map_it =
  786. mapping.find(*it);
  787. // It must exist because we populated the mapping just above.
  788. assert(!map_it->second.empty());
  789. std::string objFile = obj_dir + map_it->second;
  790. objs.push_back(objFile);
  791. }
  792. std::vector<cmSourceFile const*> externalObjectSources;
  793. gt->GetExternalObjects(externalObjectSources, configName);
  794. for (std::vector<cmSourceFile const*>::const_iterator it =
  795. externalObjectSources.begin();
  796. it != externalObjectSources.end(); ++it) {
  797. objs.push_back((*it)->GetFullPath());
  798. }
  799. for (std::vector<std::string>::iterator it = objs.begin();
  800. it != objs.end(); ++it) {
  801. std::string objFile = *it;
  802. // replace $(ConfigurationName) in the object names
  803. cmSystemTools::ReplaceString(objFile, this->GetCMakeCFGIntDir(),
  804. configName.c_str());
  805. if (cmHasLiteralSuffix(objFile, ".obj")) {
  806. fout << objFile << "\n";
  807. }
  808. }
  809. }
  810. for (std::vector<cmSourceFile const*>::const_iterator i =
  811. mdi->Sources.begin();
  812. i != mdi->Sources.end(); ++i) {
  813. fout << (*i)->GetFullPath() << "\n";
  814. }
  815. cmCustomCommandLines commandLines;
  816. commandLines.push_back(cmdl);
  817. cmCustomCommand command(gt->Target->GetMakefile(), outputs, empty, empty,
  818. commandLines, "Auto build dll exports", ".");
  819. commands.push_back(command);
  820. }