cmFileAPI.cxx 22 KB

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