cmFileAPICodemodel.cxx 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  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. std::vector<std::string> configs;
  377. cmGlobalGenerator* gg =
  378. this->FileAPI.GetCMakeInstance()->GetGlobalGenerator();
  379. auto makefiles = gg->GetMakefiles();
  380. if (!makefiles.empty()) {
  381. makefiles[0]->GetConfigurations(configs);
  382. if (configs.empty()) {
  383. configs.emplace_back();
  384. }
  385. }
  386. Json::Value configurations = Json::arrayValue;
  387. for (std::string const& config : configs) {
  388. configurations.append(this->DumpConfiguration(config));
  389. }
  390. return configurations;
  391. }
  392. Json::Value Codemodel::DumpConfiguration(std::string const& config)
  393. {
  394. CodemodelConfig configuration(this->FileAPI, this->Version, config);
  395. return configuration.Dump();
  396. }
  397. CodemodelConfig::CodemodelConfig(cmFileAPI& fileAPI, unsigned long version,
  398. std::string const& config)
  399. : FileAPI(fileAPI)
  400. , Version(version)
  401. , Config(config)
  402. , TopSource(this->FileAPI.GetCMakeInstance()->GetHomeDirectory())
  403. , TopBuild(this->FileAPI.GetCMakeInstance()->GetHomeOutputDirectory())
  404. {
  405. static_cast<void>(this->Version);
  406. }
  407. Json::Value CodemodelConfig::Dump()
  408. {
  409. Json::Value configuration = Json::objectValue;
  410. configuration["name"] = this->Config;
  411. this->ProcessDirectories();
  412. configuration["targets"] = this->DumpTargets();
  413. configuration["directories"] = this->DumpDirectories();
  414. configuration["projects"] = this->DumpProjects();
  415. return configuration;
  416. }
  417. void CodemodelConfig::ProcessDirectories()
  418. {
  419. cmGlobalGenerator* gg =
  420. this->FileAPI.GetCMakeInstance()->GetGlobalGenerator();
  421. std::vector<cmLocalGenerator*> const& localGens = gg->GetLocalGenerators();
  422. // Add directories in forward order to process parents before children.
  423. this->Directories.reserve(localGens.size());
  424. for (cmLocalGenerator* lg : localGens) {
  425. auto directoryIndex =
  426. static_cast<Json::ArrayIndex>(this->Directories.size());
  427. this->Directories.emplace_back();
  428. Directory& d = this->Directories[directoryIndex];
  429. d.Snapshot = lg->GetStateSnapshot().GetBuildsystemDirectory();
  430. d.LocalGenerator = lg;
  431. this->DirectoryMap[d.Snapshot] = directoryIndex;
  432. d.ProjectIndex = this->AddProject(d.Snapshot);
  433. this->Projects[d.ProjectIndex].DirectoryIndexes.append(directoryIndex);
  434. }
  435. // Update directories in reverse order to process children before parents.
  436. for (auto di = this->Directories.rbegin(); di != this->Directories.rend();
  437. ++di) {
  438. Directory& d = *di;
  439. // Accumulate the presence of install rules on the way up.
  440. for (auto gen : d.LocalGenerator->GetMakefile()->GetInstallGenerators()) {
  441. if (!dynamic_cast<cmInstallSubdirectoryGenerator*>(gen)) {
  442. d.HasInstallRule = true;
  443. break;
  444. }
  445. }
  446. if (!d.HasInstallRule) {
  447. for (cmStateSnapshot const& child : d.Snapshot.GetChildren()) {
  448. cmStateSnapshot childDir = child.GetBuildsystemDirectory();
  449. Json::ArrayIndex const childIndex = this->GetDirectoryIndex(childDir);
  450. if (this->Directories[childIndex].HasInstallRule) {
  451. d.HasInstallRule = true;
  452. break;
  453. }
  454. }
  455. }
  456. }
  457. }
  458. Json::ArrayIndex CodemodelConfig::GetDirectoryIndex(cmLocalGenerator const* lg)
  459. {
  460. return this->GetDirectoryIndex(
  461. lg->GetStateSnapshot().GetBuildsystemDirectory());
  462. }
  463. Json::ArrayIndex CodemodelConfig::GetDirectoryIndex(cmStateSnapshot s)
  464. {
  465. auto i = this->DirectoryMap.find(s);
  466. assert(i != this->DirectoryMap.end());
  467. return i->second;
  468. }
  469. Json::ArrayIndex CodemodelConfig::AddProject(cmStateSnapshot s)
  470. {
  471. cmStateSnapshot ps = s.GetBuildsystemDirectoryParent();
  472. if (ps.IsValid() && ps.GetProjectName() == s.GetProjectName()) {
  473. // This directory is part of its parent directory project.
  474. Json::ArrayIndex const parentDirIndex = this->GetDirectoryIndex(ps);
  475. return this->Directories[parentDirIndex].ProjectIndex;
  476. }
  477. // This directory starts a new project.
  478. auto projectIndex = static_cast<Json::ArrayIndex>(this->Projects.size());
  479. this->Projects.emplace_back();
  480. Project& p = this->Projects[projectIndex];
  481. p.Snapshot = s;
  482. this->ProjectMap[s] = projectIndex;
  483. if (ps.IsValid()) {
  484. Json::ArrayIndex const parentDirIndex = this->GetDirectoryIndex(ps);
  485. p.ParentIndex = this->Directories[parentDirIndex].ProjectIndex;
  486. this->Projects[p.ParentIndex].ChildIndexes.append(projectIndex);
  487. }
  488. return projectIndex;
  489. }
  490. Json::Value CodemodelConfig::DumpTargets()
  491. {
  492. Json::Value targets = Json::arrayValue;
  493. std::vector<cmGeneratorTarget*> targetList;
  494. cmGlobalGenerator* gg =
  495. this->FileAPI.GetCMakeInstance()->GetGlobalGenerator();
  496. for (cmLocalGenerator const* lg : gg->GetLocalGenerators()) {
  497. cmAppend(targetList, lg->GetGeneratorTargets());
  498. }
  499. std::sort(targetList.begin(), targetList.end(),
  500. [](cmGeneratorTarget* l, cmGeneratorTarget* r) {
  501. return l->GetName() < r->GetName();
  502. });
  503. for (cmGeneratorTarget* gt : targetList) {
  504. if (gt->GetType() == cmStateEnums::GLOBAL_TARGET ||
  505. gt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  506. continue;
  507. }
  508. targets.append(this->DumpTarget(gt, targets.size()));
  509. }
  510. return targets;
  511. }
  512. Json::Value CodemodelConfig::DumpTarget(cmGeneratorTarget* gt,
  513. Json::ArrayIndex ti)
  514. {
  515. Target t(gt, this->Config);
  516. std::string prefix = "target-" + gt->GetName();
  517. if (!this->Config.empty()) {
  518. prefix += "-" + this->Config;
  519. }
  520. Json::Value target = this->FileAPI.MaybeJsonFile(t.Dump(), prefix);
  521. target["name"] = gt->GetName();
  522. target["id"] = TargetId(gt, this->TopBuild);
  523. // Cross-reference directory containing target.
  524. Json::ArrayIndex di = this->GetDirectoryIndex(gt->GetLocalGenerator());
  525. target["directoryIndex"] = di;
  526. this->Directories[di].TargetIndexes.append(ti);
  527. // Cross-reference project containing target.
  528. Json::ArrayIndex pi = this->Directories[di].ProjectIndex;
  529. target["projectIndex"] = pi;
  530. this->Projects[pi].TargetIndexes.append(ti);
  531. return target;
  532. }
  533. Json::Value CodemodelConfig::DumpDirectories()
  534. {
  535. Json::Value directories = Json::arrayValue;
  536. for (Directory& d : this->Directories) {
  537. directories.append(this->DumpDirectory(d));
  538. }
  539. return directories;
  540. }
  541. Json::Value CodemodelConfig::DumpDirectory(Directory& d)
  542. {
  543. Json::Value directory = Json::objectValue;
  544. std::string sourceDir = d.Snapshot.GetDirectory().GetCurrentSource();
  545. directory["source"] = RelativeIfUnder(this->TopSource, sourceDir);
  546. std::string buildDir = d.Snapshot.GetDirectory().GetCurrentBinary();
  547. directory["build"] = RelativeIfUnder(this->TopBuild, buildDir);
  548. cmStateSnapshot parentDir = d.Snapshot.GetBuildsystemDirectoryParent();
  549. if (parentDir.IsValid()) {
  550. directory["parentIndex"] = this->GetDirectoryIndex(parentDir);
  551. }
  552. Json::Value childIndexes = Json::arrayValue;
  553. for (cmStateSnapshot const& child : d.Snapshot.GetChildren()) {
  554. childIndexes.append(
  555. this->GetDirectoryIndex(child.GetBuildsystemDirectory()));
  556. }
  557. if (!childIndexes.empty()) {
  558. directory["childIndexes"] = std::move(childIndexes);
  559. }
  560. directory["projectIndex"] = d.ProjectIndex;
  561. if (!d.TargetIndexes.empty()) {
  562. directory["targetIndexes"] = std::move(d.TargetIndexes);
  563. }
  564. Json::Value minimumCMakeVersion = this->DumpMinimumCMakeVersion(d.Snapshot);
  565. if (!minimumCMakeVersion.isNull()) {
  566. directory["minimumCMakeVersion"] = std::move(minimumCMakeVersion);
  567. }
  568. if (d.HasInstallRule) {
  569. directory["hasInstallRule"] = true;
  570. }
  571. return directory;
  572. }
  573. Json::Value CodemodelConfig::DumpProjects()
  574. {
  575. Json::Value projects = Json::arrayValue;
  576. for (Project& p : this->Projects) {
  577. projects.append(this->DumpProject(p));
  578. }
  579. return projects;
  580. }
  581. Json::Value CodemodelConfig::DumpProject(Project& p)
  582. {
  583. Json::Value project = Json::objectValue;
  584. project["name"] = p.Snapshot.GetProjectName();
  585. if (p.ParentIndex != Project::NoParentIndex) {
  586. project["parentIndex"] = p.ParentIndex;
  587. }
  588. if (!p.ChildIndexes.empty()) {
  589. project["childIndexes"] = std::move(p.ChildIndexes);
  590. }
  591. project["directoryIndexes"] = std::move(p.DirectoryIndexes);
  592. if (!p.TargetIndexes.empty()) {
  593. project["targetIndexes"] = std::move(p.TargetIndexes);
  594. }
  595. return project;
  596. }
  597. Json::Value CodemodelConfig::DumpMinimumCMakeVersion(cmStateSnapshot s)
  598. {
  599. Json::Value minimumCMakeVersion;
  600. if (std::string const* def =
  601. s.GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION")) {
  602. minimumCMakeVersion = Json::objectValue;
  603. minimumCMakeVersion["string"] = *def;
  604. }
  605. return minimumCMakeVersion;
  606. }
  607. Target::Target(cmGeneratorTarget* gt, std::string const& config)
  608. : GT(gt)
  609. , Config(config)
  610. , TopSource(gt->GetGlobalGenerator()->GetCMakeInstance()->GetHomeDirectory())
  611. , TopBuild(
  612. gt->GetGlobalGenerator()->GetCMakeInstance()->GetHomeOutputDirectory())
  613. , SourceGroupsLocal(this->GT->Makefile->GetSourceGroups())
  614. , Backtraces(this->TopSource)
  615. {
  616. }
  617. Json::Value Target::Dump()
  618. {
  619. Json::Value target = Json::objectValue;
  620. cmStateEnums::TargetType const type = this->GT->GetType();
  621. target["name"] = this->GT->GetName();
  622. target["type"] = cmState::GetTargetTypeName(type);
  623. target["id"] = TargetId(this->GT, this->TopBuild);
  624. target["paths"] = this->DumpPaths();
  625. if (this->GT->Target->GetIsGeneratorProvided()) {
  626. target["isGeneratorProvided"] = true;
  627. }
  628. this->AddBacktrace(target, this->GT->GetBacktrace());
  629. if (this->GT->Target->GetHaveInstallRule()) {
  630. target["install"] = this->DumpInstall();
  631. }
  632. if (this->GT->HaveWellDefinedOutputFiles()) {
  633. Json::Value artifacts = this->DumpArtifacts();
  634. if (!artifacts.empty()) {
  635. target["artifacts"] = std::move(artifacts);
  636. }
  637. }
  638. if (type == cmStateEnums::EXECUTABLE ||
  639. type == cmStateEnums::SHARED_LIBRARY ||
  640. type == cmStateEnums::MODULE_LIBRARY) {
  641. target["nameOnDisk"] = this->GT->GetFullName(this->Config);
  642. target["link"] = this->DumpLink();
  643. } else if (type == cmStateEnums::STATIC_LIBRARY) {
  644. target["nameOnDisk"] = this->GT->GetFullName(this->Config);
  645. target["archive"] = this->DumpArchive();
  646. }
  647. Json::Value dependencies = this->DumpDependencies();
  648. if (!dependencies.empty()) {
  649. target["dependencies"] = dependencies;
  650. }
  651. {
  652. this->ProcessLanguages();
  653. target["sources"] = this->DumpSources();
  654. Json::Value folder = this->DumpFolder();
  655. if (!folder.isNull()) {
  656. target["folder"] = std::move(folder);
  657. }
  658. Json::Value sourceGroups = this->DumpSourceGroups();
  659. if (!sourceGroups.empty()) {
  660. target["sourceGroups"] = std::move(sourceGroups);
  661. }
  662. Json::Value compileGroups = this->DumpCompileGroups();
  663. if (!compileGroups.empty()) {
  664. target["compileGroups"] = std::move(compileGroups);
  665. }
  666. }
  667. target["backtraceGraph"] = this->Backtraces.Dump();
  668. return target;
  669. }
  670. void Target::ProcessLanguages()
  671. {
  672. std::set<std::string> languages;
  673. this->GT->GetLanguages(languages, this->Config);
  674. for (std::string const& lang : languages) {
  675. this->ProcessLanguage(lang);
  676. }
  677. }
  678. void Target::ProcessLanguage(std::string const& lang)
  679. {
  680. CompileData& cd = this->CompileDataMap[lang];
  681. cd.Language = lang;
  682. if (const char* sysrootCompile =
  683. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT_COMPILE")) {
  684. cd.Sysroot = sysrootCompile;
  685. } else if (const char* sysroot =
  686. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT")) {
  687. cd.Sysroot = sysroot;
  688. }
  689. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  690. {
  691. // FIXME: Add flags from end section of ExpandRuleVariable,
  692. // which may need to be factored out.
  693. std::string flags;
  694. lg->GetTargetCompileFlags(this->GT, this->Config, lang, flags);
  695. cd.Flags.emplace_back(std::move(flags), JBTIndex());
  696. }
  697. std::set<BT<std::string>> defines =
  698. lg->GetTargetDefines(this->GT, this->Config, lang);
  699. cd.Defines.reserve(defines.size());
  700. for (BT<std::string> const& d : defines) {
  701. cd.Defines.emplace_back(this->ToJBT(d));
  702. }
  703. std::vector<BT<std::string>> includePathList =
  704. lg->GetIncludeDirectories(this->GT, lang, this->Config);
  705. for (BT<std::string> const& i : includePathList) {
  706. cd.Includes.emplace_back(
  707. this->ToJBT(i),
  708. this->GT->IsSystemIncludeDirectory(i.Value, this->Config, lang));
  709. }
  710. }
  711. Json::ArrayIndex Target::AddSourceGroup(cmSourceGroup* sg, Json::ArrayIndex si)
  712. {
  713. std::unordered_map<cmSourceGroup const*, Json::ArrayIndex>::iterator i =
  714. this->SourceGroupsMap.find(sg);
  715. if (i == this->SourceGroupsMap.end()) {
  716. auto sgIndex = static_cast<Json::ArrayIndex>(this->SourceGroups.size());
  717. i = this->SourceGroupsMap.emplace(sg, sgIndex).first;
  718. SourceGroup g;
  719. g.Name = sg->GetFullName();
  720. this->SourceGroups.push_back(std::move(g));
  721. }
  722. this->SourceGroups[i->second].SourceIndexes.append(si);
  723. return i->second;
  724. }
  725. CompileData Target::BuildCompileData(cmSourceFile* sf)
  726. {
  727. CompileData fd;
  728. fd.Language = sf->GetLanguage();
  729. if (fd.Language.empty()) {
  730. return fd;
  731. }
  732. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  733. cmGeneratorExpressionInterpreter genexInterpreter(lg, this->Config, this->GT,
  734. fd.Language);
  735. const std::string COMPILE_FLAGS("COMPILE_FLAGS");
  736. if (const char* cflags = sf->GetProperty(COMPILE_FLAGS)) {
  737. std::string flags = genexInterpreter.Evaluate(cflags, COMPILE_FLAGS);
  738. fd.Flags.emplace_back(std::move(flags), JBTIndex());
  739. }
  740. const std::string COMPILE_OPTIONS("COMPILE_OPTIONS");
  741. if (const char* coptions = sf->GetProperty(COMPILE_OPTIONS)) {
  742. std::string flags;
  743. lg->AppendCompileOptions(
  744. flags, genexInterpreter.Evaluate(coptions, COMPILE_OPTIONS));
  745. fd.Flags.emplace_back(std::move(flags), JBTIndex());
  746. }
  747. // Add include directories from source file properties.
  748. {
  749. std::vector<std::string> includes;
  750. const std::string INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
  751. if (const char* cincludes = sf->GetProperty(INCLUDE_DIRECTORIES)) {
  752. const std::string& evaluatedIncludes =
  753. genexInterpreter.Evaluate(cincludes, INCLUDE_DIRECTORIES);
  754. lg->AppendIncludeDirectories(includes, evaluatedIncludes, *sf);
  755. for (std::string const& include : includes) {
  756. bool const isSystemInclude = this->GT->IsSystemIncludeDirectory(
  757. include, this->Config, fd.Language);
  758. fd.Includes.emplace_back(include, isSystemInclude);
  759. }
  760. }
  761. }
  762. const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
  763. std::set<std::string> fileDefines;
  764. if (const char* defs = sf->GetProperty(COMPILE_DEFINITIONS)) {
  765. lg->AppendDefines(fileDefines,
  766. genexInterpreter.Evaluate(defs, COMPILE_DEFINITIONS));
  767. }
  768. const std::string defPropName =
  769. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(this->Config);
  770. if (const char* config_defs = sf->GetProperty(defPropName)) {
  771. lg->AppendDefines(
  772. fileDefines,
  773. genexInterpreter.Evaluate(config_defs, COMPILE_DEFINITIONS));
  774. }
  775. fd.Defines.reserve(fileDefines.size());
  776. for (std::string const& d : fileDefines) {
  777. fd.Defines.emplace_back(d, JBTIndex());
  778. }
  779. return fd;
  780. }
  781. CompileData Target::MergeCompileData(CompileData const& fd)
  782. {
  783. CompileData cd;
  784. cd.Language = fd.Language;
  785. if (cd.Language.empty()) {
  786. return cd;
  787. }
  788. CompileData const& td = this->CompileDataMap.at(cd.Language);
  789. // All compile groups share the sysroot of the target.
  790. cd.Sysroot = td.Sysroot;
  791. // Use target-wide flags followed by source-specific flags.
  792. cd.Flags.reserve(td.Flags.size() + fd.Flags.size());
  793. cd.Flags.insert(cd.Flags.end(), td.Flags.begin(), td.Flags.end());
  794. cd.Flags.insert(cd.Flags.end(), fd.Flags.begin(), fd.Flags.end());
  795. // Use source-specific includes followed by target-wide includes.
  796. cd.Includes.reserve(fd.Includes.size() + td.Includes.size());
  797. cd.Includes.insert(cd.Includes.end(), fd.Includes.begin(),
  798. fd.Includes.end());
  799. cd.Includes.insert(cd.Includes.end(), td.Includes.begin(),
  800. td.Includes.end());
  801. // Use target-wide defines followed by source-specific defines.
  802. cd.Defines.reserve(td.Defines.size() + fd.Defines.size());
  803. cd.Defines.insert(cd.Defines.end(), td.Defines.begin(), td.Defines.end());
  804. cd.Defines.insert(cd.Defines.end(), fd.Defines.begin(), fd.Defines.end());
  805. // De-duplicate defines.
  806. std::stable_sort(cd.Defines.begin(), cd.Defines.end(),
  807. JBT<std::string>::ValueLess);
  808. auto end = std::unique(cd.Defines.begin(), cd.Defines.end(),
  809. JBT<std::string>::ValueEq);
  810. cd.Defines.erase(end, cd.Defines.end());
  811. return cd;
  812. }
  813. Json::ArrayIndex Target::AddSourceCompileGroup(cmSourceFile* sf,
  814. Json::ArrayIndex si)
  815. {
  816. CompileData compileData = this->BuildCompileData(sf);
  817. auto i = this->CompileGroupMap.find(compileData);
  818. if (i == this->CompileGroupMap.end()) {
  819. Json::ArrayIndex cgIndex =
  820. static_cast<Json::ArrayIndex>(this->CompileGroups.size());
  821. i = this->CompileGroupMap.emplace(std::move(compileData), cgIndex).first;
  822. CompileGroup g;
  823. g.Entry = i;
  824. this->CompileGroups.push_back(std::move(g));
  825. }
  826. this->CompileGroups[i->second].SourceIndexes.append(si);
  827. return i->second;
  828. }
  829. void Target::AddBacktrace(Json::Value& object, cmListFileBacktrace const& bt)
  830. {
  831. if (JBTIndex backtrace = this->Backtraces.Add(bt)) {
  832. object["backtrace"] = backtrace.Index;
  833. }
  834. }
  835. void Target::AddBacktrace(Json::Value& object, JBTIndex bt)
  836. {
  837. if (bt) {
  838. object["backtrace"] = bt.Index;
  839. }
  840. }
  841. Json::Value Target::DumpPaths()
  842. {
  843. Json::Value paths = Json::objectValue;
  844. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  845. std::string const& sourceDir = lg->GetCurrentSourceDirectory();
  846. paths["source"] = RelativeIfUnder(this->TopSource, sourceDir);
  847. std::string const& buildDir = lg->GetCurrentBinaryDirectory();
  848. paths["build"] = RelativeIfUnder(this->TopBuild, buildDir);
  849. return paths;
  850. }
  851. Json::Value Target::DumpSources()
  852. {
  853. Json::Value sources = Json::arrayValue;
  854. cmGeneratorTarget::KindedSources const& kinded =
  855. this->GT->GetKindedSources(this->Config);
  856. for (cmGeneratorTarget::SourceAndKind const& sk : kinded.Sources) {
  857. sources.append(this->DumpSource(sk, sources.size()));
  858. }
  859. return sources;
  860. }
  861. Json::Value Target::DumpSource(cmGeneratorTarget::SourceAndKind const& sk,
  862. Json::ArrayIndex si)
  863. {
  864. Json::Value source = Json::objectValue;
  865. std::string const path = sk.Source.Value->GetFullPath();
  866. source["path"] = RelativeIfUnder(this->TopSource, path);
  867. if (sk.Source.Value->GetIsGenerated()) {
  868. source["isGenerated"] = true;
  869. }
  870. this->AddBacktrace(source, sk.Source.Backtrace);
  871. if (cmSourceGroup* sg =
  872. this->GT->Makefile->FindSourceGroup(path, this->SourceGroupsLocal)) {
  873. source["sourceGroupIndex"] = this->AddSourceGroup(sg, si);
  874. }
  875. switch (sk.Kind) {
  876. case cmGeneratorTarget::SourceKindObjectSource: {
  877. source["compileGroupIndex"] =
  878. this->AddSourceCompileGroup(sk.Source.Value, si);
  879. } break;
  880. case cmGeneratorTarget::SourceKindAppManifest:
  881. case cmGeneratorTarget::SourceKindCertificate:
  882. case cmGeneratorTarget::SourceKindCustomCommand:
  883. case cmGeneratorTarget::SourceKindExternalObject:
  884. case cmGeneratorTarget::SourceKindExtra:
  885. case cmGeneratorTarget::SourceKindHeader:
  886. case cmGeneratorTarget::SourceKindIDL:
  887. case cmGeneratorTarget::SourceKindManifest:
  888. case cmGeneratorTarget::SourceKindModuleDefinition:
  889. case cmGeneratorTarget::SourceKindResx:
  890. case cmGeneratorTarget::SourceKindXaml:
  891. break;
  892. }
  893. return source;
  894. }
  895. Json::Value Target::DumpCompileData(CompileData const& cd)
  896. {
  897. Json::Value result = Json::objectValue;
  898. if (!cd.Language.empty()) {
  899. result["language"] = cd.Language;
  900. }
  901. if (!cd.Sysroot.empty()) {
  902. result["sysroot"] = this->DumpSysroot(cd.Sysroot);
  903. }
  904. if (!cd.Flags.empty()) {
  905. result["compileCommandFragments"] = this->DumpCommandFragments(cd.Flags);
  906. }
  907. if (!cd.Includes.empty()) {
  908. Json::Value includes = Json::arrayValue;
  909. for (auto const& i : cd.Includes) {
  910. includes.append(this->DumpInclude(i));
  911. }
  912. result["includes"] = includes;
  913. }
  914. if (!cd.Defines.empty()) {
  915. Json::Value defines = Json::arrayValue;
  916. for (JBT<std::string> const& d : cd.Defines) {
  917. defines.append(this->DumpDefine(d));
  918. }
  919. result["defines"] = std::move(defines);
  920. }
  921. return result;
  922. }
  923. Json::Value Target::DumpInclude(CompileData::IncludeEntry const& inc)
  924. {
  925. Json::Value include = Json::objectValue;
  926. include["path"] = inc.Path.Value;
  927. if (inc.IsSystem) {
  928. include["isSystem"] = true;
  929. }
  930. this->AddBacktrace(include, inc.Path.Backtrace);
  931. return include;
  932. }
  933. Json::Value Target::DumpDefine(JBT<std::string> const& def)
  934. {
  935. Json::Value define = Json::objectValue;
  936. define["define"] = def.Value;
  937. this->AddBacktrace(define, def.Backtrace);
  938. return define;
  939. }
  940. Json::Value Target::DumpSourceGroups()
  941. {
  942. Json::Value sourceGroups = Json::arrayValue;
  943. for (auto& sg : this->SourceGroups) {
  944. sourceGroups.append(this->DumpSourceGroup(sg));
  945. }
  946. return sourceGroups;
  947. }
  948. Json::Value Target::DumpSourceGroup(SourceGroup& sg)
  949. {
  950. Json::Value group = Json::objectValue;
  951. group["name"] = sg.Name;
  952. group["sourceIndexes"] = std::move(sg.SourceIndexes);
  953. return group;
  954. }
  955. Json::Value Target::DumpCompileGroups()
  956. {
  957. Json::Value compileGroups = Json::arrayValue;
  958. for (auto& cg : this->CompileGroups) {
  959. compileGroups.append(this->DumpCompileGroup(cg));
  960. }
  961. return compileGroups;
  962. }
  963. Json::Value Target::DumpCompileGroup(CompileGroup& cg)
  964. {
  965. Json::Value group =
  966. this->DumpCompileData(this->MergeCompileData(cg.Entry->first));
  967. group["sourceIndexes"] = std::move(cg.SourceIndexes);
  968. return group;
  969. }
  970. Json::Value Target::DumpSysroot(std::string const& path)
  971. {
  972. Json::Value sysroot = Json::objectValue;
  973. sysroot["path"] = path;
  974. return sysroot;
  975. }
  976. Json::Value Target::DumpInstall()
  977. {
  978. Json::Value install = Json::objectValue;
  979. install["prefix"] = this->DumpInstallPrefix();
  980. install["destinations"] = this->DumpInstallDestinations();
  981. return install;
  982. }
  983. Json::Value Target::DumpInstallPrefix()
  984. {
  985. Json::Value prefix = Json::objectValue;
  986. std::string p =
  987. this->GT->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  988. cmSystemTools::ConvertToUnixSlashes(p);
  989. prefix["path"] = p;
  990. return prefix;
  991. }
  992. Json::Value Target::DumpInstallDestinations()
  993. {
  994. Json::Value destinations = Json::arrayValue;
  995. auto installGens = this->GT->Target->GetInstallGenerators();
  996. for (auto itGen : installGens) {
  997. destinations.append(this->DumpInstallDestination(itGen));
  998. }
  999. return destinations;
  1000. }
  1001. Json::Value Target::DumpInstallDestination(cmInstallTargetGenerator* itGen)
  1002. {
  1003. Json::Value destination = Json::objectValue;
  1004. destination["path"] = itGen->GetDestination(this->Config);
  1005. this->AddBacktrace(destination, itGen->GetBacktrace());
  1006. return destination;
  1007. }
  1008. Json::Value Target::DumpArtifacts()
  1009. {
  1010. Json::Value artifacts = Json::arrayValue;
  1011. // Object libraries have only object files as artifacts.
  1012. if (this->GT->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1013. if (!this->GT->GetGlobalGenerator()->HasKnownObjectFileLocation(nullptr)) {
  1014. return artifacts;
  1015. }
  1016. std::vector<cmSourceFile const*> objectSources;
  1017. this->GT->GetObjectSources(objectSources, this->Config);
  1018. std::string const obj_dir = this->GT->GetObjectDirectory(this->Config);
  1019. for (cmSourceFile const* sf : objectSources) {
  1020. const std::string& obj = this->GT->GetObjectName(sf);
  1021. Json::Value artifact = Json::objectValue;
  1022. artifact["path"] = RelativeIfUnder(this->TopBuild, obj_dir + obj);
  1023. artifacts.append(std::move(artifact)); // NOLINT(*)
  1024. }
  1025. return artifacts;
  1026. }
  1027. // Other target types always have a "main" artifact.
  1028. {
  1029. Json::Value artifact = Json::objectValue;
  1030. artifact["path"] =
  1031. RelativeIfUnder(this->TopBuild,
  1032. this->GT->GetFullPath(
  1033. this->Config, cmStateEnums::RuntimeBinaryArtifact));
  1034. artifacts.append(std::move(artifact)); // NOLINT(*)
  1035. }
  1036. // Add Windows-specific artifacts produced by the linker.
  1037. if (this->GT->HasImportLibrary(this->Config)) {
  1038. Json::Value artifact = Json::objectValue;
  1039. artifact["path"] =
  1040. RelativeIfUnder(this->TopBuild,
  1041. this->GT->GetFullPath(
  1042. this->Config, cmStateEnums::ImportLibraryArtifact));
  1043. artifacts.append(std::move(artifact)); // NOLINT(*)
  1044. }
  1045. if (this->GT->IsDLLPlatform() &&
  1046. this->GT->GetType() != cmStateEnums::STATIC_LIBRARY) {
  1047. cmGeneratorTarget::OutputInfo const* output =
  1048. this->GT->GetOutputInfo(this->Config);
  1049. if (output && !output->PdbDir.empty()) {
  1050. Json::Value artifact = Json::objectValue;
  1051. artifact["path"] = RelativeIfUnder(this->TopBuild,
  1052. output->PdbDir + '/' +
  1053. this->GT->GetPDBName(this->Config));
  1054. artifacts.append(std::move(artifact)); // NOLINT(*)
  1055. }
  1056. }
  1057. return artifacts;
  1058. }
  1059. Json::Value Target::DumpLink()
  1060. {
  1061. Json::Value link = Json::objectValue;
  1062. std::string lang = this->GT->GetLinkerLanguage(this->Config);
  1063. link["language"] = lang;
  1064. {
  1065. Json::Value commandFragments = this->DumpLinkCommandFragments();
  1066. if (!commandFragments.empty()) {
  1067. link["commandFragments"] = std::move(commandFragments);
  1068. }
  1069. }
  1070. if (const char* sysrootLink =
  1071. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT_LINK")) {
  1072. link["sysroot"] = this->DumpSysroot(sysrootLink);
  1073. } else if (const char* sysroot =
  1074. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT")) {
  1075. link["sysroot"] = this->DumpSysroot(sysroot);
  1076. }
  1077. if (this->GT->IsIPOEnabled(lang, this->Config)) {
  1078. link["lto"] = true;
  1079. }
  1080. return link;
  1081. }
  1082. Json::Value Target::DumpArchive()
  1083. {
  1084. Json::Value archive = Json::objectValue;
  1085. {
  1086. // The "link" fragments not relevant to static libraries are empty.
  1087. Json::Value commandFragments = this->DumpLinkCommandFragments();
  1088. if (!commandFragments.empty()) {
  1089. archive["commandFragments"] = std::move(commandFragments);
  1090. }
  1091. }
  1092. std::string lang = this->GT->GetLinkerLanguage(this->Config);
  1093. if (this->GT->IsIPOEnabled(lang, this->Config)) {
  1094. archive["lto"] = true;
  1095. }
  1096. return archive;
  1097. }
  1098. Json::Value Target::DumpLinkCommandFragments()
  1099. {
  1100. Json::Value linkFragments = Json::arrayValue;
  1101. std::string linkLanguageFlags;
  1102. std::string linkFlags;
  1103. std::string frameworkPath;
  1104. std::string linkPath;
  1105. std::string linkLibs;
  1106. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  1107. cmLinkLineComputer linkLineComputer(lg,
  1108. lg->GetStateSnapshot().GetDirectory());
  1109. lg->GetTargetFlags(&linkLineComputer, this->Config, linkLibs,
  1110. linkLanguageFlags, linkFlags, frameworkPath, linkPath,
  1111. this->GT);
  1112. linkLanguageFlags = cmTrimWhitespace(linkLanguageFlags);
  1113. linkFlags = cmTrimWhitespace(linkFlags);
  1114. frameworkPath = cmTrimWhitespace(frameworkPath);
  1115. linkPath = cmTrimWhitespace(linkPath);
  1116. linkLibs = cmTrimWhitespace(linkLibs);
  1117. if (!linkLanguageFlags.empty()) {
  1118. linkFragments.append(
  1119. this->DumpCommandFragment(std::move(linkLanguageFlags), "flags"));
  1120. }
  1121. if (!linkFlags.empty()) {
  1122. linkFragments.append(
  1123. this->DumpCommandFragment(std::move(linkFlags), "flags"));
  1124. }
  1125. if (!frameworkPath.empty()) {
  1126. linkFragments.append(
  1127. this->DumpCommandFragment(std::move(frameworkPath), "frameworkPath"));
  1128. }
  1129. if (!linkPath.empty()) {
  1130. linkFragments.append(
  1131. this->DumpCommandFragment(std::move(linkPath), "libraryPath"));
  1132. }
  1133. if (!linkLibs.empty()) {
  1134. linkFragments.append(
  1135. this->DumpCommandFragment(std::move(linkLibs), "libraries"));
  1136. }
  1137. return linkFragments;
  1138. }
  1139. Json::Value Target::DumpCommandFragments(
  1140. std::vector<JBT<std::string>> const& frags)
  1141. {
  1142. Json::Value commandFragments = Json::arrayValue;
  1143. for (JBT<std::string> const& f : frags) {
  1144. commandFragments.append(this->DumpCommandFragment(f));
  1145. }
  1146. return commandFragments;
  1147. }
  1148. Json::Value Target::DumpCommandFragment(JBT<std::string> const& frag,
  1149. std::string const& role)
  1150. {
  1151. Json::Value fragment = Json::objectValue;
  1152. fragment["fragment"] = frag.Value;
  1153. if (!role.empty()) {
  1154. fragment["role"] = role;
  1155. }
  1156. this->AddBacktrace(fragment, frag.Backtrace);
  1157. return fragment;
  1158. }
  1159. Json::Value Target::DumpDependencies()
  1160. {
  1161. Json::Value dependencies = Json::arrayValue;
  1162. cmGlobalGenerator* gg = this->GT->GetGlobalGenerator();
  1163. for (cmTargetDepend const& td : gg->GetTargetDirectDepends(this->GT)) {
  1164. dependencies.append(this->DumpDependency(td));
  1165. }
  1166. return dependencies;
  1167. }
  1168. Json::Value Target::DumpDependency(cmTargetDepend const& td)
  1169. {
  1170. Json::Value dependency = Json::objectValue;
  1171. dependency["id"] = TargetId(td, this->TopBuild);
  1172. this->AddBacktrace(dependency, td.GetBacktrace());
  1173. return dependency;
  1174. }
  1175. Json::Value Target::DumpFolder()
  1176. {
  1177. Json::Value folder;
  1178. if (const char* f = this->GT->GetProperty("FOLDER")) {
  1179. folder = Json::objectValue;
  1180. folder["name"] = f;
  1181. }
  1182. return folder;
  1183. }
  1184. }
  1185. Json::Value cmFileAPICodemodelDump(cmFileAPI& fileAPI, unsigned long version)
  1186. {
  1187. Codemodel codemodel(fileAPI, version);
  1188. return codemodel.Dump();
  1189. }