cmFileAPI.cxx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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::BuildObject(Object const& object)
  368. {
  369. Json::Value value;
  370. switch (object.Kind) {
  371. case ObjectKind::CodeModel:
  372. value = this->BuildCodeModel(object);
  373. break;
  374. case ObjectKind::Cache:
  375. value = this->BuildCache(object);
  376. break;
  377. case ObjectKind::CMakeFiles:
  378. value = this->BuildCMakeFiles(object);
  379. break;
  380. case ObjectKind::InternalTest:
  381. value = this->BuildInternalTest(object);
  382. break;
  383. }
  384. return value;
  385. }
  386. cmFileAPI::ClientRequests cmFileAPI::BuildClientRequests(
  387. Json::Value const& requests)
  388. {
  389. ClientRequests result;
  390. if (requests.isNull()) {
  391. result.Error = "'requests' member missing";
  392. return result;
  393. }
  394. if (!requests.isArray()) {
  395. result.Error = "'requests' member is not an array";
  396. return result;
  397. }
  398. result.reserve(requests.size());
  399. for (Json::Value const& request : requests) {
  400. result.emplace_back(this->BuildClientRequest(request));
  401. }
  402. return result;
  403. }
  404. cmFileAPI::ClientRequest cmFileAPI::BuildClientRequest(
  405. Json::Value const& request)
  406. {
  407. ClientRequest r;
  408. if (!request.isObject()) {
  409. r.Error = "request is not an object";
  410. return r;
  411. }
  412. Json::Value const& kind = request["kind"];
  413. if (kind.isNull()) {
  414. r.Error = "'kind' member missing";
  415. return r;
  416. }
  417. if (!kind.isString()) {
  418. r.Error = "'kind' member is not a string";
  419. return r;
  420. }
  421. std::string const& kindName = kind.asString();
  422. if (kindName == this->ObjectKindName(ObjectKind::CodeModel)) {
  423. r.Kind = ObjectKind::CodeModel;
  424. } else if (kindName == this->ObjectKindName(ObjectKind::Cache)) {
  425. r.Kind = ObjectKind::Cache;
  426. } else if (kindName == this->ObjectKindName(ObjectKind::CMakeFiles)) {
  427. r.Kind = ObjectKind::CMakeFiles;
  428. } else if (kindName == this->ObjectKindName(ObjectKind::InternalTest)) {
  429. r.Kind = ObjectKind::InternalTest;
  430. } else {
  431. r.Error = "unknown request kind '" + kindName + "'";
  432. return r;
  433. }
  434. Json::Value const& version = request["version"];
  435. if (version.isNull()) {
  436. r.Error = "'version' member missing";
  437. return r;
  438. }
  439. std::vector<RequestVersion> versions;
  440. if (!cmFileAPI::ReadRequestVersions(version, versions, r.Error)) {
  441. return r;
  442. }
  443. switch (r.Kind) {
  444. case ObjectKind::CodeModel:
  445. this->BuildClientRequestCodeModel(r, versions);
  446. break;
  447. case ObjectKind::Cache:
  448. this->BuildClientRequestCache(r, versions);
  449. break;
  450. case ObjectKind::CMakeFiles:
  451. this->BuildClientRequestCMakeFiles(r, versions);
  452. break;
  453. case ObjectKind::InternalTest:
  454. this->BuildClientRequestInternalTest(r, versions);
  455. break;
  456. }
  457. return r;
  458. }
  459. Json::Value cmFileAPI::BuildClientReply(ClientQuery const& q)
  460. {
  461. Json::Value reply = this->BuildReply(q.DirQuery);
  462. if (!q.HaveQueryJson) {
  463. return reply;
  464. }
  465. Json::Value& reply_query_json = reply["query.json"];
  466. ClientQueryJson const& qj = q.QueryJson;
  467. if (!qj.Error.empty()) {
  468. reply_query_json = this->BuildReplyError(qj.Error);
  469. return reply;
  470. }
  471. if (!qj.ClientValue.isNull()) {
  472. reply_query_json["client"] = qj.ClientValue;
  473. }
  474. if (!qj.RequestsValue.isNull()) {
  475. reply_query_json["requests"] = qj.RequestsValue;
  476. }
  477. reply_query_json["responses"] = this->BuildClientReplyResponses(qj.Requests);
  478. return reply;
  479. }
  480. Json::Value cmFileAPI::BuildClientReplyResponses(
  481. ClientRequests const& requests)
  482. {
  483. Json::Value responses;
  484. if (!requests.Error.empty()) {
  485. responses = this->BuildReplyError(requests.Error);
  486. return responses;
  487. }
  488. responses = Json::arrayValue;
  489. for (ClientRequest const& request : requests) {
  490. responses.append(this->BuildClientReplyResponse(request));
  491. }
  492. return responses;
  493. }
  494. Json::Value cmFileAPI::BuildClientReplyResponse(ClientRequest const& request)
  495. {
  496. Json::Value response;
  497. if (!request.Error.empty()) {
  498. response = this->BuildReplyError(request.Error);
  499. return response;
  500. }
  501. response = this->AddReplyIndexObject(request);
  502. return response;
  503. }
  504. bool cmFileAPI::ReadRequestVersions(Json::Value const& version,
  505. std::vector<RequestVersion>& versions,
  506. std::string& error)
  507. {
  508. if (version.isArray()) {
  509. for (Json::Value const& v : version) {
  510. if (!ReadRequestVersion(v, /*inArray=*/true, versions, error)) {
  511. return false;
  512. }
  513. }
  514. } else {
  515. if (!ReadRequestVersion(version, /*inArray=*/false, versions, error)) {
  516. return false;
  517. }
  518. }
  519. return true;
  520. }
  521. bool cmFileAPI::ReadRequestVersion(Json::Value const& version, bool inArray,
  522. std::vector<RequestVersion>& result,
  523. std::string& error)
  524. {
  525. if (version.isUInt()) {
  526. RequestVersion v;
  527. v.Major = version.asUInt();
  528. result.push_back(v);
  529. return true;
  530. }
  531. if (!version.isObject()) {
  532. if (inArray) {
  533. error = "'version' array entry is not a non-negative integer or object";
  534. } else {
  535. error =
  536. "'version' member is not a non-negative integer, object, or array";
  537. }
  538. return false;
  539. }
  540. Json::Value const& major = version["major"];
  541. if (major.isNull()) {
  542. error = "'version' object 'major' member missing";
  543. return false;
  544. }
  545. if (!major.isUInt()) {
  546. error = "'version' object 'major' member is not a non-negative integer";
  547. return false;
  548. }
  549. RequestVersion v;
  550. v.Major = major.asUInt();
  551. Json::Value const& minor = version["minor"];
  552. if (minor.isUInt()) {
  553. v.Minor = minor.asUInt();
  554. } else if (!minor.isNull()) {
  555. error = "'version' object 'minor' member is not a non-negative integer";
  556. return false;
  557. }
  558. result.push_back(v);
  559. return true;
  560. }
  561. std::string cmFileAPI::NoSupportedVersion(
  562. std::vector<RequestVersion> const& versions)
  563. {
  564. std::ostringstream msg;
  565. msg << "no supported version specified";
  566. if (!versions.empty()) {
  567. msg << " among:";
  568. for (RequestVersion const& v : versions) {
  569. msg << " " << v.Major << "." << v.Minor;
  570. }
  571. }
  572. return msg.str();
  573. }
  574. // The "codemodel" object kind.
  575. static unsigned int const CodeModelV2Minor = 0;
  576. void cmFileAPI::BuildClientRequestCodeModel(
  577. ClientRequest& r, std::vector<RequestVersion> const& versions)
  578. {
  579. // Select a known version from those requested.
  580. for (RequestVersion const& v : versions) {
  581. if ((v.Major == 2 && v.Minor <= CodeModelV2Minor)) {
  582. r.Version = v.Major;
  583. break;
  584. }
  585. }
  586. if (!r.Version) {
  587. r.Error = NoSupportedVersion(versions);
  588. }
  589. }
  590. Json::Value cmFileAPI::BuildCodeModel(Object const& object)
  591. {
  592. using namespace std::placeholders;
  593. Json::Value codemodel = cmFileAPICodemodelDump(*this, object.Version);
  594. codemodel["kind"] = this->ObjectKindName(object.Kind);
  595. Json::Value& version = codemodel["version"] = Json::objectValue;
  596. if (object.Version == 2) {
  597. version["major"] = 2;
  598. version["minor"] = CodeModelV2Minor;
  599. } else {
  600. return codemodel; // should be unreachable
  601. }
  602. return codemodel;
  603. }
  604. // The "cache" object kind.
  605. static unsigned int const CacheV2Minor = 0;
  606. void cmFileAPI::BuildClientRequestCache(
  607. ClientRequest& r, std::vector<RequestVersion> const& versions)
  608. {
  609. // Select a known version from those requested.
  610. for (RequestVersion const& v : versions) {
  611. if ((v.Major == 2 && v.Minor <= CacheV2Minor)) {
  612. r.Version = v.Major;
  613. break;
  614. }
  615. }
  616. if (!r.Version) {
  617. r.Error = NoSupportedVersion(versions);
  618. }
  619. }
  620. Json::Value cmFileAPI::BuildCache(Object const& object)
  621. {
  622. using namespace std::placeholders;
  623. Json::Value cache = cmFileAPICacheDump(*this, object.Version);
  624. cache["kind"] = this->ObjectKindName(object.Kind);
  625. Json::Value& version = cache["version"] = Json::objectValue;
  626. if (object.Version == 2) {
  627. version["major"] = 2;
  628. version["minor"] = CacheV2Minor;
  629. } else {
  630. return cache; // should be unreachable
  631. }
  632. return cache;
  633. }
  634. // The "cmakeFiles" object kind.
  635. static unsigned int const CMakeFilesV1Minor = 0;
  636. void cmFileAPI::BuildClientRequestCMakeFiles(
  637. ClientRequest& r, std::vector<RequestVersion> const& versions)
  638. {
  639. // Select a known version from those requested.
  640. for (RequestVersion const& v : versions) {
  641. if ((v.Major == 1 && v.Minor <= CMakeFilesV1Minor)) {
  642. r.Version = v.Major;
  643. break;
  644. }
  645. }
  646. if (!r.Version) {
  647. r.Error = NoSupportedVersion(versions);
  648. }
  649. }
  650. Json::Value cmFileAPI::BuildCMakeFiles(Object const& object)
  651. {
  652. using namespace std::placeholders;
  653. Json::Value cmakeFiles = cmFileAPICMakeFilesDump(*this, object.Version);
  654. cmakeFiles["kind"] = this->ObjectKindName(object.Kind);
  655. Json::Value& version = cmakeFiles["version"] = Json::objectValue;
  656. if (object.Version == 1) {
  657. version["major"] = 1;
  658. version["minor"] = CMakeFilesV1Minor;
  659. } else {
  660. return cmakeFiles; // should be unreachable
  661. }
  662. return cmakeFiles;
  663. }
  664. // The "__test" object kind is for internal testing of CMake.
  665. static unsigned int const InternalTestV1Minor = 3;
  666. static unsigned int const InternalTestV2Minor = 0;
  667. void cmFileAPI::BuildClientRequestInternalTest(
  668. ClientRequest& r, std::vector<RequestVersion> const& versions)
  669. {
  670. // Select a known version from those requested.
  671. for (RequestVersion const& v : versions) {
  672. if ((v.Major == 1 && v.Minor <= InternalTestV1Minor) || //
  673. (v.Major == 2 && v.Minor <= InternalTestV2Minor)) {
  674. r.Version = v.Major;
  675. break;
  676. }
  677. }
  678. if (!r.Version) {
  679. r.Error = NoSupportedVersion(versions);
  680. }
  681. }
  682. Json::Value cmFileAPI::BuildInternalTest(Object const& object)
  683. {
  684. Json::Value test = Json::objectValue;
  685. test["kind"] = this->ObjectKindName(object.Kind);
  686. Json::Value& version = test["version"] = Json::objectValue;
  687. if (object.Version == 2) {
  688. version["major"] = 2;
  689. version["minor"] = InternalTestV2Minor;
  690. } else {
  691. version["major"] = 1;
  692. version["minor"] = InternalTestV1Minor;
  693. }
  694. return test;
  695. }