cmGlobalVisualStudioGenerator.cxx 31 KB

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