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