cmFileAPI.cxx 27 KB

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