cmFileAPI.h 7.3 KB

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