cmNinjaTargetGenerator.h 5.6 KB

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