1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
5 #include "cmConfigure.h" // IWYU pragma: keep
10 #include <unordered_set>
13 #include <cm/string_view>
15 #include <cm3p/json/value.h>
17 #include "cmFileTime.h"
18 #include "cmQtAutoGen.h"
20 /** \class cmQtAutoGenerator
21 * \brief Base class for QtAutoGen generators
23 class cmQtAutoGenerator : public cmQtAutoGen
28 /** Thread safe logger. */
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; }
42 bool ColorOutput() const { return this->ColorOutput_; }
43 void SetColorOutput(bool value);
45 void Info(GenT genType, cm::string_view message) const;
47 void Warning(GenT genType, cm::string_view message) const;
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;
55 static std::string HeadLine(cm::string_view title);
57 mutable std::mutex Mutex_;
58 unsigned int Verbosity_ = 0;
59 bool ColorOutput_ = false;
62 /** Project directories. */
67 std::string CurrentSource;
68 std::string CurrentBinary;
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);
82 cmQtAutoGenerator(GenT genType);
83 virtual ~cmQtAutoGenerator();
85 cmQtAutoGenerator(cmQtAutoGenerator const&) = delete;
86 cmQtAutoGenerator& operator=(cmQtAutoGenerator const&) = delete;
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_; }
94 // -- Info file parsing
95 /** Info file reader class. */
99 InfoT(cmQtAutoGenerator& gen)
104 /** Read json data from a stream. */
105 bool Read(std::istream& istr);
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;
123 Json::Value const& GetValue(std::string const& key) const
125 return this->Json_[key];
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);
135 bool LogError(GenT genType, cm::string_view message) const;
136 bool LogError(cm::string_view message) const;
139 std::string ConfigKey(cm::string_view key) const;
142 cmQtAutoGenerator& Gen_;
146 static std::string SettingsFind(cm::string_view content,
147 cm::string_view key);
150 ProjectDirsT const& ProjectDirs() const { return this->ProjectDirs_; }
151 std::string MessagePath(cm::string_view path) const;
154 bool Run(cm::string_view infoFile, cm::string_view config);
157 // -- Abstract processing interface
158 virtual bool InitFromInfo(InfoT const& info) = 0;
159 virtual bool Process() = 0;
161 Logger const& Log() const { return this->Logger_; }
169 std::string InfoFile_;
170 std::string InfoDir_;
171 cmFileTime InfoFileTime_;
172 std::string InfoConfig_;
174 ProjectDirsT ProjectDirs_;