cmQtAutoGenerator.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. // -- Info file parsing
  80. /** Info file reader class. */
  81. class InfoT
  82. {
  83. public:
  84. InfoT(cmQtAutoGenerator& gen)
  85. : Gen_(gen)
  86. {
  87. }
  88. /** Read json data from a stream. */
  89. bool Read(std::istream& istr);
  90. /** Returns false if the JSON value isn't a string. */
  91. bool GetString(std::string const& key, std::string& value,
  92. bool required) const;
  93. bool GetStringConfig(std::string const& key, std::string& value,
  94. bool required) const;
  95. bool GetBool(std::string const& key, bool& value, bool required) const;
  96. bool GetUInt(std::string const& key, unsigned int& value,
  97. bool required) const;
  98. /** Returns false if the JSON value isn't an array. */
  99. bool GetArray(std::string const& key, std::vector<std::string>& list,
  100. bool required) const;
  101. bool GetArray(std::string const& key,
  102. std::unordered_set<std::string>& list, bool required) const;
  103. bool GetArrayConfig(std::string const& key, std::vector<std::string>& list,
  104. bool required) const;
  105. Json::Value const& GetValue(std::string const& key) const
  106. {
  107. return this->Json_[key];
  108. }
  109. /** Returns true if strings were appended to the list. */
  110. static bool GetJsonArray(std::vector<std::string>& list,
  111. Json::Value const& jval);
  112. /** Returns true if strings were found in the JSON array. */
  113. static bool GetJsonArray(std::unordered_set<std::string>& list,
  114. Json::Value const& jval);
  115. bool LogError(GenT genType, cm::string_view message) const;
  116. bool LogError(cm::string_view message) const;
  117. private:
  118. std::string ConfigKey(cm::string_view key) const;
  119. Json::Value Json_;
  120. cmQtAutoGenerator& Gen_;
  121. };
  122. // -- Settings file
  123. static std::string SettingsFind(cm::string_view content,
  124. cm::string_view key);
  125. // -- Directories
  126. ProjectDirsT const& ProjectDirs() const { return this->ProjectDirs_; }
  127. std::string MessagePath(cm::string_view path) const;
  128. // -- Run
  129. bool Run(cm::string_view infoFile, cm::string_view config);
  130. protected:
  131. // -- Abstract processing interface
  132. virtual bool InitFromInfo(InfoT const& info) = 0;
  133. virtual bool Process() = 0;
  134. // - Utility classes
  135. Logger const& Log() const { return this->Logger_; }
  136. private:
  137. // -- Generator type
  138. GenT GenType_;
  139. // -- Logging
  140. Logger Logger_;
  141. // -- Info file
  142. std::string InfoFile_;
  143. std::string InfoDir_;
  144. cmFileTime InfoFileTime_;
  145. std::string InfoConfig_;
  146. // -- Directories
  147. ProjectDirsT ProjectDirs_;
  148. };