cmFileAPI.cxx 28 KB

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