1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
14 #include "cmsys/Directory.hxx"
15 #include "cmsys/FStream.hxx"
17 #include "cmCryptoHash.h"
18 #include "cmFileAPICMakeFiles.h"
19 #include "cmFileAPICache.h"
20 #include "cmFileAPICodemodel.h"
21 #include "cmFileAPIToolchains.h"
22 #include "cmGlobalGenerator.h"
23 #include "cmStringAlgorithms.h"
24 #include "cmSystemTools.h"
25 #include "cmTimestamp.h"
28 cmFileAPI::cmFileAPI(cmake* cm)
32 this->CMakeInstance->GetHomeOutputDirectory() + "/.cmake/api/v1";
34 Json::CharReaderBuilder rbuilder;
35 rbuilder["collectComments"] = false;
36 rbuilder["failIfExtra"] = true;
37 rbuilder["rejectDupKeys"] = false;
38 rbuilder["strictRoot"] = true;
40 std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
42 Json::StreamWriterBuilder wbuilder;
43 wbuilder["indentation"] = "\t";
45 std::unique_ptr<Json::StreamWriter>(wbuilder.newStreamWriter());
48 void cmFileAPI::ReadQueries()
50 std::string const query_dir = this->APIv1 + "/query";
51 this->QueryExists = cmSystemTools::FileIsDirectory(query_dir);
52 if (!this->QueryExists) {
56 // Load queries at the top level.
57 std::vector<std::string> queries = cmFileAPI::LoadDir(query_dir);
59 // Read the queries and save for later.
60 for (std::string& query : queries) {
61 if (cmHasLiteralPrefix(query, "client-")) {
62 this->ReadClient(query);
63 } else if (!cmFileAPI::ReadQuery(query, this->TopQuery.Known)) {
64 this->TopQuery.Unknown.push_back(std::move(query));
69 void cmFileAPI::WriteReplies()
71 if (this->QueryExists) {
72 cmSystemTools::MakeDirectory(this->APIv1 + "/reply");
73 this->WriteJsonFile(this->BuildReplyIndex(), "index", ComputeSuffixTime);
76 this->RemoveOldReplyFiles();
79 std::vector<std::string> cmFileAPI::LoadDir(std::string const& dir)
81 std::vector<std::string> files;
84 for (unsigned long i = 0; i < d.GetNumberOfFiles(); ++i) {
85 std::string f = d.GetFile(i);
86 if (f != "." && f != "..") {
87 files.push_back(std::move(f));
90 std::sort(files.begin(), files.end());
94 void cmFileAPI::RemoveOldReplyFiles()
96 std::string const reply_dir = this->APIv1 + "/reply";
97 std::vector<std::string> files = this->LoadDir(reply_dir);
98 for (std::string const& f : files) {
99 if (this->ReplyFiles.find(f) == this->ReplyFiles.end()) {
100 std::string file = cmStrCat(reply_dir, "/", f);
101 cmSystemTools::RemoveFile(file);
106 bool cmFileAPI::ReadJsonFile(std::string const& file, Json::Value& value,
109 std::vector<char> content;
112 if (!cmSystemTools::FileIsDirectory(file)) {
113 fin.open(file.c_str(), std::ios::binary);
115 auto finEnd = fin.rdbuf()->pubseekoff(0, std::ios::end);
117 size_t finSize = finEnd;
119 // Allocate a buffer to read the whole file.
120 content.resize(finSize);
122 // Now read the file from the beginning.
123 fin.seekg(0, std::ios::beg);
124 fin.read(content.data(), finSize);
126 fin.setstate(std::ios::failbit);
131 value = Json::Value();
132 error = "failed to read from file";
136 // Parse our buffer as json.
137 if (!this->JsonReader->parse(content.data(), content.data() + content.size(),
139 value = Json::Value();
146 std::string cmFileAPI::WriteJsonFile(
147 Json::Value const& value, std::string const& prefix,
148 std::string (*computeSuffix)(std::string const&))
150 std::string fileName;
152 // Write the json file with a temporary name.
153 std::string const& tmpFile = this->APIv1 + "/tmp.json";
154 cmsys::ofstream ftmp(tmpFile.c_str());
155 this->JsonWriter->write(value, &ftmp);
159 cmSystemTools::RemoveFile(tmpFile);
163 // Compute the final name for the file.
164 fileName = prefix + "-" + computeSuffix(tmpFile) + ".json";
166 // Create the destination.
167 std::string file = this->APIv1 + "/reply";
168 cmSystemTools::MakeDirectory(file);
172 // If the final name already exists then assume it has proper content.
173 // Otherwise, atomically place the reply file at its final name
174 if (cmSystemTools::FileExists(file, true) ||
175 !cmSystemTools::RenameFile(tmpFile, file)) {
176 cmSystemTools::RemoveFile(tmpFile);
179 // Record this among files we have just written.
180 this->ReplyFiles.insert(fileName);
185 Json::Value cmFileAPI::MaybeJsonFile(Json::Value in, std::string const& prefix)
188 if (in.isObject() || in.isArray()) {
189 out = Json::objectValue;
190 out["jsonFile"] = this->WriteJsonFile(in, prefix);
197 std::string cmFileAPI::ComputeSuffixHash(std::string const& file)
199 cmCryptoHash hasher(cmCryptoHash::AlgoSHA3_256);
200 std::string hash = hasher.HashFile(file);
201 hash.resize(20, '0');
205 std::string cmFileAPI::ComputeSuffixTime(std::string const&)
207 std::chrono::milliseconds ms =
208 std::chrono::duration_cast<std::chrono::milliseconds>(
209 std::chrono::system_clock::now().time_since_epoch());
210 std::chrono::seconds s =
211 std::chrono::duration_cast<std::chrono::seconds>(ms);
213 std::time_t ts = s.count();
214 std::size_t tms = ms.count() % 1000;
217 std::ostringstream ss;
218 ss << cmts.CreateTimestampFromTimeT(ts, "%Y-%m-%dT%H-%M-%S", true) << '-'
219 << std::setfill('0') << std::setw(4) << tms;
223 bool cmFileAPI::ReadQuery(std::string const& query,
224 std::vector<Object>& objects)
226 // Parse the "<kind>-" syntax.
227 std::string::size_type sep_pos = query.find('-');
228 if (sep_pos == std::string::npos) {
231 std::string kindName = query.substr(0, sep_pos);
232 std::string verStr = query.substr(sep_pos + 1);
233 if (kindName == ObjectKindName(ObjectKind::CodeModel)) {
235 o.Kind = ObjectKind::CodeModel;
236 if (verStr == "v2") {
241 objects.push_back(o);
244 if (kindName == ObjectKindName(ObjectKind::Cache)) {
246 o.Kind = ObjectKind::Cache;
247 if (verStr == "v2") {
252 objects.push_back(o);
255 if (kindName == ObjectKindName(ObjectKind::CMakeFiles)) {
257 o.Kind = ObjectKind::CMakeFiles;
258 if (verStr == "v1") {
263 objects.push_back(o);
266 if (kindName == ObjectKindName(ObjectKind::Toolchains)) {
268 o.Kind = ObjectKind::Toolchains;
269 if (verStr == "v1") {
274 objects.push_back(o);
277 if (kindName == ObjectKindName(ObjectKind::InternalTest)) {
279 o.Kind = ObjectKind::InternalTest;
280 if (verStr == "v1") {
282 } else if (verStr == "v2") {
287 objects.push_back(o);
293 void cmFileAPI::ReadClient(std::string const& client)
295 // Load queries for the client.
296 std::string clientDir = this->APIv1 + "/query/" + client;
297 std::vector<std::string> queries = this->LoadDir(clientDir);
299 // Read the queries and save for later.
300 ClientQuery& clientQuery = this->ClientQueries[client];
301 for (std::string& query : queries) {
302 if (query == "query.json") {
303 clientQuery.HaveQueryJson = true;
304 this->ReadClientQuery(client, clientQuery.QueryJson);
305 } else if (!this->ReadQuery(query, clientQuery.DirQuery.Known)) {
306 clientQuery.DirQuery.Unknown.push_back(std::move(query));
311 void cmFileAPI::ReadClientQuery(std::string const& client, ClientQueryJson& q)
313 // Read the query.json file.
314 std::string queryFile = this->APIv1 + "/query/" + client + "/query.json";
316 if (!this->ReadJsonFile(queryFile, query, q.Error)) {
319 if (!query.isObject()) {
320 q.Error = "query root is not an object";
324 Json::Value const& clientValue = query["client"];
325 if (!clientValue.isNull()) {
326 q.ClientValue = clientValue;
328 q.RequestsValue = std::move(query["requests"]);
329 q.Requests = this->BuildClientRequests(q.RequestsValue);
332 Json::Value cmFileAPI::BuildReplyIndex()
334 Json::Value index(Json::objectValue);
336 // Report information about this version of CMake.
337 index["cmake"] = this->BuildCMake();
339 // Reply to all queries that we loaded.
340 Json::Value& reply = index["reply"] = this->BuildReply(this->TopQuery);
341 for (auto const& client : this->ClientQueries) {
342 std::string const& clientName = client.first;
343 ClientQuery const& clientQuery = client.second;
344 reply[clientName] = this->BuildClientReply(clientQuery);
347 // Move our index of generated objects into its field.
348 Json::Value& objects = index["objects"] = Json::arrayValue;
349 for (auto& entry : this->ReplyIndexObjects) {
350 objects.append(std::move(entry.second)); // NOLINT(*)
356 Json::Value cmFileAPI::BuildCMake()
358 Json::Value cmake = Json::objectValue;
359 cmake["version"] = this->CMakeInstance->ReportVersionJson();
360 Json::Value& cmake_paths = cmake["paths"] = Json::objectValue;
361 cmake_paths["cmake"] = cmSystemTools::GetCMakeCommand();
362 cmake_paths["ctest"] = cmSystemTools::GetCTestCommand();
363 cmake_paths["cpack"] = cmSystemTools::GetCPackCommand();
364 cmake_paths["root"] = cmSystemTools::GetCMakeRoot();
365 cmake["generator"] = this->CMakeInstance->GetGlobalGenerator()->GetJson();
369 Json::Value cmFileAPI::BuildReply(Query const& q)
371 Json::Value reply = Json::objectValue;
372 for (Object const& o : q.Known) {
373 std::string const& name = ObjectName(o);
374 reply[name] = this->AddReplyIndexObject(o);
377 for (std::string const& name : q.Unknown) {
378 reply[name] = cmFileAPI::BuildReplyError("unknown query file");
383 Json::Value cmFileAPI::BuildReplyError(std::string const& error)
385 Json::Value e = Json::objectValue;
390 Json::Value const& cmFileAPI::AddReplyIndexObject(Object const& o)
392 Json::Value& indexEntry = this->ReplyIndexObjects[o];
393 if (!indexEntry.isNull()) {
394 // The reply object has already been generated.
398 // Generate this reply object.
399 Json::Value const& object = this->BuildObject(o);
400 assert(object.isObject());
402 // Populate this index entry.
403 indexEntry = Json::objectValue;
404 indexEntry["kind"] = object["kind"];
405 indexEntry["version"] = object["version"];
406 indexEntry["jsonFile"] = this->WriteJsonFile(object, ObjectName(o));
410 const char* cmFileAPI::ObjectKindName(ObjectKind kind)
412 // Keep in sync with ObjectKind enum.
413 static const char* objectKindNames[] = {
420 return objectKindNames[static_cast<size_t>(kind)];
423 std::string cmFileAPI::ObjectName(Object const& o)
425 std::string name = cmStrCat(ObjectKindName(o.Kind), "-v", o.Version);
429 Json::Value cmFileAPI::BuildVersion(unsigned int major, unsigned int minor)
432 version["major"] = major;
433 version["minor"] = minor;
437 Json::Value cmFileAPI::BuildObject(Object const& object)
441 switch (object.Kind) {
442 case ObjectKind::CodeModel:
443 value = this->BuildCodeModel(object);
445 case ObjectKind::Cache:
446 value = this->BuildCache(object);
448 case ObjectKind::CMakeFiles:
449 value = this->BuildCMakeFiles(object);
451 case ObjectKind::Toolchains:
452 value = this->BuildToolchains(object);
454 case ObjectKind::InternalTest:
455 value = this->BuildInternalTest(object);
462 cmFileAPI::ClientRequests cmFileAPI::BuildClientRequests(
463 Json::Value const& requests)
465 ClientRequests result;
466 if (requests.isNull()) {
467 result.Error = "'requests' member missing";
470 if (!requests.isArray()) {
471 result.Error = "'requests' member is not an array";
475 result.reserve(requests.size());
476 for (Json::Value const& request : requests) {
477 result.emplace_back(this->BuildClientRequest(request));
483 cmFileAPI::ClientRequest cmFileAPI::BuildClientRequest(
484 Json::Value const& request)
488 if (!request.isObject()) {
489 r.Error = "request is not an object";
493 Json::Value const& kind = request["kind"];
495 r.Error = "'kind' member missing";
498 if (!kind.isString()) {
499 r.Error = "'kind' member is not a string";
502 std::string const& kindName = kind.asString();
504 if (kindName == this->ObjectKindName(ObjectKind::CodeModel)) {
505 r.Kind = ObjectKind::CodeModel;
506 } else if (kindName == this->ObjectKindName(ObjectKind::Cache)) {
507 r.Kind = ObjectKind::Cache;
508 } else if (kindName == this->ObjectKindName(ObjectKind::CMakeFiles)) {
509 r.Kind = ObjectKind::CMakeFiles;
510 } else if (kindName == this->ObjectKindName(ObjectKind::Toolchains)) {
511 r.Kind = ObjectKind::Toolchains;
512 } else if (kindName == this->ObjectKindName(ObjectKind::InternalTest)) {
513 r.Kind = ObjectKind::InternalTest;
515 r.Error = "unknown request kind '" + kindName + "'";
519 Json::Value const& version = request["version"];
520 if (version.isNull()) {
521 r.Error = "'version' member missing";
524 std::vector<RequestVersion> versions;
525 if (!cmFileAPI::ReadRequestVersions(version, versions, r.Error)) {
530 case ObjectKind::CodeModel:
531 this->BuildClientRequestCodeModel(r, versions);
533 case ObjectKind::Cache:
534 this->BuildClientRequestCache(r, versions);
536 case ObjectKind::CMakeFiles:
537 this->BuildClientRequestCMakeFiles(r, versions);
539 case ObjectKind::Toolchains:
540 this->BuildClientRequestToolchains(r, versions);
542 case ObjectKind::InternalTest:
543 this->BuildClientRequestInternalTest(r, versions);
550 Json::Value cmFileAPI::BuildClientReply(ClientQuery const& q)
552 Json::Value reply = this->BuildReply(q.DirQuery);
554 if (!q.HaveQueryJson) {
558 Json::Value& reply_query_json = reply["query.json"];
559 ClientQueryJson const& qj = q.QueryJson;
561 if (!qj.Error.empty()) {
562 reply_query_json = this->BuildReplyError(qj.Error);
566 if (!qj.ClientValue.isNull()) {
567 reply_query_json["client"] = qj.ClientValue;
570 if (!qj.RequestsValue.isNull()) {
571 reply_query_json["requests"] = qj.RequestsValue;
574 reply_query_json["responses"] = this->BuildClientReplyResponses(qj.Requests);
579 Json::Value cmFileAPI::BuildClientReplyResponses(
580 ClientRequests const& requests)
582 Json::Value responses;
584 if (!requests.Error.empty()) {
585 responses = this->BuildReplyError(requests.Error);
589 responses = Json::arrayValue;
590 for (ClientRequest const& request : requests) {
591 responses.append(this->BuildClientReplyResponse(request));
597 Json::Value cmFileAPI::BuildClientReplyResponse(ClientRequest const& request)
599 Json::Value response;
600 if (!request.Error.empty()) {
601 response = this->BuildReplyError(request.Error);
604 response = this->AddReplyIndexObject(request);
608 bool cmFileAPI::ReadRequestVersions(Json::Value const& version,
609 std::vector<RequestVersion>& versions,
612 if (version.isArray()) {
613 for (Json::Value const& v : version) {
614 if (!ReadRequestVersion(v, /*inArray=*/true, versions, error)) {
619 if (!ReadRequestVersion(version, /*inArray=*/false, versions, error)) {
626 bool cmFileAPI::ReadRequestVersion(Json::Value const& version, bool inArray,
627 std::vector<RequestVersion>& result,
630 if (version.isUInt()) {
632 v.Major = version.asUInt();
637 if (!version.isObject()) {
639 error = "'version' array entry is not a non-negative integer or object";
642 "'version' member is not a non-negative integer, object, or array";
647 Json::Value const& major = version["major"];
648 if (major.isNull()) {
649 error = "'version' object 'major' member missing";
652 if (!major.isUInt()) {
653 error = "'version' object 'major' member is not a non-negative integer";
658 v.Major = major.asUInt();
660 Json::Value const& minor = version["minor"];
661 if (minor.isUInt()) {
662 v.Minor = minor.asUInt();
663 } else if (!minor.isNull()) {
664 error = "'version' object 'minor' member is not a non-negative integer";
673 std::string cmFileAPI::NoSupportedVersion(
674 std::vector<RequestVersion> const& versions)
676 std::ostringstream msg;
677 msg << "no supported version specified";
678 if (!versions.empty()) {
680 for (RequestVersion const& v : versions) {
681 msg << " " << v.Major << "." << v.Minor;
687 // The "codemodel" object kind.
689 // Update Help/manual/cmake-file-api.7.rst when updating this constant.
690 static unsigned int const CodeModelV2Minor = 4;
692 void cmFileAPI::BuildClientRequestCodeModel(
693 ClientRequest& r, std::vector<RequestVersion> const& versions)
695 // Select a known version from those requested.
696 for (RequestVersion const& v : versions) {
697 if ((v.Major == 2 && v.Minor <= CodeModelV2Minor)) {
703 r.Error = NoSupportedVersion(versions);
707 Json::Value cmFileAPI::BuildCodeModel(Object const& object)
709 Json::Value codemodel = cmFileAPICodemodelDump(*this, object.Version);
710 codemodel["kind"] = this->ObjectKindName(object.Kind);
712 Json::Value& version = codemodel["version"];
713 if (object.Version == 2) {
714 version = BuildVersion(2, CodeModelV2Minor);
716 return codemodel; // should be unreachable
722 // The "cache" object kind.
724 static unsigned int const CacheV2Minor = 0;
726 void cmFileAPI::BuildClientRequestCache(
727 ClientRequest& r, std::vector<RequestVersion> const& versions)
729 // Select a known version from those requested.
730 for (RequestVersion const& v : versions) {
731 if ((v.Major == 2 && v.Minor <= CacheV2Minor)) {
737 r.Error = NoSupportedVersion(versions);
741 Json::Value cmFileAPI::BuildCache(Object const& object)
743 Json::Value cache = cmFileAPICacheDump(*this, object.Version);
744 cache["kind"] = this->ObjectKindName(object.Kind);
746 Json::Value& version = cache["version"];
747 if (object.Version == 2) {
748 version = BuildVersion(2, CacheV2Minor);
750 return cache; // should be unreachable
756 // The "cmakeFiles" object kind.
758 static unsigned int const CMakeFilesV1Minor = 0;
760 void cmFileAPI::BuildClientRequestCMakeFiles(
761 ClientRequest& r, std::vector<RequestVersion> const& versions)
763 // Select a known version from those requested.
764 for (RequestVersion const& v : versions) {
765 if ((v.Major == 1 && v.Minor <= CMakeFilesV1Minor)) {
771 r.Error = NoSupportedVersion(versions);
775 Json::Value cmFileAPI::BuildCMakeFiles(Object const& object)
777 Json::Value cmakeFiles = cmFileAPICMakeFilesDump(*this, object.Version);
778 cmakeFiles["kind"] = this->ObjectKindName(object.Kind);
780 Json::Value& version = cmakeFiles["version"];
781 if (object.Version == 1) {
782 version = BuildVersion(1, CMakeFilesV1Minor);
784 return cmakeFiles; // should be unreachable
790 // The "toolchains" object kind.
792 static unsigned int const ToolchainsV1Minor = 0;
794 void cmFileAPI::BuildClientRequestToolchains(
795 ClientRequest& r, std::vector<RequestVersion> const& versions)
797 // Select a known version from those requested.
798 for (RequestVersion const& v : versions) {
799 if ((v.Major == 1 && v.Minor <= ToolchainsV1Minor)) {
805 r.Error = NoSupportedVersion(versions);
809 Json::Value cmFileAPI::BuildToolchains(Object const& object)
811 Json::Value toolchains = cmFileAPIToolchainsDump(*this, object.Version);
812 toolchains["kind"] = this->ObjectKindName(object.Kind);
814 Json::Value& version = toolchains["version"];
815 if (object.Version == 1) {
816 version = BuildVersion(1, ToolchainsV1Minor);
818 return toolchains; // should be unreachable
824 // The "__test" object kind is for internal testing of CMake.
826 static unsigned int const InternalTestV1Minor = 3;
827 static unsigned int const InternalTestV2Minor = 0;
829 void cmFileAPI::BuildClientRequestInternalTest(
830 ClientRequest& r, std::vector<RequestVersion> const& versions)
832 // Select a known version from those requested.
833 for (RequestVersion const& v : versions) {
834 if ((v.Major == 1 && v.Minor <= InternalTestV1Minor) || //
835 (v.Major == 2 && v.Minor <= InternalTestV2Minor)) {
841 r.Error = NoSupportedVersion(versions);
845 Json::Value cmFileAPI::BuildInternalTest(Object const& object)
847 Json::Value test = Json::objectValue;
848 test["kind"] = this->ObjectKindName(object.Kind);
849 Json::Value& version = test["version"];
850 if (object.Version == 2) {
851 version = BuildVersion(2, InternalTestV2Minor);
853 version = BuildVersion(1, InternalTestV1Minor);
858 Json::Value cmFileAPI::ReportCapabilities()
860 Json::Value capabilities = Json::objectValue;
861 Json::Value& requests = capabilities["requests"] = Json::arrayValue;
864 Json::Value request = Json::objectValue;
865 request["kind"] = ObjectKindName(ObjectKind::CodeModel);
866 Json::Value& versions = request["version"] = Json::arrayValue;
867 versions.append(BuildVersion(2, CodeModelV2Minor));
868 requests.append(std::move(request)); // NOLINT(*)
872 Json::Value request = Json::objectValue;
873 request["kind"] = ObjectKindName(ObjectKind::Cache);
874 Json::Value& versions = request["version"] = Json::arrayValue;
875 versions.append(BuildVersion(2, CacheV2Minor));
876 requests.append(std::move(request)); // NOLINT(*)
880 Json::Value request = Json::objectValue;
881 request["kind"] = ObjectKindName(ObjectKind::CMakeFiles);
882 Json::Value& versions = request["version"] = Json::arrayValue;
883 versions.append(BuildVersion(1, CMakeFilesV1Minor));
884 requests.append(std::move(request)); // NOLINT(*)
888 Json::Value request = Json::objectValue;
889 request["kind"] = ObjectKindName(ObjectKind::Toolchains);
890 Json::Value& versions = request["version"] = Json::arrayValue;
891 versions.append(BuildVersion(1, ToolchainsV1Minor));
892 requests.append(std::move(request)); // NOLINT(*)