cmFileAPI.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmFileAPI_h
  4. #define cmFileAPI_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include "cm_jsoncpp_reader.h"
  7. #include "cm_jsoncpp_value.h"
  8. #include "cm_jsoncpp_writer.h"
  9. #include <map>
  10. #include <memory> // IWYU pragma: keep
  11. #include <string>
  12. #include <unordered_set>
  13. #include <vector>
  14. class cmake;
  15. class cmFileAPI
  16. {
  17. public:
  18. cmFileAPI(cmake* cm);
  19. /** Read fileapi queries from disk. */
  20. void ReadQueries();
  21. /** Write fileapi replies to disk. */
  22. void WriteReplies();
  23. /** Get the "cmake" instance with which this was constructed. */
  24. cmake* GetCMakeInstance() const { return this->CMakeInstance; }
  25. /** Convert a JSON object or array into an object with a single
  26. "jsonFile" member specifying a file named with the given prefix
  27. and holding the original object. Other JSON types are unchanged. */
  28. Json::Value MaybeJsonFile(Json::Value in, std::string const& prefix);
  29. private:
  30. cmake* CMakeInstance;
  31. /** The api/v1 directory location. */
  32. std::string APIv1;
  33. /** The set of files we have just written to the reply directory. */
  34. std::unordered_set<std::string> ReplyFiles;
  35. static std::vector<std::string> LoadDir(std::string const& dir);
  36. void RemoveOldReplyFiles();
  37. // Keep in sync with ObjectKindName.
  38. enum class ObjectKind
  39. {
  40. CodeModel,
  41. Cache,
  42. InternalTest
  43. };
  44. /** Identify one object kind and major version. */
  45. struct Object
  46. {
  47. ObjectKind Kind;
  48. unsigned long Version = 0;
  49. friend bool operator<(Object const& l, Object const& r)
  50. {
  51. if (l.Kind != r.Kind) {
  52. return l.Kind < r.Kind;
  53. }
  54. return l.Version < r.Version;
  55. }
  56. };
  57. /** Represent content of a query directory. */
  58. struct Query
  59. {
  60. /** Known object kind-version pairs. */
  61. std::vector<Object> Known;
  62. /** Unknown object kind names. */
  63. std::vector<std::string> Unknown;
  64. };
  65. /** Represent one request in a client 'query.json'. */
  66. struct ClientRequest : public Object
  67. {
  68. /** Empty if request is valid, else the error string. */
  69. std::string Error;
  70. };
  71. /** Represent the "requests" in a client 'query.json'. */
  72. struct ClientRequests : public std::vector<ClientRequest>
  73. {
  74. /** Empty if requests field is valid, else the error string. */
  75. std::string Error;
  76. };
  77. /** Represent the content of a client query.json file. */
  78. struct ClientQueryJson
  79. {
  80. /** The error string if parsing failed, else empty. */
  81. std::string Error;
  82. /** The 'query.json' object "client" member if it exists, else null. */
  83. Json::Value ClientValue;
  84. /** The 'query.json' object "requests" member if it exists, else null. */
  85. Json::Value RequestsValue;
  86. /** Requests extracted from 'query.json'. */
  87. ClientRequests Requests;
  88. };
  89. /** Represent content of a client query directory. */
  90. struct ClientQuery
  91. {
  92. /** The content of the client query directory except 'query.json'. */
  93. Query DirQuery;
  94. /** True if 'query.json' exists. */
  95. bool HaveQueryJson = false;
  96. /** The 'query.json' content. */
  97. ClientQueryJson QueryJson;
  98. };
  99. /** Whether the top-level query directory exists at all. */
  100. bool QueryExists = false;
  101. /** The content of the top-level query directory. */
  102. Query TopQuery;
  103. /** The content of each "client-$client" query directory. */
  104. std::map<std::string, ClientQuery> ClientQueries;
  105. /** Reply index object generated for object kind/version.
  106. This populates the "objects" field of the reply index. */
  107. std::map<Object, Json::Value> ReplyIndexObjects;
  108. std::unique_ptr<Json::CharReader> JsonReader;
  109. std::unique_ptr<Json::StreamWriter> JsonWriter;
  110. bool ReadJsonFile(std::string const& file, Json::Value& value,
  111. std::string& error);
  112. std::string WriteJsonFile(
  113. Json::Value const& value, std::string const& prefix,
  114. std::string (*computeSuffix)(std::string const&) = ComputeSuffixHash);
  115. static std::string ComputeSuffixHash(std::string const&);
  116. static std::string ComputeSuffixTime(std::string const&);
  117. static bool ReadQuery(std::string const& query,
  118. std::vector<Object>& objects);
  119. void ReadClient(std::string const& client);
  120. void ReadClientQuery(std::string const& client, ClientQueryJson& q);
  121. Json::Value BuildReplyIndex();
  122. Json::Value BuildCMake();
  123. Json::Value BuildReply(Query const& q);
  124. static Json::Value BuildReplyError(std::string const& error);
  125. Json::Value const& AddReplyIndexObject(Object const& o);
  126. static const char* ObjectKindName(ObjectKind kind);
  127. static std::string ObjectName(Object const& o);
  128. Json::Value BuildObject(Object const& object);
  129. ClientRequests BuildClientRequests(Json::Value const& requests);
  130. ClientRequest BuildClientRequest(Json::Value const& request);
  131. Json::Value BuildClientReply(ClientQuery const& q);
  132. Json::Value BuildClientReplyResponses(ClientRequests const& requests);
  133. Json::Value BuildClientReplyResponse(ClientRequest const& request);
  134. struct RequestVersion
  135. {
  136. unsigned int Major = 0;
  137. unsigned int Minor = 0;
  138. };
  139. static bool ReadRequestVersions(Json::Value const& version,
  140. std::vector<RequestVersion>& versions,
  141. std::string& error);
  142. static bool ReadRequestVersion(Json::Value const& version, bool inArray,
  143. std::vector<RequestVersion>& result,
  144. std::string& error);
  145. static std::string NoSupportedVersion(
  146. std::vector<RequestVersion> const& versions);
  147. void BuildClientRequestCodeModel(
  148. ClientRequest& r, std::vector<RequestVersion> const& versions);
  149. Json::Value BuildCodeModel(Object const& object);
  150. void BuildClientRequestCache(ClientRequest& r,
  151. std::vector<RequestVersion> const& versions);
  152. Json::Value BuildCache(Object const& object);
  153. void BuildClientRequestInternalTest(
  154. ClientRequest& r, std::vector<RequestVersion> const& versions);
  155. Json::Value BuildInternalTest(Object const& object);
  156. };
  157. #endif