cmGlobalVisualStudioGenerator.cxx 30 KB

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