cmGlobalVisualStudioGenerator.h 7.0 KB

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