cmNinjaTargetGenerator.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. /// @return the list of link dependency for the given target @a target.
  89. cmNinjaDeps ComputeLinkDeps(const std::string& linkLanguage,
  90. const std::string& config,
  91. bool ignoreType = false) const;
  92. /// @return the source file path for the given @a source.
  93. std::string GetCompiledSourceNinjaPath(cmSourceFile const* source) const;
  94. /// @return the object file path for the given @a source.
  95. std::string GetObjectFilePath(cmSourceFile const* source,
  96. const std::string& config) const;
  97. /// @return the preprocessed source file path for the given @a source.
  98. std::string GetPreprocessedFilePath(cmSourceFile const* source,
  99. const std::string& config) const;
  100. /// @return the dyndep file path for this target.
  101. std::string GetDyndepFilePath(std::string const& lang,
  102. const std::string& config) const;
  103. /// @return the target dependency scanner info file path
  104. std::string GetTargetDependInfoPath(std::string const& lang,
  105. const std::string& config) const;
  106. /// @return the file path where the target named @a name is generated.
  107. std::string GetTargetFilePath(const std::string& name,
  108. const std::string& config) const;
  109. /// @return the output path for the target.
  110. virtual std::string GetTargetOutputDir(const std::string& config) const;
  111. void WriteLanguageRules(const std::string& language,
  112. const std::string& config);
  113. void WriteCompileRule(const std::string& language,
  114. const std::string& config);
  115. void WriteObjectBuildStatements(const std::string& config,
  116. const std::string& fileConfig,
  117. bool firstForConfig);
  118. void WriteObjectBuildStatement(cmSourceFile const* source,
  119. const std::string& config,
  120. const std::string& fileConfig,
  121. bool firstForConfig);
  122. void WriteTargetDependInfo(std::string const& lang,
  123. const std::string& config);
  124. void EmitSwiftDependencyInfo(cmSourceFile const* source,
  125. const std::string& config);
  126. void ExportObjectCompileCommand(
  127. std::string const& language, std::string const& sourceFileName,
  128. std::string const& objectDir, std::string const& objectFileName,
  129. std::string const& objectFileDir, std::string const& flags,
  130. std::string const& defines, std::string const& includes,
  131. std::string const& outputConfig);
  132. void AdditionalCleanFiles(const std::string& config);
  133. cmNinjaDeps GetObjects(const std::string& config) const;
  134. void EnsureDirectoryExists(const std::string& dir) const;
  135. void EnsureParentDirectoryExists(const std::string& path) const;
  136. // write rules for macOS Application Bundle content.
  137. struct MacOSXContentGeneratorType
  138. : cmOSXBundleGenerator::MacOSXContentGeneratorType
  139. {
  140. MacOSXContentGeneratorType(cmNinjaTargetGenerator* g,
  141. std::string fileConfig)
  142. : Generator(g)
  143. , FileConfig(std::move(fileConfig))
  144. {
  145. }
  146. void operator()(cmSourceFile const& source, const char* pkgloc,
  147. const std::string& config) override;
  148. private:
  149. cmNinjaTargetGenerator* Generator;
  150. std::string FileConfig;
  151. };
  152. friend struct MacOSXContentGeneratorType;
  153. // Properly initialized by sub-classes.
  154. std::unique_ptr<cmOSXBundleGenerator> OSXBundleGenerator;
  155. std::set<std::string> MacContentFolders;
  156. void addPoolNinjaVariable(const std::string& pool_property,
  157. cmGeneratorTarget* target, cmNinjaVars& vars);
  158. bool ForceResponseFile();
  159. private:
  160. cmLocalNinjaGenerator* LocalGenerator;
  161. struct ByConfig
  162. {
  163. /// List of object files for this target.
  164. cmNinjaDeps Objects;
  165. // Fortran Support
  166. std::map<std::string, cmNinjaDeps> DDIFiles;
  167. // Swift Support
  168. Json::Value SwiftOutputMap;
  169. std::vector<cmCustomCommand const*> CustomCommands;
  170. cmNinjaDeps ExtraFiles;
  171. std::unique_ptr<MacOSXContentGeneratorType> MacOSXContentGenerator;
  172. };
  173. std::map<std::string, ByConfig> Configs;
  174. };