cmFileAPICodemodel.cxx 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  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 const& 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 const& 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->HasImportLibrary(this->Config)) {
  933. Json::Value artifact = Json::objectValue;
  934. artifact["path"] =
  935. RelativeIfUnder(this->TopBuild,
  936. this->GT->GetFullPath(
  937. this->Config, cmStateEnums::ImportLibraryArtifact));
  938. artifacts.append(std::move(artifact)); // NOLINT(*)
  939. }
  940. if (this->GT->IsDLLPlatform() &&
  941. this->GT->GetType() != cmStateEnums::STATIC_LIBRARY) {
  942. cmGeneratorTarget::OutputInfo const* output =
  943. this->GT->GetOutputInfo(this->Config);
  944. if (output && !output->PdbDir.empty()) {
  945. Json::Value artifact = Json::objectValue;
  946. artifact["path"] = RelativeIfUnder(this->TopBuild,
  947. output->PdbDir + '/' +
  948. this->GT->GetPDBName(this->Config));
  949. artifacts.append(std::move(artifact)); // NOLINT(*)
  950. }
  951. }
  952. return artifacts;
  953. }
  954. Json::Value Target::DumpLink()
  955. {
  956. Json::Value link = Json::objectValue;
  957. std::string lang = this->GT->GetLinkerLanguage(this->Config);
  958. link["language"] = lang;
  959. {
  960. Json::Value commandFragments = this->DumpLinkCommandFragments();
  961. if (!commandFragments.empty()) {
  962. link["commandFragments"] = std::move(commandFragments);
  963. }
  964. }
  965. if (const char* sysrootLink =
  966. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT_LINK")) {
  967. link["sysroot"] = this->DumpSysroot(sysrootLink);
  968. } else if (const char* sysroot =
  969. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT")) {
  970. link["sysroot"] = this->DumpSysroot(sysroot);
  971. }
  972. if (this->GT->IsIPOEnabled(lang, this->Config)) {
  973. link["lto"] = true;
  974. }
  975. return link;
  976. }
  977. Json::Value Target::DumpArchive()
  978. {
  979. Json::Value archive = Json::objectValue;
  980. {
  981. // The "link" fragments not relevant to static libraries are empty.
  982. Json::Value commandFragments = this->DumpLinkCommandFragments();
  983. if (!commandFragments.empty()) {
  984. archive["commandFragments"] = std::move(commandFragments);
  985. }
  986. }
  987. std::string lang = this->GT->GetLinkerLanguage(this->Config);
  988. if (this->GT->IsIPOEnabled(lang, this->Config)) {
  989. archive["lto"] = true;
  990. }
  991. return archive;
  992. }
  993. Json::Value Target::DumpLinkCommandFragments()
  994. {
  995. Json::Value linkFragments = Json::arrayValue;
  996. std::string linkLanguageFlags;
  997. std::string linkFlags;
  998. std::string frameworkPath;
  999. std::string linkPath;
  1000. std::string linkLibs;
  1001. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  1002. cmLinkLineComputer linkLineComputer(lg,
  1003. lg->GetStateSnapshot().GetDirectory());
  1004. lg->GetTargetFlags(&linkLineComputer, this->Config, linkLibs,
  1005. linkLanguageFlags, linkFlags, frameworkPath, linkPath,
  1006. this->GT);
  1007. linkLanguageFlags = cmSystemTools::TrimWhitespace(linkLanguageFlags);
  1008. linkFlags = cmSystemTools::TrimWhitespace(linkFlags);
  1009. frameworkPath = cmSystemTools::TrimWhitespace(frameworkPath);
  1010. linkPath = cmSystemTools::TrimWhitespace(linkPath);
  1011. linkLibs = cmSystemTools::TrimWhitespace(linkLibs);
  1012. if (!linkLanguageFlags.empty()) {
  1013. linkFragments.append(
  1014. this->DumpCommandFragment(std::move(linkLanguageFlags), "flags"));
  1015. }
  1016. if (!linkFlags.empty()) {
  1017. linkFragments.append(
  1018. this->DumpCommandFragment(std::move(linkFlags), "flags"));
  1019. }
  1020. if (!frameworkPath.empty()) {
  1021. linkFragments.append(
  1022. this->DumpCommandFragment(std::move(frameworkPath), "frameworkPath"));
  1023. }
  1024. if (!linkPath.empty()) {
  1025. linkFragments.append(
  1026. this->DumpCommandFragment(std::move(linkPath), "libraryPath"));
  1027. }
  1028. if (!linkLibs.empty()) {
  1029. linkFragments.append(
  1030. this->DumpCommandFragment(std::move(linkLibs), "libraries"));
  1031. }
  1032. return linkFragments;
  1033. }
  1034. Json::Value Target::DumpCommandFragments(
  1035. std::vector<BT<std::string>> const& frags)
  1036. {
  1037. Json::Value commandFragments = Json::arrayValue;
  1038. for (BT<std::string> const& f : frags) {
  1039. commandFragments.append(this->DumpCommandFragment(f));
  1040. }
  1041. return commandFragments;
  1042. }
  1043. Json::Value Target::DumpCommandFragment(BT<std::string> const& frag,
  1044. std::string const& role)
  1045. {
  1046. Json::Value fragment = Json::objectValue;
  1047. fragment["fragment"] = frag.Value;
  1048. if (!role.empty()) {
  1049. fragment["role"] = role;
  1050. }
  1051. this->AddBacktrace(fragment, frag.Backtrace);
  1052. return fragment;
  1053. }
  1054. Json::Value Target::DumpDependencies()
  1055. {
  1056. Json::Value dependencies = Json::arrayValue;
  1057. cmGlobalGenerator* gg = this->GT->GetGlobalGenerator();
  1058. for (cmTargetDepend const& td : gg->GetTargetDirectDepends(this->GT)) {
  1059. dependencies.append(this->DumpDependency(td));
  1060. }
  1061. return dependencies;
  1062. }
  1063. Json::Value Target::DumpDependency(cmTargetDepend const& td)
  1064. {
  1065. Json::Value dependency = Json::objectValue;
  1066. dependency["id"] = TargetId(td, this->TopBuild);
  1067. this->AddBacktrace(dependency, td.GetBacktrace());
  1068. return dependency;
  1069. }
  1070. Json::Value Target::DumpFolder()
  1071. {
  1072. Json::Value folder;
  1073. if (const char* f = this->GT->GetProperty("FOLDER")) {
  1074. folder = Json::objectValue;
  1075. folder["name"] = f;
  1076. }
  1077. return folder;
  1078. }
  1079. }
  1080. Json::Value cmFileAPICodemodelDump(cmFileAPI& fileAPI, unsigned long version)
  1081. {
  1082. Codemodel codemodel(fileAPI, version);
  1083. return codemodel.Dump();
  1084. }