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