cmGlobalVisualStudioGenerator.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 "cmGlobalGenerator.h"
  11. #include "cmTargetDepend.h"
  12. class cmCustomCommand;
  13. class cmGeneratorTarget;
  14. class cmLocalGenerator;
  15. class cmMakefile;
  16. class cmake;
  17. /** \class cmGlobalVisualStudioGenerator
  18. * \brief Base class for global Visual Studio generators.
  19. *
  20. * cmGlobalVisualStudioGenerator provides functionality common to all
  21. * global Visual Studio generators.
  22. */
  23. class cmGlobalVisualStudioGenerator : public cmGlobalGenerator
  24. {
  25. public:
  26. /** Known versions of Visual Studio. */
  27. enum VSVersion
  28. {
  29. VS9 = 90,
  30. VS10 = 100,
  31. VS11 = 110,
  32. VS12 = 120,
  33. /* VS13 = 130 was skipped */
  34. VS14 = 140,
  35. VS15 = 150,
  36. VS16 = 160
  37. };
  38. virtual ~cmGlobalVisualStudioGenerator();
  39. VSVersion GetVersion() const;
  40. void SetVersion(VSVersion v);
  41. /** Is the installed VS an Express edition? */
  42. bool IsExpressEdition() const { return this->ExpressEdition; }
  43. void EnableLanguage(std::vector<std::string> const& languages, cmMakefile*,
  44. bool optional) override;
  45. bool SetGeneratorPlatform(std::string const& p, cmMakefile* mf) override;
  46. /**
  47. * Get the name of the target platform (architecture) for which we generate.
  48. * The names are as defined by VS, e.g. "Win32", "x64", "Itanium", "ARM".
  49. */
  50. std::string const& GetPlatformName() const;
  51. /**
  52. * Configure CMake's Visual Studio macros file into the user's Visual
  53. * Studio macros directory.
  54. */
  55. virtual void ConfigureCMakeVisualStudioMacros();
  56. /**
  57. * Where does this version of Visual Studio look for macros for the
  58. * current user? Returns the empty string if this version of Visual
  59. * Studio does not implement support for VB macros.
  60. */
  61. virtual std::string GetUserMacrosDirectory();
  62. /**
  63. * What is the reg key path to "vsmacros" for this version of Visual
  64. * Studio?
  65. */
  66. virtual std::string GetUserMacrosRegKeyBase();
  67. enum MacroName
  68. {
  69. MacroReload,
  70. MacroStop
  71. };
  72. /**
  73. * Call the ReloadProjects macro if necessary based on
  74. * GetFilesReplacedDuringGenerate results.
  75. */
  76. void CallVisualStudioMacro(MacroName m, const std::string& vsSolutionFile);
  77. // return true if target is fortran only
  78. bool TargetIsFortranOnly(const cmGeneratorTarget* gt);
  79. /** Get the top-level registry key for this VS version. */
  80. std::string GetRegistryBase();
  81. /** Get the top-level registry key for the given VS version. */
  82. static std::string GetRegistryBase(const char* version);
  83. /** Return true if the generated build tree may contain multiple builds.
  84. i.e. "Can I build Debug and Release in the same tree?" */
  85. bool IsMultiConfig() const override { return true; }
  86. /** Return true if building for Windows CE */
  87. virtual bool TargetsWindowsCE() const { return false; }
  88. bool IsIncludeExternalMSProjectSupported() const override { return true; }
  89. /** Get encoding used by generator for generated source files
  90. */
  91. codecvt::Encoding GetMakefileEncoding() const override
  92. {
  93. return codecvt::ANSI;
  94. }
  95. class TargetSet : public std::set<cmGeneratorTarget const*>
  96. {
  97. };
  98. class TargetCompare
  99. {
  100. std::string First;
  101. public:
  102. TargetCompare(std::string const& first)
  103. : First(first)
  104. {
  105. }
  106. bool operator()(cmGeneratorTarget const* l,
  107. cmGeneratorTarget const* r) const;
  108. };
  109. class OrderedTargetDependSet;
  110. bool FindMakeProgram(cmMakefile*) override;
  111. std::string ExpandCFGIntDir(const std::string& str,
  112. const std::string& config) const override;
  113. void ComputeTargetObjectDirectory(cmGeneratorTarget* gt) const override;
  114. std::string GetStartupProjectName(cmLocalGenerator const* root) const;
  115. void AddSymbolExportCommand(cmGeneratorTarget*,
  116. std::vector<cmCustomCommand>& commands,
  117. std::string const& configName);
  118. bool Open(const std::string& bindir, const std::string& projectName,
  119. bool dryRun) override;
  120. bool IsVisualStudio() const override { return true; }
  121. protected:
  122. cmGlobalVisualStudioGenerator(cmake* cm,
  123. std::string const& platformInGeneratorName);
  124. void AddExtraIDETargets() override;
  125. // Does this VS version link targets to each other if there are
  126. // dependencies in the SLN file? This was done for VS versions
  127. // below 8.
  128. virtual bool VSLinksDependencies() const { return true; }
  129. const char* GetIDEVersion() const;
  130. void WriteSLNHeader(std::ostream& fout);
  131. FindMakeProgramStage GetFindMakeProgramStage() const override
  132. {
  133. return FindMakeProgramStage::Early;
  134. }
  135. bool ComputeTargetDepends() override;
  136. class VSDependSet : public std::set<std::string>
  137. {
  138. };
  139. class VSDependMap : public std::map<cmGeneratorTarget const*, VSDependSet>
  140. {
  141. };
  142. VSDependMap VSTargetDepends;
  143. void ComputeVSTargetDepends(cmGeneratorTarget*);
  144. bool CheckTargetLinks(cmGeneratorTarget& target, const std::string& name);
  145. std::string GetUtilityForTarget(cmGeneratorTarget& target,
  146. const std::string&);
  147. virtual std::string WriteUtilityDepend(cmGeneratorTarget const*) = 0;
  148. std::string GetUtilityDepend(const cmGeneratorTarget* target);
  149. using UtilityDependsMap = std::map<cmGeneratorTarget const*, std::string>;
  150. UtilityDependsMap UtilityDepends;
  151. protected:
  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. const char*) 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. };