cmFileAPICodemodel.cxx 60 KB

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