You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

835 lines
22 KiB

  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmFileAPI.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmCryptoHash.h"
  6. #include "cmFileAPICMakeFiles.h"
  7. #include "cmFileAPICache.h"
  8. #include "cmFileAPICodemodel.h"
  9. #include "cmGlobalGenerator.h"
  10. #include "cmSystemTools.h"
  11. #include "cmTimestamp.h"
  12. #include "cmake.h"
  13. #include "cmsys/Directory.hxx"
  14. #include "cmsys/FStream.hxx"
  15. #include <algorithm>
  16. #include <cassert>
  17. #include <chrono>
  18. #include <ctime>
  19. #include <iomanip>
  20. #include <sstream>
  21. #include <utility>
  22. cmFileAPI::cmFileAPI(cmake* cm)
  23. : CMakeInstance(cm)
  24. {
  25. this->APIv1 =
  26. this->CMakeInstance->GetHomeOutputDirectory() + "/.cmake/api/v1";
  27. Json::CharReaderBuilder rbuilder;
  28. rbuilder["collectComments"] = false;
  29. rbuilder["failIfExtra"] = true;
  30. rbuilder["rejectDupKeys"] = false;
  31. rbuilder["strictRoot"] = true;
  32. this->JsonReader =
  33. std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
  34. Json::StreamWriterBuilder wbuilder;
  35. wbuilder["indentation"] = "\t";
  36. this->JsonWriter =
  37. std::unique_ptr<Json::StreamWriter>(wbuilder.newStreamWriter());
  38. }
  39. void cmFileAPI::ReadQueries()
  40. {
  41. std::string const query_dir = this->APIv1 + "/query";
  42. this->QueryExists = cmSystemTools::FileIsDirectory(query_dir);
  43. if (!this->QueryExists) {
  44. return;
  45. }
  46. // Load queries at the top level.
  47. std::vector<std::string> queries = cmFileAPI::LoadDir(query_dir);
  48. // Read the queries and save for later.
  49. for (std::string& query : queries) {
  50. if (cmHasLiteralPrefix(query, "client-")) {
  51. this->ReadClient(query);
  52. } else if (!cmFileAPI::ReadQuery(query, this->TopQuery.Known)) {
  53. this->TopQuery.Unknown.push_back(std::move(query));
  54. }
  55. }
  56. }
  57. void cmFileAPI::WriteReplies()
  58. {
  59. if (this->QueryExists) {
  60. cmSystemTools::MakeDirectory(this->APIv1 + "/reply");
  61. this->WriteJsonFile(this->BuildReplyIndex(), "index", ComputeSuffixTime);
  62. }
  63. this->RemoveOldReplyFiles();
  64. }
  65. std::vector<std::string> cmFileAPI::LoadDir(std::string const& dir)
  66. {
  67. std::vector<std::string> files;
  68. cmsys::Directory d;
  69. d.Load(dir);
  70. for (unsigned long i = 0; i < d.GetNumberOfFiles(); ++i) {
  71. std::string f = d.GetFile(i);
  72. if (f != "." && f != "..") {
  73. files.push_back(std::move(f));
  74. }
  75. }
  76. std::sort(files.begin(), files.end());
  77. return files;
  78. }
  79. void cmFileAPI::RemoveOldReplyFiles()
  80. {
  81. std::string const reply_dir = this->APIv1 + "/reply";
  82. std::vector<std::string> files = this->LoadDir(reply_dir);
  83. for (std::string const& f : files) {
  84. if (this->ReplyFiles.find(f) == this->ReplyFiles.end()) {
  85. std::string file = reply_dir + "/" + f;
  86. cmSystemTools::RemoveFile(file);
  87. }
  88. }
  89. }
  90. bool cmFileAPI::ReadJsonFile(std::string const& file, Json::Value& value,
  91. std::string& error)
  92. {
  93. std::vector<char> content;
  94. cmsys::ifstream fin;
  95. if (!cmSystemTools::FileIsDirectory(file)) {
  96. fin.open(file.c_str(), std::ios::binary);
  97. }
  98. auto finEnd = fin.rdbuf()->pubseekoff(0, std::ios::end);
  99. if (finEnd > 0) {
  100. size_t finSize = finEnd;
  101. try {
  102. // Allocate a buffer to read the whole file.
  103. content.resize(finSize);
  104. // Now read the file from the beginning.
  105. fin.seekg(0, std::ios::beg);
  106. fin.read(content.data(), finSize);
  107. } catch (...) {
  108. fin.setstate(std::ios::failbit);
  109. }
  110. }
  111. fin.close();
  112. if (!fin) {
  113. value = Json::Value();
  114. error = "failed to read from file";
  115. return false;
  116. }
  117. // Parse our buffer as json.
  118. if (!this->JsonReader->parse(content.data(), content.data() + content.size(),
  119. &value, &error)) {
  120. value = Json::Value();
  121. return false;
  122. }
  123. return true;
  124. }
  125. std::string cmFileAPI::WriteJsonFile(
  126. Json::Value const& value, std::string const& prefix,
  127. std::string (*computeSuffix)(std::string const&))
  128. {
  129. std::string fileName;
  130. // Write the json file with a temporary name.
  131. std::string const& tmpFile = this->APIv1 + "/tmp.json";
  132. cmsys::ofstream ftmp(tmpFile.c_str());
  133. this->JsonWriter->write(value, &ftmp);
  134. ftmp << "\n";
  135. ftmp.close();
  136. if (!ftmp) {
  137. cmSystemTools::RemoveFile(tmpFile);
  138. return fileName;
  139. }
  140. // Compute the final name for the file.
  141. fileName = prefix + "-" + computeSuffix(tmpFile) + ".json";
  142. // Create the destination.
  143. std::string file = this->APIv1 + "/reply";
  144. cmSystemTools::MakeDirectory(file);
  145. file += "/";
  146. file += fileName;
  147. // If the final name already exists then assume it has proper content.
  148. // Otherwise, atomically place the reply file at its final name
  149. if (cmSystemTools::FileExists(file, true) ||
  150. !cmSystemTools::RenameFile(tmpFile, file)) {
  151. cmSystemTools::RemoveFile(tmpFile);
  152. }
  153. // Record this among files we have just written.
  154. this->ReplyFiles.insert(fileName);
  155. return fileName;
  156. }
  157. Json::Value cmFileAPI::MaybeJsonFile(Json::Value in, std::string const& prefix)
  158. {
  159. Json::Value out;
  160. if (in.isObject() || in.isArray()) {
  161. out = Json::objectValue;
  162. out["jsonFile"] = this->WriteJsonFile(in, prefix);
  163. } else {
  164. out = std::move(in);
  165. }
  166. return out;
  167. }
  168. std::string cmFileAPI::ComputeSuffixHash(std::string const& file)
  169. {
  170. cmCryptoHash hasher(cmCryptoHash::AlgoSHA3_256);
  171. std::string hash = hasher.HashFile(file);
  172. hash.resize(20, '0');
  173. return hash;
  174. }
  175. std::string cmFileAPI::ComputeSuffixTime(std::string const&)
  176. {
  177. std::chrono::milliseconds ms =
  178. std::chrono::duration_cast<std::chrono::milliseconds>(
  179. std::chrono::system_clock::now().time_since_epoch());
  180. std::chrono::seconds s =
  181. std::chrono::duration_cast<std::chrono::seconds>(ms);
  182. std::time_t ts = s.count();
  183. std::size_t tms = ms.count() % 1000;
  184. cmTimestamp cmts;
  185. std::ostringstream ss;
  186. ss << cmts.CreateTimestampFromTimeT(ts, "%Y-%m-%dT%H-%M-%S", true) << '-'
  187. << std::setfill('0') << std::setw(4) << tms;
  188. return ss.str();
  189. }
  190. bool cmFileAPI::ReadQuery(std::string const& query,
  191. std::vector<Object>& objects)
  192. {
  193. // Parse the "<kind>-" syntax.
  194. std::string::size_type sep_pos = query.find('-');
  195. if (sep_pos == std::string::npos) {
  196. return false;
  197. }
  198. std::string kindName = query.substr(0, sep_pos);
  199. std::string verStr = query.substr(sep_pos + 1);
  200. if (kindName == ObjectKindName(ObjectKind::CodeModel)) {
  201. Object o;
  202. o.Kind = ObjectKind::CodeModel;
  203. if (verStr == "v2") {
  204. o.Version = 2;
  205. } else {
  206. return false;
  207. }
  208. objects.push_back(o);
  209. return true;
  210. }
  211. if (kindName == ObjectKindName(ObjectKind::Cache)) {
  212. Object o;
  213. o.Kind = ObjectKind::Cache;
  214. if (verStr == "v2") {
  215. o.Version = 2;
  216. } else {
  217. return false;
  218. }
  219. objects.push_back(o);
  220. return true;
  221. }
  222. if (kindName == ObjectKindName(ObjectKind::CMakeFiles)) {
  223. Object o;
  224. o.Kind = ObjectKind::CMakeFiles;
  225. if (verStr == "v1") {
  226. o.Version = 1;
  227. } else {
  228. return false;
  229. }
  230. objects.push_back(o);
  231. return true;
  232. }
  233. if (kindName == ObjectKindName(ObjectKind::InternalTest)) {
  234. Object o;
  235. o.Kind = ObjectKind::InternalTest;
  236. if (verStr == "v1") {
  237. o.Version = 1;
  238. } else if (verStr == "v2") {
  239. o.Version = 2;
  240. } else {
  241. return false;
  242. }
  243. objects.push_back(o);
  244. return true;
  245. }
  246. return false;
  247. }
  248. void cmFileAPI::ReadClient(std::string const& client)
  249. {
  250. // Load queries for the client.
  251. std::string clientDir = this->APIv1 + "/query/" + client;
  252. std::vector<std::string> queries = this->LoadDir(clientDir);
  253. // Read the queries and save for later.
  254. ClientQuery& clientQuery = this->ClientQueries[client];
  255. for (std::string& query : queries) {
  256. if (query == "query.json") {
  257. clientQuery.HaveQueryJson = true;
  258. this->ReadClientQuery(client, clientQuery.QueryJson);
  259. } else if (!this->ReadQuery(query, clientQuery.DirQuery.Known)) {
  260. clientQuery.DirQuery.Unknown.push_back(std::move(query));
  261. }
  262. }
  263. }
  264. void cmFileAPI::ReadClientQuery(std::string const& client, ClientQueryJson& q)
  265. {
  266. // Read the query.json file.
  267. std::string queryFile = this->APIv1 + "/query/" + client + "/query.json";
  268. Json::Value query;
  269. if (!this->ReadJsonFile(queryFile, query, q.Error)) {
  270. return;
  271. }
  272. if (!query.isObject()) {
  273. q.Error = "query root is not an object";
  274. return;
  275. }
  276. Json::Value const& clientValue = query["client"];
  277. if (!clientValue.isNull()) {
  278. q.ClientValue = clientValue;
  279. }
  280. q.RequestsValue = std::move(query["requests"]);
  281. q.Requests = this->BuildClientRequests(q.RequestsValue);
  282. }
  283. Json::Value cmFileAPI::BuildReplyIndex()
  284. {
  285. Json::Value index(Json::objectValue);
  286. // Report information about this version of CMake.
  287. index["cmake"] = this->BuildCMake();
  288. // Reply to all queries that we loaded.
  289. Json::Value& reply = index["reply"] = this->BuildReply(this->TopQuery);
  290. for (auto const& client : this->ClientQueries) {
  291. std::string const& clientName = client.first;
  292. ClientQuery const& clientQuery = client.second;
  293. reply[clientName] = this->BuildClientReply(clientQuery);
  294. }
  295. // Move our index of generated objects into its field.
  296. Json::Value& objects = index["objects"] = Json::arrayValue;
  297. for (auto& entry : this->ReplyIndexObjects) {
  298. objects.append(std::move(entry.second)); // NOLINT(*)
  299. }
  300. return index;
  301. }
  302. Json::Value cmFileAPI::BuildCMake()
  303. {
  304. Json::Value cmake = Json::objectValue;
  305. cmake["version"] = this->CMakeInstance->ReportVersionJson();
  306. Json::Value& cmake_paths = cmake["paths"] = Json::objectValue;
  307. cmake_paths["cmake"] = cmSystemTools::GetCMakeCommand();
  308. cmake_paths["ctest"] = cmSystemTools::GetCTestCommand();
  309. cmake_paths["cpack"] = cmSystemTools::GetCPackCommand();
  310. cmake_paths["root"] = cmSystemTools::GetCMakeRoot();
  311. cmake["generator"] = this->CMakeInstance->GetGlobalGenerator()->GetJson();
  312. return cmake;
  313. }
  314. Json::Value cmFileAPI::BuildReply(Query const& q)
  315. {
  316. Json::Value reply = Json::objectValue;
  317. for (Object const& o : q.Known) {
  318. std::string const& name = ObjectName(o);
  319. reply[name] = this->AddReplyIndexObject(o);
  320. }
  321. for (std::string const& name : q.Unknown) {
  322. reply[name] = cmFileAPI::BuildReplyError("unknown query file");
  323. }
  324. return reply;
  325. }
  326. Json::Value cmFileAPI::BuildReplyError(std::string const& error)
  327. {
  328. Json::Value e = Json::objectValue;
  329. e["error"] = error;
  330. return e;
  331. }
  332. Json::Value const& cmFileAPI::AddReplyIndexObject(Object const& o)
  333. {
  334. Json::Value& indexEntry = this->ReplyIndexObjects[o];
  335. if (!indexEntry.isNull()) {
  336. // The reply object has already been generated.
  337. return indexEntry;
  338. }
  339. // Generate this reply object.
  340. Json::Value const& object = this->BuildObject(o);
  341. assert(object.isObject());
  342. // Populate this index entry.
  343. indexEntry = Json::objectValue;
  344. indexEntry["kind"] = object["kind"];
  345. indexEntry["version"] = object["version"];
  346. indexEntry["jsonFile"] = this->WriteJsonFile(object, ObjectName(o));
  347. return indexEntry;
  348. }
  349. const char* cmFileAPI::ObjectKindName(ObjectKind kind)
  350. {
  351. // Keep in sync with ObjectKind enum.
  352. static const char* objectKindNames[] = {
  353. "codemodel", //
  354. "cache", //
  355. "cmakeFiles", //
  356. "__test" //
  357. };
  358. return objectKindNames[size_t(kind)];
  359. }
  360. std::string cmFileAPI::ObjectName(Object const& o)
  361. {
  362. std::string name = ObjectKindName(o.Kind);
  363. name += "-v";
  364. name += std::to_string(o.Version);
  365. return name;
  366. }
  367. Json::Value cmFileAPI::BuildVersion(unsigned int major, unsigned int minor)
  368. {
  369. Json::Value version;
  370. version["major"] = major;
  371. version["minor"] = minor;
  372. return version;
  373. }
  374. Json::Value cmFileAPI::BuildObject(Object const& object)
  375. {
  376. Json::Value value;
  377. switch (object.Kind) {
  378. case ObjectKind::CodeModel:
  379. value = this->BuildCodeModel(object);
  380. break;
  381. case ObjectKind::Cache:
  382. value = this->BuildCache(object);
  383. break;
  384. case ObjectKind::CMakeFiles:
  385. value = this->BuildCMakeFiles(object);
  386. break;
  387. case ObjectKind::InternalTest:
  388. value = this->BuildInternalTest(object);
  389. break;
  390. }
  391. return value;
  392. }
  393. cmFileAPI::ClientRequests cmFileAPI::BuildClientRequests(
  394. Json::Value const& requests)
  395. {
  396. ClientRequests result;
  397. if (requests.isNull()) {
  398. result.Error = "'requests' member missing";
  399. return result;
  400. }
  401. if (!requests.isArray()) {
  402. result.Error = "'requests' member is not an array";
  403. return result;
  404. }
  405. result.reserve(requests.size());
  406. for (Json::Value const& request : requests) {
  407. result.emplace_back(this->BuildClientRequest(request));
  408. }
  409. return result;
  410. }
  411. cmFileAPI::ClientRequest cmFileAPI::BuildClientRequest(
  412. Json::Value const& request)
  413. {
  414. ClientRequest r;
  415. if (!request.isObject()) {
  416. r.Error = "request is not an object";
  417. return r;
  418. }
  419. Json::Value const& kind = request["kind"];
  420. if (kind.isNull()) {
  421. r.Error = "'kind' member missing";
  422. return r;
  423. }
  424. if (!kind.isString()) {
  425. r.Error = "'kind' member is not a string";
  426. return r;
  427. }
  428. std::string const& kindName = kind.asString();
  429. if (kindName == this->ObjectKindName(ObjectKind::CodeModel)) {
  430. r.Kind = ObjectKind::CodeModel;
  431. } else if (kindName == this->ObjectKindName(ObjectKind::Cache)) {
  432. r.Kind = ObjectKind::Cache;
  433. } else if (kindName == this->ObjectKindName(ObjectKind::CMakeFiles)) {
  434. r.Kind = ObjectKind::CMakeFiles;
  435. } else if (kindName == this->ObjectKindName(ObjectKind::InternalTest)) {
  436. r.Kind = ObjectKind::InternalTest;
  437. } else {
  438. r.Error = "unknown request kind '" + kindName + "'";
  439. return r;
  440. }
  441. Json::Value const& version = request["version"];
  442. if (version.isNull()) {
  443. r.Error = "'version' member missing";
  444. return r;
  445. }
  446. std::vector<RequestVersion> versions;
  447. if (!cmFileAPI::ReadRequestVersions(version, versions, r.Error)) {
  448. return r;
  449. }
  450. switch (r.Kind) {
  451. case ObjectKind::CodeModel:
  452. this->BuildClientRequestCodeModel(r, versions);
  453. break;
  454. case ObjectKind::Cache:
  455. this->BuildClientRequestCache(r, versions);
  456. break;
  457. case ObjectKind::CMakeFiles:
  458. this->BuildClientRequestCMakeFiles(r, versions);
  459. break;
  460. case ObjectKind::InternalTest:
  461. this->BuildClientRequestInternalTest(r, versions);
  462. break;
  463. }
  464. return r;
  465. }
  466. Json::Value cmFileAPI::BuildClientReply(ClientQuery const& q)
  467. {
  468. Json::Value reply = this->BuildReply(q.DirQuery);
  469. if (!q.HaveQueryJson) {
  470. return reply;
  471. }
  472. Json::Value& reply_query_json = reply["query.json"];
  473. ClientQueryJson const& qj = q.QueryJson;
  474. if (!qj.Error.empty()) {
  475. reply_query_json = this->BuildReplyError(qj.Error);
  476. return reply;
  477. }
  478. if (!qj.ClientValue.isNull()) {
  479. reply_query_json["client"] = qj.ClientValue;
  480. }
  481. if (!qj.RequestsValue.isNull()) {
  482. reply_query_json["requests"] = qj.RequestsValue;
  483. }
  484. reply_query_json["responses"] = this->BuildClientReplyResponses(qj.Requests);
  485. return reply;
  486. }
  487. Json::Value cmFileAPI::BuildClientReplyResponses(
  488. ClientRequests const& requests)
  489. {
  490. Json::Value responses;
  491. if (!requests.Error.empty()) {
  492. responses = this->BuildReplyError(requests.Error);
  493. return responses;
  494. }
  495. responses = Json::arrayValue;
  496. for (ClientRequest const& request : requests) {
  497. responses.append(this->BuildClientReplyResponse(request));
  498. }
  499. return responses;
  500. }
  501. Json::Value cmFileAPI::BuildClientReplyResponse(ClientRequest const& request)
  502. {
  503. Json::Value response;
  504. if (!request.Error.empty()) {
  505. response = this->BuildReplyError(request.Error);
  506. return response;
  507. }
  508. response = this->AddReplyIndexObject(request);
  509. return response;
  510. }
  511. bool cmFileAPI::ReadRequestVersions(Json::Value const& version,
  512. std::vector<RequestVersion>& versions,
  513. std::string& error)
  514. {
  515. if (version.isArray()) {
  516. for (Json::Value const& v : version) {
  517. if (!ReadRequestVersion(v, /*inArray=*/true, versions, error)) {
  518. return false;
  519. }
  520. }
  521. } else {
  522. if (!ReadRequestVersion(version, /*inArray=*/false, versions, error)) {
  523. return false;
  524. }
  525. }
  526. return true;
  527. }
  528. bool cmFileAPI::ReadRequestVersion(Json::Value const& version, bool inArray,
  529. std::vector<RequestVersion>& result,
  530. std::string& error)
  531. {
  532. if (version.isUInt()) {
  533. RequestVersion v;
  534. v.Major = version.asUInt();
  535. result.push_back(v);
  536. return true;
  537. }
  538. if (!version.isObject()) {
  539. if (inArray) {
  540. error = "'version' array entry is not a non-negative integer or object";
  541. } else {
  542. error =
  543. "'version' member is not a non-negative integer, object, or array";
  544. }
  545. return false;
  546. }
  547. Json::Value const& major = version["major"];
  548. if (major.isNull()) {
  549. error = "'version' object 'major' member missing";
  550. return false;
  551. }
  552. if (!major.isUInt()) {
  553. error = "'version' object 'major' member is not a non-negative integer";
  554. return false;
  555. }
  556. RequestVersion v;
  557. v.Major = major.asUInt();
  558. Json::Value const& minor = version["minor"];
  559. if (minor.isUInt()) {
  560. v.Minor = minor.asUInt();
  561. } else if (!minor.isNull()) {
  562. error = "'version' object 'minor' member is not a non-negative integer";
  563. return false;
  564. }
  565. result.push_back(v);
  566. return true;
  567. }
  568. std::string cmFileAPI::NoSupportedVersion(
  569. std::vector<RequestVersion> const& versions)
  570. {
  571. std::ostringstream msg;
  572. msg << "no supported version specified";
  573. if (!versions.empty()) {
  574. msg << " among:";
  575. for (RequestVersion const& v : versions) {
  576. msg << " " << v.Major << "." << v.Minor;
  577. }
  578. }
  579. return msg.str();
  580. }
  581. // The "codemodel" object kind.
  582. static unsigned int const CodeModelV2Minor = 0;
  583. void cmFileAPI::BuildClientRequestCodeModel(
  584. ClientRequest& r, std::vector<RequestVersion> const& versions)
  585. {
  586. // Select a known version from those requested.
  587. for (RequestVersion const& v : versions) {
  588. if ((v.Major == 2 && v.Minor <= CodeModelV2Minor)) {
  589. r.Version = v.Major;
  590. break;
  591. }
  592. }
  593. if (!r.Version) {
  594. r.Error = NoSupportedVersion(versions);
  595. }
  596. }
  597. Json::Value cmFileAPI::BuildCodeModel(Object const& object)
  598. {
  599. using namespace std::placeholders;
  600. Json::Value codemodel = cmFileAPICodemodelDump(*this, object.Version);
  601. codemodel["kind"] = this->ObjectKindName(object.Kind);
  602. Json::Value& version = codemodel["version"];
  603. if (object.Version == 2) {
  604. version = BuildVersion(2, CodeModelV2Minor);
  605. } else {
  606. return codemodel; // should be unreachable
  607. }
  608. return codemodel;
  609. }
  610. // The "cache" object kind.
  611. static unsigned int const CacheV2Minor = 0;
  612. void cmFileAPI::BuildClientRequestCache(
  613. ClientRequest& r, std::vector<RequestVersion> const& versions)
  614. {
  615. // Select a known version from those requested.
  616. for (RequestVersion const& v : versions) {
  617. if ((v.Major == 2 && v.Minor <= CacheV2Minor)) {
  618. r.Version = v.Major;
  619. break;
  620. }
  621. }
  622. if (!r.Version) {
  623. r.Error = NoSupportedVersion(versions);
  624. }
  625. }
  626. Json::Value cmFileAPI::BuildCache(Object const& object)
  627. {
  628. using namespace std::placeholders;
  629. Json::Value cache = cmFileAPICacheDump(*this, object.Version);
  630. cache["kind"] = this->ObjectKindName(object.Kind);
  631. Json::Value& version = cache["version"];
  632. if (object.Version == 2) {
  633. version = BuildVersion(2, CacheV2Minor);
  634. } else {
  635. return cache; // should be unreachable
  636. }
  637. return cache;
  638. }
  639. // The "cmakeFiles" object kind.
  640. static unsigned int const CMakeFilesV1Minor = 0;
  641. void cmFileAPI::BuildClientRequestCMakeFiles(
  642. ClientRequest& r, std::vector<RequestVersion> const& versions)
  643. {
  644. // Select a known version from those requested.
  645. for (RequestVersion const& v : versions) {
  646. if ((v.Major == 1 && v.Minor <= CMakeFilesV1Minor)) {
  647. r.Version = v.Major;
  648. break;
  649. }
  650. }
  651. if (!r.Version) {
  652. r.Error = NoSupportedVersion(versions);
  653. }
  654. }
  655. Json::Value cmFileAPI::BuildCMakeFiles(Object const& object)
  656. {
  657. using namespace std::placeholders;
  658. Json::Value cmakeFiles = cmFileAPICMakeFilesDump(*this, object.Version);
  659. cmakeFiles["kind"] = this->ObjectKindName(object.Kind);
  660. Json::Value& version = cmakeFiles["version"];
  661. if (object.Version == 1) {
  662. version = BuildVersion(1, CMakeFilesV1Minor);
  663. } else {
  664. return cmakeFiles; // should be unreachable
  665. }
  666. return cmakeFiles;
  667. }
  668. // The "__test" object kind is for internal testing of CMake.
  669. static unsigned int const InternalTestV1Minor = 3;
  670. static unsigned int const InternalTestV2Minor = 0;
  671. void cmFileAPI::BuildClientRequestInternalTest(
  672. ClientRequest& r, std::vector<RequestVersion> const& versions)
  673. {
  674. // Select a known version from those requested.
  675. for (RequestVersion const& v : versions) {
  676. if ((v.Major == 1 && v.Minor <= InternalTestV1Minor) || //
  677. (v.Major == 2 && v.Minor <= InternalTestV2Minor)) {
  678. r.Version = v.Major;
  679. break;
  680. }
  681. }
  682. if (!r.Version) {
  683. r.Error = NoSupportedVersion(versions);
  684. }
  685. }
  686. Json::Value cmFileAPI::BuildInternalTest(Object const& object)
  687. {
  688. Json::Value test = Json::objectValue;
  689. test["kind"] = this->ObjectKindName(object.Kind);
  690. Json::Value& version = test["version"];
  691. if (object.Version == 2) {
  692. version = BuildVersion(2, InternalTestV2Minor);
  693. } else {
  694. version = BuildVersion(1, InternalTestV1Minor);
  695. }
  696. return test;
  697. }
  698. Json::Value cmFileAPI::ReportCapabilities()
  699. {
  700. Json::Value capabilities = Json::objectValue;
  701. Json::Value& requests = capabilities["requests"] = Json::arrayValue;
  702. {
  703. Json::Value request = Json::objectValue;
  704. request["kind"] = ObjectKindName(ObjectKind::CodeModel);
  705. Json::Value& versions = request["version"] = Json::arrayValue;
  706. versions.append(BuildVersion(2, CodeModelV2Minor));
  707. requests.append(std::move(request));
  708. }
  709. {
  710. Json::Value request = Json::objectValue;
  711. request["kind"] = ObjectKindName(ObjectKind::Cache);
  712. Json::Value& versions = request["version"] = Json::arrayValue;
  713. versions.append(BuildVersion(2, CacheV2Minor));
  714. requests.append(std::move(request));
  715. }
  716. {
  717. Json::Value request = Json::objectValue;
  718. request["kind"] = ObjectKindName(ObjectKind::CMakeFiles);
  719. Json::Value& versions = request["version"] = Json::arrayValue;
  720. versions.append(BuildVersion(1, CMakeFilesV1Minor));
  721. requests.append(std::move(request));
  722. }
  723. return capabilities;
  724. }