cmGlobalVisualStudioGenerator.cxx 34 KB

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