cmFileAPICodemodel.cxx 40 KB

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