cmFileAPICodemodel.cxx 42 KB

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