cmFileAPI.cxx 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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. 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->BuildReplyEntry(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::BuildReplyEntry(Object const& object)
  407. {
  408. return this->AddReplyIndexObject(object);
  409. }
  410. Json::Value cmFileAPI::BuildReplyError(std::string const& error)
  411. {
  412. Json::Value e = Json::objectValue;
  413. e["error"] = error;
  414. return e;
  415. }
  416. Json::Value const& cmFileAPI::AddReplyIndexObject(Object const& o)
  417. {
  418. Json::Value& indexEntry = this->ReplyIndexObjects[o];
  419. if (!indexEntry.isNull()) {
  420. // The reply object has already been generated.
  421. return indexEntry;
  422. }
  423. // Generate this reply object.
  424. Json::Value const& object = this->BuildObject(o);
  425. assert(object.isObject());
  426. // Populate this index entry.
  427. indexEntry = Json::objectValue;
  428. indexEntry["kind"] = object["kind"];
  429. indexEntry["version"] = object["version"];
  430. indexEntry["jsonFile"] = this->WriteJsonFile(object, ObjectName(o));
  431. return indexEntry;
  432. }
  433. char const* cmFileAPI::ObjectKindName(ObjectKind kind)
  434. {
  435. // Keep in sync with ObjectKind enum.
  436. static char const* objectKindNames[] = {
  437. "codemodel", //
  438. "configureLog", //
  439. "cache", //
  440. "cmakeFiles", //
  441. "toolchains", //
  442. "__test" //
  443. };
  444. return objectKindNames[static_cast<size_t>(kind)];
  445. }
  446. std::string cmFileAPI::ObjectName(Object const& o)
  447. {
  448. std::string name = cmStrCat(ObjectKindName(o.Kind), "-v", o.Version);
  449. return name;
  450. }
  451. Json::Value cmFileAPI::BuildVersion(unsigned int major, unsigned int minor)
  452. {
  453. Json::Value version;
  454. version["major"] = major;
  455. version["minor"] = minor;
  456. return version;
  457. }
  458. Json::Value cmFileAPI::BuildObject(Object const& object)
  459. {
  460. Json::Value value;
  461. switch (object.Kind) {
  462. case ObjectKind::CodeModel:
  463. value = this->BuildCodeModel(object);
  464. break;
  465. case ObjectKind::ConfigureLog:
  466. value = this->BuildConfigureLog(object);
  467. break;
  468. case ObjectKind::Cache:
  469. value = this->BuildCache(object);
  470. break;
  471. case ObjectKind::CMakeFiles:
  472. value = this->BuildCMakeFiles(object);
  473. break;
  474. case ObjectKind::Toolchains:
  475. value = this->BuildToolchains(object);
  476. break;
  477. case ObjectKind::InternalTest:
  478. value = this->BuildInternalTest(object);
  479. break;
  480. }
  481. return value;
  482. }
  483. cmFileAPI::ClientRequests cmFileAPI::BuildClientRequests(
  484. Json::Value const& requests)
  485. {
  486. ClientRequests result;
  487. if (requests.isNull()) {
  488. result.Error = "'requests' member missing";
  489. return result;
  490. }
  491. if (!requests.isArray()) {
  492. result.Error = "'requests' member is not an array";
  493. return result;
  494. }
  495. result.reserve(requests.size());
  496. for (Json::Value const& request : requests) {
  497. result.emplace_back(this->BuildClientRequest(request));
  498. }
  499. return result;
  500. }
  501. cmFileAPI::ClientRequest cmFileAPI::BuildClientRequest(
  502. Json::Value const& request)
  503. {
  504. ClientRequest r;
  505. if (!request.isObject()) {
  506. r.Error = "request is not an object";
  507. return r;
  508. }
  509. Json::Value const& kind = request["kind"];
  510. if (kind.isNull()) {
  511. r.Error = "'kind' member missing";
  512. return r;
  513. }
  514. if (!kind.isString()) {
  515. r.Error = "'kind' member is not a string";
  516. return r;
  517. }
  518. std::string const& kindName = kind.asString();
  519. if (kindName == this->ObjectKindName(ObjectKind::CodeModel)) {
  520. r.Kind = ObjectKind::CodeModel;
  521. } else if (kindName == this->ObjectKindName(ObjectKind::ConfigureLog)) {
  522. r.Kind = ObjectKind::ConfigureLog;
  523. } else if (kindName == this->ObjectKindName(ObjectKind::Cache)) {
  524. r.Kind = ObjectKind::Cache;
  525. } else if (kindName == this->ObjectKindName(ObjectKind::CMakeFiles)) {
  526. r.Kind = ObjectKind::CMakeFiles;
  527. } else if (kindName == this->ObjectKindName(ObjectKind::Toolchains)) {
  528. r.Kind = ObjectKind::Toolchains;
  529. } else if (kindName == this->ObjectKindName(ObjectKind::InternalTest)) {
  530. r.Kind = ObjectKind::InternalTest;
  531. } else {
  532. r.Error = "unknown request kind '" + kindName + "'";
  533. return r;
  534. }
  535. Json::Value const& version = request["version"];
  536. if (version.isNull()) {
  537. r.Error = "'version' member missing";
  538. return r;
  539. }
  540. std::vector<RequestVersion> versions;
  541. if (!cmFileAPI::ReadRequestVersions(version, versions, r.Error)) {
  542. return r;
  543. }
  544. switch (r.Kind) {
  545. case ObjectKind::CodeModel:
  546. this->BuildClientRequestCodeModel(r, versions);
  547. break;
  548. case ObjectKind::ConfigureLog:
  549. this->BuildClientRequestConfigureLog(r, versions);
  550. break;
  551. case ObjectKind::Cache:
  552. this->BuildClientRequestCache(r, versions);
  553. break;
  554. case ObjectKind::CMakeFiles:
  555. this->BuildClientRequestCMakeFiles(r, versions);
  556. break;
  557. case ObjectKind::Toolchains:
  558. this->BuildClientRequestToolchains(r, versions);
  559. break;
  560. case ObjectKind::InternalTest:
  561. this->BuildClientRequestInternalTest(r, versions);
  562. break;
  563. }
  564. return r;
  565. }
  566. Json::Value cmFileAPI::BuildClientReply(ClientQuery const& q)
  567. {
  568. Json::Value reply = this->BuildReply(q.DirQuery);
  569. if (!q.HaveQueryJson) {
  570. return reply;
  571. }
  572. Json::Value& reply_query_json = reply["query.json"];
  573. ClientQueryJson const& qj = q.QueryJson;
  574. if (!qj.Error.empty()) {
  575. reply_query_json = this->BuildReplyError(qj.Error);
  576. return reply;
  577. }
  578. if (!qj.ClientValue.isNull()) {
  579. reply_query_json["client"] = qj.ClientValue;
  580. }
  581. if (!qj.RequestsValue.isNull()) {
  582. reply_query_json["requests"] = qj.RequestsValue;
  583. }
  584. reply_query_json["responses"] = this->BuildClientReplyResponses(qj.Requests);
  585. return reply;
  586. }
  587. Json::Value cmFileAPI::BuildClientReplyResponses(
  588. ClientRequests const& requests)
  589. {
  590. Json::Value responses;
  591. if (!requests.Error.empty()) {
  592. responses = this->BuildReplyError(requests.Error);
  593. return responses;
  594. }
  595. responses = Json::arrayValue;
  596. for (ClientRequest const& request : requests) {
  597. responses.append(this->BuildClientReplyResponse(request));
  598. }
  599. return responses;
  600. }
  601. Json::Value cmFileAPI::BuildClientReplyResponse(ClientRequest const& request)
  602. {
  603. Json::Value response;
  604. if (!request.Error.empty()) {
  605. response = this->BuildReplyError(request.Error);
  606. return response;
  607. }
  608. response = this->BuildReplyEntry(request);
  609. return response;
  610. }
  611. bool cmFileAPI::ReadRequestVersions(Json::Value const& version,
  612. std::vector<RequestVersion>& versions,
  613. std::string& error)
  614. {
  615. if (version.isArray()) {
  616. for (Json::Value const& v : version) {
  617. if (!ReadRequestVersion(v, /*inArray=*/true, versions, error)) {
  618. return false;
  619. }
  620. }
  621. } else {
  622. if (!ReadRequestVersion(version, /*inArray=*/false, versions, error)) {
  623. return false;
  624. }
  625. }
  626. return true;
  627. }
  628. bool cmFileAPI::ReadRequestVersion(Json::Value const& version, bool inArray,
  629. std::vector<RequestVersion>& result,
  630. std::string& error)
  631. {
  632. if (version.isUInt()) {
  633. RequestVersion v;
  634. v.Major = version.asUInt();
  635. result.push_back(v);
  636. return true;
  637. }
  638. if (!version.isObject()) {
  639. if (inArray) {
  640. error = "'version' array entry is not a non-negative integer or object";
  641. } else {
  642. error =
  643. "'version' member is not a non-negative integer, object, or array";
  644. }
  645. return false;
  646. }
  647. Json::Value const& major = version["major"];
  648. if (major.isNull()) {
  649. error = "'version' object 'major' member missing";
  650. return false;
  651. }
  652. if (!major.isUInt()) {
  653. error = "'version' object 'major' member is not a non-negative integer";
  654. return false;
  655. }
  656. RequestVersion v;
  657. v.Major = major.asUInt();
  658. Json::Value const& minor = version["minor"];
  659. if (minor.isUInt()) {
  660. v.Minor = minor.asUInt();
  661. } else if (!minor.isNull()) {
  662. error = "'version' object 'minor' member is not a non-negative integer";
  663. return false;
  664. }
  665. result.push_back(v);
  666. return true;
  667. }
  668. std::string cmFileAPI::NoSupportedVersion(
  669. std::vector<RequestVersion> const& versions)
  670. {
  671. std::ostringstream msg;
  672. msg << "no supported version specified";
  673. if (!versions.empty()) {
  674. msg << " among:";
  675. for (RequestVersion const& v : versions) {
  676. msg << " " << v.Major << "." << v.Minor;
  677. }
  678. }
  679. return msg.str();
  680. }
  681. // The "codemodel" object kind.
  682. // Update Help/manual/cmake-file-api.7.rst when updating this constant.
  683. static unsigned int const CodeModelV2Minor = 8;
  684. void cmFileAPI::BuildClientRequestCodeModel(
  685. ClientRequest& r, std::vector<RequestVersion> const& versions)
  686. {
  687. // Select a known version from those requested.
  688. for (RequestVersion const& v : versions) {
  689. if ((v.Major == 2 && v.Minor <= CodeModelV2Minor)) {
  690. r.Version = v.Major;
  691. break;
  692. }
  693. }
  694. if (!r.Version) {
  695. r.Error = NoSupportedVersion(versions);
  696. }
  697. }
  698. Json::Value cmFileAPI::BuildCodeModel(Object const& object)
  699. {
  700. Json::Value codemodel = cmFileAPICodemodelDump(*this, object.Version);
  701. codemodel["kind"] = this->ObjectKindName(object.Kind);
  702. Json::Value& version = codemodel["version"];
  703. if (object.Version == 2) {
  704. version = BuildVersion(2, CodeModelV2Minor);
  705. } else {
  706. return codemodel; // should be unreachable
  707. }
  708. return codemodel;
  709. }
  710. // The "configureLog" object kind.
  711. // Update Help/manual/cmake-file-api.7.rst when updating this constant.
  712. static unsigned int const ConfigureLogV1Minor = 0;
  713. void cmFileAPI::BuildClientRequestConfigureLog(
  714. ClientRequest& r, std::vector<RequestVersion> const& versions)
  715. {
  716. // Select a known version from those requested.
  717. for (RequestVersion const& v : versions) {
  718. if ((v.Major == 1 && v.Minor <= ConfigureLogV1Minor)) {
  719. r.Version = v.Major;
  720. break;
  721. }
  722. }
  723. if (!r.Version) {
  724. r.Error = NoSupportedVersion(versions);
  725. }
  726. }
  727. Json::Value cmFileAPI::BuildConfigureLog(Object const& object)
  728. {
  729. Json::Value configureLog = cmFileAPIConfigureLogDump(*this, object.Version);
  730. configureLog["kind"] = this->ObjectKindName(object.Kind);
  731. Json::Value& version = configureLog["version"];
  732. if (object.Version == 1) {
  733. version = BuildVersion(1, ConfigureLogV1Minor);
  734. } else {
  735. return configureLog; // should be unreachable
  736. }
  737. return configureLog;
  738. }
  739. // The "cache" object kind.
  740. static unsigned int const CacheV2Minor = 0;
  741. void cmFileAPI::BuildClientRequestCache(
  742. ClientRequest& r, std::vector<RequestVersion> const& versions)
  743. {
  744. // Select a known version from those requested.
  745. for (RequestVersion const& v : versions) {
  746. if ((v.Major == 2 && v.Minor <= CacheV2Minor)) {
  747. r.Version = v.Major;
  748. break;
  749. }
  750. }
  751. if (!r.Version) {
  752. r.Error = NoSupportedVersion(versions);
  753. }
  754. }
  755. Json::Value cmFileAPI::BuildCache(Object const& object)
  756. {
  757. Json::Value cache = cmFileAPICacheDump(*this, object.Version);
  758. cache["kind"] = this->ObjectKindName(object.Kind);
  759. Json::Value& version = cache["version"];
  760. if (object.Version == 2) {
  761. version = BuildVersion(2, CacheV2Minor);
  762. } else {
  763. return cache; // should be unreachable
  764. }
  765. return cache;
  766. }
  767. // The "cmakeFiles" object kind.
  768. static unsigned int const CMakeFilesV1Minor = 1;
  769. void cmFileAPI::BuildClientRequestCMakeFiles(
  770. ClientRequest& r, std::vector<RequestVersion> const& versions)
  771. {
  772. // Select a known version from those requested.
  773. for (RequestVersion const& v : versions) {
  774. if ((v.Major == 1 && v.Minor <= CMakeFilesV1Minor)) {
  775. r.Version = v.Major;
  776. break;
  777. }
  778. }
  779. if (!r.Version) {
  780. r.Error = NoSupportedVersion(versions);
  781. }
  782. }
  783. Json::Value cmFileAPI::BuildCMakeFiles(Object const& object)
  784. {
  785. Json::Value cmakeFiles = cmFileAPICMakeFilesDump(*this, object.Version);
  786. cmakeFiles["kind"] = this->ObjectKindName(object.Kind);
  787. Json::Value& version = cmakeFiles["version"];
  788. if (object.Version == 1) {
  789. version = BuildVersion(1, CMakeFilesV1Minor);
  790. } else {
  791. return cmakeFiles; // should be unreachable
  792. }
  793. return cmakeFiles;
  794. }
  795. // The "toolchains" object kind.
  796. static unsigned int const ToolchainsV1Minor = 0;
  797. void cmFileAPI::BuildClientRequestToolchains(
  798. ClientRequest& r, std::vector<RequestVersion> const& versions)
  799. {
  800. // Select a known version from those requested.
  801. for (RequestVersion const& v : versions) {
  802. if ((v.Major == 1 && v.Minor <= ToolchainsV1Minor)) {
  803. r.Version = v.Major;
  804. break;
  805. }
  806. }
  807. if (!r.Version) {
  808. r.Error = NoSupportedVersion(versions);
  809. }
  810. }
  811. Json::Value cmFileAPI::BuildToolchains(Object const& object)
  812. {
  813. Json::Value toolchains = cmFileAPIToolchainsDump(*this, object.Version);
  814. toolchains["kind"] = this->ObjectKindName(object.Kind);
  815. Json::Value& version = toolchains["version"];
  816. if (object.Version == 1) {
  817. version = BuildVersion(1, ToolchainsV1Minor);
  818. } else {
  819. return toolchains; // should be unreachable
  820. }
  821. return toolchains;
  822. }
  823. // The "__test" object kind is for internal testing of CMake.
  824. static unsigned int const InternalTestV1Minor = 3;
  825. static unsigned int const InternalTestV2Minor = 0;
  826. void cmFileAPI::BuildClientRequestInternalTest(
  827. ClientRequest& r, std::vector<RequestVersion> const& versions)
  828. {
  829. // Select a known version from those requested.
  830. for (RequestVersion const& v : versions) {
  831. if ((v.Major == 1 && v.Minor <= InternalTestV1Minor) || //
  832. (v.Major == 2 && v.Minor <= InternalTestV2Minor)) {
  833. r.Version = v.Major;
  834. break;
  835. }
  836. }
  837. if (!r.Version) {
  838. r.Error = NoSupportedVersion(versions);
  839. }
  840. }
  841. Json::Value cmFileAPI::BuildInternalTest(Object const& object)
  842. {
  843. Json::Value test = Json::objectValue;
  844. test["kind"] = this->ObjectKindName(object.Kind);
  845. Json::Value& version = test["version"];
  846. if (object.Version == 2) {
  847. version = BuildVersion(2, InternalTestV2Minor);
  848. } else {
  849. version = BuildVersion(1, InternalTestV1Minor);
  850. }
  851. return test;
  852. }
  853. Json::Value cmFileAPI::ReportCapabilities()
  854. {
  855. Json::Value capabilities = Json::objectValue;
  856. Json::Value& requests = capabilities["requests"] = Json::arrayValue;
  857. {
  858. Json::Value request = Json::objectValue;
  859. request["kind"] = ObjectKindName(ObjectKind::CodeModel);
  860. Json::Value& versions = request["version"] = Json::arrayValue;
  861. versions.append(BuildVersion(2, CodeModelV2Minor));
  862. requests.append(std::move(request)); // NOLINT(*)
  863. }
  864. {
  865. Json::Value request = Json::objectValue;
  866. request["kind"] = ObjectKindName(ObjectKind::ConfigureLog);
  867. Json::Value& versions = request["version"] = Json::arrayValue;
  868. versions.append(BuildVersion(1, ConfigureLogV1Minor));
  869. requests.append(std::move(request)); // NOLINT(*)
  870. }
  871. {
  872. Json::Value request = Json::objectValue;
  873. request["kind"] = ObjectKindName(ObjectKind::Cache);
  874. Json::Value& versions = request["version"] = Json::arrayValue;
  875. versions.append(BuildVersion(2, CacheV2Minor));
  876. requests.append(std::move(request)); // NOLINT(*)
  877. }
  878. {
  879. Json::Value request = Json::objectValue;
  880. request["kind"] = ObjectKindName(ObjectKind::CMakeFiles);
  881. Json::Value& versions = request["version"] = Json::arrayValue;
  882. versions.append(BuildVersion(1, CMakeFilesV1Minor));
  883. requests.append(std::move(request)); // NOLINT(*)
  884. }
  885. {
  886. Json::Value request = Json::objectValue;
  887. request["kind"] = ObjectKindName(ObjectKind::Toolchains);
  888. Json::Value& versions = request["version"] = Json::arrayValue;
  889. versions.append(BuildVersion(1, ToolchainsV1Minor));
  890. requests.append(std::move(request)); // NOLINT(*)
  891. }
  892. return capabilities;
  893. }
  894. bool cmFileAPI::AddProjectQuery(cmFileAPI::ObjectKind kind,
  895. unsigned majorVersion, unsigned minorVersion)
  896. {
  897. switch (kind) {
  898. case ObjectKind::CodeModel:
  899. if (majorVersion != 2 || minorVersion > CodeModelV2Minor) {
  900. return false;
  901. }
  902. break;
  903. case ObjectKind::Cache:
  904. if (majorVersion != 2 || minorVersion > CacheV2Minor) {
  905. return false;
  906. }
  907. break;
  908. case ObjectKind::CMakeFiles:
  909. if (majorVersion != 1 || minorVersion > CMakeFilesV1Minor) {
  910. return false;
  911. }
  912. break;
  913. case ObjectKind::Toolchains:
  914. if (majorVersion != 1 || minorVersion > ToolchainsV1Minor) {
  915. return false;
  916. }
  917. break;
  918. // These cannot be requested by the project
  919. case ObjectKind::ConfigureLog:
  920. case ObjectKind::InternalTest:
  921. return false;
  922. }
  923. Object query;
  924. query.Kind = kind;
  925. query.Version = majorVersion;
  926. if (std::find(this->TopQuery.Known.begin(), this->TopQuery.Known.end(),
  927. query) == this->TopQuery.Known.end()) {
  928. this->TopQuery.Known.emplace_back(query);
  929. this->QueryExists = true;
  930. }
  931. return true;
  932. }