cmFileAPICodemodel.cxx 70 KB

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