cmFileAPI.cxx 19 KB

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