cmNinjaTargetGenerator.h 7.8 KB

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