1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
5 #include "cmConfigure.h" // IWYU pragma: keep
14 #include <cm3p/json/value.h>
16 #include "cmCommonTargetGenerator.h"
17 #include "cmGlobalNinjaGenerator.h"
18 #include "cmNinjaTypes.h"
19 #include "cmOSXBundleGenerator.h"
21 class cmCustomCommand;
22 class cmGeneratedFileStream;
23 class cmGeneratorTarget;
24 class cmLocalNinjaGenerator;
28 class cmNinjaTargetGenerator : public cmCommonTargetGenerator
31 /// Create a cmNinjaTargetGenerator according to the @a target's type.
32 static std::unique_ptr<cmNinjaTargetGenerator> New(
33 cmGeneratorTarget* target);
35 /// Build a NinjaTargetGenerator.
36 cmNinjaTargetGenerator(cmGeneratorTarget* target);
39 ~cmNinjaTargetGenerator() override;
41 virtual void Generate(const std::string& config) = 0;
43 std::string GetTargetName() const;
46 bool SetMsvcTargetPdbVariable(cmNinjaVars&, const std::string& config) const;
48 cmGeneratedFileStream& GetImplFileStream(const std::string& config) const;
49 cmGeneratedFileStream& GetCommonFileStream() const;
50 cmGeneratedFileStream& GetRulesFileStream() const;
52 cmGeneratorTarget* GetGeneratorTarget() const
54 return this->GeneratorTarget;
57 cmLocalNinjaGenerator* GetLocalGenerator() const
59 return this->LocalGenerator;
62 cmGlobalNinjaGenerator* GetGlobalGenerator() const;
64 cmMakefile* GetMakefile() const { return this->Makefile; }
66 std::string LanguageCompilerRule(const std::string& lang,
67 const std::string& config) const;
68 std::string LanguagePreprocessAndScanRule(std::string const& lang,
69 const std::string& config) const;
70 std::string LanguageScanRule(std::string const& lang,
71 const std::string& config) const;
72 std::string LanguageDyndepRule(std::string const& lang,
73 const std::string& config) const;
74 bool NeedDyndep(std::string const& lang, std::string const& config) const;
75 bool NeedExplicitPreprocessing(std::string const& lang) const;
76 bool CompileWithDefines(std::string const& lang) const;
77 bool NeedCxxModuleSupport(std::string const& lang,
78 std::string const& config) const;
80 std::string OrderDependsTargetForTarget(const std::string& config);
82 std::string ComputeOrderDependsForTarget();
85 * Compute the flags for compilation of object files for a given @a language.
86 * @note Generally it is the value of the variable whose name is computed
87 * by LanguageFlagsVarName().
89 std::string ComputeFlagsForObject(cmSourceFile const* source,
90 const std::string& language,
91 const std::string& config);
93 void AddIncludeFlags(std::string& flags, std::string const& lang,
94 const std::string& config) override;
96 std::string ComputeDefines(cmSourceFile const* source,
97 const std::string& language,
98 const std::string& config);
100 std::string ComputeIncludes(cmSourceFile const* source,
101 const std::string& language,
102 const std::string& config);
104 std::string const& ConvertToNinjaPath(const std::string& path) const
106 return this->GetGlobalGenerator()->ConvertToNinjaPath(path);
108 cmGlobalNinjaGenerator::MapToNinjaPathImpl MapToNinjaPath() const
110 return this->GetGlobalGenerator()->MapToNinjaPath();
113 std::string ConvertToNinjaAbsPath(std::string path) const
115 return this->GetGlobalGenerator()->ConvertToNinjaAbsPath(std::move(path));
118 /// @return the list of link dependency for the given target @a target.
119 cmNinjaDeps ComputeLinkDeps(const std::string& linkLanguage,
120 const std::string& config,
121 bool ignoreType = false) const;
123 /// @return the source file path for the given @a source.
124 std::string GetCompiledSourceNinjaPath(cmSourceFile const* source) const;
126 /// @return the object file path for the given @a source.
127 std::string GetObjectFilePath(cmSourceFile const* source,
128 const std::string& config) const;
130 /// @return the preprocessed source file path for the given @a source.
131 std::string GetPreprocessedFilePath(cmSourceFile const* source,
132 const std::string& config) const;
134 /// @return the dyndep file path for this target.
135 std::string GetDyndepFilePath(std::string const& lang,
136 const std::string& config) const;
138 /// @return the target dependency scanner info file path
139 std::string GetTargetDependInfoPath(std::string const& lang,
140 const std::string& config) const;
142 /// @return the file path where the target named @a name is generated.
143 std::string GetTargetFilePath(const std::string& name,
144 const std::string& config) const;
146 /// @return the output path for the target.
147 virtual std::string GetTargetOutputDir(const std::string& config) const;
149 void WriteLanguageRules(const std::string& language,
150 const std::string& config);
151 void WriteCompileRule(const std::string& language,
152 const std::string& config);
153 void WriteObjectBuildStatements(const std::string& config,
154 const std::string& fileConfig,
155 bool firstForConfig);
156 void WriteObjectBuildStatement(cmSourceFile const* source,
157 const std::string& config,
158 const std::string& fileConfig,
159 bool firstForConfig);
160 void WriteTargetDependInfo(std::string const& lang,
161 const std::string& config);
163 void EmitSwiftDependencyInfo(cmSourceFile const* source,
164 const std::string& config);
166 void ExportObjectCompileCommand(
167 std::string const& language, std::string const& sourceFileName,
168 std::string const& objectDir, std::string const& objectFileName,
169 std::string const& objectFileDir, std::string const& flags,
170 std::string const& defines, std::string const& includes,
171 std::string const& outputConfig);
173 void AdditionalCleanFiles(const std::string& config);
175 cmNinjaDeps GetObjects(const std::string& config) const;
177 void EnsureDirectoryExists(const std::string& dir) const;
178 void EnsureParentDirectoryExists(const std::string& path) const;
180 // write rules for macOS Application Bundle content.
181 struct MacOSXContentGeneratorType
182 : cmOSXBundleGenerator::MacOSXContentGeneratorType
184 MacOSXContentGeneratorType(cmNinjaTargetGenerator* g,
185 std::string fileConfig)
187 , FileConfig(std::move(fileConfig))
191 void operator()(cmSourceFile const& source, const char* pkgloc,
192 const std::string& config) override;
195 cmNinjaTargetGenerator* Generator;
196 std::string FileConfig;
198 friend struct MacOSXContentGeneratorType;
200 // Properly initialized by sub-classes.
201 std::unique_ptr<cmOSXBundleGenerator> OSXBundleGenerator;
202 std::set<std::string> MacContentFolders;
204 void addPoolNinjaVariable(const std::string& pool_property,
205 cmGeneratorTarget* target, cmNinjaVars& vars);
207 bool ForceResponseFile();
210 cmLocalNinjaGenerator* LocalGenerator;
214 /// List of object files for this target.
217 std::map<std::string, cmNinjaDeps> DDIFiles;
219 Json::Value SwiftOutputMap;
220 std::vector<cmCustomCommand const*> CustomCommands;
221 cmNinjaDeps ExtraFiles;
222 std::unique_ptr<MacOSXContentGeneratorType> MacOSXContentGenerator;
225 std::map<std::string, ByConfig> Configs;