cmGlobalVisualStudioGenerator.cxx 34 KB

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