You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
6.4 KiB

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