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