cmGlobalVisualStudioGenerator.cxx 33 KB

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