cmGlobalVisualStudioGenerator.cxx 33 KB

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