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