resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmQtAutoGenerator.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 <istream>
8 #include <mutex>
9 #include <string>
10 #include <unordered_set>
11 #include <vector>
12
13 #include <cm/string_view>
14
15 #include <cm3p/json/value.h>
16
17 #include "cmFileTime.h"
18 #include "cmQtAutoGen.h"
19
20 /** \class cmQtAutoGenerator
21  * \brief Base class for QtAutoGen generators
22  */
23 class cmQtAutoGenerator : public cmQtAutoGen
24 {
25 public:
26   // -- Types
27
28   /** Thread safe logger.  */
29   class Logger
30   {
31   public:
32     // -- Construction
33     Logger();
34     ~Logger() = default;
35     // -- Verbosity
36     unsigned int Verbosity() const { return this->Verbosity_; }
37     void SetVerbosity(unsigned int value) { this->Verbosity_ = value; }
38     void RaiseVerbosity(unsigned int value);
39     bool Verbose() const { return (this->Verbosity_ != 0); }
40     void SetVerbose(bool value) { this->Verbosity_ = value ? 1 : 0; }
41     // -- Color output
42     bool ColorOutput() const { return this->ColorOutput_; }
43     void SetColorOutput(bool value);
44     // -- Log info
45     void Info(GenT genType, cm::string_view message) const;
46     // -- Log warning
47     void Warning(GenT genType, cm::string_view message) const;
48     // -- Log error
49     void Error(GenT genType, cm::string_view message) const;
50     void ErrorCommand(GenT genType, cm::string_view message,
51                       std::vector<std::string> const& command,
52                       std::string const& output) const;
53
54   private:
55     static std::string HeadLine(cm::string_view title);
56
57     mutable std::mutex Mutex_;
58     unsigned int Verbosity_ = 0;
59     bool ColorOutput_ = false;
60   };
61
62   /** Project directories.  */
63   struct ProjectDirsT
64   {
65     std::string Source;
66     std::string Binary;
67     std::string CurrentSource;
68     std::string CurrentBinary;
69   };
70
71   // -- File system methods
72   static bool MakeParentDirectory(std::string const& filename);
73   static bool FileRead(std::string& content, std::string const& filename,
74                        std::string* error = nullptr);
75   static bool FileWrite(std::string const& filename,
76                         std::string const& content,
77                         std::string* error = nullptr);
78   static bool FileDiffers(std::string const& filename,
79                           std::string const& content);
80
81   // -- Constructors
82   cmQtAutoGenerator(GenT genType);
83   virtual ~cmQtAutoGenerator();
84
85   cmQtAutoGenerator(cmQtAutoGenerator const&) = delete;
86   cmQtAutoGenerator& operator=(cmQtAutoGenerator const&) = delete;
87
88   // -- Info options
89   std::string const& InfoFile() const { return this->InfoFile_; }
90   std::string const& InfoDir() const { return this->InfoDir_; }
91   cmFileTime const& InfoFileTime() const { return this->InfoFileTime_; }
92   std::string const& InfoConfig() const { return this->InfoConfig_; }
93
94   // -- Info file parsing
95   /** Info file reader class. */
96   class InfoT
97   {
98   public:
99     InfoT(cmQtAutoGenerator& gen)
100       : Gen_(gen)
101     {
102     }
103
104     /** Read json data from a stream.  */
105     bool Read(std::istream& istr);
106
107     /** Returns false if the JSON value isn't a string.  */
108     bool GetString(std::string const& key, std::string& value,
109                    bool required) const;
110     bool GetStringConfig(std::string const& key, std::string& value,
111                          bool required) const;
112     bool GetBool(std::string const& key, bool& value, bool required) const;
113     bool GetUInt(std::string const& key, unsigned int& value,
114                  bool required) const;
115     /** Returns false if the JSON value isn't an array.  */
116     bool GetArray(std::string const& key, std::vector<std::string>& list,
117                   bool required) const;
118     bool GetArray(std::string const& key,
119                   std::unordered_set<std::string>& list, bool required) const;
120     bool GetArrayConfig(std::string const& key, std::vector<std::string>& list,
121                         bool required) const;
122
123     Json::Value const& GetValue(std::string const& key) const
124     {
125       return this->Json_[key];
126     }
127
128     /** Returns true if strings were appended to the list.  */
129     static bool GetJsonArray(std::vector<std::string>& list,
130                              Json::Value const& jval);
131     /** Returns true if strings were found in the JSON array.  */
132     static bool GetJsonArray(std::unordered_set<std::string>& list,
133                              Json::Value const& jval);
134
135     bool LogError(GenT genType, cm::string_view message) const;
136     bool LogError(cm::string_view message) const;
137
138   private:
139     std::string ConfigKey(cm::string_view key) const;
140
141     Json::Value Json_;
142     cmQtAutoGenerator& Gen_;
143   };
144
145   // -- Settings file
146   static std::string SettingsFind(cm::string_view content,
147                                   cm::string_view key);
148
149   // -- Directories
150   ProjectDirsT const& ProjectDirs() const { return this->ProjectDirs_; }
151   std::string MessagePath(cm::string_view path) const;
152
153   // -- Run
154   bool Run(cm::string_view infoFile, cm::string_view config);
155
156 protected:
157   // -- Abstract processing interface
158   virtual bool InitFromInfo(InfoT const& info) = 0;
159   virtual bool Process() = 0;
160   // - Utility classes
161   Logger const& Log() const { return this->Logger_; }
162
163 private:
164   // -- Generator type
165   GenT GenType_;
166   // -- Logging
167   Logger Logger_;
168   // -- Info file
169   std::string InfoFile_;
170   std::string InfoDir_;
171   cmFileTime InfoFileTime_;
172   std::string InfoConfig_;
173   // -- Directories
174   ProjectDirsT ProjectDirs_;
175 };