cmFileAPI.cxx 30 KB

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