cmFileAPICodemodel.cxx 64 KB

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