cmNinjaTargetGenerator.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 <map>
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include <cm3p/json/value.h>
  12. #include "cmCommonTargetGenerator.h"
  13. #include "cmGlobalNinjaGenerator.h"
  14. #include "cmNinjaTypes.h"
  15. #include "cmOSXBundleGenerator.h"
  16. class cmCustomCommand;
  17. class cmGeneratedFileStream;
  18. class cmGeneratorTarget;
  19. class cmLocalNinjaGenerator;
  20. class cmMakefile;
  21. class cmSourceFile;
  22. class cmNinjaTargetGenerator : public cmCommonTargetGenerator
  23. {
  24. public:
  25. /// Create a cmNinjaTargetGenerator according to the @a target's type.
  26. static std::unique_ptr<cmNinjaTargetGenerator> New(
  27. cmGeneratorTarget* target);
  28. /// Build a NinjaTargetGenerator.
  29. cmNinjaTargetGenerator(cmGeneratorTarget* target);
  30. /// Destructor.
  31. ~cmNinjaTargetGenerator() override;
  32. virtual void Generate(const std::string& config) = 0;
  33. std::string GetTargetName() const;
  34. protected:
  35. bool SetMsvcTargetPdbVariable(cmNinjaVars&, const std::string& config) const;
  36. cmGeneratedFileStream& GetImplFileStream(const std::string& config) const;
  37. cmGeneratedFileStream& GetCommonFileStream() const;
  38. cmGeneratedFileStream& GetRulesFileStream() const;
  39. cmGeneratorTarget* GetGeneratorTarget() const
  40. {
  41. return this->GeneratorTarget;
  42. }
  43. cmLocalNinjaGenerator* GetLocalGenerator() const
  44. {
  45. return this->LocalGenerator;
  46. }
  47. cmGlobalNinjaGenerator* GetGlobalGenerator() const;
  48. cmMakefile* GetMakefile() const { return this->Makefile; }
  49. std::string LanguageCompilerRule(const std::string& lang,
  50. const std::string& config) const;
  51. std::string LanguagePreprocessAndScanRule(std::string const& lang,
  52. const std::string& config) const;
  53. std::string LanguageScanRule(std::string const& lang,
  54. const std::string& config) const;
  55. std::string LanguageDyndepRule(std::string const& lang,
  56. const std::string& config) const;
  57. bool NeedDyndep(std::string const& lang, std::string const& config) const;
  58. bool NeedExplicitPreprocessing(std::string const& lang) const;
  59. bool CompileWithDefines(std::string const& lang) const;
  60. bool NeedCxxModuleSupport(std::string const& lang,
  61. std::string const& config) const;
  62. std::string OrderDependsTargetForTarget(const std::string& config);
  63. std::string ComputeOrderDependsForTarget();
  64. /**
  65. * Compute the flags for compilation of object files for a given @a language.
  66. * @note Generally it is the value of the variable whose name is computed
  67. * by LanguageFlagsVarName().
  68. */
  69. std::string ComputeFlagsForObject(cmSourceFile const* source,
  70. const std::string& language,
  71. const std::string& config);
  72. void AddIncludeFlags(std::string& flags, std::string const& lang,
  73. const std::string& config) override;
  74. std::string ComputeDefines(cmSourceFile const* source,
  75. const std::string& language,
  76. const std::string& config);
  77. std::string ComputeIncludes(cmSourceFile const* source,
  78. const std::string& language,
  79. const std::string& config);
  80. std::string const& ConvertToNinjaPath(const std::string& path) const
  81. {
  82. return this->GetGlobalGenerator()->ConvertToNinjaPath(path);
  83. }
  84. cmGlobalNinjaGenerator::MapToNinjaPathImpl MapToNinjaPath() const
  85. {
  86. return this->GetGlobalGenerator()->MapToNinjaPath();
  87. }
  88. std::string ConvertToNinjaAbsPath(std::string path) const
  89. {
  90. return this->GetGlobalGenerator()->ConvertToNinjaAbsPath(std::move(path));
  91. }
  92. /// @return the list of link dependency for the given target @a target.
  93. cmNinjaDeps ComputeLinkDeps(const std::string& linkLanguage,
  94. const std::string& config,
  95. bool ignoreType = false) const;
  96. /// @return the source file path for the given @a source.
  97. std::string GetCompiledSourceNinjaPath(cmSourceFile const* source) const;
  98. /// @return the object file path for the given @a source.
  99. std::string GetObjectFilePath(cmSourceFile const* source,
  100. const std::string& config) const;
  101. /// @return the preprocessed source file path for the given @a source.
  102. std::string GetPreprocessedFilePath(cmSourceFile const* source,
  103. const std::string& config) const;
  104. /// @return the dyndep file path for this target.
  105. std::string GetDyndepFilePath(std::string const& lang,
  106. const std::string& config) const;
  107. /// @return the target dependency scanner info file path
  108. std::string GetTargetDependInfoPath(std::string const& lang,
  109. const std::string& config) const;
  110. /// @return the file path where the target named @a name is generated.
  111. std::string GetTargetFilePath(const std::string& name,
  112. const std::string& config) const;
  113. /// @return the output path for the target.
  114. virtual std::string GetTargetOutputDir(const std::string& config) const;
  115. void WriteLanguageRules(const std::string& language,
  116. const std::string& config);
  117. void WriteCompileRule(const std::string& language,
  118. const std::string& config);
  119. void WriteObjectBuildStatements(const std::string& config,
  120. const std::string& fileConfig,
  121. bool firstForConfig);
  122. void WriteObjectBuildStatement(cmSourceFile const* source,
  123. const std::string& config,
  124. const std::string& fileConfig,
  125. bool firstForConfig);
  126. void WriteTargetDependInfo(std::string const& lang,
  127. const std::string& config);
  128. void EmitSwiftDependencyInfo(cmSourceFile const* source,
  129. const std::string& config);
  130. void ExportObjectCompileCommand(
  131. std::string const& language, std::string const& sourceFileName,
  132. std::string const& objectDir, std::string const& objectFileName,
  133. std::string const& objectFileDir, std::string const& flags,
  134. std::string const& defines, std::string const& includes,
  135. std::string const& outputConfig);
  136. void AdditionalCleanFiles(const std::string& config);
  137. cmNinjaDeps GetObjects(const std::string& config) const;
  138. void EnsureDirectoryExists(const std::string& dir) const;
  139. void EnsureParentDirectoryExists(const std::string& path) const;
  140. // write rules for macOS Application Bundle content.
  141. struct MacOSXContentGeneratorType
  142. : cmOSXBundleGenerator::MacOSXContentGeneratorType
  143. {
  144. MacOSXContentGeneratorType(cmNinjaTargetGenerator* g,
  145. std::string fileConfig)
  146. : Generator(g)
  147. , FileConfig(std::move(fileConfig))
  148. {
  149. }
  150. void operator()(cmSourceFile const& source, const char* pkgloc,
  151. const std::string& config) override;
  152. private:
  153. cmNinjaTargetGenerator* Generator;
  154. std::string FileConfig;
  155. };
  156. friend struct MacOSXContentGeneratorType;
  157. // Properly initialized by sub-classes.
  158. std::unique_ptr<cmOSXBundleGenerator> OSXBundleGenerator;
  159. std::set<std::string> MacContentFolders;
  160. void addPoolNinjaVariable(const std::string& pool_property,
  161. cmGeneratorTarget* target, cmNinjaVars& vars);
  162. bool ForceResponseFile();
  163. private:
  164. cmLocalNinjaGenerator* LocalGenerator;
  165. struct ByConfig
  166. {
  167. /// List of object files for this target.
  168. cmNinjaDeps Objects;
  169. // Fortran Support
  170. std::map<std::string, cmNinjaDeps> DDIFiles;
  171. // Swift Support
  172. Json::Value SwiftOutputMap;
  173. std::vector<cmCustomCommand const*> CustomCommands;
  174. cmNinjaDeps ExtraFiles;
  175. std::unique_ptr<MacOSXContentGeneratorType> MacOSXContentGenerator;
  176. };
  177. std::map<std::string, ByConfig> Configs;
  178. };