cmGlobalVisualStudioGenerator.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <iosfwd>
  6. #include <map>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10. #include "cm_codecvt_Encoding.hxx"
  11. #include "cmGlobalGenerator.h"
  12. #include "cmTargetDepend.h"
  13. #include "cmValue.h"
  14. class cmCustomCommand;
  15. class cmGeneratorTarget;
  16. class cmLocalGenerator;
  17. class cmMakefile;
  18. class cmake;
  19. /** \class cmGlobalVisualStudioGenerator
  20. * \brief Base class for global Visual Studio generators.
  21. *
  22. * cmGlobalVisualStudioGenerator provides functionality common to all
  23. * global Visual Studio generators.
  24. */
  25. class cmGlobalVisualStudioGenerator : public cmGlobalGenerator
  26. {
  27. public:
  28. /** Known versions of Visual Studio. */
  29. enum class VSVersion : uint16_t
  30. {
  31. VS14 = 140,
  32. VS15 = 150,
  33. VS16 = 160,
  34. VS17 = 170
  35. };
  36. ~cmGlobalVisualStudioGenerator() override;
  37. VSVersion GetVersion() const;
  38. void SetVersion(VSVersion v);
  39. /** Is the installed VS an Express edition? */
  40. bool IsExpressEdition() const { return this->ExpressEdition; }
  41. void EnableLanguage(std::vector<std::string> const& languages, cmMakefile*,
  42. bool optional) override;
  43. bool SetGeneratorPlatform(std::string const& p, cmMakefile* mf) override;
  44. /**
  45. * Get the name of the target platform (architecture) for which we generate.
  46. * The names are as defined by VS, e.g. "Win32", "x64", "Itanium", "ARM".
  47. */
  48. std::string const& GetPlatformName() const;
  49. /**
  50. * Configure CMake's Visual Studio macros file into the user's Visual
  51. * Studio macros directory.
  52. */
  53. virtual void ConfigureCMakeVisualStudioMacros();
  54. /**
  55. * Where does this version of Visual Studio look for macros for the
  56. * current user? Returns the empty string if this version of Visual
  57. * Studio does not implement support for VB macros.
  58. */
  59. virtual std::string GetUserMacrosDirectory();
  60. /**
  61. * What is the reg key path to "vsmacros" for this version of Visual
  62. * Studio?
  63. */
  64. virtual std::string GetUserMacrosRegKeyBase();
  65. cmValue GetDebuggerWorkingDirectory(cmGeneratorTarget* gt) const override;
  66. enum MacroName
  67. {
  68. MacroReload,
  69. MacroStop
  70. };
  71. /**
  72. * Call the ReloadProjects macro if necessary based on
  73. * GetFilesReplacedDuringGenerate results.
  74. */
  75. void CallVisualStudioMacro(MacroName m, std::string const& vsSolutionFile);
  76. // return true if target is fortran only
  77. bool TargetIsFortranOnly(cmGeneratorTarget const* gt) const;
  78. // return true if target should be included in solution.
  79. virtual bool IsInSolution(cmGeneratorTarget const* gt) const;
  80. // return true if project dependency should be included in solution.
  81. virtual bool IsDepInSolution(std::string const& targetName) const;
  82. /** Get the top-level registry key for this VS version. */
  83. std::string GetRegistryBase();
  84. /** Get the top-level registry key for the given VS version. */
  85. static std::string GetRegistryBase(char const* version);
  86. /** Return true if the generated build tree may contain multiple builds.
  87. i.e. "Can I build Debug and Release in the same tree?" */
  88. bool IsMultiConfig() const override { return true; }
  89. /** Return true if building for Windows CE */
  90. virtual bool TargetsWindowsCE() const { return false; }
  91. bool IsIncludeExternalMSProjectSupported() const override { return true; }
  92. /** Get encoding used by generator for generated source files
  93. */
  94. codecvt_Encoding GetMakefileEncoding() const override
  95. {
  96. return codecvt_Encoding::ANSI;
  97. }
  98. class TargetSet : public std::set<cmGeneratorTarget const*>
  99. {
  100. };
  101. class TargetCompare
  102. {
  103. std::string First;
  104. public:
  105. TargetCompare(std::string first)
  106. : First(std::move(first))
  107. {
  108. }
  109. bool operator()(cmGeneratorTarget const* l,
  110. cmGeneratorTarget const* r) const;
  111. };
  112. class OrderedTargetDependSet;
  113. bool FindMakeProgram(cmMakefile*) override;
  114. std::string ExpandCFGIntDir(std::string const& str,
  115. std::string const& config) const override;
  116. void ComputeTargetObjectDirectory(cmGeneratorTarget* gt) const override;
  117. std::string GetStartupProjectName(cmLocalGenerator const* root) const;
  118. void AddSymbolExportCommand(cmGeneratorTarget*,
  119. std::vector<cmCustomCommand>& commands,
  120. std::string const& configName);
  121. bool Open(std::string const& bindir, std::string const& projectName,
  122. bool dryRun) override;
  123. bool IsVisualStudio() const override { return true; }
  124. protected:
  125. cmGlobalVisualStudioGenerator(cmake* cm);
  126. virtual bool InitializePlatform(cmMakefile* mf);
  127. void AddExtraIDETargets() override;
  128. // Does this VS version link targets to each other if there are
  129. // dependencies in the SLN file? This was done for VS versions
  130. // below 8.
  131. virtual bool VSLinksDependencies() const { return true; }
  132. char const* GetIDEVersion() const;
  133. void WriteSLNHeader(std::ostream& fout) const;
  134. bool ComputeTargetDepends() override;
  135. class VSDependSet : public std::set<std::string>
  136. {
  137. };
  138. class VSDependMap : public std::map<cmGeneratorTarget const*, VSDependSet>
  139. {
  140. };
  141. VSDependMap VSTargetDepends;
  142. void ComputeVSTargetDepends(cmGeneratorTarget*);
  143. virtual std::string WriteUtilityDepend(cmGeneratorTarget const*) = 0;
  144. std::string GetUtilityDepend(cmGeneratorTarget const* target);
  145. using UtilityDependsMap = std::map<cmGeneratorTarget const*, std::string>;
  146. UtilityDependsMap UtilityDepends;
  147. VSVersion Version;
  148. bool ExpressEdition;
  149. std::string GeneratorPlatform;
  150. std::string DefaultPlatformName;
  151. private:
  152. virtual std::string GetVSMakeProgram() = 0;
  153. void PrintCompilerAdvice(std::ostream&, std::string const&,
  154. cmValue) const override
  155. {
  156. }
  157. void FollowLinkDepends(cmGeneratorTarget const* target,
  158. std::set<cmGeneratorTarget const*>& linked);
  159. class TargetSetMap : public std::map<cmGeneratorTarget*, TargetSet>
  160. {
  161. };
  162. TargetSetMap TargetLinkClosure;
  163. void FillLinkClosure(cmGeneratorTarget const* target, TargetSet& linked);
  164. TargetSet const& GetTargetLinkClosure(cmGeneratorTarget* target);
  165. };
  166. class cmGlobalVisualStudioGenerator::OrderedTargetDependSet
  167. : public std::multiset<cmTargetDepend,
  168. cmGlobalVisualStudioGenerator::TargetCompare>
  169. {
  170. using derived = std::multiset<cmTargetDepend,
  171. cmGlobalVisualStudioGenerator::TargetCompare>;
  172. public:
  173. using TargetDependSet = cmGlobalGenerator::TargetDependSet;
  174. using TargetSet = cmGlobalVisualStudioGenerator::TargetSet;
  175. OrderedTargetDependSet(TargetDependSet const&, std::string const& first);
  176. OrderedTargetDependSet(TargetSet const&, std::string const& first);
  177. };