cmFileAPICodemodel.cxx 65 KB

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