33749ba74b36f0b836260e89ceae269eb602229b
[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   cmQtAutoGenGlobalInitializer* GlobalInitializer = nullptr;
158   cmGeneratorTarget* GenTarget = nullptr;
159   cmGlobalGenerator* GlobalGen = nullptr;
160   cmLocalGenerator* LocalGen = nullptr;
161   cmMakefile* Makefile = nullptr;
162   cmFilePathChecksum const PathCheckSum;
163
164   // -- Configuration
165   IntegerVersion QtVersion;
166   unsigned int Verbosity = 0;
167   bool MultiConfig = false;
168   bool CMP0071Accept = false;
169   bool CMP0071Warn = false;
170   bool CMP0100Accept = false;
171   bool CMP0100Warn = false;
172   std::string ConfigDefault;
173   std::vector<std::string> ConfigsList;
174   std::string TargetsFolder;
175
176   /** Common directories.  */
177   struct
178   {
179     std::string Info;
180     std::string Build;
181     std::string RelativeBuild;
182     std::string Work;
183     ConfigString Include;
184     std::string IncludeGenExp;
185   } Dir;
186
187   /** Autogen target variables.  */
188   struct
189   {
190     std::string Name;
191     bool GlobalTarget = false;
192     // Settings
193     unsigned int Parallel = 1;
194     // Configuration files
195     std::string InfoFile;
196     ConfigString SettingsFile;
197     ConfigString ParseCacheFile;
198     // Dependencies
199     bool DependOrigin = false;
200     std::set<std::string> DependFiles;
201     std::set<cmTarget*> DependTargets;
202     std::string DepFile;
203     std::string DepFileRuleName;
204     // Sources to process
205     std::unordered_map<cmSourceFile*, MUFileHandle> Headers;
206     std::unordered_map<cmSourceFile*, MUFileHandle> Sources;
207     std::vector<MUFile*> FilesGenerated;
208     std::vector<cmSourceFile*> CMP0100HeadersWarn;
209   } AutogenTarget;
210
211   /** moc variables.  */
212   struct MocT : public GenVarsT
213   {
214     MocT()
215       : GenVarsT(GenT::MOC)
216     {
217     }
218
219     bool RelaxedMode = false;
220     bool PathPrefix = false;
221     ConfigString CompilationFile;
222     std::string CompilationFileGenex;
223     // Compiler implicit pre defines
224     std::vector<std::string> PredefsCmd;
225     ConfigString PredefsFile;
226     // Defines
227     ConfigStrings<std::set<std::string>> Defines;
228     // Includes
229     ConfigStrings<std::vector<std::string>> Includes;
230     // Options
231     std::vector<std::string> Options;
232     // Filters
233     std::vector<std::string> MacroNames;
234     std::vector<std::pair<std::string, std::string>> DependFilters;
235     // Utility
236     std::unordered_set<std::string> EmittedBuildPaths;
237   } Moc;
238
239   /** uic variables.  */
240   struct UicT : public GenVarsT
241   {
242     using UiFileT = std::pair<std::string, std::vector<std::string>>;
243
244     UicT()
245       : GenVarsT(GenT::UIC)
246     {
247     }
248
249     std::set<std::string> SkipUi;
250     std::vector<std::string> UiFilesNoOptions;
251     std::vector<UiFileT> UiFilesWithOptions;
252     ConfigStrings<std::vector<std::string>> Options;
253     std::vector<std::string> SearchPaths;
254     std::vector<std::pair<ConfigString /*ui header*/, std::string /*genex*/>>
255       UiHeaders;
256   } Uic;
257
258   /** rcc variables.  */
259   struct RccT : public GenVarsT
260   {
261     RccT()
262       : GenVarsT(GenT::RCC)
263     {
264     }
265
266     bool GlobalTarget = false;
267     std::vector<Qrc> Qrcs;
268   } Rcc;
269 };