Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Source / cmQtAutoGenInitializer.h
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
5 #include "cmConfigure.h" // IWYU pragma: keep
6
7 #include <cstddef>
8 #include <memory>
9 #include <set>
10 #include <string>
11 #include <unordered_map>
12 #include <unordered_set>
13 #include <utility>
14 #include <vector>
15
16 #include <cm/string_view>
17
18 #include "cmFilePathChecksum.h"
19 #include "cmQtAutoGen.h"
20
21 class cmGeneratorTarget;
22 class cmGlobalGenerator;
23 class cmLocalGenerator;
24 class cmMakefile;
25 class cmQtAutoGenGlobalInitializer;
26 class cmSourceFile;
27 class cmTarget;
28
29 /** \class cmQtAutoGenerator
30  * \brief Initializes the QtAutoGen generators
31  */
32 class cmQtAutoGenInitializer : public cmQtAutoGen
33 {
34 public:
35   /** String value with per configuration variants.  */
36   class ConfigString
37   {
38   public:
39     std::string Default;
40     std::unordered_map<std::string, std::string> Config;
41   };
42
43   /** String values with per configuration variants.  */
44   template <typename C>
45   class ConfigStrings
46   {
47   public:
48     C Default;
49     std::unordered_map<std::string, C> Config;
50   };
51
52   /** rcc job.  */
53   class Qrc
54   {
55   public:
56     std::string LockFile;
57     std::string QrcFile;
58     std::string QrcName;
59     std::string QrcPathChecksum;
60     std::string InfoFile;
61     ConfigString SettingsFile;
62     std::string OutputFile;
63     bool Generated = false;
64     bool Unique = false;
65     std::vector<std::string> Options;
66     std::vector<std::string> Resources;
67   };
68
69   /** moc and/or uic file.  */
70   struct MUFile
71   {
72     std::string FullPath;
73     cmSourceFile* SF = nullptr;
74     std::vector<size_t> Configs;
75     bool Generated = false;
76     bool SkipMoc = false;
77     bool SkipUic = false;
78     bool MocIt = false;
79     bool UicIt = false;
80   };
81   using MUFileHandle = std::unique_ptr<MUFile>;
82
83   /** Abstract moc/uic/rcc generator variables base class.  */
84   struct GenVarsT
85   {
86     bool Enabled = false;
87     // Generator type/name
88     GenT Gen;
89     cm::string_view GenNameUpper;
90     // Executable
91     std::string ExecutableTargetName;
92     cmGeneratorTarget* ExecutableTarget = nullptr;
93     std::string Executable;
94     CompilerFeaturesHandle ExecutableFeatures;
95
96     GenVarsT(GenT gen)
97       : Gen(gen)
98       , GenNameUpper(cmQtAutoGen::GeneratorNameUpper(gen))
99     {
100     }
101   };
102
103   /** @param mocExecutable The file path to the moc executable. Will be used as
104      fallback to query the version
105       @return The detected Qt version and the required Qt major version. */
106   static std::pair<IntegerVersion, unsigned int> GetQtVersion(
107     cmGeneratorTarget const* genTarget, std::string mocExecutable);
108
109   cmQtAutoGenInitializer(cmQtAutoGenGlobalInitializer* globalInitializer,
110                          cmGeneratorTarget* genTarget,
111                          IntegerVersion const& qtVersion, bool mocEnabled,
112                          bool uicEnabled, bool rccEnabled,
113                          bool globalAutogenTarget, bool globalAutoRccTarget);
114
115   bool InitCustomTargets();
116   bool SetupCustomTargets();
117
118 private:
119   /** If moc or uic is enabled, the autogen target will be generated.  */
120   bool MocOrUicEnabled() const
121   {
122     return (this->Moc.Enabled || this->Uic.Enabled);
123   }
124
125   bool InitMoc();
126   bool InitUic();
127   bool InitRcc();
128
129   bool InitScanFiles();
130   bool InitAutogenTarget();
131   bool InitRccTargets();
132
133   bool SetupWriteAutogenInfo();
134   bool SetupWriteRccInfo();
135
136   cmSourceFile* RegisterGeneratedSource(std::string const& filename);
137   cmSourceFile* AddGeneratedSource(std::string const& filename,
138                                    GenVarsT const& genVars,
139                                    bool prepend = false);
140   void AddGeneratedSource(ConfigString const& filename,
141                           GenVarsT const& genVars, bool prepend = false);
142   void AddToSourceGroup(std::string const& fileName,
143                         cm::string_view genNameUpper);
144   void AddCleanFile(std::string const& fileName);
145
146   void ConfigFileNames(ConfigString& configString, cm::string_view prefix,
147                        cm::string_view suffix);
148   void ConfigFileNamesAndGenex(ConfigString& configString, std::string& genex,
149                                cm::string_view prefix, cm::string_view suffix);
150   void ConfigFileClean(ConfigString& configString);
151
152   std::string GetMocBuildPath(MUFile const& muf);
153
154   bool GetQtExecutable(GenVarsT& genVars, const std::string& executable,
155                        bool ignoreMissingTarget) const;
156
157   void handleSkipPch(cmSourceFile* sf);
158
159   cmQtAutoGenGlobalInitializer* GlobalInitializer = nullptr;
160   cmGeneratorTarget* GenTarget = nullptr;
161   cmGlobalGenerator* GlobalGen = nullptr;
162   cmLocalGenerator* LocalGen = nullptr;
163   cmMakefile* Makefile = nullptr;
164   cmFilePathChecksum const PathCheckSum;
165
166   // -- Configuration
167   IntegerVersion QtVersion;
168   unsigned int Verbosity = 0;
169   bool MultiConfig = false;
170   bool CMP0071Accept = false;
171   bool CMP0071Warn = false;
172   bool CMP0100Accept = false;
173   bool CMP0100Warn = false;
174   std::string ConfigDefault;
175   std::vector<std::string> ConfigsList;
176   std::string TargetsFolder;
177
178   /** Common directories.  */
179   struct
180   {
181     std::string Info;
182     std::string Build;
183     std::string RelativeBuild;
184     std::string Work;
185     ConfigString Include;
186     std::string IncludeGenExp;
187   } Dir;
188
189   /** Autogen target variables.  */
190   struct
191   {
192     std::string Name;
193     bool GlobalTarget = false;
194     // Settings
195     unsigned int Parallel = 1;
196     // Configuration files
197     std::string InfoFile;
198     ConfigString SettingsFile;
199     ConfigString ParseCacheFile;
200     // Dependencies
201     bool DependOrigin = false;
202     std::set<std::string> DependFiles;
203     std::set<cmTarget*> DependTargets;
204     std::string DepFile;
205     std::string DepFileRuleName;
206     // Sources to process
207     std::unordered_map<cmSourceFile*, MUFileHandle> Headers;
208     std::unordered_map<cmSourceFile*, MUFileHandle> Sources;
209     std::vector<MUFile*> FilesGenerated;
210     std::vector<cmSourceFile*> CMP0100HeadersWarn;
211   } AutogenTarget;
212
213   /** moc variables.  */
214   struct MocT : public GenVarsT
215   {
216     MocT()
217       : GenVarsT(GenT::MOC)
218     {
219     }
220
221     bool RelaxedMode = false;
222     bool PathPrefix = false;
223     ConfigString CompilationFile;
224     std::string CompilationFileGenex;
225     // Compiler implicit pre defines
226     std::vector<std::string> PredefsCmd;
227     ConfigString PredefsFile;
228     // Defines
229     ConfigStrings<std::set<std::string>> Defines;
230     // Includes
231     ConfigStrings<std::vector<std::string>> Includes;
232     // Options
233     std::vector<std::string> Options;
234     // Filters
235     std::vector<std::string> MacroNames;
236     std::vector<std::pair<std::string, std::string>> DependFilters;
237     // Utility
238     std::unordered_set<std::string> EmittedBuildPaths;
239   } Moc;
240
241   /** uic variables.  */
242   struct UicT : public GenVarsT
243   {
244     using UiFileT = std::pair<std::string, std::vector<std::string>>;
245
246     UicT()
247       : GenVarsT(GenT::UIC)
248     {
249     }
250
251     std::set<std::string> SkipUi;
252     std::vector<std::string> UiFilesNoOptions;
253     std::vector<UiFileT> UiFilesWithOptions;
254     ConfigStrings<std::vector<std::string>> Options;
255     std::vector<std::string> SearchPaths;
256     std::vector<std::pair<ConfigString /*ui header*/, std::string /*genex*/>>
257       UiHeaders;
258   } Uic;
259
260   /** rcc variables.  */
261   struct RccT : public GenVarsT
262   {
263     RccT()
264       : GenVarsT(GenT::RCC)
265     {
266     }
267
268     bool GlobalTarget = false;
269     std::vector<Qrc> Qrcs;
270   } Rcc;
271 };