cmFileAPI.cxx 23 KB

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