cmNinjaTargetGenerator.h 5.8 KB

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