cmFileAPICodemodel.cxx 69 KB

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