cmGlobalVisualStudioGenerator.h 6.9 KB

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