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. #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. bool NeedExplicitPreprocessing(std::string const& lang) const;
  57. std::string LanguageDyndepRule(std::string const& lang,
  58. const std::string& config) const;
  59. bool NeedDyndep(std::string const& lang) const;
  60. bool UsePreprocessedSource(std::string const& lang) const;
  61. bool CompilePreprocessedSourceWithDefines(std::string const& lang) const;
  62. std::string OrderDependsTargetForTarget(const std::string& config);
  63. std::string ComputeOrderDependsForTarget();
  64. /**
  65. * Compute the flags for compilation of object files for a given @a language.
  66. * @note Generally it is the value of the variable whose name is computed
  67. * by LanguageFlagsVarName().
  68. */
  69. std::string ComputeFlagsForObject(cmSourceFile const* source,
  70. const std::string& language,
  71. const std::string& config);
  72. void AddIncludeFlags(std::string& flags, std::string const& lang,
  73. const std::string& config) override;
  74. std::string ComputeDefines(cmSourceFile const* source,
  75. const std::string& language,
  76. const std::string& config);
  77. std::string ComputeIncludes(cmSourceFile const* source,
  78. const std::string& language,
  79. const std::string& config);
  80. std::string ConvertToNinjaPath(const std::string& path) const
  81. {
  82. return this->GetGlobalGenerator()->ConvertToNinjaPath(path);
  83. }
  84. cmGlobalNinjaGenerator::MapToNinjaPathImpl MapToNinjaPath() const
  85. {
  86. return this->GetGlobalGenerator()->MapToNinjaPath();
  87. }
  88. /// @return the list of link dependency for the given target @a target.
  89. cmNinjaDeps ComputeLinkDeps(const std::string& linkLanguage,
  90. const std::string& config,
  91. bool ignoreType = false) const;
  92. /// @return the source file path for the given @a source.
  93. std::string GetSourceFilePath(cmSourceFile const* source) const;
  94. /// @return the object file path for the given @a source.
  95. std::string GetObjectFilePath(cmSourceFile const* source,
  96. const std::string& config) const;
  97. /// @return the preprocessed source file path for the given @a source.
  98. std::string GetPreprocessedFilePath(cmSourceFile const* source,
  99. const std::string& config) const;
  100. /// @return the dyndep file path for this target.
  101. std::string GetDyndepFilePath(std::string const& lang,
  102. const std::string& config) const;
  103. /// @return the target dependency scanner info file path
  104. std::string GetTargetDependInfoPath(std::string const& lang,
  105. const std::string& config) const;
  106. /// @return the file path where the target named @a name is generated.
  107. std::string GetTargetFilePath(const std::string& name,
  108. const std::string& config) const;
  109. /// @return the output path for the target.
  110. virtual std::string GetTargetOutputDir(const std::string& config) const;
  111. void WriteLanguageRules(const std::string& language,
  112. const std::string& config);
  113. void WriteCompileRule(const std::string& language,
  114. const std::string& config);
  115. void WriteObjectBuildStatements(const std::string& config,
  116. const std::string& fileConfig,
  117. bool firstForConfig);
  118. void WriteObjectBuildStatement(cmSourceFile const* source,
  119. const std::string& config,
  120. const std::string& fileConfig,
  121. bool firstForConfig);
  122. void WriteTargetDependInfo(std::string const& lang,
  123. const std::string& config);
  124. void EmitSwiftDependencyInfo(cmSourceFile const* source,
  125. const std::string& config);
  126. void ExportObjectCompileCommand(
  127. std::string const& language, std::string const& sourceFileName,
  128. std::string const& objectDir, std::string const& objectFileName,
  129. std::string const& objectFileDir, std::string const& flags,
  130. std::string const& defines, std::string const& includes);
  131. void AdditionalCleanFiles(const std::string& config);
  132. cmNinjaDeps GetObjects(const std::string& config) const;
  133. void EnsureDirectoryExists(const std::string& dir) const;
  134. void EnsureParentDirectoryExists(const std::string& path) const;
  135. // write rules for macOS Application Bundle content.
  136. struct MacOSXContentGeneratorType
  137. : cmOSXBundleGenerator::MacOSXContentGeneratorType
  138. {
  139. MacOSXContentGeneratorType(cmNinjaTargetGenerator* g,
  140. std::string fileConfig)
  141. : Generator(g)
  142. , FileConfig(std::move(fileConfig))
  143. {
  144. }
  145. void operator()(cmSourceFile const& source, const char* pkgloc,
  146. const std::string& config) override;
  147. private:
  148. cmNinjaTargetGenerator* Generator;
  149. std::string FileConfig;
  150. };
  151. friend struct MacOSXContentGeneratorType;
  152. // Properly initialized by sub-classes.
  153. std::unique_ptr<cmOSXBundleGenerator> OSXBundleGenerator;
  154. std::set<std::string> MacContentFolders;
  155. void addPoolNinjaVariable(const std::string& pool_property,
  156. cmGeneratorTarget* target, cmNinjaVars& vars);
  157. bool ForceResponseFile();
  158. private:
  159. cmLocalNinjaGenerator* LocalGenerator;
  160. struct ByConfig
  161. {
  162. /// List of object files for this target.
  163. cmNinjaDeps Objects;
  164. // Fortran Support
  165. std::map<std::string, cmNinjaDeps> DDIFiles;
  166. // Swift Support
  167. Json::Value SwiftOutputMap;
  168. std::vector<cmCustomCommand const*> CustomCommands;
  169. cmNinjaDeps ExtraFiles;
  170. std::unique_ptr<MacOSXContentGeneratorType> MacOSXContentGenerator;
  171. };
  172. std::map<std::string, ByConfig> Configs;
  173. };