cmQtAutoGenerator.h 5.6 KB

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