cmGlobalVisualStudioGenerator.cxx 33 KB

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