cmFileAPI.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. CMakeFiles,
  43. InternalTest
  44. };
  45. /** Identify one object kind and major version. */
  46. struct Object
  47. {
  48. ObjectKind Kind;
  49. unsigned long Version = 0;
  50. friend bool operator<(Object const& l, Object const& r)
  51. {
  52. if (l.Kind != r.Kind) {
  53. return l.Kind < r.Kind;
  54. }
  55. return l.Version < r.Version;
  56. }
  57. };
  58. /** Represent content of a query directory. */
  59. struct Query
  60. {
  61. /** Known object kind-version pairs. */
  62. std::vector<Object> Known;
  63. /** Unknown object kind names. */
  64. std::vector<std::string> Unknown;
  65. };
  66. /** Represent one request in a client 'query.json'. */
  67. struct ClientRequest : public Object
  68. {
  69. /** Empty if request is valid, else the error string. */
  70. std::string Error;
  71. };
  72. /** Represent the "requests" in a client 'query.json'. */
  73. struct ClientRequests : public std::vector<ClientRequest>
  74. {
  75. /** Empty if requests field is valid, else the error string. */
  76. std::string Error;
  77. };
  78. /** Represent the content of a client query.json file. */
  79. struct ClientQueryJson
  80. {
  81. /** The error string if parsing failed, else empty. */
  82. std::string Error;
  83. /** The 'query.json' object "client" member if it exists, else null. */
  84. Json::Value ClientValue;
  85. /** The 'query.json' object "requests" member if it exists, else null. */
  86. Json::Value RequestsValue;
  87. /** Requests extracted from 'query.json'. */
  88. ClientRequests Requests;
  89. };
  90. /** Represent content of a client query directory. */
  91. struct ClientQuery
  92. {
  93. /** The content of the client query directory except 'query.json'. */
  94. Query DirQuery;
  95. /** True if 'query.json' exists. */
  96. bool HaveQueryJson = false;
  97. /** The 'query.json' content. */
  98. ClientQueryJson QueryJson;
  99. };
  100. /** Whether the top-level query directory exists at all. */
  101. bool QueryExists = false;
  102. /** The content of the top-level query directory. */
  103. Query TopQuery;
  104. /** The content of each "client-$client" query directory. */
  105. std::map<std::string, ClientQuery> ClientQueries;
  106. /** Reply index object generated for object kind/version.
  107. This populates the "objects" field of the reply index. */
  108. std::map<Object, Json::Value> ReplyIndexObjects;
  109. std::unique_ptr<Json::CharReader> JsonReader;
  110. std::unique_ptr<Json::StreamWriter> JsonWriter;
  111. bool ReadJsonFile(std::string const& file, Json::Value& value,
  112. std::string& error);
  113. std::string WriteJsonFile(
  114. Json::Value const& value, std::string const& prefix,
  115. std::string (*computeSuffix)(std::string const&) = ComputeSuffixHash);
  116. static std::string ComputeSuffixHash(std::string const&);
  117. static std::string ComputeSuffixTime(std::string const&);
  118. static bool ReadQuery(std::string const& query,
  119. std::vector<Object>& objects);
  120. void ReadClient(std::string const& client);
  121. void ReadClientQuery(std::string const& client, ClientQueryJson& q);
  122. Json::Value BuildReplyIndex();
  123. Json::Value BuildCMake();
  124. Json::Value BuildReply(Query const& q);
  125. static Json::Value BuildReplyError(std::string const& error);
  126. Json::Value const& AddReplyIndexObject(Object const& o);
  127. static const char* ObjectKindName(ObjectKind kind);
  128. static std::string ObjectName(Object const& o);
  129. Json::Value BuildObject(Object const& object);
  130. ClientRequests BuildClientRequests(Json::Value const& requests);
  131. ClientRequest BuildClientRequest(Json::Value const& request);
  132. Json::Value BuildClientReply(ClientQuery const& q);
  133. Json::Value BuildClientReplyResponses(ClientRequests const& requests);
  134. Json::Value BuildClientReplyResponse(ClientRequest const& request);
  135. struct RequestVersion
  136. {
  137. unsigned int Major = 0;
  138. unsigned int Minor = 0;
  139. };
  140. static bool ReadRequestVersions(Json::Value const& version,
  141. std::vector<RequestVersion>& versions,
  142. std::string& error);
  143. static bool ReadRequestVersion(Json::Value const& version, bool inArray,
  144. std::vector<RequestVersion>& result,
  145. std::string& error);
  146. static std::string NoSupportedVersion(
  147. std::vector<RequestVersion> const& versions);
  148. void BuildClientRequestCodeModel(
  149. ClientRequest& r, std::vector<RequestVersion> const& versions);
  150. Json::Value BuildCodeModel(Object const& object);
  151. void BuildClientRequestCache(ClientRequest& r,
  152. std::vector<RequestVersion> const& versions);
  153. Json::Value BuildCache(Object const& object);
  154. void BuildClientRequestCMakeFiles(
  155. ClientRequest& r, std::vector<RequestVersion> const& versions);
  156. Json::Value BuildCMakeFiles(Object const& object);
  157. void BuildClientRequestInternalTest(
  158. ClientRequest& r, std::vector<RequestVersion> const& versions);
  159. Json::Value BuildInternalTest(Object const& object);
  160. };
  161. #endif