1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
5 #include "cmConfigure.h" // IWYU pragma: keep
10 #include <unordered_set>
13 #include <cm3p/json/reader.h>
14 #include <cm3p/json/value.h>
15 #include <cm3p/json/writer.h>
24 /** Read fileapi queries from disk. */
27 /** Write fileapi replies to disk. */
30 /** Get the "cmake" instance with which this was constructed. */
31 cmake* GetCMakeInstance() const { return this->CMakeInstance; }
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);
38 /** Report file-api capabilities for cmake -E capabilities. */
39 static Json::Value ReportCapabilities();
44 /** The api/v1 directory location. */
47 /** The set of files we have just written to the reply directory. */
48 std::unordered_set<std::string> ReplyFiles;
50 static std::vector<std::string> LoadDir(std::string const& dir);
51 void RemoveOldReplyFiles();
53 // Keep in sync with ObjectKindName.
63 /** Identify one object kind and major version. */
67 unsigned long Version = 0;
68 friend bool operator<(Object const& l, Object const& r)
70 if (l.Kind != r.Kind) {
71 return l.Kind < r.Kind;
73 return l.Version < r.Version;
77 /** Represent content of a query directory. */
80 /** Known object kind-version pairs. */
81 std::vector<Object> Known;
82 /** Unknown object kind names. */
83 std::vector<std::string> Unknown;
86 /** Represent one request in a client 'query.json'. */
87 struct ClientRequest : public Object
89 /** Empty if request is valid, else the error string. */
93 /** Represent the "requests" in a client 'query.json'. */
94 struct ClientRequests : public std::vector<ClientRequest>
96 /** Empty if requests field is valid, else the error string. */
100 /** Represent the content of a client query.json file. */
101 struct ClientQueryJson
103 /** The error string if parsing failed, else empty. */
106 /** The 'query.json' object "client" member if it exists, else null. */
107 Json::Value ClientValue;
109 /** The 'query.json' object "requests" member if it exists, else null. */
110 Json::Value RequestsValue;
112 /** Requests extracted from 'query.json'. */
113 ClientRequests Requests;
116 /** Represent content of a client query directory. */
119 /** The content of the client query directory except 'query.json'. */
122 /** True if 'query.json' exists. */
123 bool HaveQueryJson = false;
125 /** The 'query.json' content. */
126 ClientQueryJson QueryJson;
129 /** Whether the top-level query directory exists at all. */
130 bool QueryExists = false;
132 /** The content of the top-level query directory. */
135 /** The content of each "client-$client" query directory. */
136 std::map<std::string, ClientQuery> ClientQueries;
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;
142 std::unique_ptr<Json::CharReader> JsonReader;
143 std::unique_ptr<Json::StreamWriter> JsonWriter;
145 bool ReadJsonFile(std::string const& file, Json::Value& value,
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&);
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);
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);
165 static const char* ObjectKindName(ObjectKind kind);
166 static std::string ObjectName(Object const& o);
168 static Json::Value BuildVersion(unsigned int major, unsigned int minor);
170 Json::Value BuildObject(Object const& object);
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);
178 struct RequestVersion
180 unsigned int Major = 0;
181 unsigned int Minor = 0;
183 static bool ReadRequestVersions(Json::Value const& version,
184 std::vector<RequestVersion>& versions,
186 static bool ReadRequestVersion(Json::Value const& version, bool inArray,
187 std::vector<RequestVersion>& result,
189 static std::string NoSupportedVersion(
190 std::vector<RequestVersion> const& versions);
192 void BuildClientRequestCodeModel(
193 ClientRequest& r, std::vector<RequestVersion> const& versions);
194 Json::Value BuildCodeModel(Object const& object);
196 void BuildClientRequestCache(ClientRequest& r,
197 std::vector<RequestVersion> const& versions);
198 Json::Value BuildCache(Object const& object);
200 void BuildClientRequestCMakeFiles(
201 ClientRequest& r, std::vector<RequestVersion> const& versions);
202 Json::Value BuildCMakeFiles(Object const& object);
204 void BuildClientRequestToolchains(
205 ClientRequest& r, std::vector<RequestVersion> const& versions);
206 Json::Value BuildToolchains(Object const& object);
208 void BuildClientRequestInternalTest(
209 ClientRequest& r, std::vector<RequestVersion> const& versions);
210 Json::Value BuildInternalTest(Object const& object);