cmFileAPICodemodel.cxx 56 KB

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