cmGlobalVisualStudioGenerator.cxx 30 KB

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