cmFileAPI.cxx 26 KB

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