cmFileAPI.h 7.5 KB

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