cmGlobalVisualStudioGenerator.h 6.9 KB

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