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