cmFileAPICodemodel.cxx 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851
  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 <cm3p/json/value.h>
  18. #include "cmCryptoHash.h"
  19. #include "cmExportSet.h"
  20. #include "cmFileAPI.h"
  21. #include "cmGeneratorExpression.h"
  22. #include "cmGeneratorTarget.h"
  23. #include "cmGlobalGenerator.h"
  24. #include "cmInstallDirectoryGenerator.h"
  25. #include "cmInstallExportGenerator.h"
  26. #include "cmInstallFilesGenerator.h"
  27. #include "cmInstallGenerator.h"
  28. #include "cmInstallScriptGenerator.h"
  29. #include "cmInstallSubdirectoryGenerator.h"
  30. #include "cmInstallTargetGenerator.h"
  31. #include "cmLinkLineComputer.h"
  32. #include "cmListFileCache.h"
  33. #include "cmLocalGenerator.h"
  34. #include "cmMakefile.h"
  35. #include "cmProperty.h"
  36. #include "cmSourceFile.h"
  37. #include "cmSourceGroup.h"
  38. #include "cmState.h"
  39. #include "cmStateDirectory.h"
  40. #include "cmStateSnapshot.h"
  41. #include "cmStateTypes.h"
  42. #include "cmStringAlgorithms.h"
  43. #include "cmSystemTools.h"
  44. #include "cmTarget.h"
  45. #include "cmTargetDepend.h"
  46. #include "cmTargetExport.h"
  47. #include "cmake.h"
  48. namespace {
  49. using TargetIndexMapType =
  50. std::unordered_map<cmGeneratorTarget const*, Json::ArrayIndex>;
  51. std::string RelativeIfUnder(std::string const& top, std::string const& in)
  52. {
  53. std::string out;
  54. if (in == top) {
  55. out = ".";
  56. } else if (cmSystemTools::IsSubDirectory(in, top)) {
  57. out = in.substr(top.size() + 1);
  58. } else {
  59. out = in;
  60. }
  61. return out;
  62. }
  63. class JBTIndex
  64. {
  65. public:
  66. JBTIndex() = default;
  67. explicit operator bool() const { return this->Index != None; }
  68. Json::ArrayIndex Index = None;
  69. static Json::ArrayIndex const None = static_cast<Json::ArrayIndex>(-1);
  70. };
  71. template <typename T>
  72. class JBT
  73. {
  74. public:
  75. JBT(T v = T(), JBTIndex bt = JBTIndex())
  76. : Value(std::move(v))
  77. , Backtrace(bt)
  78. {
  79. }
  80. T Value;
  81. JBTIndex Backtrace;
  82. friend bool operator==(JBT<T> const& l, JBT<T> const& r)
  83. {
  84. return l.Value == r.Value && l.Backtrace.Index == r.Backtrace.Index;
  85. }
  86. static bool ValueEq(JBT<T> const& l, JBT<T> const& r)
  87. {
  88. return l.Value == r.Value;
  89. }
  90. static bool ValueLess(JBT<T> const& l, JBT<T> const& r)
  91. {
  92. return l.Value < r.Value;
  93. }
  94. };
  95. template <typename T>
  96. class JBTs
  97. {
  98. public:
  99. JBTs(T v = T(), std::vector<JBTIndex> ids = std::vector<JBTIndex>())
  100. : Value(std::move(v))
  101. , Backtraces(std::move(ids))
  102. {
  103. }
  104. T Value;
  105. std::vector<JBTIndex> Backtraces;
  106. friend bool operator==(JBTs<T> const& l, JBTs<T> const& r)
  107. {
  108. if ((l.Value == r.Value) && (l.Backtraces.size() == r.Backtraces.size())) {
  109. for (size_t i = 0; i < l.Backtraces.size(); i++) {
  110. if (l.Backtraces[i].Index != r.Backtraces[i].Index) {
  111. return false;
  112. }
  113. }
  114. }
  115. return true;
  116. }
  117. static bool ValueEq(JBTs<T> const& l, JBTs<T> const& r)
  118. {
  119. return l.Value == r.Value;
  120. }
  121. static bool ValueLess(JBTs<T> const& l, JBTs<T> const& r)
  122. {
  123. return l.Value < r.Value;
  124. }
  125. };
  126. class BacktraceData
  127. {
  128. std::string TopSource;
  129. std::unordered_map<std::string, Json::ArrayIndex> CommandMap;
  130. std::unordered_map<std::string, Json::ArrayIndex> FileMap;
  131. std::unordered_map<cmListFileContext const*, Json::ArrayIndex> NodeMap;
  132. Json::Value Commands = Json::arrayValue;
  133. Json::Value Files = Json::arrayValue;
  134. Json::Value Nodes = Json::arrayValue;
  135. Json::ArrayIndex AddCommand(std::string const& command)
  136. {
  137. auto i = this->CommandMap.find(command);
  138. if (i == this->CommandMap.end()) {
  139. auto cmdIndex = static_cast<Json::ArrayIndex>(this->Commands.size());
  140. i = this->CommandMap.emplace(command, cmdIndex).first;
  141. this->Commands.append(command);
  142. }
  143. return i->second;
  144. }
  145. Json::ArrayIndex AddFile(std::string const& file)
  146. {
  147. auto i = this->FileMap.find(file);
  148. if (i == this->FileMap.end()) {
  149. auto fileIndex = static_cast<Json::ArrayIndex>(this->Files.size());
  150. i = this->FileMap.emplace(file, fileIndex).first;
  151. this->Files.append(RelativeIfUnder(this->TopSource, file));
  152. }
  153. return i->second;
  154. }
  155. public:
  156. BacktraceData(std::string topSource);
  157. JBTIndex Add(cmListFileBacktrace const& bt);
  158. Json::Value Dump();
  159. };
  160. BacktraceData::BacktraceData(std::string topSource)
  161. : TopSource(std::move(topSource))
  162. {
  163. }
  164. JBTIndex BacktraceData::Add(cmListFileBacktrace const& bt)
  165. {
  166. JBTIndex index;
  167. if (bt.Empty()) {
  168. return index;
  169. }
  170. cmListFileContext const* top = &bt.Top();
  171. auto found = this->NodeMap.find(top);
  172. if (found != this->NodeMap.end()) {
  173. index.Index = found->second;
  174. return index;
  175. }
  176. Json::Value entry = Json::objectValue;
  177. entry["file"] = this->AddFile(top->FilePath);
  178. if (top->Line) {
  179. entry["line"] = static_cast<int>(top->Line);
  180. }
  181. if (!top->Name.empty()) {
  182. entry["command"] = this->AddCommand(top->Name);
  183. }
  184. if (JBTIndex parent = this->Add(bt.Pop())) {
  185. entry["parent"] = parent.Index;
  186. }
  187. index.Index = this->NodeMap[top] = this->Nodes.size();
  188. this->Nodes.append(std::move(entry)); // NOLINT(*)
  189. return index;
  190. }
  191. Json::Value BacktraceData::Dump()
  192. {
  193. Json::Value backtraceGraph;
  194. this->CommandMap.clear();
  195. this->FileMap.clear();
  196. this->NodeMap.clear();
  197. backtraceGraph["commands"] = std::move(this->Commands);
  198. backtraceGraph["files"] = std::move(this->Files);
  199. backtraceGraph["nodes"] = std::move(this->Nodes);
  200. return backtraceGraph;
  201. }
  202. class Codemodel
  203. {
  204. cmFileAPI& FileAPI;
  205. unsigned long Version;
  206. Json::Value DumpPaths();
  207. Json::Value DumpConfigurations();
  208. Json::Value DumpConfiguration(std::string const& config);
  209. public:
  210. Codemodel(cmFileAPI& fileAPI, unsigned long version);
  211. Json::Value Dump();
  212. };
  213. class CodemodelConfig
  214. {
  215. cmFileAPI& FileAPI;
  216. unsigned long Version;
  217. std::string const& Config;
  218. std::string TopSource;
  219. std::string TopBuild;
  220. struct Directory
  221. {
  222. cmStateSnapshot Snapshot;
  223. cmLocalGenerator const* LocalGenerator = nullptr;
  224. Json::Value TargetIndexes = Json::arrayValue;
  225. Json::ArrayIndex ProjectIndex;
  226. bool HasInstallRule = false;
  227. };
  228. std::map<cmStateSnapshot, Json::ArrayIndex, cmStateSnapshot::StrictWeakOrder>
  229. DirectoryMap;
  230. std::vector<Directory> Directories;
  231. struct Project
  232. {
  233. cmStateSnapshot Snapshot;
  234. static const Json::ArrayIndex NoParentIndex =
  235. static_cast<Json::ArrayIndex>(-1);
  236. Json::ArrayIndex ParentIndex = NoParentIndex;
  237. Json::Value ChildIndexes = Json::arrayValue;
  238. Json::Value DirectoryIndexes = Json::arrayValue;
  239. Json::Value TargetIndexes = Json::arrayValue;
  240. };
  241. std::map<cmStateSnapshot, Json::ArrayIndex, cmStateSnapshot::StrictWeakOrder>
  242. ProjectMap;
  243. std::vector<Project> Projects;
  244. TargetIndexMapType TargetIndexMap;
  245. void ProcessDirectories();
  246. Json::ArrayIndex GetDirectoryIndex(cmLocalGenerator const* lg);
  247. Json::ArrayIndex GetDirectoryIndex(cmStateSnapshot s);
  248. Json::ArrayIndex AddProject(cmStateSnapshot s);
  249. Json::Value DumpTargets();
  250. Json::Value DumpTarget(cmGeneratorTarget* gt, Json::ArrayIndex ti);
  251. Json::Value DumpDirectories();
  252. Json::Value DumpDirectory(Directory& d);
  253. Json::Value DumpDirectoryObject(Directory& d);
  254. Json::Value DumpProjects();
  255. Json::Value DumpProject(Project& p);
  256. Json::Value DumpMinimumCMakeVersion(cmStateSnapshot s);
  257. public:
  258. CodemodelConfig(cmFileAPI& fileAPI, unsigned long version,
  259. std::string const& config);
  260. Json::Value Dump();
  261. };
  262. std::string TargetId(cmGeneratorTarget const* gt, std::string const& topBuild)
  263. {
  264. cmCryptoHash hasher(cmCryptoHash::AlgoSHA3_256);
  265. std::string path = RelativeIfUnder(
  266. topBuild, gt->GetLocalGenerator()->GetCurrentBinaryDirectory());
  267. std::string hash = hasher.HashString(path);
  268. hash.resize(20, '0');
  269. return gt->GetName() + CMAKE_DIRECTORY_ID_SEP + hash;
  270. }
  271. struct CompileData
  272. {
  273. struct IncludeEntry
  274. {
  275. JBT<std::string> Path;
  276. bool IsSystem = false;
  277. IncludeEntry(JBT<std::string> path, bool isSystem)
  278. : Path(std::move(path))
  279. , IsSystem(isSystem)
  280. {
  281. }
  282. friend bool operator==(IncludeEntry const& l, IncludeEntry const& r)
  283. {
  284. return l.Path == r.Path && l.IsSystem == r.IsSystem;
  285. }
  286. };
  287. std::string Language;
  288. std::string Sysroot;
  289. JBTs<std::string> LanguageStandard;
  290. std::vector<JBT<std::string>> Flags;
  291. std::vector<JBT<std::string>> Defines;
  292. std::vector<JBT<std::string>> PrecompileHeaders;
  293. std::vector<IncludeEntry> Includes;
  294. friend bool operator==(CompileData const& l, CompileData const& r)
  295. {
  296. return (l.Language == r.Language && l.Sysroot == r.Sysroot &&
  297. l.Flags == r.Flags && l.Defines == r.Defines &&
  298. l.PrecompileHeaders == r.PrecompileHeaders &&
  299. l.LanguageStandard == r.LanguageStandard &&
  300. l.Includes == r.Includes);
  301. }
  302. };
  303. }
  304. namespace std {
  305. template <>
  306. struct hash<CompileData>
  307. {
  308. std::size_t operator()(CompileData const& in) const
  309. {
  310. using std::hash;
  311. size_t result =
  312. hash<std::string>()(in.Language) ^ hash<std::string>()(in.Sysroot);
  313. for (auto const& i : in.Includes) {
  314. result = result ^
  315. (hash<std::string>()(i.Path.Value) ^
  316. hash<Json::ArrayIndex>()(i.Path.Backtrace.Index) ^
  317. (i.IsSystem ? std::numeric_limits<size_t>::max() : 0));
  318. }
  319. for (auto const& i : in.Flags) {
  320. result = result ^ hash<std::string>()(i.Value) ^
  321. hash<Json::ArrayIndex>()(i.Backtrace.Index);
  322. }
  323. for (auto const& i : in.Defines) {
  324. result = result ^ hash<std::string>()(i.Value) ^
  325. hash<Json::ArrayIndex>()(i.Backtrace.Index);
  326. }
  327. for (auto const& i : in.PrecompileHeaders) {
  328. result = result ^ hash<std::string>()(i.Value) ^
  329. hash<Json::ArrayIndex>()(i.Backtrace.Index);
  330. }
  331. if (!in.LanguageStandard.Value.empty()) {
  332. result = result ^ hash<std::string>()(in.LanguageStandard.Value);
  333. for (JBTIndex backtrace : in.LanguageStandard.Backtraces) {
  334. result = result ^ hash<Json::ArrayIndex>()(backtrace.Index);
  335. }
  336. }
  337. return result;
  338. }
  339. };
  340. } // namespace std
  341. namespace {
  342. class DirectoryObject
  343. {
  344. cmLocalGenerator const* LG = nullptr;
  345. std::string const& Config;
  346. TargetIndexMapType& TargetIndexMap;
  347. std::string TopSource;
  348. std::string TopBuild;
  349. BacktraceData Backtraces;
  350. void AddBacktrace(Json::Value& object, cmListFileBacktrace const& bt);
  351. Json::Value DumpPaths();
  352. Json::Value DumpInstallers();
  353. Json::Value DumpInstaller(cmInstallGenerator* gen);
  354. Json::Value DumpInstallerExportTargets(cmExportSet* exp);
  355. Json::Value DumpInstallerPath(std::string const& top,
  356. std::string const& fromPathIn,
  357. std::string const& toPath);
  358. public:
  359. DirectoryObject(cmLocalGenerator const* lg, std::string const& config,
  360. TargetIndexMapType& targetIndexMap);
  361. Json::Value Dump();
  362. };
  363. class Target
  364. {
  365. cmGeneratorTarget* GT;
  366. std::string const& Config;
  367. std::string TopSource;
  368. std::string TopBuild;
  369. std::vector<cmSourceGroup> SourceGroupsLocal;
  370. BacktraceData Backtraces;
  371. std::map<std::string, CompileData> CompileDataMap;
  372. std::unordered_map<cmSourceFile const*, Json::ArrayIndex> SourceMap;
  373. Json::Value Sources = Json::arrayValue;
  374. struct SourceGroup
  375. {
  376. std::string Name;
  377. Json::Value SourceIndexes = Json::arrayValue;
  378. };
  379. std::unordered_map<cmSourceGroup const*, Json::ArrayIndex> SourceGroupsMap;
  380. std::vector<SourceGroup> SourceGroups;
  381. struct CompileGroup
  382. {
  383. std::unordered_map<CompileData, Json::ArrayIndex>::iterator Entry;
  384. Json::Value SourceIndexes = Json::arrayValue;
  385. };
  386. std::unordered_map<CompileData, Json::ArrayIndex> CompileGroupMap;
  387. std::vector<CompileGroup> CompileGroups;
  388. template <typename T>
  389. JBT<T> ToJBT(BT<T> const& bt)
  390. {
  391. return JBT<T>(bt.Value, this->Backtraces.Add(bt.Backtrace));
  392. }
  393. template <typename T>
  394. JBTs<T> ToJBTs(BTs<T> const& bts)
  395. {
  396. std::vector<JBTIndex> ids;
  397. for (cmListFileBacktrace const& backtrace : bts.Backtraces) {
  398. ids.emplace_back(this->Backtraces.Add(backtrace));
  399. }
  400. return JBTs<T>(bts.Value, ids);
  401. }
  402. void ProcessLanguages();
  403. void ProcessLanguage(std::string const& lang);
  404. Json::ArrayIndex AddSourceGroup(cmSourceGroup* sg, Json::ArrayIndex si);
  405. CompileData BuildCompileData(cmSourceFile* sf);
  406. CompileData MergeCompileData(CompileData const& fd);
  407. Json::ArrayIndex AddSourceCompileGroup(cmSourceFile* sf,
  408. Json::ArrayIndex si);
  409. void AddBacktrace(Json::Value& object, cmListFileBacktrace const& bt);
  410. void AddBacktrace(Json::Value& object, JBTIndex bt);
  411. Json::Value DumpPaths();
  412. Json::Value DumpCompileData(CompileData const& cd);
  413. Json::Value DumpInclude(CompileData::IncludeEntry const& inc);
  414. Json::Value DumpPrecompileHeader(JBT<std::string> const& header);
  415. Json::Value DumpLanguageStandard(JBTs<std::string> const& standard);
  416. Json::Value DumpDefine(JBT<std::string> const& def);
  417. Json::Value DumpSources();
  418. Json::Value DumpSource(cmGeneratorTarget::SourceAndKind const& sk,
  419. Json::ArrayIndex si);
  420. Json::Value DumpSourceGroups();
  421. Json::Value DumpSourceGroup(SourceGroup& sg);
  422. Json::Value DumpCompileGroups();
  423. Json::Value DumpCompileGroup(CompileGroup& cg);
  424. Json::Value DumpSysroot(std::string const& path);
  425. Json::Value DumpInstall();
  426. Json::Value DumpInstallPrefix();
  427. Json::Value DumpInstallDestinations();
  428. Json::Value DumpInstallDestination(cmInstallTargetGenerator* itGen);
  429. Json::Value DumpArtifacts();
  430. Json::Value DumpLink();
  431. Json::Value DumpArchive();
  432. Json::Value DumpLinkCommandFragments();
  433. Json::Value DumpCommandFragments(std::vector<JBT<std::string>> const& frags);
  434. Json::Value DumpCommandFragment(JBT<std::string> const& frag,
  435. std::string const& role = std::string());
  436. Json::Value DumpDependencies();
  437. Json::Value DumpDependency(cmTargetDepend const& td);
  438. Json::Value DumpFolder();
  439. public:
  440. Target(cmGeneratorTarget* gt, std::string const& config);
  441. Json::Value Dump();
  442. };
  443. Codemodel::Codemodel(cmFileAPI& fileAPI, unsigned long version)
  444. : FileAPI(fileAPI)
  445. , Version(version)
  446. {
  447. }
  448. Json::Value Codemodel::Dump()
  449. {
  450. Json::Value codemodel = Json::objectValue;
  451. codemodel["paths"] = this->DumpPaths();
  452. codemodel["configurations"] = this->DumpConfigurations();
  453. return codemodel;
  454. }
  455. Json::Value Codemodel::DumpPaths()
  456. {
  457. Json::Value paths = Json::objectValue;
  458. paths["source"] = this->FileAPI.GetCMakeInstance()->GetHomeDirectory();
  459. paths["build"] = this->FileAPI.GetCMakeInstance()->GetHomeOutputDirectory();
  460. return paths;
  461. }
  462. Json::Value Codemodel::DumpConfigurations()
  463. {
  464. Json::Value configurations = Json::arrayValue;
  465. cmGlobalGenerator* gg =
  466. this->FileAPI.GetCMakeInstance()->GetGlobalGenerator();
  467. const auto& makefiles = gg->GetMakefiles();
  468. if (!makefiles.empty()) {
  469. std::vector<std::string> const& configs =
  470. makefiles[0]->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  471. for (std::string const& config : configs) {
  472. configurations.append(this->DumpConfiguration(config));
  473. }
  474. }
  475. return configurations;
  476. }
  477. Json::Value Codemodel::DumpConfiguration(std::string const& config)
  478. {
  479. CodemodelConfig configuration(this->FileAPI, this->Version, config);
  480. return configuration.Dump();
  481. }
  482. CodemodelConfig::CodemodelConfig(cmFileAPI& fileAPI, unsigned long version,
  483. std::string const& config)
  484. : FileAPI(fileAPI)
  485. , Version(version)
  486. , Config(config)
  487. , TopSource(this->FileAPI.GetCMakeInstance()->GetHomeDirectory())
  488. , TopBuild(this->FileAPI.GetCMakeInstance()->GetHomeOutputDirectory())
  489. {
  490. static_cast<void>(this->Version);
  491. }
  492. Json::Value CodemodelConfig::Dump()
  493. {
  494. Json::Value configuration = Json::objectValue;
  495. configuration["name"] = this->Config;
  496. this->ProcessDirectories();
  497. configuration["targets"] = this->DumpTargets();
  498. configuration["directories"] = this->DumpDirectories();
  499. configuration["projects"] = this->DumpProjects();
  500. return configuration;
  501. }
  502. void CodemodelConfig::ProcessDirectories()
  503. {
  504. cmGlobalGenerator* gg =
  505. this->FileAPI.GetCMakeInstance()->GetGlobalGenerator();
  506. auto const& localGens = gg->GetLocalGenerators();
  507. // Add directories in forward order to process parents before children.
  508. this->Directories.reserve(localGens.size());
  509. for (const auto& lg : localGens) {
  510. auto directoryIndex =
  511. static_cast<Json::ArrayIndex>(this->Directories.size());
  512. this->Directories.emplace_back();
  513. Directory& d = this->Directories[directoryIndex];
  514. d.Snapshot = lg->GetStateSnapshot().GetBuildsystemDirectory();
  515. d.LocalGenerator = lg.get();
  516. this->DirectoryMap[d.Snapshot] = directoryIndex;
  517. d.ProjectIndex = this->AddProject(d.Snapshot);
  518. this->Projects[d.ProjectIndex].DirectoryIndexes.append(directoryIndex);
  519. }
  520. // Update directories in reverse order to process children before parents.
  521. for (auto di = this->Directories.rbegin(); di != this->Directories.rend();
  522. ++di) {
  523. Directory& d = *di;
  524. // Accumulate the presence of install rules on the way up.
  525. for (const auto& gen :
  526. d.LocalGenerator->GetMakefile()->GetInstallGenerators()) {
  527. if (!dynamic_cast<cmInstallSubdirectoryGenerator*>(gen.get())) {
  528. d.HasInstallRule = true;
  529. break;
  530. }
  531. }
  532. if (!d.HasInstallRule) {
  533. for (cmStateSnapshot const& child : d.Snapshot.GetChildren()) {
  534. cmStateSnapshot childDir = child.GetBuildsystemDirectory();
  535. Json::ArrayIndex const childIndex = this->GetDirectoryIndex(childDir);
  536. if (this->Directories[childIndex].HasInstallRule) {
  537. d.HasInstallRule = true;
  538. break;
  539. }
  540. }
  541. }
  542. }
  543. }
  544. Json::ArrayIndex CodemodelConfig::GetDirectoryIndex(cmLocalGenerator const* lg)
  545. {
  546. return this->GetDirectoryIndex(
  547. lg->GetStateSnapshot().GetBuildsystemDirectory());
  548. }
  549. Json::ArrayIndex CodemodelConfig::GetDirectoryIndex(cmStateSnapshot s)
  550. {
  551. auto i = this->DirectoryMap.find(s);
  552. assert(i != this->DirectoryMap.end());
  553. return i->second;
  554. }
  555. Json::ArrayIndex CodemodelConfig::AddProject(cmStateSnapshot s)
  556. {
  557. cmStateSnapshot ps = s.GetBuildsystemDirectoryParent();
  558. if (ps.IsValid() && ps.GetProjectName() == s.GetProjectName()) {
  559. // This directory is part of its parent directory project.
  560. Json::ArrayIndex const parentDirIndex = this->GetDirectoryIndex(ps);
  561. return this->Directories[parentDirIndex].ProjectIndex;
  562. }
  563. // This directory starts a new project.
  564. auto projectIndex = static_cast<Json::ArrayIndex>(this->Projects.size());
  565. this->Projects.emplace_back();
  566. Project& p = this->Projects[projectIndex];
  567. p.Snapshot = s;
  568. this->ProjectMap[s] = projectIndex;
  569. if (ps.IsValid()) {
  570. Json::ArrayIndex const parentDirIndex = this->GetDirectoryIndex(ps);
  571. p.ParentIndex = this->Directories[parentDirIndex].ProjectIndex;
  572. this->Projects[p.ParentIndex].ChildIndexes.append(projectIndex);
  573. }
  574. return projectIndex;
  575. }
  576. Json::Value CodemodelConfig::DumpTargets()
  577. {
  578. Json::Value targets = Json::arrayValue;
  579. std::vector<cmGeneratorTarget*> targetList;
  580. cmGlobalGenerator* gg =
  581. this->FileAPI.GetCMakeInstance()->GetGlobalGenerator();
  582. for (const auto& lg : gg->GetLocalGenerators()) {
  583. cm::append(targetList, lg->GetGeneratorTargets());
  584. }
  585. std::sort(targetList.begin(), targetList.end(),
  586. [](cmGeneratorTarget* l, cmGeneratorTarget* r) {
  587. return l->GetName() < r->GetName();
  588. });
  589. for (cmGeneratorTarget* gt : targetList) {
  590. if (gt->GetType() == cmStateEnums::GLOBAL_TARGET ||
  591. !gt->IsInBuildSystem()) {
  592. continue;
  593. }
  594. targets.append(this->DumpTarget(gt, targets.size()));
  595. }
  596. return targets;
  597. }
  598. Json::Value CodemodelConfig::DumpTarget(cmGeneratorTarget* gt,
  599. Json::ArrayIndex ti)
  600. {
  601. Target t(gt, this->Config);
  602. std::string prefix = "target-" + gt->GetName();
  603. for (char& c : prefix) {
  604. // CMP0037 OLD behavior allows slashes in target names. Remove them.
  605. if (c == '/' || c == '\\') {
  606. c = '_';
  607. }
  608. }
  609. if (!this->Config.empty()) {
  610. prefix += "-" + this->Config;
  611. }
  612. Json::Value target = this->FileAPI.MaybeJsonFile(t.Dump(), prefix);
  613. target["name"] = gt->GetName();
  614. target["id"] = TargetId(gt, this->TopBuild);
  615. // Cross-reference directory containing target.
  616. Json::ArrayIndex di = this->GetDirectoryIndex(gt->GetLocalGenerator());
  617. target["directoryIndex"] = di;
  618. this->Directories[di].TargetIndexes.append(ti);
  619. // Cross-reference project containing target.
  620. Json::ArrayIndex pi = this->Directories[di].ProjectIndex;
  621. target["projectIndex"] = pi;
  622. this->Projects[pi].TargetIndexes.append(ti);
  623. this->TargetIndexMap[gt] = ti;
  624. return target;
  625. }
  626. Json::Value CodemodelConfig::DumpDirectories()
  627. {
  628. Json::Value directories = Json::arrayValue;
  629. for (Directory& d : this->Directories) {
  630. directories.append(this->DumpDirectory(d));
  631. }
  632. return directories;
  633. }
  634. Json::Value CodemodelConfig::DumpDirectory(Directory& d)
  635. {
  636. Json::Value directory = this->DumpDirectoryObject(d);
  637. std::string sourceDir = d.Snapshot.GetDirectory().GetCurrentSource();
  638. directory["source"] = RelativeIfUnder(this->TopSource, sourceDir);
  639. std::string buildDir = d.Snapshot.GetDirectory().GetCurrentBinary();
  640. directory["build"] = RelativeIfUnder(this->TopBuild, buildDir);
  641. cmStateSnapshot parentDir = d.Snapshot.GetBuildsystemDirectoryParent();
  642. if (parentDir.IsValid()) {
  643. directory["parentIndex"] = this->GetDirectoryIndex(parentDir);
  644. }
  645. Json::Value childIndexes = Json::arrayValue;
  646. for (cmStateSnapshot const& child : d.Snapshot.GetChildren()) {
  647. childIndexes.append(
  648. this->GetDirectoryIndex(child.GetBuildsystemDirectory()));
  649. }
  650. if (!childIndexes.empty()) {
  651. directory["childIndexes"] = std::move(childIndexes);
  652. }
  653. directory["projectIndex"] = d.ProjectIndex;
  654. if (!d.TargetIndexes.empty()) {
  655. directory["targetIndexes"] = std::move(d.TargetIndexes);
  656. }
  657. Json::Value minimumCMakeVersion = this->DumpMinimumCMakeVersion(d.Snapshot);
  658. if (!minimumCMakeVersion.isNull()) {
  659. directory["minimumCMakeVersion"] = std::move(minimumCMakeVersion);
  660. }
  661. if (d.HasInstallRule) {
  662. directory["hasInstallRule"] = true;
  663. }
  664. return directory;
  665. }
  666. Json::Value CodemodelConfig::DumpDirectoryObject(Directory& d)
  667. {
  668. std::string prefix = "directory";
  669. std::string sourceDirRel = RelativeIfUnder(
  670. this->TopSource, d.Snapshot.GetDirectory().GetCurrentSource());
  671. std::string buildDirRel = RelativeIfUnder(
  672. this->TopBuild, d.Snapshot.GetDirectory().GetCurrentBinary());
  673. if (!cmSystemTools::FileIsFullPath(buildDirRel)) {
  674. prefix = cmStrCat(prefix, '-', buildDirRel);
  675. } else if (!cmSystemTools::FileIsFullPath(sourceDirRel)) {
  676. prefix = cmStrCat(prefix, '-', sourceDirRel);
  677. }
  678. for (char& c : prefix) {
  679. if (c == '/' || c == '\\') {
  680. c = '.';
  681. }
  682. }
  683. if (!this->Config.empty()) {
  684. prefix += "-" + this->Config;
  685. }
  686. DirectoryObject dir(d.LocalGenerator, this->Config, this->TargetIndexMap);
  687. return this->FileAPI.MaybeJsonFile(dir.Dump(), prefix);
  688. }
  689. Json::Value CodemodelConfig::DumpProjects()
  690. {
  691. Json::Value projects = Json::arrayValue;
  692. for (Project& p : this->Projects) {
  693. projects.append(this->DumpProject(p));
  694. }
  695. return projects;
  696. }
  697. Json::Value CodemodelConfig::DumpProject(Project& p)
  698. {
  699. Json::Value project = Json::objectValue;
  700. project["name"] = p.Snapshot.GetProjectName();
  701. if (p.ParentIndex != Project::NoParentIndex) {
  702. project["parentIndex"] = p.ParentIndex;
  703. }
  704. if (!p.ChildIndexes.empty()) {
  705. project["childIndexes"] = std::move(p.ChildIndexes);
  706. }
  707. project["directoryIndexes"] = std::move(p.DirectoryIndexes);
  708. if (!p.TargetIndexes.empty()) {
  709. project["targetIndexes"] = std::move(p.TargetIndexes);
  710. }
  711. return project;
  712. }
  713. Json::Value CodemodelConfig::DumpMinimumCMakeVersion(cmStateSnapshot s)
  714. {
  715. Json::Value minimumCMakeVersion;
  716. if (std::string const* def =
  717. s.GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION")) {
  718. minimumCMakeVersion = Json::objectValue;
  719. minimumCMakeVersion["string"] = *def;
  720. }
  721. return minimumCMakeVersion;
  722. }
  723. DirectoryObject::DirectoryObject(cmLocalGenerator const* lg,
  724. std::string const& config,
  725. TargetIndexMapType& targetIndexMap)
  726. : LG(lg)
  727. , Config(config)
  728. , TargetIndexMap(targetIndexMap)
  729. , TopSource(lg->GetGlobalGenerator()->GetCMakeInstance()->GetHomeDirectory())
  730. , TopBuild(
  731. lg->GetGlobalGenerator()->GetCMakeInstance()->GetHomeOutputDirectory())
  732. , Backtraces(this->TopSource)
  733. {
  734. }
  735. Json::Value DirectoryObject::Dump()
  736. {
  737. Json::Value directoryObject = Json::objectValue;
  738. directoryObject["paths"] = this->DumpPaths();
  739. directoryObject["installers"] = this->DumpInstallers();
  740. directoryObject["backtraceGraph"] = this->Backtraces.Dump();
  741. return directoryObject;
  742. }
  743. void DirectoryObject::AddBacktrace(Json::Value& object,
  744. cmListFileBacktrace const& bt)
  745. {
  746. if (JBTIndex backtrace = this->Backtraces.Add(bt)) {
  747. object["backtrace"] = backtrace.Index;
  748. }
  749. }
  750. Json::Value DirectoryObject::DumpPaths()
  751. {
  752. Json::Value paths = Json::objectValue;
  753. std::string const& sourceDir = this->LG->GetCurrentSourceDirectory();
  754. paths["source"] = RelativeIfUnder(this->TopSource, sourceDir);
  755. std::string const& buildDir = this->LG->GetCurrentBinaryDirectory();
  756. paths["build"] = RelativeIfUnder(this->TopBuild, buildDir);
  757. return paths;
  758. }
  759. Json::Value DirectoryObject::DumpInstallers()
  760. {
  761. Json::Value installers = Json::arrayValue;
  762. for (const auto& gen : this->LG->GetMakefile()->GetInstallGenerators()) {
  763. Json::Value installer = this->DumpInstaller(gen.get());
  764. if (!installer.empty()) {
  765. installers.append(std::move(installer)); // NOLINT(*)
  766. }
  767. }
  768. return installers;
  769. }
  770. Json::Value DirectoryObject::DumpInstaller(cmInstallGenerator* gen)
  771. {
  772. Json::Value installer = Json::objectValue;
  773. // Exclude subdirectory installers. They are implementation details.
  774. if (dynamic_cast<cmInstallSubdirectoryGenerator*>(gen)) {
  775. return installer;
  776. }
  777. // Exclude installers not used in this configuration.
  778. if (!gen->InstallsForConfig(this->Config)) {
  779. return installer;
  780. }
  781. // Add fields specific to each kind of install generator.
  782. if (auto* installTarget = dynamic_cast<cmInstallTargetGenerator*>(gen)) {
  783. cmInstallTargetGenerator::Files const& files =
  784. installTarget->GetFiles(this->Config);
  785. if (files.From.empty()) {
  786. return installer;
  787. }
  788. installer["type"] = "target";
  789. installer["destination"] = installTarget->GetDestination(this->Config);
  790. installer["targetId"] =
  791. TargetId(installTarget->GetTarget(), this->TopBuild);
  792. installer["targetIndex"] =
  793. this->TargetIndexMap[installTarget->GetTarget()];
  794. std::string fromDir = files.FromDir;
  795. if (!fromDir.empty()) {
  796. fromDir.push_back('/');
  797. }
  798. std::string toDir = files.ToDir;
  799. if (!toDir.empty()) {
  800. toDir.push_back('/');
  801. }
  802. Json::Value paths = Json::arrayValue;
  803. for (size_t i = 0; i < files.From.size(); ++i) {
  804. std::string const& fromPath = cmStrCat(fromDir, files.From[i]);
  805. std::string const& toPath = cmStrCat(toDir, files.To[i]);
  806. paths.append(this->DumpInstallerPath(this->TopBuild, fromPath, toPath));
  807. }
  808. installer["paths"] = std::move(paths);
  809. if (installTarget->GetOptional()) {
  810. installer["isOptional"] = true;
  811. }
  812. if (installTarget->IsImportLibrary()) {
  813. installer["targetIsImportLibrary"] = true;
  814. }
  815. switch (files.NamelinkMode) {
  816. case cmInstallTargetGenerator::NamelinkModeNone:
  817. break;
  818. case cmInstallTargetGenerator::NamelinkModeOnly:
  819. installer["targetInstallNamelink"] = "only";
  820. break;
  821. case cmInstallTargetGenerator::NamelinkModeSkip:
  822. installer["targetInstallNamelink"] = "skip";
  823. break;
  824. }
  825. // FIXME: Parse FilePermissions to provide structured information.
  826. // FIXME: Thread EXPORT name through from install() call.
  827. } else if (auto* installFiles =
  828. dynamic_cast<cmInstallFilesGenerator*>(gen)) {
  829. std::vector<std::string> const& files =
  830. installFiles->GetFiles(this->Config);
  831. if (files.empty()) {
  832. return installer;
  833. }
  834. installer["type"] = "file";
  835. installer["destination"] = installFiles->GetDestination(this->Config);
  836. Json::Value paths = Json::arrayValue;
  837. std::string const& rename = installFiles->GetRename(this->Config);
  838. if (!rename.empty() && files.size() == 1) {
  839. paths.append(this->DumpInstallerPath(this->TopSource, files[0], rename));
  840. } else {
  841. for (std::string const& file : installFiles->GetFiles(this->Config)) {
  842. paths.append(RelativeIfUnder(this->TopSource, file));
  843. }
  844. }
  845. installer["paths"] = std::move(paths);
  846. if (installFiles->GetOptional()) {
  847. installer["isOptional"] = true;
  848. }
  849. // FIXME: Parse FilePermissions to provide structured information.
  850. } else if (auto* installDir =
  851. dynamic_cast<cmInstallDirectoryGenerator*>(gen)) {
  852. std::vector<std::string> const& dirs =
  853. installDir->GetDirectories(this->Config);
  854. if (dirs.empty()) {
  855. return installer;
  856. }
  857. installer["type"] = "directory";
  858. installer["destination"] = installDir->GetDestination(this->Config);
  859. Json::Value paths = Json::arrayValue;
  860. for (std::string const& dir : dirs) {
  861. if (cmHasLiteralSuffix(dir, "/")) {
  862. paths.append(this->DumpInstallerPath(
  863. this->TopSource, dir.substr(0, dir.size() - 1), "."));
  864. } else {
  865. paths.append(this->DumpInstallerPath(
  866. this->TopSource, dir, cmSystemTools::GetFilenameName(dir)));
  867. }
  868. }
  869. installer["paths"] = std::move(paths);
  870. if (installDir->GetOptional()) {
  871. installer["isOptional"] = true;
  872. }
  873. // FIXME: Parse FilePermissions, DirPermissions, and LiteralArguments.
  874. // to provide structured information.
  875. } else if (auto* installExport =
  876. dynamic_cast<cmInstallExportGenerator*>(gen)) {
  877. installer["type"] = "export";
  878. installer["destination"] = installExport->GetDestination();
  879. cmExportSet* exportSet = installExport->GetExportSet();
  880. installer["exportName"] = exportSet->GetName();
  881. installer["exportTargets"] = this->DumpInstallerExportTargets(exportSet);
  882. Json::Value paths = Json::arrayValue;
  883. paths.append(
  884. RelativeIfUnder(this->TopBuild, installExport->GetMainImportFile()));
  885. installer["paths"] = std::move(paths);
  886. } else if (auto* installScript =
  887. dynamic_cast<cmInstallScriptGenerator*>(gen)) {
  888. if (installScript->IsCode()) {
  889. installer["type"] = "code";
  890. } else {
  891. installer["type"] = "script";
  892. installer["scriptFile"] = RelativeIfUnder(
  893. this->TopSource, installScript->GetScript(this->Config));
  894. }
  895. }
  896. // Add fields common to all install generators.
  897. installer["component"] = gen->GetComponent();
  898. if (gen->GetExcludeFromAll()) {
  899. installer["isExcludeFromAll"] = true;
  900. }
  901. this->AddBacktrace(installer, gen->GetBacktrace());
  902. return installer;
  903. }
  904. Json::Value DirectoryObject::DumpInstallerExportTargets(cmExportSet* exp)
  905. {
  906. Json::Value targets = Json::arrayValue;
  907. for (auto const& targetExport : exp->GetTargetExports()) {
  908. Json::Value target = Json::objectValue;
  909. target["id"] = TargetId(targetExport->Target, this->TopBuild);
  910. target["index"] = this->TargetIndexMap[targetExport->Target];
  911. targets.append(std::move(target)); // NOLINT(*)
  912. }
  913. return targets;
  914. }
  915. Json::Value DirectoryObject::DumpInstallerPath(std::string const& top,
  916. std::string const& fromPathIn,
  917. std::string const& toPath)
  918. {
  919. Json::Value installPath;
  920. std::string fromPath = RelativeIfUnder(top, fromPathIn);
  921. // If toPath is the last component of fromPath, use just fromPath.
  922. if (toPath.find_first_of('/') == std::string::npos &&
  923. cmHasSuffix(fromPath, toPath) &&
  924. (fromPath.size() == toPath.size() ||
  925. fromPath[fromPath.size() - toPath.size() - 1] == '/')) {
  926. installPath = fromPath;
  927. } else {
  928. installPath = Json::objectValue;
  929. installPath["from"] = fromPath;
  930. installPath["to"] = toPath;
  931. }
  932. return installPath;
  933. }
  934. Target::Target(cmGeneratorTarget* gt, std::string const& config)
  935. : GT(gt)
  936. , Config(config)
  937. , TopSource(gt->GetGlobalGenerator()->GetCMakeInstance()->GetHomeDirectory())
  938. , TopBuild(
  939. gt->GetGlobalGenerator()->GetCMakeInstance()->GetHomeOutputDirectory())
  940. , SourceGroupsLocal(this->GT->Makefile->GetSourceGroups())
  941. , Backtraces(this->TopSource)
  942. {
  943. }
  944. Json::Value Target::Dump()
  945. {
  946. Json::Value target = Json::objectValue;
  947. cmStateEnums::TargetType const type = this->GT->GetType();
  948. target["name"] = this->GT->GetName();
  949. target["type"] = cmState::GetTargetTypeName(type);
  950. target["id"] = TargetId(this->GT, this->TopBuild);
  951. target["paths"] = this->DumpPaths();
  952. if (this->GT->Target->GetIsGeneratorProvided()) {
  953. target["isGeneratorProvided"] = true;
  954. }
  955. this->AddBacktrace(target, this->GT->GetBacktrace());
  956. if (this->GT->Target->GetHaveInstallRule()) {
  957. target["install"] = this->DumpInstall();
  958. }
  959. if (this->GT->HaveWellDefinedOutputFiles()) {
  960. Json::Value artifacts = this->DumpArtifacts();
  961. if (!artifacts.empty()) {
  962. target["artifacts"] = std::move(artifacts);
  963. }
  964. }
  965. if (type == cmStateEnums::EXECUTABLE ||
  966. type == cmStateEnums::SHARED_LIBRARY ||
  967. type == cmStateEnums::MODULE_LIBRARY) {
  968. target["nameOnDisk"] = this->GT->GetFullName(this->Config);
  969. target["link"] = this->DumpLink();
  970. } else if (type == cmStateEnums::STATIC_LIBRARY) {
  971. target["nameOnDisk"] = this->GT->GetFullName(this->Config);
  972. target["archive"] = this->DumpArchive();
  973. }
  974. Json::Value dependencies = this->DumpDependencies();
  975. if (!dependencies.empty()) {
  976. target["dependencies"] = dependencies;
  977. }
  978. {
  979. this->ProcessLanguages();
  980. target["sources"] = this->DumpSources();
  981. Json::Value folder = this->DumpFolder();
  982. if (!folder.isNull()) {
  983. target["folder"] = std::move(folder);
  984. }
  985. Json::Value sourceGroups = this->DumpSourceGroups();
  986. if (!sourceGroups.empty()) {
  987. target["sourceGroups"] = std::move(sourceGroups);
  988. }
  989. Json::Value compileGroups = this->DumpCompileGroups();
  990. if (!compileGroups.empty()) {
  991. target["compileGroups"] = std::move(compileGroups);
  992. }
  993. }
  994. target["backtraceGraph"] = this->Backtraces.Dump();
  995. return target;
  996. }
  997. void Target::ProcessLanguages()
  998. {
  999. std::set<std::string> languages;
  1000. this->GT->GetLanguages(languages, this->Config);
  1001. for (std::string const& lang : languages) {
  1002. this->ProcessLanguage(lang);
  1003. }
  1004. }
  1005. void Target::ProcessLanguage(std::string const& lang)
  1006. {
  1007. CompileData& cd = this->CompileDataMap[lang];
  1008. cd.Language = lang;
  1009. if (cmProp sysrootCompile =
  1010. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT_COMPILE")) {
  1011. cd.Sysroot = *sysrootCompile;
  1012. } else if (cmProp sysroot =
  1013. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT")) {
  1014. cd.Sysroot = *sysroot;
  1015. }
  1016. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  1017. {
  1018. // FIXME: Add flags from end section of ExpandRuleVariable,
  1019. // which may need to be factored out.
  1020. std::vector<BT<std::string>> flags =
  1021. lg->GetTargetCompileFlags(this->GT, this->Config, lang);
  1022. cd.Flags.reserve(flags.size());
  1023. for (const BT<std::string>& f : flags) {
  1024. cd.Flags.emplace_back(this->ToJBT(f));
  1025. }
  1026. }
  1027. std::set<BT<std::string>> defines =
  1028. lg->GetTargetDefines(this->GT, this->Config, lang);
  1029. cd.Defines.reserve(defines.size());
  1030. for (BT<std::string> const& d : defines) {
  1031. cd.Defines.emplace_back(this->ToJBT(d));
  1032. }
  1033. std::vector<BT<std::string>> includePathList =
  1034. lg->GetIncludeDirectories(this->GT, lang, this->Config);
  1035. for (BT<std::string> const& i : includePathList) {
  1036. cd.Includes.emplace_back(
  1037. this->ToJBT(i),
  1038. this->GT->IsSystemIncludeDirectory(i.Value, this->Config, lang));
  1039. }
  1040. std::vector<BT<std::string>> precompileHeaders =
  1041. this->GT->GetPrecompileHeaders(this->Config, lang);
  1042. for (BT<std::string> const& pch : precompileHeaders) {
  1043. cd.PrecompileHeaders.emplace_back(this->ToJBT(pch));
  1044. }
  1045. BTs<std::string> const* languageStandard =
  1046. this->GT->GetLanguageStandardProperty(lang, this->Config);
  1047. if (languageStandard) {
  1048. cd.LanguageStandard = this->ToJBTs(*languageStandard);
  1049. }
  1050. }
  1051. Json::ArrayIndex Target::AddSourceGroup(cmSourceGroup* sg, Json::ArrayIndex si)
  1052. {
  1053. auto i = this->SourceGroupsMap.find(sg);
  1054. if (i == this->SourceGroupsMap.end()) {
  1055. auto sgIndex = static_cast<Json::ArrayIndex>(this->SourceGroups.size());
  1056. i = this->SourceGroupsMap.emplace(sg, sgIndex).first;
  1057. SourceGroup g;
  1058. g.Name = sg->GetFullName();
  1059. this->SourceGroups.push_back(std::move(g));
  1060. }
  1061. this->SourceGroups[i->second].SourceIndexes.append(si);
  1062. return i->second;
  1063. }
  1064. CompileData Target::BuildCompileData(cmSourceFile* sf)
  1065. {
  1066. CompileData fd;
  1067. fd.Language = sf->GetOrDetermineLanguage();
  1068. if (fd.Language.empty()) {
  1069. return fd;
  1070. }
  1071. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  1072. cmGeneratorExpressionInterpreter genexInterpreter(lg, this->Config, this->GT,
  1073. fd.Language);
  1074. const std::string COMPILE_FLAGS("COMPILE_FLAGS");
  1075. if (cmProp cflags = sf->GetProperty(COMPILE_FLAGS)) {
  1076. std::string flags = genexInterpreter.Evaluate(*cflags, COMPILE_FLAGS);
  1077. fd.Flags.emplace_back(std::move(flags), JBTIndex());
  1078. }
  1079. const std::string COMPILE_OPTIONS("COMPILE_OPTIONS");
  1080. for (BT<std::string> tmpOpt : sf->GetCompileOptions()) {
  1081. tmpOpt.Value = genexInterpreter.Evaluate(tmpOpt.Value, COMPILE_OPTIONS);
  1082. // After generator evaluation we need to use the AppendCompileOptions
  1083. // method so we handle situations where backtrace entries have lists
  1084. // and properly escape flags.
  1085. std::string tmp;
  1086. lg->AppendCompileOptions(tmp, tmpOpt.Value);
  1087. BT<std::string> opt(tmp, tmpOpt.Backtrace);
  1088. fd.Flags.emplace_back(this->ToJBT(opt));
  1089. }
  1090. // Add precompile headers compile options.
  1091. std::vector<std::string> architectures;
  1092. this->GT->GetAppleArchs(this->Config, architectures);
  1093. if (architectures.empty()) {
  1094. architectures.emplace_back();
  1095. }
  1096. std::unordered_map<std::string, std::string> pchSources;
  1097. for (const std::string& arch : architectures) {
  1098. const std::string pchSource =
  1099. this->GT->GetPchSource(this->Config, fd.Language, arch);
  1100. if (!pchSource.empty()) {
  1101. pchSources.insert(std::make_pair(pchSource, arch));
  1102. }
  1103. }
  1104. if (!pchSources.empty() && !sf->GetProperty("SKIP_PRECOMPILE_HEADERS")) {
  1105. std::string pchOptions;
  1106. auto pchIt = pchSources.find(sf->ResolveFullPath());
  1107. if (pchIt != pchSources.end()) {
  1108. pchOptions = this->GT->GetPchCreateCompileOptions(
  1109. this->Config, fd.Language, pchIt->second);
  1110. } else {
  1111. pchOptions =
  1112. this->GT->GetPchUseCompileOptions(this->Config, fd.Language);
  1113. }
  1114. BT<std::string> tmpOpt(pchOptions);
  1115. tmpOpt.Value = genexInterpreter.Evaluate(tmpOpt.Value, COMPILE_OPTIONS);
  1116. // After generator evaluation we need to use the AppendCompileOptions
  1117. // method so we handle situations where backtrace entries have lists
  1118. // and properly escape flags.
  1119. std::string tmp;
  1120. lg->AppendCompileOptions(tmp, tmpOpt.Value);
  1121. BT<std::string> opt(tmp, tmpOpt.Backtrace);
  1122. fd.Flags.emplace_back(this->ToJBT(opt));
  1123. }
  1124. // Add include directories from source file properties.
  1125. {
  1126. const std::string INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
  1127. for (BT<std::string> tmpInclude : sf->GetIncludeDirectories()) {
  1128. tmpInclude.Value =
  1129. genexInterpreter.Evaluate(tmpInclude.Value, INCLUDE_DIRECTORIES);
  1130. // After generator evaluation we need to use the AppendIncludeDirectories
  1131. // method so we handle situations where backtrace entries have lists.
  1132. std::vector<std::string> tmp;
  1133. lg->AppendIncludeDirectories(tmp, tmpInclude.Value, *sf);
  1134. for (std::string& i : tmp) {
  1135. bool const isSystemInclude =
  1136. this->GT->IsSystemIncludeDirectory(i, this->Config, fd.Language);
  1137. BT<std::string> include(i, tmpInclude.Backtrace);
  1138. fd.Includes.emplace_back(this->ToJBT(include), isSystemInclude);
  1139. }
  1140. }
  1141. }
  1142. const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
  1143. std::set<BT<std::string>> fileDefines;
  1144. for (BT<std::string> tmpDef : sf->GetCompileDefinitions()) {
  1145. tmpDef.Value =
  1146. genexInterpreter.Evaluate(tmpDef.Value, COMPILE_DEFINITIONS);
  1147. // After generator evaluation we need to use the AppendDefines method
  1148. // so we handle situations where backtrace entries have lists.
  1149. std::set<std::string> tmp;
  1150. lg->AppendDefines(tmp, tmpDef.Value);
  1151. for (const std::string& i : tmp) {
  1152. BT<std::string> def(i, tmpDef.Backtrace);
  1153. fileDefines.insert(def);
  1154. }
  1155. }
  1156. std::set<std::string> configFileDefines;
  1157. const std::string defPropName =
  1158. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(this->Config);
  1159. if (cmProp config_defs = sf->GetProperty(defPropName)) {
  1160. lg->AppendDefines(
  1161. configFileDefines,
  1162. genexInterpreter.Evaluate(*config_defs, COMPILE_DEFINITIONS));
  1163. }
  1164. fd.Defines.reserve(fileDefines.size() + configFileDefines.size());
  1165. for (BT<std::string> const& def : fileDefines) {
  1166. fd.Defines.emplace_back(this->ToJBT(def));
  1167. }
  1168. for (std::string const& d : configFileDefines) {
  1169. fd.Defines.emplace_back(d, JBTIndex());
  1170. }
  1171. return fd;
  1172. }
  1173. CompileData Target::MergeCompileData(CompileData const& fd)
  1174. {
  1175. CompileData cd;
  1176. cd.Language = fd.Language;
  1177. if (cd.Language.empty()) {
  1178. return cd;
  1179. }
  1180. CompileData const& td = this->CompileDataMap.at(cd.Language);
  1181. // All compile groups share the sysroot of the target.
  1182. cd.Sysroot = td.Sysroot;
  1183. // All compile groups share the precompile headers of the target.
  1184. cd.PrecompileHeaders = td.PrecompileHeaders;
  1185. // All compile groups share the language standard of the target.
  1186. cd.LanguageStandard = td.LanguageStandard;
  1187. // Use target-wide flags followed by source-specific flags.
  1188. cd.Flags.reserve(td.Flags.size() + fd.Flags.size());
  1189. cd.Flags.insert(cd.Flags.end(), td.Flags.begin(), td.Flags.end());
  1190. cd.Flags.insert(cd.Flags.end(), fd.Flags.begin(), fd.Flags.end());
  1191. // Use source-specific includes followed by target-wide includes.
  1192. cd.Includes.reserve(fd.Includes.size() + td.Includes.size());
  1193. cd.Includes.insert(cd.Includes.end(), fd.Includes.begin(),
  1194. fd.Includes.end());
  1195. cd.Includes.insert(cd.Includes.end(), td.Includes.begin(),
  1196. td.Includes.end());
  1197. // Use target-wide defines followed by source-specific defines.
  1198. cd.Defines.reserve(td.Defines.size() + fd.Defines.size());
  1199. cd.Defines.insert(cd.Defines.end(), td.Defines.begin(), td.Defines.end());
  1200. cd.Defines.insert(cd.Defines.end(), fd.Defines.begin(), fd.Defines.end());
  1201. // De-duplicate defines.
  1202. std::stable_sort(cd.Defines.begin(), cd.Defines.end(),
  1203. JBT<std::string>::ValueLess);
  1204. auto end = std::unique(cd.Defines.begin(), cd.Defines.end(),
  1205. JBT<std::string>::ValueEq);
  1206. cd.Defines.erase(end, cd.Defines.end());
  1207. return cd;
  1208. }
  1209. Json::ArrayIndex Target::AddSourceCompileGroup(cmSourceFile* sf,
  1210. Json::ArrayIndex si)
  1211. {
  1212. CompileData compileData = this->BuildCompileData(sf);
  1213. auto i = this->CompileGroupMap.find(compileData);
  1214. if (i == this->CompileGroupMap.end()) {
  1215. Json::ArrayIndex cgIndex =
  1216. static_cast<Json::ArrayIndex>(this->CompileGroups.size());
  1217. i = this->CompileGroupMap.emplace(std::move(compileData), cgIndex).first;
  1218. CompileGroup g;
  1219. g.Entry = i;
  1220. this->CompileGroups.push_back(std::move(g));
  1221. }
  1222. this->CompileGroups[i->second].SourceIndexes.append(si);
  1223. return i->second;
  1224. }
  1225. void Target::AddBacktrace(Json::Value& object, cmListFileBacktrace const& bt)
  1226. {
  1227. if (JBTIndex backtrace = this->Backtraces.Add(bt)) {
  1228. object["backtrace"] = backtrace.Index;
  1229. }
  1230. }
  1231. void Target::AddBacktrace(Json::Value& object, JBTIndex bt)
  1232. {
  1233. if (bt) {
  1234. object["backtrace"] = bt.Index;
  1235. }
  1236. }
  1237. Json::Value Target::DumpPaths()
  1238. {
  1239. Json::Value paths = Json::objectValue;
  1240. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  1241. std::string const& sourceDir = lg->GetCurrentSourceDirectory();
  1242. paths["source"] = RelativeIfUnder(this->TopSource, sourceDir);
  1243. std::string const& buildDir = lg->GetCurrentBinaryDirectory();
  1244. paths["build"] = RelativeIfUnder(this->TopBuild, buildDir);
  1245. return paths;
  1246. }
  1247. Json::Value Target::DumpSources()
  1248. {
  1249. Json::Value sources = Json::arrayValue;
  1250. cmGeneratorTarget::KindedSources const& kinded =
  1251. this->GT->GetKindedSources(this->Config);
  1252. for (cmGeneratorTarget::SourceAndKind const& sk : kinded.Sources) {
  1253. sources.append(this->DumpSource(sk, sources.size()));
  1254. }
  1255. return sources;
  1256. }
  1257. Json::Value Target::DumpSource(cmGeneratorTarget::SourceAndKind const& sk,
  1258. Json::ArrayIndex si)
  1259. {
  1260. Json::Value source = Json::objectValue;
  1261. std::string const path = sk.Source.Value->ResolveFullPath();
  1262. source["path"] = RelativeIfUnder(this->TopSource, path);
  1263. if (sk.Source.Value->GetIsGenerated()) {
  1264. source["isGenerated"] = true;
  1265. }
  1266. this->AddBacktrace(source, sk.Source.Backtrace);
  1267. if (cmSourceGroup* sg =
  1268. this->GT->Makefile->FindSourceGroup(path, this->SourceGroupsLocal)) {
  1269. source["sourceGroupIndex"] = this->AddSourceGroup(sg, si);
  1270. }
  1271. switch (sk.Kind) {
  1272. case cmGeneratorTarget::SourceKindObjectSource: {
  1273. source["compileGroupIndex"] =
  1274. this->AddSourceCompileGroup(sk.Source.Value, si);
  1275. } break;
  1276. case cmGeneratorTarget::SourceKindAppManifest:
  1277. case cmGeneratorTarget::SourceKindCertificate:
  1278. case cmGeneratorTarget::SourceKindCustomCommand:
  1279. case cmGeneratorTarget::SourceKindExternalObject:
  1280. case cmGeneratorTarget::SourceKindExtra:
  1281. case cmGeneratorTarget::SourceKindHeader:
  1282. case cmGeneratorTarget::SourceKindIDL:
  1283. case cmGeneratorTarget::SourceKindManifest:
  1284. case cmGeneratorTarget::SourceKindModuleDefinition:
  1285. case cmGeneratorTarget::SourceKindResx:
  1286. case cmGeneratorTarget::SourceKindXaml:
  1287. case cmGeneratorTarget::SourceKindUnityBatched:
  1288. break;
  1289. }
  1290. return source;
  1291. }
  1292. Json::Value Target::DumpCompileData(CompileData const& cd)
  1293. {
  1294. Json::Value result = Json::objectValue;
  1295. if (!cd.Language.empty()) {
  1296. result["language"] = cd.Language;
  1297. }
  1298. if (!cd.Sysroot.empty()) {
  1299. result["sysroot"] = this->DumpSysroot(cd.Sysroot);
  1300. }
  1301. if (!cd.Flags.empty()) {
  1302. result["compileCommandFragments"] = this->DumpCommandFragments(cd.Flags);
  1303. }
  1304. if (!cd.Includes.empty()) {
  1305. Json::Value includes = Json::arrayValue;
  1306. for (auto const& i : cd.Includes) {
  1307. includes.append(this->DumpInclude(i));
  1308. }
  1309. result["includes"] = includes;
  1310. }
  1311. if (!cd.Defines.empty()) {
  1312. Json::Value defines = Json::arrayValue;
  1313. for (JBT<std::string> const& d : cd.Defines) {
  1314. defines.append(this->DumpDefine(d));
  1315. }
  1316. result["defines"] = std::move(defines);
  1317. }
  1318. if (!cd.PrecompileHeaders.empty()) {
  1319. Json::Value precompileHeaders = Json::arrayValue;
  1320. for (JBT<std::string> const& pch : cd.PrecompileHeaders) {
  1321. precompileHeaders.append(this->DumpPrecompileHeader(pch));
  1322. }
  1323. result["precompileHeaders"] = std::move(precompileHeaders);
  1324. }
  1325. if (!cd.LanguageStandard.Value.empty()) {
  1326. result["languageStandard"] =
  1327. this->DumpLanguageStandard(cd.LanguageStandard);
  1328. }
  1329. return result;
  1330. }
  1331. Json::Value Target::DumpInclude(CompileData::IncludeEntry const& inc)
  1332. {
  1333. Json::Value include = Json::objectValue;
  1334. include["path"] = inc.Path.Value;
  1335. if (inc.IsSystem) {
  1336. include["isSystem"] = true;
  1337. }
  1338. this->AddBacktrace(include, inc.Path.Backtrace);
  1339. return include;
  1340. }
  1341. Json::Value Target::DumpPrecompileHeader(JBT<std::string> const& header)
  1342. {
  1343. Json::Value precompileHeader = Json::objectValue;
  1344. precompileHeader["header"] = header.Value;
  1345. this->AddBacktrace(precompileHeader, header.Backtrace);
  1346. return precompileHeader;
  1347. }
  1348. Json::Value Target::DumpLanguageStandard(JBTs<std::string> const& standard)
  1349. {
  1350. Json::Value languageStandard = Json::objectValue;
  1351. languageStandard["standard"] = standard.Value;
  1352. if (!standard.Backtraces.empty()) {
  1353. Json::Value backtraces = Json::arrayValue;
  1354. for (JBTIndex backtrace : standard.Backtraces) {
  1355. backtraces.append(backtrace.Index);
  1356. }
  1357. languageStandard["backtraces"] = backtraces;
  1358. }
  1359. return languageStandard;
  1360. }
  1361. Json::Value Target::DumpDefine(JBT<std::string> const& def)
  1362. {
  1363. Json::Value define = Json::objectValue;
  1364. define["define"] = def.Value;
  1365. this->AddBacktrace(define, def.Backtrace);
  1366. return define;
  1367. }
  1368. Json::Value Target::DumpSourceGroups()
  1369. {
  1370. Json::Value sourceGroups = Json::arrayValue;
  1371. for (auto& sg : this->SourceGroups) {
  1372. sourceGroups.append(this->DumpSourceGroup(sg));
  1373. }
  1374. return sourceGroups;
  1375. }
  1376. Json::Value Target::DumpSourceGroup(SourceGroup& sg)
  1377. {
  1378. Json::Value group = Json::objectValue;
  1379. group["name"] = sg.Name;
  1380. group["sourceIndexes"] = std::move(sg.SourceIndexes);
  1381. return group;
  1382. }
  1383. Json::Value Target::DumpCompileGroups()
  1384. {
  1385. Json::Value compileGroups = Json::arrayValue;
  1386. for (auto& cg : this->CompileGroups) {
  1387. compileGroups.append(this->DumpCompileGroup(cg));
  1388. }
  1389. return compileGroups;
  1390. }
  1391. Json::Value Target::DumpCompileGroup(CompileGroup& cg)
  1392. {
  1393. Json::Value group =
  1394. this->DumpCompileData(this->MergeCompileData(cg.Entry->first));
  1395. group["sourceIndexes"] = std::move(cg.SourceIndexes);
  1396. return group;
  1397. }
  1398. Json::Value Target::DumpSysroot(std::string const& path)
  1399. {
  1400. Json::Value sysroot = Json::objectValue;
  1401. sysroot["path"] = path;
  1402. return sysroot;
  1403. }
  1404. Json::Value Target::DumpInstall()
  1405. {
  1406. Json::Value install = Json::objectValue;
  1407. install["prefix"] = this->DumpInstallPrefix();
  1408. install["destinations"] = this->DumpInstallDestinations();
  1409. return install;
  1410. }
  1411. Json::Value Target::DumpInstallPrefix()
  1412. {
  1413. Json::Value prefix = Json::objectValue;
  1414. std::string p =
  1415. this->GT->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  1416. cmSystemTools::ConvertToUnixSlashes(p);
  1417. prefix["path"] = p;
  1418. return prefix;
  1419. }
  1420. Json::Value Target::DumpInstallDestinations()
  1421. {
  1422. Json::Value destinations = Json::arrayValue;
  1423. auto installGens = this->GT->Target->GetInstallGenerators();
  1424. for (auto* itGen : installGens) {
  1425. destinations.append(this->DumpInstallDestination(itGen));
  1426. }
  1427. return destinations;
  1428. }
  1429. Json::Value Target::DumpInstallDestination(cmInstallTargetGenerator* itGen)
  1430. {
  1431. Json::Value destination = Json::objectValue;
  1432. destination["path"] = itGen->GetDestination(this->Config);
  1433. this->AddBacktrace(destination, itGen->GetBacktrace());
  1434. return destination;
  1435. }
  1436. Json::Value Target::DumpArtifacts()
  1437. {
  1438. Json::Value artifacts = Json::arrayValue;
  1439. // Object libraries have only object files as artifacts.
  1440. if (this->GT->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1441. if (!this->GT->GetGlobalGenerator()->HasKnownObjectFileLocation(nullptr)) {
  1442. return artifacts;
  1443. }
  1444. std::vector<cmSourceFile const*> objectSources;
  1445. this->GT->GetObjectSources(objectSources, this->Config);
  1446. std::string const obj_dir = this->GT->GetObjectDirectory(this->Config);
  1447. for (cmSourceFile const* sf : objectSources) {
  1448. const std::string& obj = this->GT->GetObjectName(sf);
  1449. Json::Value artifact = Json::objectValue;
  1450. artifact["path"] = RelativeIfUnder(this->TopBuild, obj_dir + obj);
  1451. artifacts.append(std::move(artifact)); // NOLINT(*)
  1452. }
  1453. return artifacts;
  1454. }
  1455. // Other target types always have a "main" artifact.
  1456. {
  1457. Json::Value artifact = Json::objectValue;
  1458. artifact["path"] =
  1459. RelativeIfUnder(this->TopBuild,
  1460. this->GT->GetFullPath(
  1461. this->Config, cmStateEnums::RuntimeBinaryArtifact));
  1462. artifacts.append(std::move(artifact)); // NOLINT(*)
  1463. }
  1464. // Add Windows-specific artifacts produced by the linker.
  1465. if (this->GT->HasImportLibrary(this->Config)) {
  1466. Json::Value artifact = Json::objectValue;
  1467. artifact["path"] =
  1468. RelativeIfUnder(this->TopBuild,
  1469. this->GT->GetFullPath(
  1470. this->Config, cmStateEnums::ImportLibraryArtifact));
  1471. artifacts.append(std::move(artifact)); // NOLINT(*)
  1472. }
  1473. if (this->GT->IsDLLPlatform() &&
  1474. this->GT->GetType() != cmStateEnums::STATIC_LIBRARY) {
  1475. cmGeneratorTarget::OutputInfo const* output =
  1476. this->GT->GetOutputInfo(this->Config);
  1477. if (output && !output->PdbDir.empty()) {
  1478. Json::Value artifact = Json::objectValue;
  1479. artifact["path"] = RelativeIfUnder(this->TopBuild,
  1480. output->PdbDir + '/' +
  1481. this->GT->GetPDBName(this->Config));
  1482. artifacts.append(std::move(artifact)); // NOLINT(*)
  1483. }
  1484. }
  1485. return artifacts;
  1486. }
  1487. Json::Value Target::DumpLink()
  1488. {
  1489. Json::Value link = Json::objectValue;
  1490. std::string lang = this->GT->GetLinkerLanguage(this->Config);
  1491. link["language"] = lang;
  1492. {
  1493. Json::Value commandFragments = this->DumpLinkCommandFragments();
  1494. if (!commandFragments.empty()) {
  1495. link["commandFragments"] = std::move(commandFragments);
  1496. }
  1497. }
  1498. if (cmProp sysrootLink =
  1499. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT_LINK")) {
  1500. link["sysroot"] = this->DumpSysroot(*sysrootLink);
  1501. } else if (cmProp sysroot =
  1502. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT")) {
  1503. link["sysroot"] = this->DumpSysroot(*sysroot);
  1504. }
  1505. if (this->GT->IsIPOEnabled(lang, this->Config)) {
  1506. link["lto"] = true;
  1507. }
  1508. return link;
  1509. }
  1510. Json::Value Target::DumpArchive()
  1511. {
  1512. Json::Value archive = Json::objectValue;
  1513. {
  1514. // The "link" fragments not relevant to static libraries are empty.
  1515. Json::Value commandFragments = this->DumpLinkCommandFragments();
  1516. if (!commandFragments.empty()) {
  1517. archive["commandFragments"] = std::move(commandFragments);
  1518. }
  1519. }
  1520. std::string lang = this->GT->GetLinkerLanguage(this->Config);
  1521. if (this->GT->IsIPOEnabled(lang, this->Config)) {
  1522. archive["lto"] = true;
  1523. }
  1524. return archive;
  1525. }
  1526. Json::Value Target::DumpLinkCommandFragments()
  1527. {
  1528. Json::Value linkFragments = Json::arrayValue;
  1529. std::string linkLanguageFlags;
  1530. std::vector<BT<std::string>> linkFlags;
  1531. std::string frameworkPath;
  1532. std::vector<BT<std::string>> linkPath;
  1533. std::vector<BT<std::string>> linkLibs;
  1534. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  1535. cmLinkLineComputer linkLineComputer(lg,
  1536. lg->GetStateSnapshot().GetDirectory());
  1537. lg->GetTargetFlags(&linkLineComputer, this->Config, linkLibs,
  1538. linkLanguageFlags, linkFlags, frameworkPath, linkPath,
  1539. this->GT);
  1540. linkLanguageFlags = cmTrimWhitespace(linkLanguageFlags);
  1541. frameworkPath = cmTrimWhitespace(frameworkPath);
  1542. if (!linkLanguageFlags.empty()) {
  1543. linkFragments.append(
  1544. this->DumpCommandFragment(std::move(linkLanguageFlags), "flags"));
  1545. }
  1546. if (!linkFlags.empty()) {
  1547. for (BT<std::string> frag : linkFlags) {
  1548. frag.Value = cmTrimWhitespace(frag.Value);
  1549. linkFragments.append(
  1550. this->DumpCommandFragment(this->ToJBT(frag), "flags"));
  1551. }
  1552. }
  1553. if (!frameworkPath.empty()) {
  1554. linkFragments.append(
  1555. this->DumpCommandFragment(std::move(frameworkPath), "frameworkPath"));
  1556. }
  1557. if (!linkPath.empty()) {
  1558. for (BT<std::string> frag : linkPath) {
  1559. frag.Value = cmTrimWhitespace(frag.Value);
  1560. linkFragments.append(
  1561. this->DumpCommandFragment(this->ToJBT(frag), "libraryPath"));
  1562. }
  1563. }
  1564. if (!linkLibs.empty()) {
  1565. for (BT<std::string> frag : linkLibs) {
  1566. frag.Value = cmTrimWhitespace(frag.Value);
  1567. linkFragments.append(
  1568. this->DumpCommandFragment(this->ToJBT(frag), "libraries"));
  1569. }
  1570. }
  1571. return linkFragments;
  1572. }
  1573. Json::Value Target::DumpCommandFragments(
  1574. std::vector<JBT<std::string>> const& frags)
  1575. {
  1576. Json::Value commandFragments = Json::arrayValue;
  1577. for (JBT<std::string> const& f : frags) {
  1578. commandFragments.append(this->DumpCommandFragment(f));
  1579. }
  1580. return commandFragments;
  1581. }
  1582. Json::Value Target::DumpCommandFragment(JBT<std::string> const& frag,
  1583. std::string const& role)
  1584. {
  1585. Json::Value fragment = Json::objectValue;
  1586. fragment["fragment"] = frag.Value;
  1587. if (!role.empty()) {
  1588. fragment["role"] = role;
  1589. }
  1590. this->AddBacktrace(fragment, frag.Backtrace);
  1591. return fragment;
  1592. }
  1593. Json::Value Target::DumpDependencies()
  1594. {
  1595. Json::Value dependencies = Json::arrayValue;
  1596. cmGlobalGenerator* gg = this->GT->GetGlobalGenerator();
  1597. for (cmTargetDepend const& td : gg->GetTargetDirectDepends(this->GT)) {
  1598. dependencies.append(this->DumpDependency(td));
  1599. }
  1600. return dependencies;
  1601. }
  1602. Json::Value Target::DumpDependency(cmTargetDepend const& td)
  1603. {
  1604. Json::Value dependency = Json::objectValue;
  1605. dependency["id"] = TargetId(td, this->TopBuild);
  1606. this->AddBacktrace(dependency, td.GetBacktrace());
  1607. return dependency;
  1608. }
  1609. Json::Value Target::DumpFolder()
  1610. {
  1611. Json::Value folder;
  1612. if (cmProp f = this->GT->GetProperty("FOLDER")) {
  1613. folder = Json::objectValue;
  1614. folder["name"] = *f;
  1615. }
  1616. return folder;
  1617. }
  1618. }
  1619. Json::Value cmFileAPICodemodelDump(cmFileAPI& fileAPI, unsigned long version)
  1620. {
  1621. Codemodel codemodel(fileAPI, version);
  1622. return codemodel.Dump();
  1623. }