cmFileAPI.cxx 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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. std::string suffix = computeSuffix(tmpFile);
  180. std::string suffixWithExtension = cmStrCat("-", suffix, ".json");
  181. fileName = cmStrCat(prefix, suffixWithExtension);
  182. // Truncate the file name length
  183. // eCryptFS has a maximal file name length recommendation of 140
  184. size_t const maxFileNameLength = 140;
  185. size_t const fileNameLength = fileName.size();
  186. if (fileNameLength > maxFileNameLength) {
  187. size_t const newHashLength = 20;
  188. size_t const newSuffixLength =
  189. suffixWithExtension.size() - suffix.size() + newHashLength;
  190. size_t const overLength =
  191. fileNameLength - maxFileNameLength + newSuffixLength;
  192. size_t const startPos = fileNameLength - overLength;
  193. std::string const toBeRemoved = fileName.substr(startPos, overLength);
  194. suffix = cmCryptoHash(cmCryptoHash::AlgoSHA256)
  195. .HashString(toBeRemoved)
  196. .substr(0, newHashLength);
  197. suffixWithExtension = cmStrCat("-", suffix, ".json");
  198. fileName.replace(startPos, overLength, suffixWithExtension);
  199. }
  200. // Create the destination.
  201. std::string file = this->APIv1 + "/reply";
  202. cmSystemTools::MakeDirectory(file);
  203. file += "/";
  204. file += fileName;
  205. // If the final name already exists then assume it has proper content.
  206. // Otherwise, atomically place the reply file at its final name
  207. if (cmSystemTools::FileExists(file, true) ||
  208. !cmSystemTools::RenameFile(tmpFile, file)) {
  209. cmSystemTools::RemoveFile(tmpFile);
  210. }
  211. // Record this among files we have just written.
  212. this->ReplyFiles.insert(fileName);
  213. return fileName;
  214. }
  215. Json::Value cmFileAPI::MaybeJsonFile(Json::Value in, std::string const& prefix)
  216. {
  217. Json::Value out;
  218. if (in.isObject() || in.isArray()) {
  219. out = Json::objectValue;
  220. out["jsonFile"] = this->WriteJsonFile(in, prefix);
  221. } else {
  222. out = std::move(in);
  223. }
  224. return out;
  225. }
  226. std::string cmFileAPI::ComputeSuffixHash(std::string const& file)
  227. {
  228. cmCryptoHash hasher(cmCryptoHash::AlgoSHA3_256);
  229. std::string hash = hasher.HashFile(file);
  230. hash.resize(20, '0');
  231. return hash;
  232. }
  233. std::string cmFileAPI::ComputeSuffixTime(std::string const&)
  234. {
  235. std::chrono::milliseconds ms =
  236. std::chrono::duration_cast<std::chrono::milliseconds>(
  237. std::chrono::system_clock::now().time_since_epoch());
  238. std::chrono::seconds s =
  239. std::chrono::duration_cast<std::chrono::seconds>(ms);
  240. std::time_t ts = s.count();
  241. std::size_t tms = ms.count() % 1000;
  242. cmTimestamp cmts;
  243. std::ostringstream ss;
  244. ss << cmts.CreateTimestampFromTimeT(ts, "%Y-%m-%dT%H-%M-%S", true) << '-'
  245. << std::setfill('0') << std::setw(4) << tms;
  246. return ss.str();
  247. }
  248. bool cmFileAPI::ReadQuery(std::string const& query,
  249. std::vector<Object>& objects)
  250. {
  251. // Parse the "<kind>-" syntax.
  252. std::string::size_type sep_pos = query.find('-');
  253. if (sep_pos == std::string::npos) {
  254. return false;
  255. }
  256. std::string kindName = query.substr(0, sep_pos);
  257. std::string verStr = query.substr(sep_pos + 1);
  258. if (kindName == ObjectKindName(ObjectKind::CodeModel)) {
  259. Object o;
  260. o.Kind = ObjectKind::CodeModel;
  261. if (verStr == "v2") {
  262. o.Version = 2;
  263. } else {
  264. return false;
  265. }
  266. objects.push_back(o);
  267. return true;
  268. }
  269. if (kindName == ObjectKindName(ObjectKind::ConfigureLog)) {
  270. Object o;
  271. o.Kind = ObjectKind::ConfigureLog;
  272. if (verStr == "v1") {
  273. o.Version = 1;
  274. } else {
  275. return false;
  276. }
  277. objects.push_back(o);
  278. return true;
  279. }
  280. if (kindName == ObjectKindName(ObjectKind::Cache)) {
  281. Object o;
  282. o.Kind = ObjectKind::Cache;
  283. if (verStr == "v2") {
  284. o.Version = 2;
  285. } else {
  286. return false;
  287. }
  288. objects.push_back(o);
  289. return true;
  290. }
  291. if (kindName == ObjectKindName(ObjectKind::CMakeFiles)) {
  292. Object o;
  293. o.Kind = ObjectKind::CMakeFiles;
  294. if (verStr == "v1") {
  295. o.Version = 1;
  296. } else {
  297. return false;
  298. }
  299. objects.push_back(o);
  300. return true;
  301. }
  302. if (kindName == ObjectKindName(ObjectKind::Toolchains)) {
  303. Object o;
  304. o.Kind = ObjectKind::Toolchains;
  305. if (verStr == "v1") {
  306. o.Version = 1;
  307. } else {
  308. return false;
  309. }
  310. objects.push_back(o);
  311. return true;
  312. }
  313. if (kindName == ObjectKindName(ObjectKind::InternalTest)) {
  314. Object o;
  315. o.Kind = ObjectKind::InternalTest;
  316. if (verStr == "v1") {
  317. o.Version = 1;
  318. } else if (verStr == "v2") {
  319. o.Version = 2;
  320. } else {
  321. return false;
  322. }
  323. objects.push_back(o);
  324. return true;
  325. }
  326. return false;
  327. }
  328. void cmFileAPI::ReadClient(std::string const& client)
  329. {
  330. // Load queries for the client.
  331. std::string clientDir = this->APIv1 + "/query/" + client;
  332. std::vector<std::string> queries = this->LoadDir(clientDir);
  333. // Read the queries and save for later.
  334. ClientQuery& clientQuery = this->ClientQueries[client];
  335. for (std::string& query : queries) {
  336. if (query == "query.json") {
  337. clientQuery.HaveQueryJson = true;
  338. this->ReadClientQuery(client, clientQuery.QueryJson);
  339. } else if (!this->ReadQuery(query, clientQuery.DirQuery.Known)) {
  340. clientQuery.DirQuery.Unknown.push_back(std::move(query));
  341. }
  342. }
  343. }
  344. void cmFileAPI::ReadClientQuery(std::string const& client, ClientQueryJson& q)
  345. {
  346. // Read the query.json file.
  347. std::string queryFile = this->APIv1 + "/query/" + client + "/query.json";
  348. Json::Value query;
  349. if (!this->ReadJsonFile(queryFile, query, q.Error)) {
  350. return;
  351. }
  352. if (!query.isObject()) {
  353. q.Error = "query root is not an object";
  354. return;
  355. }
  356. Json::Value const& clientValue = query["client"];
  357. if (!clientValue.isNull()) {
  358. q.ClientValue = clientValue;
  359. }
  360. q.RequestsValue = std::move(query["requests"]);
  361. q.Requests = this->BuildClientRequests(q.RequestsValue);
  362. }
  363. Json::Value cmFileAPI::BuildReplyIndex()
  364. {
  365. Json::Value index(Json::objectValue);
  366. // Report information about this version of CMake.
  367. index["cmake"] = this->BuildCMake();
  368. // Reply to all queries that we loaded.
  369. Json::Value& reply = index["reply"] = this->BuildReply(this->TopQuery);
  370. for (auto const& client : this->ClientQueries) {
  371. std::string const& clientName = client.first;
  372. ClientQuery const& clientQuery = client.second;
  373. reply[clientName] = this->BuildClientReply(clientQuery);
  374. }
  375. // Move our index of generated objects into its field.
  376. Json::Value& objects = index["objects"] = Json::arrayValue;
  377. for (auto& entry : this->ReplyIndexObjects) {
  378. objects.append(std::move(entry.second)); // NOLINT(*)
  379. }
  380. return index;
  381. }
  382. Json::Value cmFileAPI::BuildCMake()
  383. {
  384. Json::Value cmake = Json::objectValue;
  385. cmake["version"] = this->CMakeInstance->ReportVersionJson();
  386. Json::Value& cmake_paths = cmake["paths"] = Json::objectValue;
  387. cmake_paths["cmake"] = cmSystemTools::GetCMakeCommand();
  388. cmake_paths["ctest"] = cmSystemTools::GetCTestCommand();
  389. cmake_paths["cpack"] = cmSystemTools::GetCPackCommand();
  390. cmake_paths["root"] = cmSystemTools::GetCMakeRoot();
  391. cmake["generator"] = this->CMakeInstance->GetGlobalGenerator()->GetJson();
  392. return cmake;
  393. }
  394. Json::Value cmFileAPI::BuildReply(Query const& q)
  395. {
  396. Json::Value reply = Json::objectValue;
  397. for (Object const& o : q.Known) {
  398. std::string const& name = ObjectName(o);
  399. reply[name] = this->AddReplyIndexObject(o);
  400. }
  401. for (std::string const& name : q.Unknown) {
  402. reply[name] = cmFileAPI::BuildReplyError("unknown query file");
  403. }
  404. return reply;
  405. }
  406. Json::Value cmFileAPI::BuildReplyError(std::string const& error)
  407. {
  408. Json::Value e = Json::objectValue;
  409. e["error"] = error;
  410. return e;
  411. }
  412. Json::Value const& cmFileAPI::AddReplyIndexObject(Object const& o)
  413. {
  414. Json::Value& indexEntry = this->ReplyIndexObjects[o];
  415. if (!indexEntry.isNull()) {
  416. // The reply object has already been generated.
  417. return indexEntry;
  418. }
  419. // Generate this reply object.
  420. Json::Value const& object = this->BuildObject(o);
  421. assert(object.isObject());
  422. // Populate this index entry.
  423. indexEntry = Json::objectValue;
  424. indexEntry["kind"] = object["kind"];
  425. indexEntry["version"] = object["version"];
  426. indexEntry["jsonFile"] = this->WriteJsonFile(object, ObjectName(o));
  427. return indexEntry;
  428. }
  429. char const* cmFileAPI::ObjectKindName(ObjectKind kind)
  430. {
  431. // Keep in sync with ObjectKind enum.
  432. static char const* objectKindNames[] = {
  433. "codemodel", //
  434. "configureLog", //
  435. "cache", //
  436. "cmakeFiles", //
  437. "toolchains", //
  438. "__test" //
  439. };
  440. return objectKindNames[static_cast<size_t>(kind)];
  441. }
  442. std::string cmFileAPI::ObjectName(Object const& o)
  443. {
  444. std::string name = cmStrCat(ObjectKindName(o.Kind), "-v", o.Version);
  445. return name;
  446. }
  447. Json::Value cmFileAPI::BuildVersion(unsigned int major, unsigned int minor)
  448. {
  449. Json::Value version;
  450. version["major"] = major;
  451. version["minor"] = minor;
  452. return version;
  453. }
  454. Json::Value cmFileAPI::BuildObject(Object const& object)
  455. {
  456. Json::Value value;
  457. switch (object.Kind) {
  458. case ObjectKind::CodeModel:
  459. value = this->BuildCodeModel(object);
  460. break;
  461. case ObjectKind::ConfigureLog:
  462. value = this->BuildConfigureLog(object);
  463. break;
  464. case ObjectKind::Cache:
  465. value = this->BuildCache(object);
  466. break;
  467. case ObjectKind::CMakeFiles:
  468. value = this->BuildCMakeFiles(object);
  469. break;
  470. case ObjectKind::Toolchains:
  471. value = this->BuildToolchains(object);
  472. break;
  473. case ObjectKind::InternalTest:
  474. value = this->BuildInternalTest(object);
  475. break;
  476. }
  477. return value;
  478. }
  479. cmFileAPI::ClientRequests cmFileAPI::BuildClientRequests(
  480. Json::Value const& requests)
  481. {
  482. ClientRequests result;
  483. if (requests.isNull()) {
  484. result.Error = "'requests' member missing";
  485. return result;
  486. }
  487. if (!requests.isArray()) {
  488. result.Error = "'requests' member is not an array";
  489. return result;
  490. }
  491. result.reserve(requests.size());
  492. for (Json::Value const& request : requests) {
  493. result.emplace_back(this->BuildClientRequest(request));
  494. }
  495. return result;
  496. }
  497. cmFileAPI::ClientRequest cmFileAPI::BuildClientRequest(
  498. Json::Value const& request)
  499. {
  500. ClientRequest r;
  501. if (!request.isObject()) {
  502. r.Error = "request is not an object";
  503. return r;
  504. }
  505. Json::Value const& kind = request["kind"];
  506. if (kind.isNull()) {
  507. r.Error = "'kind' member missing";
  508. return r;
  509. }
  510. if (!kind.isString()) {
  511. r.Error = "'kind' member is not a string";
  512. return r;
  513. }
  514. std::string const& kindName = kind.asString();
  515. if (kindName == this->ObjectKindName(ObjectKind::CodeModel)) {
  516. r.Kind = ObjectKind::CodeModel;
  517. } else if (kindName == this->ObjectKindName(ObjectKind::ConfigureLog)) {
  518. r.Kind = ObjectKind::ConfigureLog;
  519. } else if (kindName == this->ObjectKindName(ObjectKind::Cache)) {
  520. r.Kind = ObjectKind::Cache;
  521. } else if (kindName == this->ObjectKindName(ObjectKind::CMakeFiles)) {
  522. r.Kind = ObjectKind::CMakeFiles;
  523. } else if (kindName == this->ObjectKindName(ObjectKind::Toolchains)) {
  524. r.Kind = ObjectKind::Toolchains;
  525. } else if (kindName == this->ObjectKindName(ObjectKind::InternalTest)) {
  526. r.Kind = ObjectKind::InternalTest;
  527. } else {
  528. r.Error = "unknown request kind '" + kindName + "'";
  529. return r;
  530. }
  531. Json::Value const& version = request["version"];
  532. if (version.isNull()) {
  533. r.Error = "'version' member missing";
  534. return r;
  535. }
  536. std::vector<RequestVersion> versions;
  537. if (!cmFileAPI::ReadRequestVersions(version, versions, r.Error)) {
  538. return r;
  539. }
  540. switch (r.Kind) {
  541. case ObjectKind::CodeModel:
  542. this->BuildClientRequestCodeModel(r, versions);
  543. break;
  544. case ObjectKind::ConfigureLog:
  545. this->BuildClientRequestConfigureLog(r, versions);
  546. break;
  547. case ObjectKind::Cache:
  548. this->BuildClientRequestCache(r, versions);
  549. break;
  550. case ObjectKind::CMakeFiles:
  551. this->BuildClientRequestCMakeFiles(r, versions);
  552. break;
  553. case ObjectKind::Toolchains:
  554. this->BuildClientRequestToolchains(r, versions);
  555. break;
  556. case ObjectKind::InternalTest:
  557. this->BuildClientRequestInternalTest(r, versions);
  558. break;
  559. }
  560. return r;
  561. }
  562. Json::Value cmFileAPI::BuildClientReply(ClientQuery const& q)
  563. {
  564. Json::Value reply = this->BuildReply(q.DirQuery);
  565. if (!q.HaveQueryJson) {
  566. return reply;
  567. }
  568. Json::Value& reply_query_json = reply["query.json"];
  569. ClientQueryJson const& qj = q.QueryJson;
  570. if (!qj.Error.empty()) {
  571. reply_query_json = this->BuildReplyError(qj.Error);
  572. return reply;
  573. }
  574. if (!qj.ClientValue.isNull()) {
  575. reply_query_json["client"] = qj.ClientValue;
  576. }
  577. if (!qj.RequestsValue.isNull()) {
  578. reply_query_json["requests"] = qj.RequestsValue;
  579. }
  580. reply_query_json["responses"] = this->BuildClientReplyResponses(qj.Requests);
  581. return reply;
  582. }
  583. Json::Value cmFileAPI::BuildClientReplyResponses(
  584. ClientRequests const& requests)
  585. {
  586. Json::Value responses;
  587. if (!requests.Error.empty()) {
  588. responses = this->BuildReplyError(requests.Error);
  589. return responses;
  590. }
  591. responses = Json::arrayValue;
  592. for (ClientRequest const& request : requests) {
  593. responses.append(this->BuildClientReplyResponse(request));
  594. }
  595. return responses;
  596. }
  597. Json::Value cmFileAPI::BuildClientReplyResponse(ClientRequest const& request)
  598. {
  599. Json::Value response;
  600. if (!request.Error.empty()) {
  601. response = this->BuildReplyError(request.Error);
  602. return response;
  603. }
  604. response = this->AddReplyIndexObject(request);
  605. return response;
  606. }
  607. bool cmFileAPI::ReadRequestVersions(Json::Value const& version,
  608. std::vector<RequestVersion>& versions,
  609. std::string& error)
  610. {
  611. if (version.isArray()) {
  612. for (Json::Value const& v : version) {
  613. if (!ReadRequestVersion(v, /*inArray=*/true, versions, error)) {
  614. return false;
  615. }
  616. }
  617. } else {
  618. if (!ReadRequestVersion(version, /*inArray=*/false, versions, error)) {
  619. return false;
  620. }
  621. }
  622. return true;
  623. }
  624. bool cmFileAPI::ReadRequestVersion(Json::Value const& version, bool inArray,
  625. std::vector<RequestVersion>& result,
  626. std::string& error)
  627. {
  628. if (version.isUInt()) {
  629. RequestVersion v;
  630. v.Major = version.asUInt();
  631. result.push_back(v);
  632. return true;
  633. }
  634. if (!version.isObject()) {
  635. if (inArray) {
  636. error = "'version' array entry is not a non-negative integer or object";
  637. } else {
  638. error =
  639. "'version' member is not a non-negative integer, object, or array";
  640. }
  641. return false;
  642. }
  643. Json::Value const& major = version["major"];
  644. if (major.isNull()) {
  645. error = "'version' object 'major' member missing";
  646. return false;
  647. }
  648. if (!major.isUInt()) {
  649. error = "'version' object 'major' member is not a non-negative integer";
  650. return false;
  651. }
  652. RequestVersion v;
  653. v.Major = major.asUInt();
  654. Json::Value const& minor = version["minor"];
  655. if (minor.isUInt()) {
  656. v.Minor = minor.asUInt();
  657. } else if (!minor.isNull()) {
  658. error = "'version' object 'minor' member is not a non-negative integer";
  659. return false;
  660. }
  661. result.push_back(v);
  662. return true;
  663. }
  664. std::string cmFileAPI::NoSupportedVersion(
  665. std::vector<RequestVersion> const& versions)
  666. {
  667. std::ostringstream msg;
  668. msg << "no supported version specified";
  669. if (!versions.empty()) {
  670. msg << " among:";
  671. for (RequestVersion const& v : versions) {
  672. msg << " " << v.Major << "." << v.Minor;
  673. }
  674. }
  675. return msg.str();
  676. }
  677. // The "codemodel" object kind.
  678. // Update Help/manual/cmake-file-api.7.rst when updating this constant.
  679. static unsigned int const CodeModelV2Minor = 8;
  680. void cmFileAPI::BuildClientRequestCodeModel(
  681. ClientRequest& r, std::vector<RequestVersion> const& versions)
  682. {
  683. // Select a known version from those requested.
  684. for (RequestVersion const& v : versions) {
  685. if ((v.Major == 2 && v.Minor <= CodeModelV2Minor)) {
  686. r.Version = v.Major;
  687. break;
  688. }
  689. }
  690. if (!r.Version) {
  691. r.Error = NoSupportedVersion(versions);
  692. }
  693. }
  694. Json::Value cmFileAPI::BuildCodeModel(Object const& object)
  695. {
  696. Json::Value codemodel = cmFileAPICodemodelDump(*this, object.Version);
  697. codemodel["kind"] = this->ObjectKindName(object.Kind);
  698. Json::Value& version = codemodel["version"];
  699. if (object.Version == 2) {
  700. version = BuildVersion(2, CodeModelV2Minor);
  701. } else {
  702. return codemodel; // should be unreachable
  703. }
  704. return codemodel;
  705. }
  706. // The "configureLog" object kind.
  707. // Update Help/manual/cmake-file-api.7.rst when updating this constant.
  708. static unsigned int const ConfigureLogV1Minor = 0;
  709. void cmFileAPI::BuildClientRequestConfigureLog(
  710. ClientRequest& r, std::vector<RequestVersion> const& versions)
  711. {
  712. // Select a known version from those requested.
  713. for (RequestVersion const& v : versions) {
  714. if ((v.Major == 1 && v.Minor <= ConfigureLogV1Minor)) {
  715. r.Version = v.Major;
  716. break;
  717. }
  718. }
  719. if (!r.Version) {
  720. r.Error = NoSupportedVersion(versions);
  721. }
  722. }
  723. Json::Value cmFileAPI::BuildConfigureLog(Object const& object)
  724. {
  725. Json::Value configureLog = cmFileAPIConfigureLogDump(*this, object.Version);
  726. configureLog["kind"] = this->ObjectKindName(object.Kind);
  727. Json::Value& version = configureLog["version"];
  728. if (object.Version == 1) {
  729. version = BuildVersion(1, ConfigureLogV1Minor);
  730. } else {
  731. return configureLog; // should be unreachable
  732. }
  733. return configureLog;
  734. }
  735. // The "cache" object kind.
  736. static unsigned int const CacheV2Minor = 0;
  737. void cmFileAPI::BuildClientRequestCache(
  738. ClientRequest& r, std::vector<RequestVersion> const& versions)
  739. {
  740. // Select a known version from those requested.
  741. for (RequestVersion const& v : versions) {
  742. if ((v.Major == 2 && v.Minor <= CacheV2Minor)) {
  743. r.Version = v.Major;
  744. break;
  745. }
  746. }
  747. if (!r.Version) {
  748. r.Error = NoSupportedVersion(versions);
  749. }
  750. }
  751. Json::Value cmFileAPI::BuildCache(Object const& object)
  752. {
  753. Json::Value cache = cmFileAPICacheDump(*this, object.Version);
  754. cache["kind"] = this->ObjectKindName(object.Kind);
  755. Json::Value& version = cache["version"];
  756. if (object.Version == 2) {
  757. version = BuildVersion(2, CacheV2Minor);
  758. } else {
  759. return cache; // should be unreachable
  760. }
  761. return cache;
  762. }
  763. // The "cmakeFiles" object kind.
  764. static unsigned int const CMakeFilesV1Minor = 1;
  765. void cmFileAPI::BuildClientRequestCMakeFiles(
  766. ClientRequest& r, std::vector<RequestVersion> const& versions)
  767. {
  768. // Select a known version from those requested.
  769. for (RequestVersion const& v : versions) {
  770. if ((v.Major == 1 && v.Minor <= CMakeFilesV1Minor)) {
  771. r.Version = v.Major;
  772. break;
  773. }
  774. }
  775. if (!r.Version) {
  776. r.Error = NoSupportedVersion(versions);
  777. }
  778. }
  779. Json::Value cmFileAPI::BuildCMakeFiles(Object const& object)
  780. {
  781. Json::Value cmakeFiles = cmFileAPICMakeFilesDump(*this, object.Version);
  782. cmakeFiles["kind"] = this->ObjectKindName(object.Kind);
  783. Json::Value& version = cmakeFiles["version"];
  784. if (object.Version == 1) {
  785. version = BuildVersion(1, CMakeFilesV1Minor);
  786. } else {
  787. return cmakeFiles; // should be unreachable
  788. }
  789. return cmakeFiles;
  790. }
  791. // The "toolchains" object kind.
  792. static unsigned int const ToolchainsV1Minor = 0;
  793. void cmFileAPI::BuildClientRequestToolchains(
  794. ClientRequest& r, std::vector<RequestVersion> const& versions)
  795. {
  796. // Select a known version from those requested.
  797. for (RequestVersion const& v : versions) {
  798. if ((v.Major == 1 && v.Minor <= ToolchainsV1Minor)) {
  799. r.Version = v.Major;
  800. break;
  801. }
  802. }
  803. if (!r.Version) {
  804. r.Error = NoSupportedVersion(versions);
  805. }
  806. }
  807. Json::Value cmFileAPI::BuildToolchains(Object const& object)
  808. {
  809. Json::Value toolchains = cmFileAPIToolchainsDump(*this, object.Version);
  810. toolchains["kind"] = this->ObjectKindName(object.Kind);
  811. Json::Value& version = toolchains["version"];
  812. if (object.Version == 1) {
  813. version = BuildVersion(1, ToolchainsV1Minor);
  814. } else {
  815. return toolchains; // should be unreachable
  816. }
  817. return toolchains;
  818. }
  819. // The "__test" object kind is for internal testing of CMake.
  820. static unsigned int const InternalTestV1Minor = 3;
  821. static unsigned int const InternalTestV2Minor = 0;
  822. void cmFileAPI::BuildClientRequestInternalTest(
  823. ClientRequest& r, std::vector<RequestVersion> const& versions)
  824. {
  825. // Select a known version from those requested.
  826. for (RequestVersion const& v : versions) {
  827. if ((v.Major == 1 && v.Minor <= InternalTestV1Minor) || //
  828. (v.Major == 2 && v.Minor <= InternalTestV2Minor)) {
  829. r.Version = v.Major;
  830. break;
  831. }
  832. }
  833. if (!r.Version) {
  834. r.Error = NoSupportedVersion(versions);
  835. }
  836. }
  837. Json::Value cmFileAPI::BuildInternalTest(Object const& object)
  838. {
  839. Json::Value test = Json::objectValue;
  840. test["kind"] = this->ObjectKindName(object.Kind);
  841. Json::Value& version = test["version"];
  842. if (object.Version == 2) {
  843. version = BuildVersion(2, InternalTestV2Minor);
  844. } else {
  845. version = BuildVersion(1, InternalTestV1Minor);
  846. }
  847. return test;
  848. }
  849. Json::Value cmFileAPI::ReportCapabilities()
  850. {
  851. Json::Value capabilities = Json::objectValue;
  852. Json::Value& requests = capabilities["requests"] = Json::arrayValue;
  853. {
  854. Json::Value request = Json::objectValue;
  855. request["kind"] = ObjectKindName(ObjectKind::CodeModel);
  856. Json::Value& versions = request["version"] = Json::arrayValue;
  857. versions.append(BuildVersion(2, CodeModelV2Minor));
  858. requests.append(std::move(request)); // NOLINT(*)
  859. }
  860. {
  861. Json::Value request = Json::objectValue;
  862. request["kind"] = ObjectKindName(ObjectKind::ConfigureLog);
  863. Json::Value& versions = request["version"] = Json::arrayValue;
  864. versions.append(BuildVersion(1, ConfigureLogV1Minor));
  865. requests.append(std::move(request)); // NOLINT(*)
  866. }
  867. {
  868. Json::Value request = Json::objectValue;
  869. request["kind"] = ObjectKindName(ObjectKind::Cache);
  870. Json::Value& versions = request["version"] = Json::arrayValue;
  871. versions.append(BuildVersion(2, CacheV2Minor));
  872. requests.append(std::move(request)); // NOLINT(*)
  873. }
  874. {
  875. Json::Value request = Json::objectValue;
  876. request["kind"] = ObjectKindName(ObjectKind::CMakeFiles);
  877. Json::Value& versions = request["version"] = Json::arrayValue;
  878. versions.append(BuildVersion(1, CMakeFilesV1Minor));
  879. requests.append(std::move(request)); // NOLINT(*)
  880. }
  881. {
  882. Json::Value request = Json::objectValue;
  883. request["kind"] = ObjectKindName(ObjectKind::Toolchains);
  884. Json::Value& versions = request["version"] = Json::arrayValue;
  885. versions.append(BuildVersion(1, ToolchainsV1Minor));
  886. requests.append(std::move(request)); // NOLINT(*)
  887. }
  888. return capabilities;
  889. }
  890. bool cmFileAPI::AddProjectQuery(cmFileAPI::ObjectKind kind,
  891. unsigned majorVersion, unsigned minorVersion)
  892. {
  893. switch (kind) {
  894. case ObjectKind::CodeModel:
  895. if (majorVersion != 2 || minorVersion > CodeModelV2Minor) {
  896. return false;
  897. }
  898. break;
  899. case ObjectKind::Cache:
  900. if (majorVersion != 2 || minorVersion > CacheV2Minor) {
  901. return false;
  902. }
  903. break;
  904. case ObjectKind::CMakeFiles:
  905. if (majorVersion != 1 || minorVersion > CMakeFilesV1Minor) {
  906. return false;
  907. }
  908. break;
  909. case ObjectKind::Toolchains:
  910. if (majorVersion != 1 || minorVersion > ToolchainsV1Minor) {
  911. return false;
  912. }
  913. break;
  914. // These cannot be requested by the project
  915. case ObjectKind::ConfigureLog:
  916. case ObjectKind::InternalTest:
  917. return false;
  918. }
  919. Object query;
  920. query.Kind = kind;
  921. query.Version = majorVersion;
  922. if (std::find(this->TopQuery.Known.begin(), this->TopQuery.Known.end(),
  923. query) == this->TopQuery.Known.end()) {
  924. this->TopQuery.Known.emplace_back(query);
  925. this->QueryExists = true;
  926. }
  927. return true;
  928. }