cmFileAPI.cxx 30 KB

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