cmGlobalVisualStudioGenerator.cxx 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. std::string projects = cmJoin(filenames, ";");
  288. cmCallVisualStudioMacro::CallMacro(
  289. topLevelSlnName, CMAKE_VSMACROS_RELOAD_MACRONAME, projects,
  290. this->GetCMakeInstance()->GetDebugOutput());
  291. }
  292. } else if (m == MacroStop) {
  293. cmCallVisualStudioMacro::CallMacro(
  294. topLevelSlnName, CMAKE_VSMACROS_STOP_MACRONAME, "",
  295. this->GetCMakeInstance()->GetDebugOutput());
  296. }
  297. }
  298. }
  299. }
  300. std::string cmGlobalVisualStudioGenerator::GetUserMacrosDirectory()
  301. {
  302. return "";
  303. }
  304. std::string cmGlobalVisualStudioGenerator::GetUserMacrosRegKeyBase()
  305. {
  306. return "";
  307. }
  308. void cmGlobalVisualStudioGenerator::FillLinkClosure(
  309. const cmGeneratorTarget* target, TargetSet& linked)
  310. {
  311. if (linked.insert(target).second) {
  312. TargetDependSet const& depends = this->GetTargetDirectDepends(target);
  313. for (cmTargetDepend const& di : depends) {
  314. if (di.IsLink()) {
  315. this->FillLinkClosure(di, linked);
  316. }
  317. }
  318. }
  319. }
  320. cmGlobalVisualStudioGenerator::TargetSet const&
  321. cmGlobalVisualStudioGenerator::GetTargetLinkClosure(cmGeneratorTarget* target)
  322. {
  323. auto i = this->TargetLinkClosure.find(target);
  324. if (i == this->TargetLinkClosure.end()) {
  325. TargetSetMap::value_type entry(target, TargetSet());
  326. i = this->TargetLinkClosure.insert(entry).first;
  327. this->FillLinkClosure(target, i->second);
  328. }
  329. return i->second;
  330. }
  331. void cmGlobalVisualStudioGenerator::FollowLinkDepends(
  332. const cmGeneratorTarget* target, std::set<const cmGeneratorTarget*>& linked)
  333. {
  334. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  335. return;
  336. }
  337. if (linked.insert(target).second &&
  338. target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  339. // Static library targets do not list their link dependencies so
  340. // we must follow them transitively now.
  341. TargetDependSet const& depends = this->GetTargetDirectDepends(target);
  342. for (cmTargetDepend const& di : depends) {
  343. if (di.IsLink()) {
  344. this->FollowLinkDepends(di, linked);
  345. }
  346. }
  347. }
  348. }
  349. bool cmGlobalVisualStudioGenerator::ComputeTargetDepends()
  350. {
  351. if (!this->cmGlobalGenerator::ComputeTargetDepends()) {
  352. return false;
  353. }
  354. for (auto const& it : this->ProjectMap) {
  355. std::vector<cmLocalGenerator*> const& gen = it.second;
  356. for (const cmLocalGenerator* i : gen) {
  357. std::vector<cmGeneratorTarget*> const& targets =
  358. i->GetGeneratorTargets();
  359. for (cmGeneratorTarget* ti : targets) {
  360. this->ComputeVSTargetDepends(ti);
  361. }
  362. }
  363. }
  364. return true;
  365. }
  366. static bool VSLinkable(cmGeneratorTarget const* t)
  367. {
  368. return t->IsLinkable() || t->GetType() == cmStateEnums::OBJECT_LIBRARY;
  369. }
  370. void cmGlobalVisualStudioGenerator::ComputeVSTargetDepends(
  371. cmGeneratorTarget* target)
  372. {
  373. if (this->VSTargetDepends.find(target) != this->VSTargetDepends.end()) {
  374. return;
  375. }
  376. VSDependSet& vsTargetDepend = this->VSTargetDepends[target];
  377. // VS <= 7.1 has two behaviors that affect solution dependencies.
  378. //
  379. // (1) Solution-level dependencies between a linkable target and a
  380. // library cause that library to be linked. We use an intermedite
  381. // empty utility target to express the dependency. (VS 8 and above
  382. // provide a project file "LinkLibraryDependencies" setting to
  383. // choose whether to activate this behavior. We disable it except
  384. // when linking external project files.)
  385. //
  386. // (2) We cannot let static libraries depend directly on targets to
  387. // which they "link" because the librarian tool will copy the
  388. // targets into the static library. While the work-around for
  389. // behavior (1) would also avoid this, it would create a large
  390. // number of extra utility targets for little gain. Instead, use
  391. // the above work-around only for dependencies explicitly added by
  392. // the add_dependencies() command. Approximate link dependencies by
  393. // leaving them out for the static library itself but following them
  394. // transitively for other targets.
  395. bool allowLinkable = (target->GetType() != cmStateEnums::STATIC_LIBRARY &&
  396. target->GetType() != cmStateEnums::SHARED_LIBRARY &&
  397. target->GetType() != cmStateEnums::MODULE_LIBRARY &&
  398. target->GetType() != cmStateEnums::EXECUTABLE);
  399. TargetDependSet const& depends = this->GetTargetDirectDepends(target);
  400. // Collect implicit link dependencies (target_link_libraries).
  401. // Static libraries cannot depend on their link implementation
  402. // due to behavior (2), but they do not really need to.
  403. std::set<cmGeneratorTarget const*> linkDepends;
  404. if (target->GetType() != cmStateEnums::STATIC_LIBRARY) {
  405. for (cmTargetDepend const& di : depends) {
  406. cmTargetDepend dep = di;
  407. if (dep.IsLink()) {
  408. this->FollowLinkDepends(di, linkDepends);
  409. }
  410. }
  411. }
  412. // Collect explicit util dependencies (add_dependencies).
  413. std::set<cmGeneratorTarget const*> utilDepends;
  414. for (cmTargetDepend const& di : depends) {
  415. cmTargetDepend dep = di;
  416. if (dep.IsUtil()) {
  417. this->FollowLinkDepends(di, utilDepends);
  418. }
  419. }
  420. // Collect all targets linked by this target so we can avoid
  421. // intermediate targets below.
  422. TargetSet linked;
  423. if (target->GetType() != cmStateEnums::STATIC_LIBRARY) {
  424. linked = this->GetTargetLinkClosure(target);
  425. }
  426. // Emit link dependencies.
  427. for (cmGeneratorTarget const* dep : linkDepends) {
  428. vsTargetDepend.insert(dep->GetName());
  429. }
  430. // Emit util dependencies. Possibly use intermediate targets.
  431. for (cmGeneratorTarget const* dgt : utilDepends) {
  432. if (allowLinkable || !VSLinkable(dgt) || linked.count(dgt)) {
  433. // Direct dependency allowed.
  434. vsTargetDepend.insert(dgt->GetName());
  435. } else {
  436. // Direct dependency on linkable target not allowed.
  437. // Use an intermediate utility target.
  438. vsTargetDepend.insert(this->GetUtilityDepend(dgt));
  439. }
  440. }
  441. }
  442. bool cmGlobalVisualStudioGenerator::FindMakeProgram(cmMakefile* mf)
  443. {
  444. // Visual Studio generators know how to lookup their build tool
  445. // directly instead of needing a helper module to do it, so we
  446. // do not actually need to put CMAKE_MAKE_PROGRAM into the cache.
  447. if (cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM"))) {
  448. mf->AddDefinition("CMAKE_MAKE_PROGRAM", this->GetVSMakeProgram().c_str());
  449. }
  450. return true;
  451. }
  452. std::string cmGlobalVisualStudioGenerator::GetUtilityDepend(
  453. cmGeneratorTarget const* target)
  454. {
  455. auto i = this->UtilityDepends.find(target);
  456. if (i == this->UtilityDepends.end()) {
  457. std::string name = this->WriteUtilityDepend(target);
  458. UtilityDependsMap::value_type entry(target, name);
  459. i = this->UtilityDepends.insert(entry).first;
  460. }
  461. return i->second;
  462. }
  463. std::string cmGlobalVisualStudioGenerator::GetStartupProjectName(
  464. cmLocalGenerator const* root) const
  465. {
  466. const char* n = root->GetMakefile()->GetProperty("VS_STARTUP_PROJECT");
  467. if (n && *n) {
  468. std::string startup = n;
  469. if (this->FindTarget(startup)) {
  470. return startup;
  471. } else {
  472. root->GetMakefile()->IssueMessage(
  473. MessageType::AUTHOR_WARNING,
  474. "Directory property VS_STARTUP_PROJECT specifies target "
  475. "'" +
  476. startup + "' that does not exist. Ignoring.");
  477. }
  478. }
  479. // default, if not specified
  480. return this->GetAllTargetName();
  481. }
  482. bool IsVisualStudioMacrosFileRegistered(const std::string& macrosFile,
  483. const std::string& regKeyBase,
  484. std::string& nextAvailableSubKeyName)
  485. {
  486. bool macrosRegistered = false;
  487. std::string s1;
  488. std::string s2;
  489. // Make lowercase local copies, convert to Unix slashes, and
  490. // see if the resulting strings are the same:
  491. s1 = cmSystemTools::LowerCase(macrosFile);
  492. cmSystemTools::ConvertToUnixSlashes(s1);
  493. std::string keyname;
  494. HKEY hkey = NULL;
  495. LONG result = ERROR_SUCCESS;
  496. DWORD index = 0;
  497. keyname = regKeyBase + "\\OtherProjects7";
  498. hkey = NULL;
  499. result =
  500. RegOpenKeyExW(HKEY_CURRENT_USER, cmsys::Encoding::ToWide(keyname).c_str(),
  501. 0, KEY_READ, &hkey);
  502. if (ERROR_SUCCESS == result) {
  503. // Iterate the subkeys and look for the values of interest in each subkey:
  504. wchar_t subkeyname[256];
  505. DWORD cch_subkeyname = sizeof(subkeyname) * sizeof(subkeyname[0]);
  506. wchar_t keyclass[256];
  507. DWORD cch_keyclass = sizeof(keyclass) * sizeof(keyclass[0]);
  508. FILETIME lastWriteTime;
  509. lastWriteTime.dwHighDateTime = 0;
  510. lastWriteTime.dwLowDateTime = 0;
  511. while (ERROR_SUCCESS ==
  512. RegEnumKeyExW(hkey, index, subkeyname, &cch_subkeyname, 0, keyclass,
  513. &cch_keyclass, &lastWriteTime)) {
  514. // Open the subkey and query the values of interest:
  515. HKEY hsubkey = NULL;
  516. result = RegOpenKeyExW(hkey, subkeyname, 0, KEY_READ, &hsubkey);
  517. if (ERROR_SUCCESS == result) {
  518. DWORD valueType = REG_SZ;
  519. wchar_t data1[256];
  520. DWORD cch_data1 = sizeof(data1) * sizeof(data1[0]);
  521. RegQueryValueExW(hsubkey, L"Path", 0, &valueType, (LPBYTE)&data1[0],
  522. &cch_data1);
  523. DWORD data2 = 0;
  524. DWORD cch_data2 = sizeof(data2);
  525. RegQueryValueExW(hsubkey, L"Security", 0, &valueType, (LPBYTE)&data2,
  526. &cch_data2);
  527. DWORD data3 = 0;
  528. DWORD cch_data3 = sizeof(data3);
  529. RegQueryValueExW(hsubkey, L"StorageFormat", 0, &valueType,
  530. (LPBYTE)&data3, &cch_data3);
  531. s2 = cmSystemTools::LowerCase(cmsys::Encoding::ToNarrow(data1));
  532. cmSystemTools::ConvertToUnixSlashes(s2);
  533. if (s2 == s1) {
  534. macrosRegistered = true;
  535. }
  536. std::string fullname = cmsys::Encoding::ToNarrow(data1);
  537. std::string filename;
  538. std::string filepath;
  539. std::string filepathname;
  540. std::string filepathpath;
  541. if (cmSystemTools::FileExists(fullname.c_str())) {
  542. filename = cmSystemTools::GetFilenameName(fullname);
  543. filepath = cmSystemTools::GetFilenamePath(fullname);
  544. filepathname = cmSystemTools::GetFilenameName(filepath);
  545. filepathpath = cmSystemTools::GetFilenamePath(filepath);
  546. }
  547. // std::cout << keyname << "\\" << subkeyname << ":" << std::endl;
  548. // std::cout << " Path: " << data1 << std::endl;
  549. // std::cout << " Security: " << data2 << std::endl;
  550. // std::cout << " StorageFormat: " << data3 << std::endl;
  551. // std::cout << " filename: " << filename << std::endl;
  552. // std::cout << " filepath: " << filepath << std::endl;
  553. // std::cout << " filepathname: " << filepathname << std::endl;
  554. // std::cout << " filepathpath: " << filepathpath << std::endl;
  555. // std::cout << std::endl;
  556. RegCloseKey(hsubkey);
  557. } else {
  558. std::cout << "error opening subkey: " << subkeyname << std::endl;
  559. std::cout << std::endl;
  560. }
  561. ++index;
  562. cch_subkeyname = sizeof(subkeyname) * sizeof(subkeyname[0]);
  563. cch_keyclass = sizeof(keyclass) * sizeof(keyclass[0]);
  564. lastWriteTime.dwHighDateTime = 0;
  565. lastWriteTime.dwLowDateTime = 0;
  566. }
  567. RegCloseKey(hkey);
  568. } else {
  569. std::cout << "error opening key: " << keyname << std::endl;
  570. std::cout << std::endl;
  571. }
  572. // Pass back next available sub key name, assuming sub keys always
  573. // follow the expected naming scheme. Expected naming scheme is that
  574. // the subkeys of OtherProjects7 is 0 to n-1, so it's ok to use "n"
  575. // as the name of the next subkey.
  576. std::ostringstream ossNext;
  577. ossNext << index;
  578. nextAvailableSubKeyName = ossNext.str();
  579. keyname = regKeyBase + "\\RecordingProject7";
  580. hkey = NULL;
  581. result =
  582. RegOpenKeyExW(HKEY_CURRENT_USER, cmsys::Encoding::ToWide(keyname).c_str(),
  583. 0, KEY_READ, &hkey);
  584. if (ERROR_SUCCESS == result) {
  585. DWORD valueType = REG_SZ;
  586. wchar_t data1[256];
  587. DWORD cch_data1 = sizeof(data1) * sizeof(data1[0]);
  588. RegQueryValueExW(hkey, L"Path", 0, &valueType, (LPBYTE)&data1[0],
  589. &cch_data1);
  590. DWORD data2 = 0;
  591. DWORD cch_data2 = sizeof(data2);
  592. RegQueryValueExW(hkey, L"Security", 0, &valueType, (LPBYTE)&data2,
  593. &cch_data2);
  594. DWORD data3 = 0;
  595. DWORD cch_data3 = sizeof(data3);
  596. RegQueryValueExW(hkey, L"StorageFormat", 0, &valueType, (LPBYTE)&data3,
  597. &cch_data3);
  598. s2 = cmSystemTools::LowerCase(cmsys::Encoding::ToNarrow(data1));
  599. cmSystemTools::ConvertToUnixSlashes(s2);
  600. if (s2 == s1) {
  601. macrosRegistered = true;
  602. }
  603. // std::cout << keyname << ":" << std::endl;
  604. // std::cout << " Path: " << data1 << std::endl;
  605. // std::cout << " Security: " << data2 << std::endl;
  606. // std::cout << " StorageFormat: " << data3 << std::endl;
  607. // std::cout << std::endl;
  608. RegCloseKey(hkey);
  609. } else {
  610. std::cout << "error opening key: " << keyname << std::endl;
  611. std::cout << std::endl;
  612. }
  613. return macrosRegistered;
  614. }
  615. void WriteVSMacrosFileRegistryEntry(const std::string& nextAvailableSubKeyName,
  616. const std::string& macrosFile,
  617. const std::string& regKeyBase)
  618. {
  619. std::string keyname = regKeyBase + "\\OtherProjects7";
  620. HKEY hkey = NULL;
  621. LONG result =
  622. RegOpenKeyExW(HKEY_CURRENT_USER, cmsys::Encoding::ToWide(keyname).c_str(),
  623. 0, KEY_READ | KEY_WRITE, &hkey);
  624. if (ERROR_SUCCESS == result) {
  625. // Create the subkey and set the values of interest:
  626. HKEY hsubkey = NULL;
  627. wchar_t lpClass[] = L"";
  628. result = RegCreateKeyExW(
  629. hkey, cmsys::Encoding::ToWide(nextAvailableSubKeyName).c_str(), 0,
  630. lpClass, 0, KEY_READ | KEY_WRITE, 0, &hsubkey, 0);
  631. if (ERROR_SUCCESS == result) {
  632. DWORD dw = 0;
  633. std::string s(macrosFile);
  634. std::replace(s.begin(), s.end(), '/', '\\');
  635. std::wstring ws = cmsys::Encoding::ToWide(s);
  636. result =
  637. RegSetValueExW(hsubkey, L"Path", 0, REG_SZ, (LPBYTE)ws.c_str(),
  638. static_cast<DWORD>(ws.size() + 1) * sizeof(wchar_t));
  639. if (ERROR_SUCCESS != result) {
  640. std::cout << "error result 1: " << result << std::endl;
  641. std::cout << std::endl;
  642. }
  643. // Security value is always "1" for sample macros files (seems to be "2"
  644. // if you put the file somewhere outside the standard VSMacros folder)
  645. dw = 1;
  646. result = RegSetValueExW(hsubkey, L"Security", 0, REG_DWORD, (LPBYTE)&dw,
  647. sizeof(DWORD));
  648. if (ERROR_SUCCESS != result) {
  649. std::cout << "error result 2: " << result << std::endl;
  650. std::cout << std::endl;
  651. }
  652. // StorageFormat value is always "0" for sample macros files
  653. dw = 0;
  654. result = RegSetValueExW(hsubkey, L"StorageFormat", 0, REG_DWORD,
  655. (LPBYTE)&dw, sizeof(DWORD));
  656. if (ERROR_SUCCESS != result) {
  657. std::cout << "error result 3: " << result << std::endl;
  658. std::cout << std::endl;
  659. }
  660. RegCloseKey(hsubkey);
  661. } else {
  662. std::cout << "error creating subkey: " << nextAvailableSubKeyName
  663. << std::endl;
  664. std::cout << std::endl;
  665. }
  666. RegCloseKey(hkey);
  667. } else {
  668. std::cout << "error opening key: " << keyname << std::endl;
  669. std::cout << std::endl;
  670. }
  671. }
  672. void RegisterVisualStudioMacros(const std::string& macrosFile,
  673. const std::string& regKeyBase)
  674. {
  675. bool macrosRegistered;
  676. std::string nextAvailableSubKeyName;
  677. macrosRegistered = IsVisualStudioMacrosFileRegistered(
  678. macrosFile, regKeyBase, nextAvailableSubKeyName);
  679. if (!macrosRegistered) {
  680. int count =
  681. cmCallVisualStudioMacro::GetNumberOfRunningVisualStudioInstances("ALL");
  682. // Only register the macros file if there are *no* instances of Visual
  683. // Studio running. If we register it while one is running, first, it has
  684. // no effect on the running instance; second, and worse, Visual Studio
  685. // removes our newly added registration entry when it quits. Instead,
  686. // emit a warning asking the user to exit all running Visual Studio
  687. // instances...
  688. //
  689. if (0 != count) {
  690. std::ostringstream oss;
  691. oss << "Could not register CMake's Visual Studio macros file '"
  692. << CMAKE_VSMACROS_FILENAME "' while Visual Studio is running."
  693. << " Please exit all running instances of Visual Studio before"
  694. << " continuing." << std::endl
  695. << std::endl
  696. << "CMake needs to register Visual Studio macros when its macros"
  697. << " file is updated or when it detects that its current macros file"
  698. << " is no longer registered with Visual Studio." << std::endl;
  699. cmSystemTools::Message(oss.str(), "Warning");
  700. // Count them again now that the warning is over. In the case of a GUI
  701. // warning, the user may have gone to close Visual Studio and then come
  702. // back to the CMake GUI and clicked ok on the above warning. If so,
  703. // then register the macros *now* if the count is *now* 0...
  704. //
  705. count = cmCallVisualStudioMacro::GetNumberOfRunningVisualStudioInstances(
  706. "ALL");
  707. // Also re-get the nextAvailableSubKeyName in case Visual Studio
  708. // wrote out new registered macros information as it was exiting:
  709. //
  710. if (0 == count) {
  711. IsVisualStudioMacrosFileRegistered(macrosFile, regKeyBase,
  712. nextAvailableSubKeyName);
  713. }
  714. }
  715. // Do another if check - 'count' may have changed inside the above if:
  716. //
  717. if (0 == count) {
  718. WriteVSMacrosFileRegistryEntry(nextAvailableSubKeyName, macrosFile,
  719. regKeyBase);
  720. }
  721. }
  722. }
  723. bool cmGlobalVisualStudioGenerator::TargetIsFortranOnly(
  724. cmGeneratorTarget const* gt)
  725. {
  726. // check to see if this is a fortran build
  727. {
  728. // Issue diagnostic if the source files depend on the config.
  729. std::vector<cmSourceFile*> sources;
  730. if (!gt->GetConfigCommonSourceFiles(sources)) {
  731. return false;
  732. }
  733. }
  734. // If there's only one source language, Fortran has to be used
  735. // in order for the sources to compile.
  736. std::set<std::string> languages;
  737. gt->GetLanguages(languages, "");
  738. // Consider an explicit linker language property, but *not* the
  739. // computed linker language that may depend on linked targets.
  740. // This allows the project to control the language choice in
  741. // a target with none of its own sources, e.g. when also using
  742. // object libraries.
  743. const char* linkLang = gt->GetProperty("LINKER_LANGUAGE");
  744. if (linkLang && *linkLang) {
  745. languages.insert(linkLang);
  746. }
  747. // Intel Fortran .vfproj files do support the resource compiler.
  748. languages.erase("RC");
  749. return languages.size() == 1 && *languages.begin() == "Fortran";
  750. }
  751. bool cmGlobalVisualStudioGenerator::TargetCompare::operator()(
  752. cmGeneratorTarget const* l, cmGeneratorTarget const* r) const
  753. {
  754. // Make sure a given named target is ordered first,
  755. // e.g. to set ALL_BUILD as the default active project.
  756. // When the empty string is named this is a no-op.
  757. if (r->GetName() == this->First) {
  758. return false;
  759. }
  760. if (l->GetName() == this->First) {
  761. return true;
  762. }
  763. return l->GetName() < r->GetName();
  764. }
  765. cmGlobalVisualStudioGenerator::OrderedTargetDependSet::OrderedTargetDependSet(
  766. TargetDependSet const& targets, std::string const& first)
  767. : derived(TargetCompare(first))
  768. {
  769. this->insert(targets.begin(), targets.end());
  770. }
  771. cmGlobalVisualStudioGenerator::OrderedTargetDependSet::OrderedTargetDependSet(
  772. TargetSet const& targets, std::string const& first)
  773. : derived(TargetCompare(first))
  774. {
  775. for (cmGeneratorTarget const* it : targets) {
  776. this->insert(it);
  777. }
  778. }
  779. std::string cmGlobalVisualStudioGenerator::ExpandCFGIntDir(
  780. const std::string& str, const std::string& config) const
  781. {
  782. std::string replace = GetCMakeCFGIntDir();
  783. std::string tmp = str;
  784. for (std::string::size_type i = tmp.find(replace); i != std::string::npos;
  785. i = tmp.find(replace, i)) {
  786. tmp.replace(i, replace.size(), config);
  787. i += config.size();
  788. }
  789. return tmp;
  790. }
  791. void cmGlobalVisualStudioGenerator::AddSymbolExportCommand(
  792. cmGeneratorTarget* gt, std::vector<cmCustomCommand>& commands,
  793. std::string const& configName)
  794. {
  795. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  796. gt->GetModuleDefinitionInfo(configName);
  797. if (!mdi || !mdi->DefFileGenerated) {
  798. return;
  799. }
  800. std::vector<std::string> outputs;
  801. outputs.push_back(mdi->DefFile);
  802. std::vector<std::string> empty;
  803. std::vector<cmSourceFile const*> objectSources;
  804. gt->GetObjectSources(objectSources, configName);
  805. std::map<cmSourceFile const*, std::string> mapping;
  806. for (cmSourceFile const* it : objectSources) {
  807. mapping[it];
  808. }
  809. gt->LocalGenerator->ComputeObjectFilenames(mapping, gt);
  810. std::string obj_dir = gt->ObjectDirectory;
  811. std::string cmakeCommand = cmSystemTools::GetCMakeCommand();
  812. cmSystemTools::ConvertToWindowsExtendedPath(cmakeCommand);
  813. cmCustomCommandLine cmdl;
  814. cmdl.push_back(cmakeCommand);
  815. cmdl.push_back("-E");
  816. cmdl.push_back("__create_def");
  817. cmdl.push_back(mdi->DefFile);
  818. std::string obj_dir_expanded = obj_dir;
  819. cmSystemTools::ReplaceString(obj_dir_expanded, this->GetCMakeCFGIntDir(),
  820. configName.c_str());
  821. cmSystemTools::MakeDirectory(obj_dir_expanded);
  822. std::string const objs_file = obj_dir_expanded + "/objects.txt";
  823. cmdl.push_back(objs_file);
  824. cmGeneratedFileStream fout(objs_file.c_str());
  825. if (!fout) {
  826. cmSystemTools::Error("could not open " + objs_file);
  827. return;
  828. }
  829. if (mdi->WindowsExportAllSymbols) {
  830. std::vector<std::string> objs;
  831. for (cmSourceFile const* it : objectSources) {
  832. // Find the object file name corresponding to this source file.
  833. // It must exist because we populated the mapping just above.
  834. const auto& v = mapping[it];
  835. assert(!v.empty());
  836. std::string objFile = obj_dir + v;
  837. objs.push_back(objFile);
  838. }
  839. std::vector<cmSourceFile const*> externalObjectSources;
  840. gt->GetExternalObjects(externalObjectSources, configName);
  841. for (cmSourceFile const* it : externalObjectSources) {
  842. objs.push_back(it->GetFullPath());
  843. }
  844. for (std::string const& it : objs) {
  845. std::string objFile = it;
  846. // replace $(ConfigurationName) in the object names
  847. cmSystemTools::ReplaceString(objFile, this->GetCMakeCFGIntDir(),
  848. configName.c_str());
  849. if (cmHasLiteralSuffix(objFile, ".obj")) {
  850. fout << objFile << "\n";
  851. }
  852. }
  853. }
  854. for (cmSourceFile const* i : mdi->Sources) {
  855. fout << i->GetFullPath() << "\n";
  856. }
  857. cmCustomCommandLines commandLines;
  858. commandLines.push_back(cmdl);
  859. cmCustomCommand command(gt->Target->GetMakefile(), outputs, empty, empty,
  860. commandLines, "Auto build dll exports", ".");
  861. commands.push_back(command);
  862. }
  863. static bool OpenSolution(std::string sln)
  864. {
  865. HRESULT comInitialized =
  866. CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
  867. if (FAILED(comInitialized)) {
  868. return false;
  869. }
  870. HINSTANCE hi =
  871. ShellExecuteA(NULL, "open", sln.c_str(), NULL, NULL, SW_SHOWNORMAL);
  872. CoUninitialize();
  873. return reinterpret_cast<intptr_t>(hi) > 32;
  874. }
  875. bool cmGlobalVisualStudioGenerator::Open(const std::string& bindir,
  876. const std::string& projectName,
  877. bool dryRun)
  878. {
  879. std::string sln = bindir + "/" + projectName + ".sln";
  880. if (dryRun) {
  881. return cmSystemTools::FileExists(sln, true);
  882. }
  883. sln = cmSystemTools::ConvertToOutputPath(sln);
  884. return std::async(std::launch::async, OpenSolution, sln).get();
  885. }