cmFileAPICodemodel.cxx 67 KB

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