cmFileAPICodemodel.cxx 67 KB

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