Merge branch 'release-3.29'
[kiteware-cmake.git] / Source / cmNinjaTargetGenerator.h
blob2bfed80b56bf61af3ae893bcec66060e40910883
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
5 #include "cmConfigure.h" // IWYU pragma: keep
7 #include <map>
8 #include <memory>
9 #include <set>
10 #include <string>
11 #include <utility>
12 #include <vector>
14 #include <cm3p/json/value.h>
16 #include "cmCommonTargetGenerator.h"
17 #include "cmGlobalNinjaGenerator.h"
18 #include "cmImportedCxxModuleInfo.h"
19 #include "cmNinjaTypes.h"
20 #include "cmOSXBundleGenerator.h"
22 class cmGeneratedFileStream;
23 class cmGeneratorTarget;
24 class cmLocalNinjaGenerator;
25 class cmMakefile;
26 class cmSourceFile;
28 class cmNinjaTargetGenerator : public cmCommonTargetGenerator
30 public:
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);
38 /// Destructor.
39 ~cmNinjaTargetGenerator() override;
41 virtual void Generate(const std::string& config) = 0;
43 std::string GetTargetName() const;
45 protected:
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 enum class WithScanning
68 No,
69 Yes,
71 std::string LanguageCompilerRule(const std::string& lang,
72 const std::string& config,
73 WithScanning withScanning) const;
74 std::string LanguagePreprocessAndScanRule(std::string const& lang,
75 const std::string& config) const;
76 std::string LanguageScanRule(std::string const& lang,
77 const std::string& config) const;
78 std::string LanguageDyndepRule(std::string const& lang,
79 const std::string& config) const;
80 bool NeedExplicitPreprocessing(std::string const& lang) const;
81 bool CompileWithDefines(std::string const& lang) const;
83 std::string OrderDependsTargetForTarget(const std::string& config);
84 std::string OrderDependsTargetForTargetPrivate(const std::string& config);
86 std::string ComputeOrderDependsForTarget();
88 /**
89 * Compute the flags for compilation of object files for a given @a language.
90 * @note Generally it is the value of the variable whose name is computed
91 * by LanguageFlagsVarName().
93 std::string ComputeFlagsForObject(cmSourceFile const* source,
94 const std::string& language,
95 const std::string& config,
96 const std::string& objectFileName);
98 void AddIncludeFlags(std::string& flags, std::string const& lang,
99 const std::string& config) override;
101 std::string ComputeDefines(cmSourceFile const* source,
102 const std::string& language,
103 const std::string& config);
105 std::string ComputeIncludes(cmSourceFile const* source,
106 const std::string& language,
107 const std::string& config);
109 std::string const& ConvertToNinjaPath(const std::string& path) const
111 return this->GetGlobalGenerator()->ConvertToNinjaPath(path);
113 cmGlobalNinjaGenerator::MapToNinjaPathImpl MapToNinjaPath() const
115 return this->GetGlobalGenerator()->MapToNinjaPath();
118 std::string ConvertToNinjaAbsPath(std::string path) const
120 return this->GetGlobalGenerator()->ConvertToNinjaAbsPath(std::move(path));
123 /// @return the list of link dependency for the given target @a target.
124 cmNinjaDeps ComputeLinkDeps(const std::string& linkLanguage,
125 const std::string& config,
126 bool ignoreType = false) const;
128 /// @return the source file path for the given @a source.
129 std::string GetCompiledSourceNinjaPath(cmSourceFile const* source) const;
131 std::string GetObjectFileDir(const std::string& config) const;
132 /// @return the object file path for the given @a source.
133 std::string GetObjectFilePath(cmSourceFile const* source,
134 const std::string& config) const;
135 std::string GetBmiFilePath(cmSourceFile const* source,
136 const std::string& config) const;
138 /// @return the preprocessed source file path for the given @a source.
139 std::string GetPreprocessedFilePath(cmSourceFile const* source,
140 const std::string& config) const;
142 /// @return the clang-tidy replacements file path for the given @a source.
143 std::string GetClangTidyReplacementsFilePath(
144 std::string const& directory, cmSourceFile const& source,
145 std::string const& config) const override;
147 /// @return the dyndep file path for this target.
148 std::string GetDyndepFilePath(std::string const& lang,
149 const std::string& config) const;
151 /// @return the target dependency scanner info file path
152 std::string GetTargetDependInfoPath(std::string const& lang,
153 const std::string& config) const;
155 /// @return the file path where the target named @a name is generated.
156 std::string GetTargetFilePath(const std::string& name,
157 const std::string& config) const;
159 /// @return the output path for the target.
160 virtual std::string GetTargetOutputDir(const std::string& config) const;
162 void WriteLanguageRules(const std::string& language,
163 const std::string& config);
164 void WriteCompileRule(const std::string& language,
165 const std::string& config);
166 void WriteCompileRule(const std::string& language, const std::string& config,
167 WithScanning withScanning);
168 void WriteObjectBuildStatements(const std::string& config,
169 const std::string& fileConfig,
170 bool firstForConfig);
171 void WriteCxxModuleBmiBuildStatement(cmSourceFile const* source,
172 const std::string& config,
173 const std::string& fileConfig,
174 bool firstForConfig);
175 void WriteSwiftObjectBuildStatement(
176 std::vector<cmSourceFile const*> const& sources, const std::string& config,
177 const std::string& fileConfig, bool firstForConfig);
178 void WriteObjectBuildStatement(cmSourceFile const* source,
179 const std::string& config,
180 const std::string& fileConfig,
181 bool firstForConfig);
182 void WriteTargetDependInfo(std::string const& lang,
183 const std::string& config);
185 void EmitSwiftDependencyInfo(cmSourceFile const* source,
186 const std::string& config);
188 void GenerateSwiftOutputFileMap(const std::string& config,
189 std::string& flags);
191 void ExportObjectCompileCommand(
192 std::string const& language, std::string const& sourceFileName,
193 std::string const& objectDir, std::string const& objectFileName,
194 std::string const& objectFileDir, std::string const& flags,
195 std::string const& defines, std::string const& includes,
196 std::string const& targetCompilePdb, std::string const& targetPdb,
197 std::string const& outputConfig, WithScanning withScanning);
199 void ExportSwiftObjectCompileCommand(
200 std::vector<cmSourceFile const*> const& moduleSourceFiles,
201 std::string const& moduleObjectFilename, std::string const& flags,
202 std::string const& defines, std::string const& includes,
203 std::string const& outputConfig, bool singleOutput);
205 void AdditionalCleanFiles(const std::string& config);
207 cmNinjaDeps GetObjects(const std::string& config) const;
209 void EnsureDirectoryExists(const std::string& dir) const;
210 void EnsureParentDirectoryExists(const std::string& path) const;
212 // write rules for macOS Application Bundle content.
213 struct MacOSXContentGeneratorType
214 : cmOSXBundleGenerator::MacOSXContentGeneratorType
216 MacOSXContentGeneratorType(cmNinjaTargetGenerator* g,
217 std::string fileConfig)
218 : Generator(g)
219 , FileConfig(std::move(fileConfig))
223 void operator()(cmSourceFile const& source, const char* pkgloc,
224 const std::string& config) override;
226 private:
227 cmNinjaTargetGenerator* Generator;
228 std::string FileConfig;
230 friend struct MacOSXContentGeneratorType;
232 // Properly initialized by sub-classes.
233 std::unique_ptr<cmOSXBundleGenerator> OSXBundleGenerator;
234 std::set<std::string> MacContentFolders;
236 void addPoolNinjaVariable(const std::string& pool_property,
237 cmGeneratorTarget* target, cmNinjaVars& vars);
239 bool ForceResponseFile();
241 private:
242 cmLocalNinjaGenerator* LocalGenerator;
243 bool HasPrivateGeneratedSources = false;
245 struct ScanningFiles
247 bool IsEmpty() const
249 return this->ScanningOutput.empty() && this->ModuleMapFile.empty();
252 std::string ScanningOutput;
253 std::string ModuleMapFile;
256 struct ByConfig
258 /// List of object files for this target.
259 cmNinjaDeps Objects;
260 // Dyndep Support
261 std::map<std::string, std::vector<ScanningFiles>> ScanningInfo;
262 // Imported C++ module info.
263 mutable ImportedCxxModuleLookup ImportedCxxModules;
264 // Swift Support
265 Json::Value SwiftOutputMap;
266 cmNinjaDeps ExtraFiles;
267 std::unique_ptr<MacOSXContentGeneratorType> MacOSXContentGenerator;
270 std::map<std::string, ByConfig> Configs;