cmGlobalVisualStudioGenerator.cxx 34 KB

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