/src/CMake/Source/cmNinjaTargetGenerator.h
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | #pragma once |
4 | | |
5 | | #include "cmConfigure.h" // IWYU pragma: keep |
6 | | |
7 | | #include <map> |
8 | | #include <memory> |
9 | | #include <set> |
10 | | #include <string> |
11 | | #include <utility> |
12 | | #include <vector> |
13 | | |
14 | | #include <cm/string_view> |
15 | | |
16 | | #include <cm3p/json/value.h> |
17 | | |
18 | | #include "cmCommonTargetGenerator.h" |
19 | | #include "cmGlobalNinjaGenerator.h" |
20 | | #include "cmImportedCxxModuleInfo.h" |
21 | | #include "cmNinjaTypes.h" |
22 | | #include "cmOSXBundleGenerator.h" |
23 | | |
24 | | class cmGeneratedFileStream; |
25 | | class cmGeneratorTarget; |
26 | | class cmLocalNinjaGenerator; |
27 | | class cmMakefile; |
28 | | class cmSourceFile; |
29 | | |
30 | | class cmNinjaTargetGenerator : public cmCommonTargetGenerator |
31 | | { |
32 | | public: |
33 | | /// Create a cmNinjaTargetGenerator according to the @a target's type. |
34 | | static std::unique_ptr<cmNinjaTargetGenerator> New( |
35 | | cmGeneratorTarget* target); |
36 | | |
37 | | /// Build a NinjaTargetGenerator. |
38 | | cmNinjaTargetGenerator(cmGeneratorTarget* target); |
39 | | |
40 | | /// Destructor. |
41 | | ~cmNinjaTargetGenerator() override; |
42 | | |
43 | | virtual void Generate(std::string const& config) = 0; |
44 | | |
45 | | std::string GetTargetName() const; |
46 | | |
47 | | std::string ConvertToOutputFormatForShell(cm::string_view path) const; |
48 | | |
49 | | void AddDepfileBinding(cmNinjaVars& vars, std::string depfile) const; |
50 | | void RemoveDepfileBinding(cmNinjaVars& vars) const; |
51 | | |
52 | | protected: |
53 | | bool SetMsvcTargetPdbVariable(cmNinjaVars&, std::string const& config) const; |
54 | | |
55 | | cmGeneratedFileStream& GetImplFileStream(std::string const& config) const; |
56 | | cmGeneratedFileStream& GetCommonFileStream() const; |
57 | | cmGeneratedFileStream& GetRulesFileStream() const; |
58 | | |
59 | | cmGeneratorTarget* GetGeneratorTarget() const |
60 | 0 | { |
61 | 0 | return this->GeneratorTarget; |
62 | 0 | } |
63 | | |
64 | | cmLocalNinjaGenerator* GetLocalGenerator() const |
65 | 0 | { |
66 | 0 | return this->LocalGenerator; |
67 | 0 | } |
68 | | |
69 | | cmGlobalNinjaGenerator* GetGlobalGenerator() const; |
70 | | |
71 | 0 | cmMakefile* GetMakefile() const { return this->Makefile; } |
72 | | |
73 | | enum class WithScanning |
74 | | { |
75 | | No, |
76 | | Yes, |
77 | | }; |
78 | | std::string LanguageCompilerRule(std::string const& lang, |
79 | | std::string const& config, |
80 | | WithScanning withScanning) const; |
81 | | std::string LanguagePreprocessAndScanRule(std::string const& lang, |
82 | | std::string const& config) const; |
83 | | std::string LanguageScanRule(std::string const& lang, |
84 | | std::string const& config) const; |
85 | | std::string LanguageDyndepRule(std::string const& lang, |
86 | | std::string const& config) const; |
87 | | bool NeedExplicitPreprocessing(std::string const& lang) const; |
88 | | bool CompileWithDefines(std::string const& lang) const; |
89 | | |
90 | | std::string OrderDependsTargetForTarget(std::string const& config); |
91 | | std::string OrderDependsTargetForTargetPrivate(std::string const& config); |
92 | | |
93 | | std::string ComputeOrderDependsForTarget(); |
94 | | |
95 | | /** |
96 | | * Compute the flags for compilation of object files for a given @a language. |
97 | | * @note Generally it is the value of the variable whose name is computed |
98 | | * by LanguageFlagsVarName(). |
99 | | */ |
100 | | std::string ComputeFlagsForObject(cmSourceFile const* source, |
101 | | std::string const& language, |
102 | | std::string const& config, |
103 | | std::string const& objectFileName); |
104 | | |
105 | | void AddIncludeFlags(std::string& flags, std::string const& lang, |
106 | | std::string const& config) override; |
107 | | |
108 | | std::string ComputeDefines(cmSourceFile const* source, |
109 | | std::string const& language, |
110 | | std::string const& config); |
111 | | |
112 | | std::string ComputeIncludes(cmSourceFile const* source, |
113 | | std::string const& language, |
114 | | std::string const& config); |
115 | | |
116 | | std::string const& ConvertToNinjaPath(std::string const& path) const |
117 | 0 | { |
118 | 0 | return this->GetGlobalGenerator()->ConvertToNinjaPath(path); |
119 | 0 | } |
120 | | cmGlobalNinjaGenerator::MapToNinjaPathImpl MapToNinjaPath() const |
121 | 0 | { |
122 | 0 | return this->GetGlobalGenerator()->MapToNinjaPath(); |
123 | 0 | } |
124 | | |
125 | | std::string ConvertToNinjaAbsPath(std::string path) const |
126 | 0 | { |
127 | 0 | return this->GetGlobalGenerator()->ConvertToNinjaAbsPath(std::move(path)); |
128 | 0 | } |
129 | | |
130 | | /// @return the list of link dependency for the given target @a target. |
131 | | cmNinjaDeps ComputeLinkDeps(std::string const& linkLanguage, |
132 | | std::string const& config, |
133 | | bool ignoreType = false) const; |
134 | | |
135 | | /// @return the source file path for the given @a source. |
136 | | std::string GetCompiledSourceNinjaPath(cmSourceFile const* source) const; |
137 | | |
138 | | std::string GetObjectFileDir(std::string const& config) const; |
139 | | /// @return the object file path for the given @a source. |
140 | | std::string GetObjectFilePath(cmSourceFile const* source, |
141 | | std::string const& config) const; |
142 | | std::string GetBmiFilePath(cmSourceFile const* source, |
143 | | std::string const& config) const; |
144 | | |
145 | | /// @return the preprocessed source file path for the given @a source. |
146 | | std::string GetPreprocessedFilePath(cmSourceFile const* source, |
147 | | std::string const& config) const; |
148 | | |
149 | | /// @return the clang-tidy replacements file path for the given @a source. |
150 | | std::string GetClangTidyReplacementsFilePath( |
151 | | std::string const& directory, cmSourceFile const& source, |
152 | | std::string const& config) const override; |
153 | | |
154 | | /// @return the dyndep file path for this target. |
155 | | std::string GetDyndepFilePath(std::string const& lang, |
156 | | std::string const& config) const; |
157 | | |
158 | | /// @return the target dependency scanner info file path |
159 | | std::string GetTargetDependInfoPath(std::string const& lang, |
160 | | std::string const& config) const; |
161 | | |
162 | | /// @return the file path where the target named @a name is generated. |
163 | | std::string GetTargetFilePath(std::string const& name, |
164 | | std::string const& config) const; |
165 | | |
166 | | /// @return the output path for the target. |
167 | | virtual std::string GetTargetOutputDir(std::string const& config) const; |
168 | | |
169 | | void WriteLanguageRules(std::string const& language, |
170 | | std::string const& config); |
171 | | void WriteCompileRule(std::string const& language, |
172 | | std::string const& config); |
173 | | void WriteCompileRule(std::string const& language, std::string const& config, |
174 | | WithScanning withScanning); |
175 | | void WriteObjectBuildStatements(std::string const& config, |
176 | | std::string const& fileConfig, |
177 | | bool firstForConfig); |
178 | | void WriteCxxModuleBmiBuildStatement(cmSourceFile const* source, |
179 | | std::string const& config, |
180 | | std::string const& fileConfig, |
181 | | bool firstForConfig); |
182 | | void WriteSwiftObjectBuildStatement( |
183 | | std::vector<cmSourceFile const*> const& sources, std::string const& config, |
184 | | std::string const& fileConfig, bool firstForConfig); |
185 | | void WriteObjectBuildStatement(cmSourceFile const* source, |
186 | | std::string const& config, |
187 | | std::string const& fileConfig, |
188 | | bool firstForConfig); |
189 | | void WriteTargetDependInfo(std::string const& lang, |
190 | | std::string const& config); |
191 | | |
192 | | void EmitSwiftDependencyInfo(cmSourceFile const* source, |
193 | | std::string const& config); |
194 | | |
195 | | void GenerateSwiftOutputFileMap(std::string const& config, |
196 | | std::string& flags); |
197 | | |
198 | | void ExportObjectCompileCommand( |
199 | | std::string const& language, std::string const& sourceFileName, |
200 | | std::string const& objectDir, std::string const& targetSupportDir, |
201 | | std::string const& objectFileName, std::string const& objectFileDir, |
202 | | std::string const& flags, std::string const& defines, |
203 | | std::string const& includes, std::string const& targetCompilePdb, |
204 | | std::string const& targetPdb, std::string const& outputConfig, |
205 | | WithScanning withScanning); |
206 | | |
207 | | void ExportSwiftObjectCompileCommand( |
208 | | std::vector<cmSourceFile const*> const& moduleSourceFiles, |
209 | | std::string const& moduleObjectFilename, std::string const& flags, |
210 | | std::string const& defines, std::string const& includes, |
211 | | std::string const& outputConfig, bool singleOutput); |
212 | | |
213 | | void AdditionalCleanFiles(std::string const& config); |
214 | | |
215 | | cmNinjaDeps GetObjects(std::string const& config) const; |
216 | | |
217 | | void EnsureDirectoryExists(std::string const& dir) const; |
218 | | void EnsureParentDirectoryExists(std::string const& path) const; |
219 | | |
220 | | // write rules for macOS Application Bundle content. |
221 | | struct MacOSXContentGeneratorType |
222 | | : cmOSXBundleGenerator::MacOSXContentGeneratorType |
223 | | { |
224 | | MacOSXContentGeneratorType(cmNinjaTargetGenerator* g, |
225 | | std::string fileConfig) |
226 | 0 | : Generator(g) |
227 | 0 | , FileConfig(std::move(fileConfig)) |
228 | 0 | { |
229 | 0 | } |
230 | | |
231 | | void operator()(cmSourceFile const& source, char const* pkgloc, |
232 | | std::string const& config) override; |
233 | | |
234 | | private: |
235 | | cmNinjaTargetGenerator* Generator; |
236 | | std::string FileConfig; |
237 | | }; |
238 | | friend struct MacOSXContentGeneratorType; |
239 | | |
240 | | // Properly initialized by sub-classes. |
241 | | std::unique_ptr<cmOSXBundleGenerator> OSXBundleGenerator; |
242 | | std::set<std::string> MacContentFolders; |
243 | | |
244 | | /// @param source may be nullptr. |
245 | | void addPoolNinjaVariable(std::string const& pool_property, |
246 | | cmGeneratorTarget* target, |
247 | | cmSourceFile const* source, cmNinjaVars& vars); |
248 | | |
249 | | bool ForceResponseFile(); |
250 | | |
251 | | private: |
252 | | cmLocalNinjaGenerator* LocalGenerator; |
253 | | bool HasPrivateGeneratedSources = false; |
254 | | |
255 | | struct ScanningFiles |
256 | | { |
257 | | bool IsEmpty() const |
258 | 0 | { |
259 | 0 | return this->ScanningOutput.empty() && this->ModuleMapFile.empty(); |
260 | 0 | } |
261 | | |
262 | | std::string ScanningOutput; |
263 | | std::string ModuleMapFile; |
264 | | }; |
265 | | |
266 | | struct ByConfig |
267 | | { |
268 | | /// List of object files for this target. |
269 | | cmNinjaDeps Objects; |
270 | | // Dyndep Support |
271 | | std::map<std::string, std::vector<ScanningFiles>> ScanningInfo; |
272 | | // Imported C++ module info. |
273 | | mutable ImportedCxxModuleLookup ImportedCxxModules; |
274 | | // Swift Support |
275 | | Json::Value SwiftOutputMap; |
276 | | cmNinjaDeps ExtraFiles; |
277 | | std::unique_ptr<MacOSXContentGeneratorType> MacOSXContentGenerator; |
278 | | }; |
279 | | |
280 | | std::map<std::string, ByConfig> Configs; |
281 | | }; |