cmGlobalVisualStudioGenerator.cxx 33 KB

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