cmGlobalVisualStudioGenerator.cxx 30 KB

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