cmFileAPI.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 long> 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. private:
  53. cmake* CMakeInstance;
  54. /** The api/v1 directory location. */
  55. std::string APIv1;
  56. /** api/v1 directory in the user's shared CMake config directory. */
  57. std::string UserAPIv1;
  58. /** The set of files we have just written to the reply directory. */
  59. std::unordered_set<std::string> ReplyFiles;
  60. static std::vector<std::string> LoadDir(std::string const& dir);
  61. void RemoveOldReplyFiles();
  62. /** Identify one object kind and major version. */
  63. struct Object
  64. {
  65. ObjectKind Kind;
  66. unsigned long Version = 0;
  67. friend bool operator<(Object const& l, Object const& r)
  68. {
  69. if (l.Kind != r.Kind) {
  70. return l.Kind < r.Kind;
  71. }
  72. return l.Version < r.Version;
  73. }
  74. friend bool operator==(Object const& l, Object const& r)
  75. {
  76. return l.Kind == r.Kind && l.Version == r.Version;
  77. }
  78. friend bool operator!=(Object const& l, Object const& r)
  79. {
  80. return !(l == r);
  81. }
  82. };
  83. /** Represent content of a query directory. */
  84. struct Query
  85. {
  86. /** Known object kind-version pairs. */
  87. std::vector<Object> Known;
  88. /** Unknown object kind names. */
  89. std::vector<std::string> Unknown;
  90. };
  91. /** Represent one request in a client 'query.json'. */
  92. struct ClientRequest : public Object
  93. {
  94. /** Empty if request is valid, else the error string. */
  95. std::string Error;
  96. };
  97. /** Represent the "requests" in a client 'query.json'. */
  98. struct ClientRequests : public std::vector<ClientRequest>
  99. {
  100. /** Empty if requests field is valid, else the error string. */
  101. std::string Error;
  102. };
  103. /** Represent the content of a client query.json file. */
  104. struct ClientQueryJson
  105. {
  106. /** The error string if parsing failed, else empty. */
  107. std::string Error;
  108. /** The 'query.json' object "client" member if it exists, else null. */
  109. Json::Value ClientValue;
  110. /** The 'query.json' object "requests" member if it exists, else null. */
  111. Json::Value RequestsValue;
  112. /** Requests extracted from 'query.json'. */
  113. ClientRequests Requests;
  114. };
  115. /** Represent content of a client query directory. */
  116. struct ClientQuery
  117. {
  118. /** The content of the client query directory except 'query.json'. */
  119. Query DirQuery;
  120. /** True if 'query.json' exists. */
  121. bool HaveQueryJson = false;
  122. /** The 'query.json' content. */
  123. ClientQueryJson QueryJson;
  124. };
  125. /** Whether the top-level query directory exists at all. */
  126. bool QueryExists = false;
  127. /** The content of the top-level query directory. */
  128. Query TopQuery;
  129. /** The content of each "client-$client" query directory. */
  130. std::map<std::string, ClientQuery> ClientQueries;
  131. /** Reply index object generated for object kind/version.
  132. This populates the "objects" field of the reply index. */
  133. std::map<Object, Json::Value> ReplyIndexObjects;
  134. /** Identify the situation in which WriteReplies was called. */
  135. IndexFor ReplyIndexFor = IndexFor::Success;
  136. std::unique_ptr<Json::CharReader> JsonReader;
  137. std::unique_ptr<Json::StreamWriter> JsonWriter;
  138. bool ReadJsonFile(std::string const& file, Json::Value& value,
  139. std::string& error);
  140. std::string WriteJsonFile(
  141. Json::Value const& value, std::string const& prefix,
  142. std::string (*computeSuffix)(std::string const&) = ComputeSuffixHash);
  143. static std::string ComputeSuffixHash(std::string const&);
  144. static std::string ComputeSuffixTime(std::string const&);
  145. static bool ReadQuery(std::string const& query,
  146. std::vector<Object>& objects);
  147. void ReadClient(std::string const& client);
  148. void ReadClientQuery(std::string const& client, ClientQueryJson& q);
  149. Json::Value BuildReplyIndex();
  150. Json::Value BuildCMake();
  151. Json::Value BuildReply(Query const& q);
  152. Json::Value BuildReplyEntry(Object const& object);
  153. static Json::Value BuildReplyError(std::string const& error);
  154. Json::Value const& AddReplyIndexObject(Object const& o);
  155. static char const* ObjectKindName(ObjectKind kind);
  156. static std::string ObjectName(Object const& o);
  157. static Json::Value BuildVersion(unsigned int major, unsigned int minor);
  158. Json::Value BuildObject(Object const& object);
  159. ClientRequests BuildClientRequests(Json::Value const& requests);
  160. ClientRequest BuildClientRequest(Json::Value const& request);
  161. Json::Value BuildClientReply(ClientQuery const& q);
  162. Json::Value BuildClientReplyResponses(ClientRequests const& requests);
  163. Json::Value BuildClientReplyResponse(ClientRequest const& request);
  164. struct RequestVersion
  165. {
  166. unsigned int Major = 0;
  167. unsigned int Minor = 0;
  168. };
  169. static bool ReadRequestVersions(Json::Value const& version,
  170. std::vector<RequestVersion>& versions,
  171. std::string& error);
  172. static bool ReadRequestVersion(Json::Value const& version, bool inArray,
  173. std::vector<RequestVersion>& result,
  174. std::string& error);
  175. static std::string NoSupportedVersion(
  176. std::vector<RequestVersion> const& versions);
  177. void BuildClientRequestCodeModel(
  178. ClientRequest& r, std::vector<RequestVersion> const& versions);
  179. Json::Value BuildCodeModel(Object const& object);
  180. void BuildClientRequestConfigureLog(
  181. ClientRequest& r, std::vector<RequestVersion> const& versions);
  182. Json::Value BuildConfigureLog(Object const& object);
  183. void BuildClientRequestCache(ClientRequest& r,
  184. std::vector<RequestVersion> const& versions);
  185. Json::Value BuildCache(Object const& object);
  186. void BuildClientRequestCMakeFiles(
  187. ClientRequest& r, std::vector<RequestVersion> const& versions);
  188. Json::Value BuildCMakeFiles(Object const& object);
  189. void BuildClientRequestToolchains(
  190. ClientRequest& r, std::vector<RequestVersion> const& versions);
  191. Json::Value BuildToolchains(Object const& object);
  192. void BuildClientRequestInternalTest(
  193. ClientRequest& r, std::vector<RequestVersion> const& versions);
  194. Json::Value BuildInternalTest(Object const& object);
  195. };