cmFileAPICodemodel.cxx 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  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::string flags;
  697. lg->GetTargetCompileFlags(this->GT, this->Config, lang, flags);
  698. cd.Flags.emplace_back(std::move(flags), JBTIndex());
  699. }
  700. std::set<BT<std::string>> defines =
  701. lg->GetTargetDefines(this->GT, this->Config, lang);
  702. cd.Defines.reserve(defines.size());
  703. for (BT<std::string> const& d : defines) {
  704. cd.Defines.emplace_back(this->ToJBT(d));
  705. }
  706. std::vector<BT<std::string>> includePathList =
  707. lg->GetIncludeDirectories(this->GT, lang, this->Config);
  708. for (BT<std::string> const& i : includePathList) {
  709. cd.Includes.emplace_back(
  710. this->ToJBT(i),
  711. this->GT->IsSystemIncludeDirectory(i.Value, this->Config, lang));
  712. }
  713. }
  714. Json::ArrayIndex Target::AddSourceGroup(cmSourceGroup* sg, Json::ArrayIndex si)
  715. {
  716. auto i = this->SourceGroupsMap.find(sg);
  717. if (i == this->SourceGroupsMap.end()) {
  718. auto sgIndex = static_cast<Json::ArrayIndex>(this->SourceGroups.size());
  719. i = this->SourceGroupsMap.emplace(sg, sgIndex).first;
  720. SourceGroup g;
  721. g.Name = sg->GetFullName();
  722. this->SourceGroups.push_back(std::move(g));
  723. }
  724. this->SourceGroups[i->second].SourceIndexes.append(si);
  725. return i->second;
  726. }
  727. CompileData Target::BuildCompileData(cmSourceFile* sf)
  728. {
  729. CompileData fd;
  730. fd.Language = sf->GetOrDetermineLanguage();
  731. if (fd.Language.empty()) {
  732. return fd;
  733. }
  734. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  735. cmGeneratorExpressionInterpreter genexInterpreter(lg, this->Config, this->GT,
  736. fd.Language);
  737. const std::string COMPILE_FLAGS("COMPILE_FLAGS");
  738. if (const char* cflags = sf->GetProperty(COMPILE_FLAGS)) {
  739. std::string flags = genexInterpreter.Evaluate(cflags, COMPILE_FLAGS);
  740. fd.Flags.emplace_back(std::move(flags), JBTIndex());
  741. }
  742. const std::string COMPILE_OPTIONS("COMPILE_OPTIONS");
  743. if (const char* coptions = sf->GetProperty(COMPILE_OPTIONS)) {
  744. std::string flags;
  745. lg->AppendCompileOptions(
  746. flags, genexInterpreter.Evaluate(coptions, COMPILE_OPTIONS));
  747. fd.Flags.emplace_back(std::move(flags), JBTIndex());
  748. }
  749. // Add include directories from source file properties.
  750. {
  751. std::vector<std::string> includes;
  752. const std::string INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
  753. if (const char* cincludes = sf->GetProperty(INCLUDE_DIRECTORIES)) {
  754. const std::string& evaluatedIncludes =
  755. genexInterpreter.Evaluate(cincludes, INCLUDE_DIRECTORIES);
  756. lg->AppendIncludeDirectories(includes, evaluatedIncludes, *sf);
  757. for (std::string const& include : includes) {
  758. bool const isSystemInclude = this->GT->IsSystemIncludeDirectory(
  759. include, this->Config, fd.Language);
  760. fd.Includes.emplace_back(include, isSystemInclude);
  761. }
  762. }
  763. }
  764. const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
  765. std::set<std::string> fileDefines;
  766. if (const char* defs = sf->GetProperty(COMPILE_DEFINITIONS)) {
  767. lg->AppendDefines(fileDefines,
  768. genexInterpreter.Evaluate(defs, COMPILE_DEFINITIONS));
  769. }
  770. const std::string defPropName =
  771. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(this->Config);
  772. if (const char* config_defs = sf->GetProperty(defPropName)) {
  773. lg->AppendDefines(
  774. fileDefines,
  775. genexInterpreter.Evaluate(config_defs, COMPILE_DEFINITIONS));
  776. }
  777. fd.Defines.reserve(fileDefines.size());
  778. for (std::string const& d : fileDefines) {
  779. fd.Defines.emplace_back(d, JBTIndex());
  780. }
  781. return fd;
  782. }
  783. CompileData Target::MergeCompileData(CompileData const& fd)
  784. {
  785. CompileData cd;
  786. cd.Language = fd.Language;
  787. if (cd.Language.empty()) {
  788. return cd;
  789. }
  790. CompileData const& td = this->CompileDataMap.at(cd.Language);
  791. // All compile groups share the sysroot of the target.
  792. cd.Sysroot = td.Sysroot;
  793. // Use target-wide flags followed by source-specific flags.
  794. cd.Flags.reserve(td.Flags.size() + fd.Flags.size());
  795. cd.Flags.insert(cd.Flags.end(), td.Flags.begin(), td.Flags.end());
  796. cd.Flags.insert(cd.Flags.end(), fd.Flags.begin(), fd.Flags.end());
  797. // Use source-specific includes followed by target-wide includes.
  798. cd.Includes.reserve(fd.Includes.size() + td.Includes.size());
  799. cd.Includes.insert(cd.Includes.end(), fd.Includes.begin(),
  800. fd.Includes.end());
  801. cd.Includes.insert(cd.Includes.end(), td.Includes.begin(),
  802. td.Includes.end());
  803. // Use target-wide defines followed by source-specific defines.
  804. cd.Defines.reserve(td.Defines.size() + fd.Defines.size());
  805. cd.Defines.insert(cd.Defines.end(), td.Defines.begin(), td.Defines.end());
  806. cd.Defines.insert(cd.Defines.end(), fd.Defines.begin(), fd.Defines.end());
  807. // De-duplicate defines.
  808. std::stable_sort(cd.Defines.begin(), cd.Defines.end(),
  809. JBT<std::string>::ValueLess);
  810. auto end = std::unique(cd.Defines.begin(), cd.Defines.end(),
  811. JBT<std::string>::ValueEq);
  812. cd.Defines.erase(end, cd.Defines.end());
  813. return cd;
  814. }
  815. Json::ArrayIndex Target::AddSourceCompileGroup(cmSourceFile* sf,
  816. Json::ArrayIndex si)
  817. {
  818. CompileData compileData = this->BuildCompileData(sf);
  819. auto i = this->CompileGroupMap.find(compileData);
  820. if (i == this->CompileGroupMap.end()) {
  821. Json::ArrayIndex cgIndex =
  822. static_cast<Json::ArrayIndex>(this->CompileGroups.size());
  823. i = this->CompileGroupMap.emplace(std::move(compileData), cgIndex).first;
  824. CompileGroup g;
  825. g.Entry = i;
  826. this->CompileGroups.push_back(std::move(g));
  827. }
  828. this->CompileGroups[i->second].SourceIndexes.append(si);
  829. return i->second;
  830. }
  831. void Target::AddBacktrace(Json::Value& object, cmListFileBacktrace const& bt)
  832. {
  833. if (JBTIndex backtrace = this->Backtraces.Add(bt)) {
  834. object["backtrace"] = backtrace.Index;
  835. }
  836. }
  837. void Target::AddBacktrace(Json::Value& object, JBTIndex bt)
  838. {
  839. if (bt) {
  840. object["backtrace"] = bt.Index;
  841. }
  842. }
  843. Json::Value Target::DumpPaths()
  844. {
  845. Json::Value paths = Json::objectValue;
  846. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  847. std::string const& sourceDir = lg->GetCurrentSourceDirectory();
  848. paths["source"] = RelativeIfUnder(this->TopSource, sourceDir);
  849. std::string const& buildDir = lg->GetCurrentBinaryDirectory();
  850. paths["build"] = RelativeIfUnder(this->TopBuild, buildDir);
  851. return paths;
  852. }
  853. Json::Value Target::DumpSources()
  854. {
  855. Json::Value sources = Json::arrayValue;
  856. cmGeneratorTarget::KindedSources const& kinded =
  857. this->GT->GetKindedSources(this->Config);
  858. for (cmGeneratorTarget::SourceAndKind const& sk : kinded.Sources) {
  859. sources.append(this->DumpSource(sk, sources.size()));
  860. }
  861. return sources;
  862. }
  863. Json::Value Target::DumpSource(cmGeneratorTarget::SourceAndKind const& sk,
  864. Json::ArrayIndex si)
  865. {
  866. Json::Value source = Json::objectValue;
  867. std::string const path = sk.Source.Value->ResolveFullPath();
  868. source["path"] = RelativeIfUnder(this->TopSource, path);
  869. if (sk.Source.Value->GetIsGenerated()) {
  870. source["isGenerated"] = true;
  871. }
  872. this->AddBacktrace(source, sk.Source.Backtrace);
  873. if (cmSourceGroup* sg =
  874. this->GT->Makefile->FindSourceGroup(path, this->SourceGroupsLocal)) {
  875. source["sourceGroupIndex"] = this->AddSourceGroup(sg, si);
  876. }
  877. switch (sk.Kind) {
  878. case cmGeneratorTarget::SourceKindObjectSource: {
  879. source["compileGroupIndex"] =
  880. this->AddSourceCompileGroup(sk.Source.Value, si);
  881. } break;
  882. case cmGeneratorTarget::SourceKindAppManifest:
  883. case cmGeneratorTarget::SourceKindCertificate:
  884. case cmGeneratorTarget::SourceKindCustomCommand:
  885. case cmGeneratorTarget::SourceKindExternalObject:
  886. case cmGeneratorTarget::SourceKindExtra:
  887. case cmGeneratorTarget::SourceKindHeader:
  888. case cmGeneratorTarget::SourceKindIDL:
  889. case cmGeneratorTarget::SourceKindManifest:
  890. case cmGeneratorTarget::SourceKindModuleDefinition:
  891. case cmGeneratorTarget::SourceKindResx:
  892. case cmGeneratorTarget::SourceKindXaml:
  893. break;
  894. }
  895. return source;
  896. }
  897. Json::Value Target::DumpCompileData(CompileData const& cd)
  898. {
  899. Json::Value result = Json::objectValue;
  900. if (!cd.Language.empty()) {
  901. result["language"] = cd.Language;
  902. }
  903. if (!cd.Sysroot.empty()) {
  904. result["sysroot"] = this->DumpSysroot(cd.Sysroot);
  905. }
  906. if (!cd.Flags.empty()) {
  907. result["compileCommandFragments"] = this->DumpCommandFragments(cd.Flags);
  908. }
  909. if (!cd.Includes.empty()) {
  910. Json::Value includes = Json::arrayValue;
  911. for (auto const& i : cd.Includes) {
  912. includes.append(this->DumpInclude(i));
  913. }
  914. result["includes"] = includes;
  915. }
  916. if (!cd.Defines.empty()) {
  917. Json::Value defines = Json::arrayValue;
  918. for (JBT<std::string> const& d : cd.Defines) {
  919. defines.append(this->DumpDefine(d));
  920. }
  921. result["defines"] = std::move(defines);
  922. }
  923. return result;
  924. }
  925. Json::Value Target::DumpInclude(CompileData::IncludeEntry const& inc)
  926. {
  927. Json::Value include = Json::objectValue;
  928. include["path"] = inc.Path.Value;
  929. if (inc.IsSystem) {
  930. include["isSystem"] = true;
  931. }
  932. this->AddBacktrace(include, inc.Path.Backtrace);
  933. return include;
  934. }
  935. Json::Value Target::DumpDefine(JBT<std::string> const& def)
  936. {
  937. Json::Value define = Json::objectValue;
  938. define["define"] = def.Value;
  939. this->AddBacktrace(define, def.Backtrace);
  940. return define;
  941. }
  942. Json::Value Target::DumpSourceGroups()
  943. {
  944. Json::Value sourceGroups = Json::arrayValue;
  945. for (auto& sg : this->SourceGroups) {
  946. sourceGroups.append(this->DumpSourceGroup(sg));
  947. }
  948. return sourceGroups;
  949. }
  950. Json::Value Target::DumpSourceGroup(SourceGroup& sg)
  951. {
  952. Json::Value group = Json::objectValue;
  953. group["name"] = sg.Name;
  954. group["sourceIndexes"] = std::move(sg.SourceIndexes);
  955. return group;
  956. }
  957. Json::Value Target::DumpCompileGroups()
  958. {
  959. Json::Value compileGroups = Json::arrayValue;
  960. for (auto& cg : this->CompileGroups) {
  961. compileGroups.append(this->DumpCompileGroup(cg));
  962. }
  963. return compileGroups;
  964. }
  965. Json::Value Target::DumpCompileGroup(CompileGroup& cg)
  966. {
  967. Json::Value group =
  968. this->DumpCompileData(this->MergeCompileData(cg.Entry->first));
  969. group["sourceIndexes"] = std::move(cg.SourceIndexes);
  970. return group;
  971. }
  972. Json::Value Target::DumpSysroot(std::string const& path)
  973. {
  974. Json::Value sysroot = Json::objectValue;
  975. sysroot["path"] = path;
  976. return sysroot;
  977. }
  978. Json::Value Target::DumpInstall()
  979. {
  980. Json::Value install = Json::objectValue;
  981. install["prefix"] = this->DumpInstallPrefix();
  982. install["destinations"] = this->DumpInstallDestinations();
  983. return install;
  984. }
  985. Json::Value Target::DumpInstallPrefix()
  986. {
  987. Json::Value prefix = Json::objectValue;
  988. std::string p =
  989. this->GT->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  990. cmSystemTools::ConvertToUnixSlashes(p);
  991. prefix["path"] = p;
  992. return prefix;
  993. }
  994. Json::Value Target::DumpInstallDestinations()
  995. {
  996. Json::Value destinations = Json::arrayValue;
  997. auto installGens = this->GT->Target->GetInstallGenerators();
  998. for (auto itGen : installGens) {
  999. destinations.append(this->DumpInstallDestination(itGen));
  1000. }
  1001. return destinations;
  1002. }
  1003. Json::Value Target::DumpInstallDestination(cmInstallTargetGenerator* itGen)
  1004. {
  1005. Json::Value destination = Json::objectValue;
  1006. destination["path"] = itGen->GetDestination(this->Config);
  1007. this->AddBacktrace(destination, itGen->GetBacktrace());
  1008. return destination;
  1009. }
  1010. Json::Value Target::DumpArtifacts()
  1011. {
  1012. Json::Value artifacts = Json::arrayValue;
  1013. // Object libraries have only object files as artifacts.
  1014. if (this->GT->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1015. if (!this->GT->GetGlobalGenerator()->HasKnownObjectFileLocation(nullptr)) {
  1016. return artifacts;
  1017. }
  1018. std::vector<cmSourceFile const*> objectSources;
  1019. this->GT->GetObjectSources(objectSources, this->Config);
  1020. std::string const obj_dir = this->GT->GetObjectDirectory(this->Config);
  1021. for (cmSourceFile const* sf : objectSources) {
  1022. const std::string& obj = this->GT->GetObjectName(sf);
  1023. Json::Value artifact = Json::objectValue;
  1024. artifact["path"] = RelativeIfUnder(this->TopBuild, obj_dir + obj);
  1025. artifacts.append(std::move(artifact)); // NOLINT(*)
  1026. }
  1027. return artifacts;
  1028. }
  1029. // Other target types always have a "main" artifact.
  1030. {
  1031. Json::Value artifact = Json::objectValue;
  1032. artifact["path"] =
  1033. RelativeIfUnder(this->TopBuild,
  1034. this->GT->GetFullPath(
  1035. this->Config, cmStateEnums::RuntimeBinaryArtifact));
  1036. artifacts.append(std::move(artifact)); // NOLINT(*)
  1037. }
  1038. // Add Windows-specific artifacts produced by the linker.
  1039. if (this->GT->HasImportLibrary(this->Config)) {
  1040. Json::Value artifact = Json::objectValue;
  1041. artifact["path"] =
  1042. RelativeIfUnder(this->TopBuild,
  1043. this->GT->GetFullPath(
  1044. this->Config, cmStateEnums::ImportLibraryArtifact));
  1045. artifacts.append(std::move(artifact)); // NOLINT(*)
  1046. }
  1047. if (this->GT->IsDLLPlatform() &&
  1048. this->GT->GetType() != cmStateEnums::STATIC_LIBRARY) {
  1049. cmGeneratorTarget::OutputInfo const* output =
  1050. this->GT->GetOutputInfo(this->Config);
  1051. if (output && !output->PdbDir.empty()) {
  1052. Json::Value artifact = Json::objectValue;
  1053. artifact["path"] = RelativeIfUnder(this->TopBuild,
  1054. output->PdbDir + '/' +
  1055. this->GT->GetPDBName(this->Config));
  1056. artifacts.append(std::move(artifact)); // NOLINT(*)
  1057. }
  1058. }
  1059. return artifacts;
  1060. }
  1061. Json::Value Target::DumpLink()
  1062. {
  1063. Json::Value link = Json::objectValue;
  1064. std::string lang = this->GT->GetLinkerLanguage(this->Config);
  1065. link["language"] = lang;
  1066. {
  1067. Json::Value commandFragments = this->DumpLinkCommandFragments();
  1068. if (!commandFragments.empty()) {
  1069. link["commandFragments"] = std::move(commandFragments);
  1070. }
  1071. }
  1072. if (const char* sysrootLink =
  1073. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT_LINK")) {
  1074. link["sysroot"] = this->DumpSysroot(sysrootLink);
  1075. } else if (const char* sysroot =
  1076. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT")) {
  1077. link["sysroot"] = this->DumpSysroot(sysroot);
  1078. }
  1079. if (this->GT->IsIPOEnabled(lang, this->Config)) {
  1080. link["lto"] = true;
  1081. }
  1082. return link;
  1083. }
  1084. Json::Value Target::DumpArchive()
  1085. {
  1086. Json::Value archive = Json::objectValue;
  1087. {
  1088. // The "link" fragments not relevant to static libraries are empty.
  1089. Json::Value commandFragments = this->DumpLinkCommandFragments();
  1090. if (!commandFragments.empty()) {
  1091. archive["commandFragments"] = std::move(commandFragments);
  1092. }
  1093. }
  1094. std::string lang = this->GT->GetLinkerLanguage(this->Config);
  1095. if (this->GT->IsIPOEnabled(lang, this->Config)) {
  1096. archive["lto"] = true;
  1097. }
  1098. return archive;
  1099. }
  1100. Json::Value Target::DumpLinkCommandFragments()
  1101. {
  1102. Json::Value linkFragments = Json::arrayValue;
  1103. std::string linkLanguageFlags;
  1104. std::string linkFlags;
  1105. std::string frameworkPath;
  1106. std::string linkPath;
  1107. std::string linkLibs;
  1108. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  1109. cmLinkLineComputer linkLineComputer(lg,
  1110. lg->GetStateSnapshot().GetDirectory());
  1111. lg->GetTargetFlags(&linkLineComputer, this->Config, linkLibs,
  1112. linkLanguageFlags, linkFlags, frameworkPath, linkPath,
  1113. this->GT);
  1114. linkLanguageFlags = cmTrimWhitespace(linkLanguageFlags);
  1115. linkFlags = cmTrimWhitespace(linkFlags);
  1116. frameworkPath = cmTrimWhitespace(frameworkPath);
  1117. linkPath = cmTrimWhitespace(linkPath);
  1118. linkLibs = cmTrimWhitespace(linkLibs);
  1119. if (!linkLanguageFlags.empty()) {
  1120. linkFragments.append(
  1121. this->DumpCommandFragment(std::move(linkLanguageFlags), "flags"));
  1122. }
  1123. if (!linkFlags.empty()) {
  1124. linkFragments.append(
  1125. this->DumpCommandFragment(std::move(linkFlags), "flags"));
  1126. }
  1127. if (!frameworkPath.empty()) {
  1128. linkFragments.append(
  1129. this->DumpCommandFragment(std::move(frameworkPath), "frameworkPath"));
  1130. }
  1131. if (!linkPath.empty()) {
  1132. linkFragments.append(
  1133. this->DumpCommandFragment(std::move(linkPath), "libraryPath"));
  1134. }
  1135. if (!linkLibs.empty()) {
  1136. linkFragments.append(
  1137. this->DumpCommandFragment(std::move(linkLibs), "libraries"));
  1138. }
  1139. return linkFragments;
  1140. }
  1141. Json::Value Target::DumpCommandFragments(
  1142. std::vector<JBT<std::string>> const& frags)
  1143. {
  1144. Json::Value commandFragments = Json::arrayValue;
  1145. for (JBT<std::string> const& f : frags) {
  1146. commandFragments.append(this->DumpCommandFragment(f));
  1147. }
  1148. return commandFragments;
  1149. }
  1150. Json::Value Target::DumpCommandFragment(JBT<std::string> const& frag,
  1151. std::string const& role)
  1152. {
  1153. Json::Value fragment = Json::objectValue;
  1154. fragment["fragment"] = frag.Value;
  1155. if (!role.empty()) {
  1156. fragment["role"] = role;
  1157. }
  1158. this->AddBacktrace(fragment, frag.Backtrace);
  1159. return fragment;
  1160. }
  1161. Json::Value Target::DumpDependencies()
  1162. {
  1163. Json::Value dependencies = Json::arrayValue;
  1164. cmGlobalGenerator* gg = this->GT->GetGlobalGenerator();
  1165. for (cmTargetDepend const& td : gg->GetTargetDirectDepends(this->GT)) {
  1166. dependencies.append(this->DumpDependency(td));
  1167. }
  1168. return dependencies;
  1169. }
  1170. Json::Value Target::DumpDependency(cmTargetDepend const& td)
  1171. {
  1172. Json::Value dependency = Json::objectValue;
  1173. dependency["id"] = TargetId(td, this->TopBuild);
  1174. this->AddBacktrace(dependency, td.GetBacktrace());
  1175. return dependency;
  1176. }
  1177. Json::Value Target::DumpFolder()
  1178. {
  1179. Json::Value folder;
  1180. if (const char* f = this->GT->GetProperty("FOLDER")) {
  1181. folder = Json::objectValue;
  1182. folder["name"] = f;
  1183. }
  1184. return folder;
  1185. }
  1186. }
  1187. Json::Value cmFileAPICodemodelDump(cmFileAPI& fileAPI, unsigned long version)
  1188. {
  1189. Codemodel codemodel(fileAPI, version);
  1190. return codemodel.Dump();
  1191. }