cmNinjaTargetGenerator.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "cm_jsoncpp_value.h"
  7. #include "cmCommonTargetGenerator.h"
  8. #include "cmGlobalNinjaGenerator.h"
  9. #include "cmNinjaTypes.h"
  10. #include "cmOSXBundleGenerator.h"
  11. #include <map>
  12. #include <memory>
  13. #include <set>
  14. #include <string>
  15. #include <vector>
  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() = 0;
  33. std::string GetTargetName() const;
  34. bool NeedDepTypeMSVC(const std::string& lang) const;
  35. protected:
  36. bool SetMsvcTargetPdbVariable(cmNinjaVars&) const;
  37. cmGeneratedFileStream& GetBuildFileStream() 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) const;
  50. std::string LanguagePreprocessRule(std::string const& lang) const;
  51. bool NeedExplicitPreprocessing(std::string const& lang) const;
  52. std::string LanguageDyndepRule(std::string const& lang) const;
  53. bool NeedDyndep(std::string const& lang) const;
  54. bool UsePreprocessedSource(std::string const& lang) const;
  55. std::string OrderDependsTargetForTarget();
  56. std::string ComputeOrderDependsForTarget();
  57. /**
  58. * Compute the flags for compilation of object files for a given @a language.
  59. * @note Generally it is the value of the variable whose name is computed
  60. * by LanguageFlagsVarName().
  61. */
  62. std::string ComputeFlagsForObject(cmSourceFile const* source,
  63. const std::string& language);
  64. void AddIncludeFlags(std::string& flags, std::string const& lang) override;
  65. std::string ComputeDefines(cmSourceFile const* source,
  66. const std::string& language);
  67. std::string ComputeIncludes(cmSourceFile const* source,
  68. const std::string& language);
  69. std::string ConvertToNinjaPath(const std::string& path) const
  70. {
  71. return this->GetGlobalGenerator()->ConvertToNinjaPath(path);
  72. }
  73. cmGlobalNinjaGenerator::MapToNinjaPathImpl MapToNinjaPath() const
  74. {
  75. return this->GetGlobalGenerator()->MapToNinjaPath();
  76. }
  77. /// @return the list of link dependency for the given target @a target.
  78. cmNinjaDeps ComputeLinkDeps(const std::string& linkLanguage) const;
  79. /// @return the source file path for the given @a source.
  80. std::string GetSourceFilePath(cmSourceFile const* source) const;
  81. /// @return the object file path for the given @a source.
  82. std::string GetObjectFilePath(cmSourceFile const* source) const;
  83. /// @return the preprocessed source file path for the given @a source.
  84. std::string GetPreprocessedFilePath(cmSourceFile const* source) const;
  85. /// @return the dyndep file path for this target.
  86. std::string GetDyndepFilePath(std::string const& lang) const;
  87. /// @return the target dependency scanner info file path
  88. std::string GetTargetDependInfoPath(std::string const& lang) const;
  89. /// @return the file path where the target named @a name is generated.
  90. std::string GetTargetFilePath(const std::string& name) const;
  91. /// @return the output path for the target.
  92. virtual std::string GetTargetOutputDir() const;
  93. void WriteLanguageRules(const std::string& language);
  94. void WriteCompileRule(const std::string& language);
  95. void WriteObjectBuildStatements();
  96. void WriteObjectBuildStatement(cmSourceFile const* source);
  97. void WriteTargetDependInfo(std::string const& lang);
  98. void EmitSwiftDependencyInfo(cmSourceFile const* source);
  99. void ExportObjectCompileCommand(
  100. std::string const& language, std::string const& sourceFileName,
  101. std::string const& objectDir, std::string const& objectFileName,
  102. std::string const& objectFileDir, std::string const& flags,
  103. std::string const& defines, std::string const& includes);
  104. void AdditionalCleanFiles();
  105. cmNinjaDeps GetObjects() const { return this->Objects; }
  106. void EnsureDirectoryExists(const std::string& dir) const;
  107. void EnsureParentDirectoryExists(const std::string& path) const;
  108. // write rules for macOS Application Bundle content.
  109. struct MacOSXContentGeneratorType
  110. : cmOSXBundleGenerator::MacOSXContentGeneratorType
  111. {
  112. MacOSXContentGeneratorType(cmNinjaTargetGenerator* g)
  113. : Generator(g)
  114. {
  115. }
  116. void operator()(cmSourceFile const& source, const char* pkgloc) override;
  117. private:
  118. cmNinjaTargetGenerator* Generator;
  119. };
  120. friend struct MacOSXContentGeneratorType;
  121. std::unique_ptr<MacOSXContentGeneratorType> MacOSXContentGenerator;
  122. // Properly initialized by sub-classes.
  123. std::unique_ptr<cmOSXBundleGenerator> OSXBundleGenerator;
  124. std::set<std::string> MacContentFolders;
  125. void addPoolNinjaVariable(const std::string& pool_property,
  126. cmGeneratorTarget* target, cmNinjaVars& vars);
  127. bool ForceResponseFile();
  128. private:
  129. cmLocalNinjaGenerator* LocalGenerator;
  130. /// List of object files for this target.
  131. cmNinjaDeps Objects;
  132. // Fortran Support
  133. std::map<std::string, cmNinjaDeps> DDIFiles;
  134. // Swift Support
  135. Json::Value SwiftOutputMap;
  136. std::vector<cmCustomCommand const*> CustomCommands;
  137. cmNinjaDeps ExtraFiles;
  138. };
  139. #endif // ! cmNinjaTargetGenerator_h