cmFileAPICodemodel.cxx 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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 "cmFileAPICodemodel.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmCryptoHash.h"
  6. #include "cmFileAPI.h"
  7. #include "cmGeneratorExpression.h"
  8. #include "cmGeneratorTarget.h"
  9. #include "cmGlobalGenerator.h"
  10. #include "cmInstallGenerator.h"
  11. #include "cmInstallSubdirectoryGenerator.h"
  12. #include "cmInstallTargetGenerator.h"
  13. #include "cmLinkLineComputer.h"
  14. #include "cmListFileCache.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmSourceFile.h"
  18. #include "cmSourceGroup.h"
  19. #include "cmState.h"
  20. #include "cmStateDirectory.h"
  21. #include "cmStateSnapshot.h"
  22. #include "cmStateTypes.h"
  23. #include "cmSystemTools.h"
  24. #include "cmTarget.h"
  25. #include "cmTargetDepend.h"
  26. #include "cmake.h"
  27. #include "cm_jsoncpp_value.h"
  28. #include <algorithm>
  29. #include <cassert>
  30. #include <map>
  31. #include <memory>
  32. #include <set>
  33. #include <string>
  34. #include <unordered_map>
  35. #include <utility>
  36. #include <vector>
  37. namespace {
  38. class Codemodel
  39. {
  40. cmFileAPI& FileAPI;
  41. unsigned long Version;
  42. Json::Value DumpPaths();
  43. Json::Value DumpConfigurations();
  44. Json::Value DumpConfiguration(std::string const& config);
  45. public:
  46. Codemodel(cmFileAPI& fileAPI, unsigned long version);
  47. Json::Value Dump();
  48. };
  49. class CodemodelConfig
  50. {
  51. cmFileAPI& FileAPI;
  52. unsigned long Version;
  53. std::string const& Config;
  54. std::string TopSource;
  55. std::string TopBuild;
  56. struct Directory
  57. {
  58. cmStateSnapshot Snapshot;
  59. cmLocalGenerator const* LocalGenerator = nullptr;
  60. Json::Value TargetIndexes = Json::arrayValue;
  61. Json::ArrayIndex ProjectIndex;
  62. bool HasInstallRule = false;
  63. };
  64. std::map<cmStateSnapshot, Json::ArrayIndex, cmStateSnapshot::StrictWeakOrder>
  65. DirectoryMap;
  66. std::vector<Directory> Directories;
  67. struct Project
  68. {
  69. cmStateSnapshot Snapshot;
  70. static const Json::ArrayIndex NoParentIndex =
  71. static_cast<Json::ArrayIndex>(-1);
  72. Json::ArrayIndex ParentIndex = NoParentIndex;
  73. Json::Value ChildIndexes = Json::arrayValue;
  74. Json::Value DirectoryIndexes = Json::arrayValue;
  75. Json::Value TargetIndexes = Json::arrayValue;
  76. };
  77. std::map<cmStateSnapshot, Json::ArrayIndex, cmStateSnapshot::StrictWeakOrder>
  78. ProjectMap;
  79. std::vector<Project> Projects;
  80. void ProcessDirectories();
  81. Json::ArrayIndex GetDirectoryIndex(cmLocalGenerator const* lg);
  82. Json::ArrayIndex GetDirectoryIndex(cmStateSnapshot s);
  83. Json::ArrayIndex AddProject(cmStateSnapshot s);
  84. Json::Value DumpTargets();
  85. Json::Value DumpTarget(cmGeneratorTarget* gt, Json::ArrayIndex ti);
  86. Json::Value DumpDirectories();
  87. Json::Value DumpDirectory(Directory& d);
  88. Json::Value DumpProjects();
  89. Json::Value DumpProject(Project& p);
  90. Json::Value DumpMinimumCMakeVersion(cmStateSnapshot s);
  91. public:
  92. CodemodelConfig(cmFileAPI& fileAPI, unsigned long version,
  93. std::string const& config);
  94. Json::Value Dump();
  95. };
  96. std::string RelativeIfUnder(std::string const& top, std::string const& in)
  97. {
  98. std::string out;
  99. if (in == top) {
  100. out = ".";
  101. } else if (cmSystemTools::IsSubDirectory(in, top)) {
  102. out = in.substr(top.size() + 1);
  103. } else {
  104. out = in;
  105. }
  106. return out;
  107. }
  108. std::string TargetId(cmGeneratorTarget const* gt, std::string const& topBuild)
  109. {
  110. cmCryptoHash hasher(cmCryptoHash::AlgoSHA3_256);
  111. std::string path = RelativeIfUnder(
  112. topBuild, gt->GetLocalGenerator()->GetCurrentBinaryDirectory());
  113. std::string hash = hasher.HashString(path);
  114. hash.resize(20, '0');
  115. return gt->GetName() + CMAKE_DIRECTORY_ID_SEP + hash;
  116. }
  117. class BacktraceData
  118. {
  119. std::string TopSource;
  120. std::unordered_map<std::string, Json::ArrayIndex> CommandMap;
  121. std::unordered_map<std::string, Json::ArrayIndex> FileMap;
  122. std::unordered_map<cmListFileContext const*, Json::ArrayIndex> NodeMap;
  123. Json::Value Commands = Json::arrayValue;
  124. Json::Value Files = Json::arrayValue;
  125. Json::Value Nodes = Json::arrayValue;
  126. Json::ArrayIndex AddCommand(std::string const& command)
  127. {
  128. auto i = this->CommandMap.find(command);
  129. if (i == this->CommandMap.end()) {
  130. auto cmdIndex = static_cast<Json::ArrayIndex>(this->Commands.size());
  131. i = this->CommandMap.emplace(command, cmdIndex).first;
  132. this->Commands.append(command);
  133. }
  134. return i->second;
  135. }
  136. Json::ArrayIndex AddFile(std::string const& file)
  137. {
  138. auto i = this->FileMap.find(file);
  139. if (i == this->FileMap.end()) {
  140. auto fileIndex = static_cast<Json::ArrayIndex>(this->Files.size());
  141. i = this->FileMap.emplace(file, fileIndex).first;
  142. this->Files.append(RelativeIfUnder(this->TopSource, file));
  143. }
  144. return i->second;
  145. }
  146. public:
  147. BacktraceData(std::string topSource);
  148. bool Add(cmListFileBacktrace const& bt, Json::ArrayIndex& index);
  149. Json::Value Dump();
  150. };
  151. BacktraceData::BacktraceData(std::string topSource)
  152. : TopSource(std::move(topSource))
  153. {
  154. }
  155. bool BacktraceData::Add(cmListFileBacktrace const& bt, Json::ArrayIndex& index)
  156. {
  157. if (bt.Empty()) {
  158. return false;
  159. }
  160. cmListFileContext const* top = &bt.Top();
  161. auto found = this->NodeMap.find(top);
  162. if (found != this->NodeMap.end()) {
  163. index = found->second;
  164. return true;
  165. }
  166. Json::Value entry = Json::objectValue;
  167. entry["file"] = this->AddFile(top->FilePath);
  168. if (top->Line) {
  169. entry["line"] = static_cast<int>(top->Line);
  170. }
  171. if (!top->Name.empty()) {
  172. entry["command"] = this->AddCommand(top->Name);
  173. }
  174. Json::ArrayIndex parent;
  175. if (this->Add(bt.Pop(), parent)) {
  176. entry["parent"] = parent;
  177. }
  178. index = this->NodeMap[top] = this->Nodes.size();
  179. this->Nodes.append(std::move(entry)); // NOLINT(*)
  180. return true;
  181. }
  182. Json::Value BacktraceData::Dump()
  183. {
  184. Json::Value backtraceGraph;
  185. this->CommandMap.clear();
  186. this->FileMap.clear();
  187. this->NodeMap.clear();
  188. backtraceGraph["commands"] = std::move(this->Commands);
  189. backtraceGraph["files"] = std::move(this->Files);
  190. backtraceGraph["nodes"] = std::move(this->Nodes);
  191. return backtraceGraph;
  192. }
  193. struct CompileData
  194. {
  195. struct IncludeEntry
  196. {
  197. BT<std::string> Path;
  198. bool IsSystem = false;
  199. IncludeEntry(BT<std::string> path, bool isSystem)
  200. : Path(std::move(path))
  201. , IsSystem(isSystem)
  202. {
  203. }
  204. };
  205. void SetDefines(std::set<BT<std::string>> const& defines);
  206. std::string Language;
  207. std::string Sysroot;
  208. std::vector<BT<std::string>> Flags;
  209. std::vector<BT<std::string>> Defines;
  210. std::vector<IncludeEntry> Includes;
  211. };
  212. void CompileData::SetDefines(std::set<BT<std::string>> const& defines)
  213. {
  214. this->Defines.reserve(defines.size());
  215. for (BT<std::string> const& d : defines) {
  216. this->Defines.push_back(d);
  217. }
  218. }
  219. class Target
  220. {
  221. cmGeneratorTarget* GT;
  222. std::string const& Config;
  223. std::string TopSource;
  224. std::string TopBuild;
  225. std::vector<cmSourceGroup> SourceGroupsLocal;
  226. BacktraceData Backtraces;
  227. std::map<std::string, CompileData> CompileDataMap;
  228. std::unordered_map<cmSourceFile const*, Json::ArrayIndex> SourceMap;
  229. Json::Value Sources = Json::arrayValue;
  230. struct SourceGroup
  231. {
  232. std::string Name;
  233. Json::Value SourceIndexes = Json::arrayValue;
  234. };
  235. std::unordered_map<cmSourceGroup const*, Json::ArrayIndex> SourceGroupsMap;
  236. std::vector<SourceGroup> SourceGroups;
  237. struct CompileGroup
  238. {
  239. std::map<Json::Value, Json::ArrayIndex>::iterator Entry;
  240. Json::Value SourceIndexes = Json::arrayValue;
  241. };
  242. std::map<Json::Value, Json::ArrayIndex> CompileGroupMap;
  243. std::vector<CompileGroup> CompileGroups;
  244. void ProcessLanguages();
  245. void ProcessLanguage(std::string const& lang);
  246. Json::ArrayIndex AddSourceGroup(cmSourceGroup* sg, Json::ArrayIndex si);
  247. CompileData BuildCompileData(cmSourceFile* sf);
  248. Json::ArrayIndex AddSourceCompileGroup(cmSourceFile* sf,
  249. Json::ArrayIndex si);
  250. void AddBacktrace(Json::Value& object, cmListFileBacktrace const& bt);
  251. Json::Value DumpPaths();
  252. Json::Value DumpCompileData(CompileData cd);
  253. Json::Value DumpInclude(CompileData::IncludeEntry const& inc);
  254. Json::Value DumpDefine(BT<std::string> const& def);
  255. Json::Value DumpSources();
  256. Json::Value DumpSource(cmGeneratorTarget::SourceAndKind const& sk,
  257. Json::ArrayIndex si);
  258. Json::Value DumpSourceGroups();
  259. Json::Value DumpSourceGroup(SourceGroup& sg);
  260. Json::Value DumpCompileGroups();
  261. Json::Value DumpCompileGroup(CompileGroup& cg);
  262. Json::Value DumpSysroot(std::string const& path);
  263. Json::Value DumpInstall();
  264. Json::Value DumpInstallPrefix();
  265. Json::Value DumpInstallDestinations();
  266. Json::Value DumpInstallDestination(cmInstallTargetGenerator* itGen);
  267. Json::Value DumpArtifacts();
  268. Json::Value DumpLink();
  269. Json::Value DumpArchive();
  270. Json::Value DumpLinkCommandFragments();
  271. Json::Value DumpCommandFragments(std::vector<BT<std::string>> const& frags);
  272. Json::Value DumpCommandFragment(BT<std::string> const& frag,
  273. std::string const& role = std::string());
  274. Json::Value DumpDependencies();
  275. Json::Value DumpDependency(cmTargetDepend const& td);
  276. Json::Value DumpFolder();
  277. public:
  278. Target(cmGeneratorTarget* gt, std::string const& config);
  279. Json::Value Dump();
  280. };
  281. Codemodel::Codemodel(cmFileAPI& fileAPI, unsigned long version)
  282. : FileAPI(fileAPI)
  283. , Version(version)
  284. {
  285. }
  286. Json::Value Codemodel::Dump()
  287. {
  288. Json::Value codemodel = Json::objectValue;
  289. codemodel["paths"] = this->DumpPaths();
  290. codemodel["configurations"] = this->DumpConfigurations();
  291. return codemodel;
  292. }
  293. Json::Value Codemodel::DumpPaths()
  294. {
  295. Json::Value paths = Json::objectValue;
  296. paths["source"] = this->FileAPI.GetCMakeInstance()->GetHomeDirectory();
  297. paths["build"] = this->FileAPI.GetCMakeInstance()->GetHomeOutputDirectory();
  298. return paths;
  299. }
  300. Json::Value Codemodel::DumpConfigurations()
  301. {
  302. std::vector<std::string> configs;
  303. cmGlobalGenerator* gg =
  304. this->FileAPI.GetCMakeInstance()->GetGlobalGenerator();
  305. auto makefiles = gg->GetMakefiles();
  306. if (!makefiles.empty()) {
  307. makefiles[0]->GetConfigurations(configs);
  308. if (configs.empty()) {
  309. configs.emplace_back();
  310. }
  311. }
  312. Json::Value configurations = Json::arrayValue;
  313. for (std::string const& config : configs) {
  314. configurations.append(this->DumpConfiguration(config));
  315. }
  316. return configurations;
  317. }
  318. Json::Value Codemodel::DumpConfiguration(std::string const& config)
  319. {
  320. CodemodelConfig configuration(this->FileAPI, this->Version, config);
  321. return configuration.Dump();
  322. }
  323. CodemodelConfig::CodemodelConfig(cmFileAPI& fileAPI, unsigned long version,
  324. std::string const& config)
  325. : FileAPI(fileAPI)
  326. , Version(version)
  327. , Config(config)
  328. , TopSource(this->FileAPI.GetCMakeInstance()->GetHomeDirectory())
  329. , TopBuild(this->FileAPI.GetCMakeInstance()->GetHomeOutputDirectory())
  330. {
  331. static_cast<void>(this->Version);
  332. }
  333. Json::Value CodemodelConfig::Dump()
  334. {
  335. Json::Value configuration = Json::objectValue;
  336. configuration["name"] = this->Config;
  337. this->ProcessDirectories();
  338. configuration["targets"] = this->DumpTargets();
  339. configuration["directories"] = this->DumpDirectories();
  340. configuration["projects"] = this->DumpProjects();
  341. return configuration;
  342. }
  343. void CodemodelConfig::ProcessDirectories()
  344. {
  345. cmGlobalGenerator* gg =
  346. this->FileAPI.GetCMakeInstance()->GetGlobalGenerator();
  347. std::vector<cmLocalGenerator*> const& localGens = gg->GetLocalGenerators();
  348. // Add directories in forward order to process parents before children.
  349. this->Directories.reserve(localGens.size());
  350. for (cmLocalGenerator* lg : localGens) {
  351. auto directoryIndex =
  352. static_cast<Json::ArrayIndex>(this->Directories.size());
  353. this->Directories.emplace_back();
  354. Directory& d = this->Directories[directoryIndex];
  355. d.Snapshot = lg->GetStateSnapshot().GetBuildsystemDirectory();
  356. d.LocalGenerator = lg;
  357. this->DirectoryMap[d.Snapshot] = directoryIndex;
  358. d.ProjectIndex = this->AddProject(d.Snapshot);
  359. this->Projects[d.ProjectIndex].DirectoryIndexes.append(directoryIndex);
  360. }
  361. // Update directories in reverse order to process children before parents.
  362. for (auto di = this->Directories.rbegin(); di != this->Directories.rend();
  363. ++di) {
  364. Directory& d = *di;
  365. // Accumulate the presence of install rules on the way up.
  366. for (auto gen : d.LocalGenerator->GetMakefile()->GetInstallGenerators()) {
  367. if (!dynamic_cast<cmInstallSubdirectoryGenerator*>(gen)) {
  368. d.HasInstallRule = true;
  369. break;
  370. }
  371. }
  372. if (!d.HasInstallRule) {
  373. for (cmStateSnapshot const& child : d.Snapshot.GetChildren()) {
  374. cmStateSnapshot childDir = child.GetBuildsystemDirectory();
  375. Json::ArrayIndex const childIndex = this->GetDirectoryIndex(childDir);
  376. if (this->Directories[childIndex].HasInstallRule) {
  377. d.HasInstallRule = true;
  378. break;
  379. }
  380. }
  381. }
  382. }
  383. }
  384. Json::ArrayIndex CodemodelConfig::GetDirectoryIndex(cmLocalGenerator const* lg)
  385. {
  386. return this->GetDirectoryIndex(
  387. lg->GetStateSnapshot().GetBuildsystemDirectory());
  388. }
  389. Json::ArrayIndex CodemodelConfig::GetDirectoryIndex(cmStateSnapshot s)
  390. {
  391. auto i = this->DirectoryMap.find(s);
  392. assert(i != this->DirectoryMap.end());
  393. return i->second;
  394. }
  395. Json::ArrayIndex CodemodelConfig::AddProject(cmStateSnapshot s)
  396. {
  397. cmStateSnapshot ps = s.GetBuildsystemDirectoryParent();
  398. if (ps.IsValid() && ps.GetProjectName() == s.GetProjectName()) {
  399. // This directory is part of its parent directory project.
  400. Json::ArrayIndex const parentDirIndex = this->GetDirectoryIndex(ps);
  401. return this->Directories[parentDirIndex].ProjectIndex;
  402. }
  403. // This directory starts a new project.
  404. auto projectIndex = static_cast<Json::ArrayIndex>(this->Projects.size());
  405. this->Projects.emplace_back();
  406. Project& p = this->Projects[projectIndex];
  407. p.Snapshot = s;
  408. this->ProjectMap[s] = projectIndex;
  409. if (ps.IsValid()) {
  410. Json::ArrayIndex const parentDirIndex = this->GetDirectoryIndex(ps);
  411. p.ParentIndex = this->Directories[parentDirIndex].ProjectIndex;
  412. this->Projects[p.ParentIndex].ChildIndexes.append(projectIndex);
  413. }
  414. return projectIndex;
  415. }
  416. Json::Value CodemodelConfig::DumpTargets()
  417. {
  418. Json::Value targets = Json::arrayValue;
  419. std::vector<cmGeneratorTarget*> targetList;
  420. cmGlobalGenerator* gg =
  421. this->FileAPI.GetCMakeInstance()->GetGlobalGenerator();
  422. for (cmLocalGenerator const* lg : gg->GetLocalGenerators()) {
  423. cmAppend(targetList, lg->GetGeneratorTargets());
  424. }
  425. std::sort(targetList.begin(), targetList.end(),
  426. [](cmGeneratorTarget* l, cmGeneratorTarget* r) {
  427. return l->GetName() < r->GetName();
  428. });
  429. for (cmGeneratorTarget* gt : targetList) {
  430. if (gt->GetType() == cmStateEnums::GLOBAL_TARGET ||
  431. gt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  432. continue;
  433. }
  434. targets.append(this->DumpTarget(gt, targets.size()));
  435. }
  436. return targets;
  437. }
  438. Json::Value CodemodelConfig::DumpTarget(cmGeneratorTarget* gt,
  439. Json::ArrayIndex ti)
  440. {
  441. Target t(gt, this->Config);
  442. std::string prefix = "target-" + gt->GetName();
  443. if (!this->Config.empty()) {
  444. prefix += "-" + this->Config;
  445. }
  446. Json::Value target = this->FileAPI.MaybeJsonFile(t.Dump(), prefix);
  447. target["name"] = gt->GetName();
  448. target["id"] = TargetId(gt, this->TopBuild);
  449. // Cross-reference directory containing target.
  450. Json::ArrayIndex di = this->GetDirectoryIndex(gt->GetLocalGenerator());
  451. target["directoryIndex"] = di;
  452. this->Directories[di].TargetIndexes.append(ti);
  453. // Cross-reference project containing target.
  454. Json::ArrayIndex pi = this->Directories[di].ProjectIndex;
  455. target["projectIndex"] = pi;
  456. this->Projects[pi].TargetIndexes.append(ti);
  457. return target;
  458. }
  459. Json::Value CodemodelConfig::DumpDirectories()
  460. {
  461. Json::Value directories = Json::arrayValue;
  462. for (Directory& d : this->Directories) {
  463. directories.append(this->DumpDirectory(d));
  464. }
  465. return directories;
  466. }
  467. Json::Value CodemodelConfig::DumpDirectory(Directory& d)
  468. {
  469. Json::Value directory = Json::objectValue;
  470. std::string sourceDir = d.Snapshot.GetDirectory().GetCurrentSource();
  471. directory["source"] = RelativeIfUnder(this->TopSource, sourceDir);
  472. std::string buildDir = d.Snapshot.GetDirectory().GetCurrentBinary();
  473. directory["build"] = RelativeIfUnder(this->TopBuild, buildDir);
  474. cmStateSnapshot parentDir = d.Snapshot.GetBuildsystemDirectoryParent();
  475. if (parentDir.IsValid()) {
  476. directory["parentIndex"] = this->GetDirectoryIndex(parentDir);
  477. }
  478. Json::Value childIndexes = Json::arrayValue;
  479. for (cmStateSnapshot const& child : d.Snapshot.GetChildren()) {
  480. childIndexes.append(
  481. this->GetDirectoryIndex(child.GetBuildsystemDirectory()));
  482. }
  483. if (!childIndexes.empty()) {
  484. directory["childIndexes"] = std::move(childIndexes);
  485. }
  486. directory["projectIndex"] = d.ProjectIndex;
  487. if (!d.TargetIndexes.empty()) {
  488. directory["targetIndexes"] = std::move(d.TargetIndexes);
  489. }
  490. Json::Value minimumCMakeVersion = this->DumpMinimumCMakeVersion(d.Snapshot);
  491. if (!minimumCMakeVersion.isNull()) {
  492. directory["minimumCMakeVersion"] = std::move(minimumCMakeVersion);
  493. }
  494. if (d.HasInstallRule) {
  495. directory["hasInstallRule"] = true;
  496. }
  497. return directory;
  498. }
  499. Json::Value CodemodelConfig::DumpProjects()
  500. {
  501. Json::Value projects = Json::arrayValue;
  502. for (Project& p : this->Projects) {
  503. projects.append(this->DumpProject(p));
  504. }
  505. return projects;
  506. }
  507. Json::Value CodemodelConfig::DumpProject(Project& p)
  508. {
  509. Json::Value project = Json::objectValue;
  510. project["name"] = p.Snapshot.GetProjectName();
  511. if (p.ParentIndex != Project::NoParentIndex) {
  512. project["parentIndex"] = p.ParentIndex;
  513. }
  514. if (!p.ChildIndexes.empty()) {
  515. project["childIndexes"] = std::move(p.ChildIndexes);
  516. }
  517. project["directoryIndexes"] = std::move(p.DirectoryIndexes);
  518. if (!p.TargetIndexes.empty()) {
  519. project["targetIndexes"] = std::move(p.TargetIndexes);
  520. }
  521. return project;
  522. }
  523. Json::Value CodemodelConfig::DumpMinimumCMakeVersion(cmStateSnapshot s)
  524. {
  525. Json::Value minimumCMakeVersion;
  526. if (std::string const* def =
  527. s.GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION")) {
  528. minimumCMakeVersion = Json::objectValue;
  529. minimumCMakeVersion["string"] = *def;
  530. }
  531. return minimumCMakeVersion;
  532. }
  533. Target::Target(cmGeneratorTarget* gt, std::string const& config)
  534. : GT(gt)
  535. , Config(config)
  536. , TopSource(gt->GetGlobalGenerator()->GetCMakeInstance()->GetHomeDirectory())
  537. , TopBuild(
  538. gt->GetGlobalGenerator()->GetCMakeInstance()->GetHomeOutputDirectory())
  539. , SourceGroupsLocal(this->GT->Makefile->GetSourceGroups())
  540. , Backtraces(this->TopSource)
  541. {
  542. }
  543. Json::Value Target::Dump()
  544. {
  545. Json::Value target = Json::objectValue;
  546. cmStateEnums::TargetType const type = this->GT->GetType();
  547. target["name"] = this->GT->GetName();
  548. target["type"] = cmState::GetTargetTypeName(type);
  549. target["id"] = TargetId(this->GT, this->TopBuild);
  550. target["paths"] = this->DumpPaths();
  551. if (this->GT->Target->GetIsGeneratorProvided()) {
  552. target["isGeneratorProvided"] = true;
  553. }
  554. this->AddBacktrace(target, this->GT->GetBacktrace());
  555. if (this->GT->Target->GetHaveInstallRule()) {
  556. target["install"] = this->DumpInstall();
  557. }
  558. if (this->GT->HaveWellDefinedOutputFiles()) {
  559. Json::Value artifacts = this->DumpArtifacts();
  560. if (!artifacts.empty()) {
  561. target["artifacts"] = std::move(artifacts);
  562. }
  563. }
  564. if (type == cmStateEnums::EXECUTABLE ||
  565. type == cmStateEnums::SHARED_LIBRARY ||
  566. type == cmStateEnums::MODULE_LIBRARY) {
  567. target["nameOnDisk"] = this->GT->GetFullName(this->Config);
  568. target["link"] = this->DumpLink();
  569. } else if (type == cmStateEnums::STATIC_LIBRARY) {
  570. target["nameOnDisk"] = this->GT->GetFullName(this->Config);
  571. target["archive"] = this->DumpArchive();
  572. }
  573. Json::Value dependencies = this->DumpDependencies();
  574. if (!dependencies.empty()) {
  575. target["dependencies"] = dependencies;
  576. }
  577. {
  578. this->ProcessLanguages();
  579. target["sources"] = this->DumpSources();
  580. Json::Value folder = this->DumpFolder();
  581. if (!folder.isNull()) {
  582. target["folder"] = std::move(folder);
  583. }
  584. Json::Value sourceGroups = this->DumpSourceGroups();
  585. if (!sourceGroups.empty()) {
  586. target["sourceGroups"] = std::move(sourceGroups);
  587. }
  588. Json::Value compileGroups = this->DumpCompileGroups();
  589. if (!compileGroups.empty()) {
  590. target["compileGroups"] = std::move(compileGroups);
  591. }
  592. }
  593. target["backtraceGraph"] = this->Backtraces.Dump();
  594. return target;
  595. }
  596. void Target::ProcessLanguages()
  597. {
  598. std::set<std::string> languages;
  599. this->GT->GetLanguages(languages, this->Config);
  600. for (std::string const& lang : languages) {
  601. this->ProcessLanguage(lang);
  602. }
  603. }
  604. void Target::ProcessLanguage(std::string const& lang)
  605. {
  606. CompileData& cd = this->CompileDataMap[lang];
  607. cd.Language = lang;
  608. if (const char* sysrootCompile =
  609. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT_COMPILE")) {
  610. cd.Sysroot = sysrootCompile;
  611. } else if (const char* sysroot =
  612. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT")) {
  613. cd.Sysroot = sysroot;
  614. }
  615. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  616. {
  617. // FIXME: Add flags from end section of ExpandRuleVariable,
  618. // which may need to be factored out.
  619. std::string flags;
  620. lg->GetTargetCompileFlags(this->GT, this->Config, lang, flags);
  621. cd.Flags.emplace_back(std::move(flags), cmListFileBacktrace());
  622. }
  623. std::set<BT<std::string>> defines =
  624. lg->GetTargetDefines(this->GT, this->Config, lang);
  625. cd.SetDefines(defines);
  626. std::vector<BT<std::string>> includePathList =
  627. lg->GetIncludeDirectories(this->GT, lang, this->Config);
  628. for (BT<std::string> const& i : includePathList) {
  629. cd.Includes.emplace_back(
  630. i, this->GT->IsSystemIncludeDirectory(i.Value, this->Config, lang));
  631. }
  632. }
  633. Json::ArrayIndex Target::AddSourceGroup(cmSourceGroup* sg, Json::ArrayIndex si)
  634. {
  635. std::unordered_map<cmSourceGroup const*, Json::ArrayIndex>::iterator i =
  636. this->SourceGroupsMap.find(sg);
  637. if (i == this->SourceGroupsMap.end()) {
  638. auto sgIndex = static_cast<Json::ArrayIndex>(this->SourceGroups.size());
  639. i = this->SourceGroupsMap.emplace(sg, sgIndex).first;
  640. SourceGroup g;
  641. g.Name = sg->GetFullName();
  642. this->SourceGroups.push_back(std::move(g));
  643. }
  644. this->SourceGroups[i->second].SourceIndexes.append(si);
  645. return i->second;
  646. }
  647. CompileData Target::BuildCompileData(cmSourceFile* sf)
  648. {
  649. CompileData fd;
  650. fd.Language = sf->GetLanguage();
  651. if (fd.Language.empty()) {
  652. return fd;
  653. }
  654. CompileData const& cd = this->CompileDataMap.at(fd.Language);
  655. fd.Sysroot = cd.Sysroot;
  656. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  657. cmGeneratorExpressionInterpreter genexInterpreter(lg, this->Config, this->GT,
  658. fd.Language);
  659. fd.Flags = cd.Flags;
  660. const std::string COMPILE_FLAGS("COMPILE_FLAGS");
  661. if (const char* cflags = sf->GetProperty(COMPILE_FLAGS)) {
  662. std::string flags = genexInterpreter.Evaluate(cflags, COMPILE_FLAGS);
  663. fd.Flags.emplace_back(std::move(flags), cmListFileBacktrace());
  664. }
  665. const std::string COMPILE_OPTIONS("COMPILE_OPTIONS");
  666. if (const char* coptions = sf->GetProperty(COMPILE_OPTIONS)) {
  667. std::string flags;
  668. lg->AppendCompileOptions(
  669. flags, genexInterpreter.Evaluate(coptions, COMPILE_OPTIONS));
  670. fd.Flags.emplace_back(std::move(flags), cmListFileBacktrace());
  671. }
  672. // Add include directories from source file properties.
  673. {
  674. std::vector<std::string> includes;
  675. const std::string INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
  676. if (const char* cincludes = sf->GetProperty(INCLUDE_DIRECTORIES)) {
  677. const std::string& evaluatedIncludes =
  678. genexInterpreter.Evaluate(cincludes, INCLUDE_DIRECTORIES);
  679. lg->AppendIncludeDirectories(includes, evaluatedIncludes, *sf);
  680. for (std::string const& include : includes) {
  681. bool const isSystemInclude = this->GT->IsSystemIncludeDirectory(
  682. include, this->Config, fd.Language);
  683. fd.Includes.emplace_back(include, isSystemInclude);
  684. }
  685. }
  686. }
  687. fd.Includes.insert(fd.Includes.end(), cd.Includes.begin(),
  688. cd.Includes.end());
  689. const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
  690. std::set<std::string> fileDefines;
  691. if (const char* defs = sf->GetProperty(COMPILE_DEFINITIONS)) {
  692. lg->AppendDefines(fileDefines,
  693. genexInterpreter.Evaluate(defs, COMPILE_DEFINITIONS));
  694. }
  695. const std::string defPropName =
  696. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(this->Config);
  697. if (const char* config_defs = sf->GetProperty(defPropName)) {
  698. lg->AppendDefines(
  699. fileDefines,
  700. genexInterpreter.Evaluate(config_defs, COMPILE_DEFINITIONS));
  701. }
  702. std::set<BT<std::string>> defines;
  703. defines.insert(fileDefines.begin(), fileDefines.end());
  704. defines.insert(cd.Defines.begin(), cd.Defines.end());
  705. fd.SetDefines(defines);
  706. return fd;
  707. }
  708. Json::ArrayIndex Target::AddSourceCompileGroup(cmSourceFile* sf,
  709. Json::ArrayIndex si)
  710. {
  711. Json::Value compileDataJson =
  712. this->DumpCompileData(this->BuildCompileData(sf));
  713. std::map<Json::Value, Json::ArrayIndex>::iterator i =
  714. this->CompileGroupMap.find(compileDataJson);
  715. if (i == this->CompileGroupMap.end()) {
  716. Json::ArrayIndex cgIndex =
  717. static_cast<Json::ArrayIndex>(this->CompileGroups.size());
  718. i =
  719. this->CompileGroupMap.emplace(std::move(compileDataJson), cgIndex).first;
  720. CompileGroup g;
  721. g.Entry = i;
  722. this->CompileGroups.push_back(std::move(g));
  723. }
  724. this->CompileGroups[i->second].SourceIndexes.append(si);
  725. return i->second;
  726. }
  727. void Target::AddBacktrace(Json::Value& object, cmListFileBacktrace const& bt)
  728. {
  729. Json::ArrayIndex backtrace;
  730. if (this->Backtraces.Add(bt, backtrace)) {
  731. object["backtrace"] = backtrace;
  732. }
  733. }
  734. Json::Value Target::DumpPaths()
  735. {
  736. Json::Value paths = Json::objectValue;
  737. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  738. std::string const& sourceDir = lg->GetCurrentSourceDirectory();
  739. paths["source"] = RelativeIfUnder(this->TopSource, sourceDir);
  740. std::string const& buildDir = lg->GetCurrentBinaryDirectory();
  741. paths["build"] = RelativeIfUnder(this->TopBuild, buildDir);
  742. return paths;
  743. }
  744. Json::Value Target::DumpSources()
  745. {
  746. Json::Value sources = Json::arrayValue;
  747. cmGeneratorTarget::KindedSources const& kinded =
  748. this->GT->GetKindedSources(this->Config);
  749. for (cmGeneratorTarget::SourceAndKind const& sk : kinded.Sources) {
  750. sources.append(this->DumpSource(sk, sources.size()));
  751. }
  752. return sources;
  753. }
  754. Json::Value Target::DumpSource(cmGeneratorTarget::SourceAndKind const& sk,
  755. Json::ArrayIndex si)
  756. {
  757. Json::Value source = Json::objectValue;
  758. std::string const path = sk.Source.Value->GetFullPath();
  759. source["path"] = RelativeIfUnder(this->TopSource, path);
  760. if (sk.Source.Value->GetIsGenerated()) {
  761. source["isGenerated"] = true;
  762. }
  763. this->AddBacktrace(source, sk.Source.Backtrace);
  764. if (cmSourceGroup* sg =
  765. this->GT->Makefile->FindSourceGroup(path, this->SourceGroupsLocal)) {
  766. source["sourceGroupIndex"] = this->AddSourceGroup(sg, si);
  767. }
  768. switch (sk.Kind) {
  769. case cmGeneratorTarget::SourceKindObjectSource: {
  770. source["compileGroupIndex"] =
  771. this->AddSourceCompileGroup(sk.Source.Value, si);
  772. } break;
  773. case cmGeneratorTarget::SourceKindAppManifest:
  774. case cmGeneratorTarget::SourceKindCertificate:
  775. case cmGeneratorTarget::SourceKindCustomCommand:
  776. case cmGeneratorTarget::SourceKindExternalObject:
  777. case cmGeneratorTarget::SourceKindExtra:
  778. case cmGeneratorTarget::SourceKindHeader:
  779. case cmGeneratorTarget::SourceKindIDL:
  780. case cmGeneratorTarget::SourceKindManifest:
  781. case cmGeneratorTarget::SourceKindModuleDefinition:
  782. case cmGeneratorTarget::SourceKindResx:
  783. case cmGeneratorTarget::SourceKindXaml:
  784. break;
  785. }
  786. return source;
  787. }
  788. Json::Value Target::DumpCompileData(CompileData cd)
  789. {
  790. Json::Value result = Json::objectValue;
  791. if (!cd.Language.empty()) {
  792. result["language"] = cd.Language;
  793. }
  794. if (!cd.Sysroot.empty()) {
  795. result["sysroot"] = this->DumpSysroot(cd.Sysroot);
  796. }
  797. if (!cd.Flags.empty()) {
  798. result["compileCommandFragments"] = this->DumpCommandFragments(cd.Flags);
  799. }
  800. if (!cd.Includes.empty()) {
  801. Json::Value includes = Json::arrayValue;
  802. for (auto const& i : cd.Includes) {
  803. includes.append(this->DumpInclude(i));
  804. }
  805. result["includes"] = includes;
  806. }
  807. if (!cd.Defines.empty()) {
  808. Json::Value defines = Json::arrayValue;
  809. for (BT<std::string> const& d : cd.Defines) {
  810. defines.append(this->DumpDefine(d));
  811. }
  812. result["defines"] = std::move(defines);
  813. }
  814. return result;
  815. }
  816. Json::Value Target::DumpInclude(CompileData::IncludeEntry const& inc)
  817. {
  818. Json::Value include = Json::objectValue;
  819. include["path"] = inc.Path.Value;
  820. if (inc.IsSystem) {
  821. include["isSystem"] = true;
  822. }
  823. this->AddBacktrace(include, inc.Path.Backtrace);
  824. return include;
  825. }
  826. Json::Value Target::DumpDefine(BT<std::string> const& def)
  827. {
  828. Json::Value define = Json::objectValue;
  829. define["define"] = def.Value;
  830. this->AddBacktrace(define, def.Backtrace);
  831. return define;
  832. }
  833. Json::Value Target::DumpSourceGroups()
  834. {
  835. Json::Value sourceGroups = Json::arrayValue;
  836. for (auto& sg : this->SourceGroups) {
  837. sourceGroups.append(this->DumpSourceGroup(sg));
  838. }
  839. return sourceGroups;
  840. }
  841. Json::Value Target::DumpSourceGroup(SourceGroup& sg)
  842. {
  843. Json::Value group = Json::objectValue;
  844. group["name"] = sg.Name;
  845. group["sourceIndexes"] = std::move(sg.SourceIndexes);
  846. return group;
  847. }
  848. Json::Value Target::DumpCompileGroups()
  849. {
  850. Json::Value compileGroups = Json::arrayValue;
  851. for (auto& cg : this->CompileGroups) {
  852. compileGroups.append(this->DumpCompileGroup(cg));
  853. }
  854. return compileGroups;
  855. }
  856. Json::Value Target::DumpCompileGroup(CompileGroup& cg)
  857. {
  858. Json::Value group = cg.Entry->first;
  859. group["sourceIndexes"] = std::move(cg.SourceIndexes);
  860. return group;
  861. }
  862. Json::Value Target::DumpSysroot(std::string const& path)
  863. {
  864. Json::Value sysroot = Json::objectValue;
  865. sysroot["path"] = path;
  866. return sysroot;
  867. }
  868. Json::Value Target::DumpInstall()
  869. {
  870. Json::Value install = Json::objectValue;
  871. install["prefix"] = this->DumpInstallPrefix();
  872. install["destinations"] = this->DumpInstallDestinations();
  873. return install;
  874. }
  875. Json::Value Target::DumpInstallPrefix()
  876. {
  877. Json::Value prefix = Json::objectValue;
  878. std::string p =
  879. this->GT->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  880. cmSystemTools::ConvertToUnixSlashes(p);
  881. prefix["path"] = p;
  882. return prefix;
  883. }
  884. Json::Value Target::DumpInstallDestinations()
  885. {
  886. Json::Value destinations = Json::arrayValue;
  887. auto installGens = this->GT->Makefile->GetInstallGenerators();
  888. for (auto iGen : installGens) {
  889. auto itGen = dynamic_cast<cmInstallTargetGenerator*>(iGen);
  890. if (itGen != nullptr && itGen->GetTarget() == this->GT) {
  891. destinations.append(this->DumpInstallDestination(itGen));
  892. }
  893. }
  894. return destinations;
  895. }
  896. Json::Value Target::DumpInstallDestination(cmInstallTargetGenerator* itGen)
  897. {
  898. Json::Value destination = Json::objectValue;
  899. destination["path"] = itGen->GetDestination(this->Config);
  900. this->AddBacktrace(destination, itGen->GetBacktrace());
  901. return destination;
  902. }
  903. Json::Value Target::DumpArtifacts()
  904. {
  905. Json::Value artifacts = Json::arrayValue;
  906. // Object libraries have only object files as artifacts.
  907. if (this->GT->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  908. if (!this->GT->GetGlobalGenerator()->HasKnownObjectFileLocation(nullptr)) {
  909. return artifacts;
  910. }
  911. std::vector<cmSourceFile const*> objectSources;
  912. this->GT->GetObjectSources(objectSources, this->Config);
  913. std::string const obj_dir = this->GT->GetObjectDirectory(this->Config);
  914. for (cmSourceFile const* sf : objectSources) {
  915. const std::string& obj = this->GT->GetObjectName(sf);
  916. Json::Value artifact = Json::objectValue;
  917. artifact["path"] = RelativeIfUnder(this->TopBuild, obj_dir + obj);
  918. artifacts.append(std::move(artifact)); // NOLINT(*)
  919. }
  920. return artifacts;
  921. }
  922. // Other target types always have a "main" artifact.
  923. {
  924. Json::Value artifact = Json::objectValue;
  925. artifact["path"] =
  926. RelativeIfUnder(this->TopBuild,
  927. this->GT->GetFullPath(
  928. this->Config, cmStateEnums::RuntimeBinaryArtifact));
  929. artifacts.append(std::move(artifact)); // NOLINT(*)
  930. }
  931. // Add Windows-specific artifacts produced by the linker.
  932. if (this->GT->IsDLLPlatform() &&
  933. this->GT->GetType() != cmStateEnums::STATIC_LIBRARY) {
  934. if (this->GT->GetType() == cmStateEnums::SHARED_LIBRARY ||
  935. this->GT->IsExecutableWithExports()) {
  936. Json::Value artifact = Json::objectValue;
  937. artifact["path"] =
  938. RelativeIfUnder(this->TopBuild,
  939. this->GT->GetFullPath(
  940. this->Config, cmStateEnums::ImportLibraryArtifact));
  941. artifacts.append(std::move(artifact)); // NOLINT(*)
  942. }
  943. cmGeneratorTarget::OutputInfo const* output =
  944. this->GT->GetOutputInfo(this->Config);
  945. if (output && !output->PdbDir.empty()) {
  946. Json::Value artifact = Json::objectValue;
  947. artifact["path"] = RelativeIfUnder(this->TopBuild,
  948. output->PdbDir + '/' +
  949. this->GT->GetPDBName(this->Config));
  950. artifacts.append(std::move(artifact)); // NOLINT(*)
  951. }
  952. }
  953. return artifacts;
  954. }
  955. Json::Value Target::DumpLink()
  956. {
  957. Json::Value link = Json::objectValue;
  958. std::string lang = this->GT->GetLinkerLanguage(this->Config);
  959. link["language"] = lang;
  960. {
  961. Json::Value commandFragments = this->DumpLinkCommandFragments();
  962. if (!commandFragments.empty()) {
  963. link["commandFragments"] = std::move(commandFragments);
  964. }
  965. }
  966. if (const char* sysrootLink =
  967. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT_LINK")) {
  968. link["sysroot"] = this->DumpSysroot(sysrootLink);
  969. } else if (const char* sysroot =
  970. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT")) {
  971. link["sysroot"] = this->DumpSysroot(sysroot);
  972. }
  973. if (this->GT->IsIPOEnabled(lang, this->Config)) {
  974. link["lto"] = true;
  975. }
  976. return link;
  977. }
  978. Json::Value Target::DumpArchive()
  979. {
  980. Json::Value archive = Json::objectValue;
  981. {
  982. // The "link" fragments not relevant to static libraries are empty.
  983. Json::Value commandFragments = this->DumpLinkCommandFragments();
  984. if (!commandFragments.empty()) {
  985. archive["commandFragments"] = std::move(commandFragments);
  986. }
  987. }
  988. std::string lang = this->GT->GetLinkerLanguage(this->Config);
  989. if (this->GT->IsIPOEnabled(lang, this->Config)) {
  990. archive["lto"] = true;
  991. }
  992. return archive;
  993. }
  994. Json::Value Target::DumpLinkCommandFragments()
  995. {
  996. Json::Value linkFragments = Json::arrayValue;
  997. std::string linkLanguageFlags;
  998. std::string linkFlags;
  999. std::string frameworkPath;
  1000. std::string linkPath;
  1001. std::string linkLibs;
  1002. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  1003. cmLinkLineComputer linkLineComputer(lg,
  1004. lg->GetStateSnapshot().GetDirectory());
  1005. lg->GetTargetFlags(&linkLineComputer, this->Config, linkLibs,
  1006. linkLanguageFlags, linkFlags, frameworkPath, linkPath,
  1007. this->GT);
  1008. linkLanguageFlags = cmSystemTools::TrimWhitespace(linkLanguageFlags);
  1009. linkFlags = cmSystemTools::TrimWhitespace(linkFlags);
  1010. frameworkPath = cmSystemTools::TrimWhitespace(frameworkPath);
  1011. linkPath = cmSystemTools::TrimWhitespace(linkPath);
  1012. linkLibs = cmSystemTools::TrimWhitespace(linkLibs);
  1013. if (!linkLanguageFlags.empty()) {
  1014. linkFragments.append(
  1015. this->DumpCommandFragment(std::move(linkLanguageFlags), "flags"));
  1016. }
  1017. if (!linkFlags.empty()) {
  1018. linkFragments.append(
  1019. this->DumpCommandFragment(std::move(linkFlags), "flags"));
  1020. }
  1021. if (!frameworkPath.empty()) {
  1022. linkFragments.append(
  1023. this->DumpCommandFragment(std::move(frameworkPath), "frameworkPath"));
  1024. }
  1025. if (!linkPath.empty()) {
  1026. linkFragments.append(
  1027. this->DumpCommandFragment(std::move(linkPath), "libraryPath"));
  1028. }
  1029. if (!linkLibs.empty()) {
  1030. linkFragments.append(
  1031. this->DumpCommandFragment(std::move(linkLibs), "libraries"));
  1032. }
  1033. return linkFragments;
  1034. }
  1035. Json::Value Target::DumpCommandFragments(
  1036. std::vector<BT<std::string>> const& frags)
  1037. {
  1038. Json::Value commandFragments = Json::arrayValue;
  1039. for (BT<std::string> const& f : frags) {
  1040. commandFragments.append(this->DumpCommandFragment(f));
  1041. }
  1042. return commandFragments;
  1043. }
  1044. Json::Value Target::DumpCommandFragment(BT<std::string> const& frag,
  1045. std::string const& role)
  1046. {
  1047. Json::Value fragment = Json::objectValue;
  1048. fragment["fragment"] = frag.Value;
  1049. if (!role.empty()) {
  1050. fragment["role"] = role;
  1051. }
  1052. this->AddBacktrace(fragment, frag.Backtrace);
  1053. return fragment;
  1054. }
  1055. Json::Value Target::DumpDependencies()
  1056. {
  1057. Json::Value dependencies = Json::arrayValue;
  1058. cmGlobalGenerator* gg = this->GT->GetGlobalGenerator();
  1059. for (cmTargetDepend const& td : gg->GetTargetDirectDepends(this->GT)) {
  1060. dependencies.append(this->DumpDependency(td));
  1061. }
  1062. return dependencies;
  1063. }
  1064. Json::Value Target::DumpDependency(cmTargetDepend const& td)
  1065. {
  1066. Json::Value dependency = Json::objectValue;
  1067. dependency["id"] = TargetId(td, this->TopBuild);
  1068. this->AddBacktrace(dependency, td.GetBacktrace());
  1069. return dependency;
  1070. }
  1071. Json::Value Target::DumpFolder()
  1072. {
  1073. Json::Value folder;
  1074. if (const char* f = this->GT->GetProperty("FOLDER")) {
  1075. folder = Json::objectValue;
  1076. folder["name"] = f;
  1077. }
  1078. return folder;
  1079. }
  1080. }
  1081. Json::Value cmFileAPICodemodelDump(cmFileAPI& fileAPI, unsigned long version)
  1082. {
  1083. Codemodel codemodel(fileAPI, version);
  1084. return codemodel.Dump();
  1085. }