1
0

cmFileAPI.h 7.2 KB

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