CMake Nightly Date Stamp
[kiteware-cmake.git] / Source / cmQtAutoGenerator.h
blob4b15fc783a0ac31abd67d5a0d891ce321dac191b
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 <istream>
8 #include <mutex>
9 #include <string>
10 #include <unordered_set>
11 #include <vector>
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
25 public:
26 // -- Types
28 /** Thread safe logger. */
29 class Logger
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;
54 private:
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. */
63 struct ProjectDirsT
65 std::string Source;
66 std::string Binary;
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);
81 // -- Constructors
82 cmQtAutoGenerator(GenT genType);
83 virtual ~cmQtAutoGenerator();
85 cmQtAutoGenerator(cmQtAutoGenerator const&) = delete;
86 cmQtAutoGenerator& operator=(cmQtAutoGenerator const&) = delete;
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 std::string const& ExecutableConfig() const
95 return this->ExecutableConfig_;
98 // -- Info file parsing
99 /** Info file reader class. */
100 class InfoT
102 public:
103 InfoT(cmQtAutoGenerator& gen)
104 : Gen_(gen)
108 /** Read json data from a stream. */
109 bool Read(std::istream& istr);
111 /** Returns false if the JSON value isn't a string. */
112 bool GetString(std::string const& key, std::string& value,
113 bool required) const;
114 bool GetStringConfig(std::string const& key, std::string& value,
115 bool required) const;
116 bool GetBool(std::string const& key, bool& value, bool required) const;
117 bool GetUInt(std::string const& key, unsigned int& value,
118 bool required) const;
119 /** Returns false if the JSON value isn't an array. */
120 bool GetArray(std::string const& key, std::vector<std::string>& list,
121 bool required) const;
122 bool GetArray(std::string const& key,
123 std::unordered_set<std::string>& list, bool required) const;
124 bool GetArrayConfig(std::string const& key, std::vector<std::string>& list,
125 bool required) const;
127 Json::Value const& GetValue(std::string const& key) const
129 return this->Json_[key];
132 /** Returns true if strings were appended to the list. */
133 static bool GetJsonArray(std::vector<std::string>& list,
134 Json::Value const& jval);
135 /** Returns true if strings were found in the JSON array. */
136 static bool GetJsonArray(std::unordered_set<std::string>& list,
137 Json::Value const& jval);
139 bool LogError(GenT genType, cm::string_view message) const;
140 bool LogError(cm::string_view message) const;
142 private:
143 std::string ConfigKey(cm::string_view key) const;
145 Json::Value Json_;
146 cmQtAutoGenerator& Gen_;
149 // -- Settings file
150 static std::string SettingsFind(cm::string_view content,
151 cm::string_view key);
153 // -- Directories
154 ProjectDirsT const& ProjectDirs() const { return this->ProjectDirs_; }
155 std::string MessagePath(cm::string_view path) const;
157 // -- Run
158 bool Run(cm::string_view infoFile, cm::string_view config,
159 cm::string_view executableConfig);
161 protected:
162 // -- Abstract processing interface
163 virtual bool InitFromInfo(InfoT const& info) = 0;
164 virtual bool Process() = 0;
165 // - Utility classes
166 Logger const& Log() const { return this->Logger_; }
168 private:
169 // -- Generator type
170 GenT GenType_;
171 // -- Logging
172 Logger Logger_;
173 // -- Info file
174 std::string InfoFile_;
175 std::string InfoDir_;
176 cmFileTime InfoFileTime_;
177 std::string InfoConfig_;
178 std::string ExecutableConfig_;
179 // -- Directories
180 ProjectDirsT ProjectDirs_;