cmFileAPICodemodel.cxx 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132
  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. targets.append(this->DumpTarget(gt, targets.size()));
  613. }
  614. return targets;
  615. }
  616. Json::Value CodemodelConfig::DumpTarget(cmGeneratorTarget* gt,
  617. Json::ArrayIndex ti)
  618. {
  619. Target t(gt, this->Config);
  620. std::string prefix = "target-" + gt->GetName();
  621. for (char& c : prefix) {
  622. // CMP0037 OLD behavior allows slashes in target names. Remove them.
  623. if (c == '/' || c == '\\') {
  624. c = '_';
  625. }
  626. }
  627. if (!this->Config.empty()) {
  628. prefix += "-" + this->Config;
  629. }
  630. Json::Value target = this->FileAPI.MaybeJsonFile(t.Dump(), prefix);
  631. target["name"] = gt->GetName();
  632. target["id"] = TargetId(gt, this->TopBuild);
  633. // Cross-reference directory containing target.
  634. Json::ArrayIndex di = this->GetDirectoryIndex(gt->GetLocalGenerator());
  635. target["directoryIndex"] = di;
  636. this->Directories[di].TargetIndexes.append(ti);
  637. // Cross-reference project containing target.
  638. Json::ArrayIndex pi = this->Directories[di].ProjectIndex;
  639. target["projectIndex"] = pi;
  640. this->Projects[pi].TargetIndexes.append(ti);
  641. this->TargetIndexMap[gt] = ti;
  642. return target;
  643. }
  644. Json::Value CodemodelConfig::DumpDirectories()
  645. {
  646. Json::Value directories = Json::arrayValue;
  647. for (Directory& d : this->Directories) {
  648. directories.append(this->DumpDirectory(d));
  649. }
  650. return directories;
  651. }
  652. Json::Value CodemodelConfig::DumpDirectory(Directory& d)
  653. {
  654. Json::Value directory = this->DumpDirectoryObject(d);
  655. std::string sourceDir = d.Snapshot.GetDirectory().GetCurrentSource();
  656. directory["source"] = RelativeIfUnder(this->TopSource, sourceDir);
  657. std::string buildDir = d.Snapshot.GetDirectory().GetCurrentBinary();
  658. directory["build"] = RelativeIfUnder(this->TopBuild, buildDir);
  659. cmStateSnapshot parentDir = d.Snapshot.GetBuildsystemDirectoryParent();
  660. if (parentDir.IsValid()) {
  661. directory["parentIndex"] = this->GetDirectoryIndex(parentDir);
  662. }
  663. Json::Value childIndexes = Json::arrayValue;
  664. for (cmStateSnapshot const& child : d.Snapshot.GetChildren()) {
  665. childIndexes.append(
  666. this->GetDirectoryIndex(child.GetBuildsystemDirectory()));
  667. }
  668. if (!childIndexes.empty()) {
  669. directory["childIndexes"] = std::move(childIndexes);
  670. }
  671. directory["projectIndex"] = d.ProjectIndex;
  672. if (!d.TargetIndexes.empty()) {
  673. directory["targetIndexes"] = std::move(d.TargetIndexes);
  674. }
  675. Json::Value minimumCMakeVersion = this->DumpMinimumCMakeVersion(d.Snapshot);
  676. if (!minimumCMakeVersion.isNull()) {
  677. directory["minimumCMakeVersion"] = std::move(minimumCMakeVersion);
  678. }
  679. if (d.HasInstallRule) {
  680. directory["hasInstallRule"] = true;
  681. }
  682. return directory;
  683. }
  684. Json::Value CodemodelConfig::DumpDirectoryObject(Directory& d)
  685. {
  686. std::string prefix = "directory";
  687. std::string sourceDirRel = RelativeIfUnder(
  688. this->TopSource, d.Snapshot.GetDirectory().GetCurrentSource());
  689. std::string buildDirRel = RelativeIfUnder(
  690. this->TopBuild, d.Snapshot.GetDirectory().GetCurrentBinary());
  691. if (!cmSystemTools::FileIsFullPath(buildDirRel)) {
  692. prefix = cmStrCat(prefix, '-', buildDirRel);
  693. } else if (!cmSystemTools::FileIsFullPath(sourceDirRel)) {
  694. prefix = cmStrCat(prefix, '-', sourceDirRel);
  695. }
  696. for (char& c : prefix) {
  697. if (c == '/' || c == '\\') {
  698. c = '.';
  699. }
  700. }
  701. if (!this->Config.empty()) {
  702. prefix += "-" + this->Config;
  703. }
  704. DirectoryObject dir(d.LocalGenerator, this->Config, this->TargetIndexMap);
  705. return this->FileAPI.MaybeJsonFile(dir.Dump(), prefix);
  706. }
  707. Json::Value CodemodelConfig::DumpProjects()
  708. {
  709. Json::Value projects = Json::arrayValue;
  710. for (Project& p : this->Projects) {
  711. projects.append(this->DumpProject(p));
  712. }
  713. return projects;
  714. }
  715. Json::Value CodemodelConfig::DumpProject(Project& p)
  716. {
  717. Json::Value project = Json::objectValue;
  718. project["name"] = p.Snapshot.GetProjectName();
  719. if (p.ParentIndex != Project::NoParentIndex) {
  720. project["parentIndex"] = p.ParentIndex;
  721. }
  722. if (!p.ChildIndexes.empty()) {
  723. project["childIndexes"] = std::move(p.ChildIndexes);
  724. }
  725. project["directoryIndexes"] = std::move(p.DirectoryIndexes);
  726. if (!p.TargetIndexes.empty()) {
  727. project["targetIndexes"] = std::move(p.TargetIndexes);
  728. }
  729. return project;
  730. }
  731. Json::Value CodemodelConfig::DumpMinimumCMakeVersion(cmStateSnapshot s)
  732. {
  733. Json::Value minimumCMakeVersion;
  734. if (cmValue def = s.GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION")) {
  735. minimumCMakeVersion = Json::objectValue;
  736. minimumCMakeVersion["string"] = *def;
  737. }
  738. return minimumCMakeVersion;
  739. }
  740. DirectoryObject::DirectoryObject(cmLocalGenerator const* lg,
  741. std::string const& config,
  742. TargetIndexMapType& targetIndexMap)
  743. : LG(lg)
  744. , Config(config)
  745. , TargetIndexMap(targetIndexMap)
  746. , TopSource(lg->GetGlobalGenerator()->GetCMakeInstance()->GetHomeDirectory())
  747. , TopBuild(
  748. lg->GetGlobalGenerator()->GetCMakeInstance()->GetHomeOutputDirectory())
  749. , Backtraces(this->TopSource)
  750. {
  751. }
  752. Json::Value DirectoryObject::Dump()
  753. {
  754. Json::Value directoryObject = Json::objectValue;
  755. directoryObject["paths"] = this->DumpPaths();
  756. directoryObject["installers"] = this->DumpInstallers();
  757. directoryObject["backtraceGraph"] = this->Backtraces.Dump();
  758. return directoryObject;
  759. }
  760. void DirectoryObject::AddBacktrace(Json::Value& object,
  761. cmListFileBacktrace const& bt)
  762. {
  763. if (JBTIndex backtrace = this->Backtraces.Add(bt)) {
  764. object["backtrace"] = backtrace.Index;
  765. }
  766. }
  767. Json::Value DirectoryObject::DumpPaths()
  768. {
  769. Json::Value paths = Json::objectValue;
  770. std::string const& sourceDir = this->LG->GetCurrentSourceDirectory();
  771. paths["source"] = RelativeIfUnder(this->TopSource, sourceDir);
  772. std::string const& buildDir = this->LG->GetCurrentBinaryDirectory();
  773. paths["build"] = RelativeIfUnder(this->TopBuild, buildDir);
  774. return paths;
  775. }
  776. Json::Value DirectoryObject::DumpInstallers()
  777. {
  778. Json::Value installers = Json::arrayValue;
  779. for (const auto& gen : this->LG->GetMakefile()->GetInstallGenerators()) {
  780. Json::Value installer = this->DumpInstaller(gen.get());
  781. if (!installer.empty()) {
  782. installers.append(std::move(installer)); // NOLINT(*)
  783. }
  784. }
  785. return installers;
  786. }
  787. Json::Value DirectoryObject::DumpInstaller(cmInstallGenerator* gen)
  788. {
  789. assert(gen);
  790. Json::Value installer = Json::objectValue;
  791. // Exclude subdirectory installers and file(GET_RUNTIME_DEPENDENCIES)
  792. // installers. They are implementation details.
  793. if (dynamic_cast<cmInstallSubdirectoryGenerator*>(gen) ||
  794. dynamic_cast<cmInstallGetRuntimeDependenciesGenerator*>(gen)) {
  795. return installer;
  796. }
  797. // Exclude installers not used in this configuration.
  798. if (!gen->InstallsForConfig(this->Config)) {
  799. return installer;
  800. }
  801. // Add fields specific to each kind of install generator.
  802. if (auto* installTarget = dynamic_cast<cmInstallTargetGenerator*>(gen)) {
  803. cmInstallTargetGenerator::Files const& files =
  804. installTarget->GetFiles(this->Config);
  805. if (files.From.empty()) {
  806. return installer;
  807. }
  808. installer["type"] = "target";
  809. installer["destination"] = installTarget->GetDestination(this->Config);
  810. installer["targetId"] =
  811. TargetId(installTarget->GetTarget(), this->TopBuild);
  812. installer["targetIndex"] =
  813. this->TargetIndexMap[installTarget->GetTarget()];
  814. std::string fromDir = files.FromDir;
  815. if (!fromDir.empty()) {
  816. fromDir.push_back('/');
  817. }
  818. std::string toDir = files.ToDir;
  819. if (!toDir.empty()) {
  820. toDir.push_back('/');
  821. }
  822. Json::Value paths = Json::arrayValue;
  823. for (size_t i = 0; i < files.From.size(); ++i) {
  824. std::string const& fromPath = cmStrCat(fromDir, files.From[i]);
  825. std::string const& toPath = cmStrCat(toDir, files.To[i]);
  826. paths.append(this->DumpInstallerPath(this->TopBuild, fromPath, toPath));
  827. }
  828. installer["paths"] = std::move(paths);
  829. if (installTarget->GetOptional()) {
  830. installer["isOptional"] = true;
  831. }
  832. if (installTarget->IsImportLibrary()) {
  833. installer["targetIsImportLibrary"] = true;
  834. }
  835. switch (files.NamelinkMode) {
  836. case cmInstallTargetGenerator::NamelinkModeNone:
  837. break;
  838. case cmInstallTargetGenerator::NamelinkModeOnly:
  839. installer["targetInstallNamelink"] = "only";
  840. break;
  841. case cmInstallTargetGenerator::NamelinkModeSkip:
  842. installer["targetInstallNamelink"] = "skip";
  843. break;
  844. }
  845. // FIXME: Parse FilePermissions to provide structured information.
  846. // FIXME: Thread EXPORT name through from install() call.
  847. } else if (auto* installFiles =
  848. dynamic_cast<cmInstallFilesGenerator*>(gen)) {
  849. std::vector<std::string> const& files =
  850. installFiles->GetFiles(this->Config);
  851. if (files.empty()) {
  852. return installer;
  853. }
  854. installer["type"] = "file";
  855. installer["destination"] = installFiles->GetDestination(this->Config);
  856. Json::Value paths = Json::arrayValue;
  857. std::string const& rename = installFiles->GetRename(this->Config);
  858. if (!rename.empty() && files.size() == 1) {
  859. paths.append(this->DumpInstallerPath(this->TopSource, files[0], rename));
  860. } else {
  861. for (std::string const& file : installFiles->GetFiles(this->Config)) {
  862. paths.append(RelativeIfUnder(this->TopSource, file));
  863. }
  864. }
  865. installer["paths"] = std::move(paths);
  866. if (installFiles->GetOptional()) {
  867. installer["isOptional"] = true;
  868. }
  869. // FIXME: Parse FilePermissions to provide structured information.
  870. } else if (auto* installDir =
  871. dynamic_cast<cmInstallDirectoryGenerator*>(gen)) {
  872. std::vector<std::string> const& dirs =
  873. installDir->GetDirectories(this->Config);
  874. if (dirs.empty()) {
  875. return installer;
  876. }
  877. installer["type"] = "directory";
  878. installer["destination"] = installDir->GetDestination(this->Config);
  879. Json::Value paths = Json::arrayValue;
  880. for (std::string const& dir : dirs) {
  881. if (cmHasLiteralSuffix(dir, "/")) {
  882. paths.append(this->DumpInstallerPath(
  883. this->TopSource, dir.substr(0, dir.size() - 1), "."));
  884. } else {
  885. paths.append(this->DumpInstallerPath(
  886. this->TopSource, dir, cmSystemTools::GetFilenameName(dir)));
  887. }
  888. }
  889. installer["paths"] = std::move(paths);
  890. if (installDir->GetOptional()) {
  891. installer["isOptional"] = true;
  892. }
  893. // FIXME: Parse FilePermissions, DirPermissions, and LiteralArguments.
  894. // to provide structured information.
  895. } else if (auto* installExport =
  896. dynamic_cast<cmInstallExportGenerator*>(gen)) {
  897. installer["type"] = "export";
  898. installer["destination"] = installExport->GetDestination();
  899. cmExportSet* exportSet = installExport->GetExportSet();
  900. installer["exportName"] = exportSet->GetName();
  901. installer["exportTargets"] = this->DumpInstallerExportTargets(exportSet);
  902. Json::Value paths = Json::arrayValue;
  903. paths.append(
  904. RelativeIfUnder(this->TopBuild, installExport->GetMainImportFile()));
  905. installer["paths"] = std::move(paths);
  906. } else if (auto* installScript =
  907. dynamic_cast<cmInstallScriptGenerator*>(gen)) {
  908. if (installScript->IsCode()) {
  909. installer["type"] = "code";
  910. } else {
  911. installer["type"] = "script";
  912. installer["scriptFile"] = RelativeIfUnder(
  913. this->TopSource, installScript->GetScript(this->Config));
  914. }
  915. } else if (auto* installImportedRuntimeArtifacts =
  916. dynamic_cast<cmInstallImportedRuntimeArtifactsGenerator*>(
  917. gen)) {
  918. installer["type"] = "importedRuntimeArtifacts";
  919. installer["destination"] =
  920. installImportedRuntimeArtifacts->GetDestination(this->Config);
  921. if (installImportedRuntimeArtifacts->GetOptional()) {
  922. installer["isOptional"] = true;
  923. }
  924. } else if (auto* installRuntimeDependencySet =
  925. dynamic_cast<cmInstallRuntimeDependencySetGenerator*>(gen)) {
  926. installer["type"] = "runtimeDependencySet";
  927. installer["destination"] =
  928. installRuntimeDependencySet->GetDestination(this->Config);
  929. std::string name(
  930. installRuntimeDependencySet->GetRuntimeDependencySet()->GetName());
  931. if (!name.empty()) {
  932. installer["runtimeDependencySetName"] = name;
  933. }
  934. switch (installRuntimeDependencySet->GetDependencyType()) {
  935. case cmInstallRuntimeDependencySetGenerator::DependencyType::Framework:
  936. installer["runtimeDependencySetType"] = "framework";
  937. break;
  938. case cmInstallRuntimeDependencySetGenerator::DependencyType::Library:
  939. installer["runtimeDependencySetType"] = "library";
  940. break;
  941. }
  942. } else if (auto* installFileSet =
  943. dynamic_cast<cmInstallFileSetGenerator*>(gen)) {
  944. installer["type"] = "fileSet";
  945. installer["destination"] = installFileSet->GetDestination(this->Config);
  946. auto* fileSet = installFileSet->GetFileSet();
  947. auto* target = installFileSet->GetTarget();
  948. auto dirCges = fileSet->CompileDirectoryEntries();
  949. auto dirs = fileSet->EvaluateDirectoryEntries(
  950. dirCges, target->GetLocalGenerator(), this->Config, target);
  951. auto entryCges = fileSet->CompileFileEntries();
  952. std::map<std::string, std::vector<std::string>> entries;
  953. for (auto const& entryCge : entryCges) {
  954. fileSet->EvaluateFileEntry(dirs, entries, entryCge,
  955. target->GetLocalGenerator(), this->Config,
  956. target);
  957. }
  958. Json::Value files = Json::arrayValue;
  959. for (auto const& it : entries) {
  960. auto dir = it.first;
  961. if (!dir.empty()) {
  962. dir += '/';
  963. }
  964. for (auto const& file : it.second) {
  965. files.append(this->DumpInstallerPath(
  966. this->TopSource, file,
  967. cmStrCat(dir, cmSystemTools::GetFilenameName(file))));
  968. }
  969. }
  970. installer["paths"] = std::move(files);
  971. installer["fileSetName"] = fileSet->GetName();
  972. installer["fileSetType"] = fileSet->GetType();
  973. installer["fileSetDirectories"] = Json::arrayValue;
  974. for (auto const& dir : dirs) {
  975. installer["fileSetDirectories"].append(
  976. RelativeIfUnder(this->TopSource, dir));
  977. }
  978. installer["fileSetTarget"] = Json::objectValue;
  979. installer["fileSetTarget"]["id"] = TargetId(target, this->TopBuild);
  980. installer["fileSetTarget"]["index"] = this->TargetIndexMap[target];
  981. if (installFileSet->GetOptional()) {
  982. installer["isOptional"] = true;
  983. }
  984. } else if (auto* cxxModuleBmi =
  985. dynamic_cast<cmInstallCxxModuleBmiGenerator*>(gen)) {
  986. installer["type"] = "cxxModuleBmi";
  987. installer["destination"] = cxxModuleBmi->GetDestination(this->Config);
  988. auto const* target = cxxModuleBmi->GetTarget();
  989. installer["cxxModuleBmiTarget"] = Json::objectValue;
  990. installer["cxxModuleBmiTarget"]["id"] = TargetId(target, this->TopBuild);
  991. installer["cxxModuleBmiTarget"]["index"] = this->TargetIndexMap[target];
  992. // FIXME: Parse FilePermissions.
  993. // FIXME: Parse MessageLevel.
  994. if (cxxModuleBmi->GetOptional()) {
  995. installer["isOptional"] = true;
  996. }
  997. }
  998. // Add fields common to all install generators.
  999. installer["component"] = gen->GetComponent();
  1000. if (gen->GetExcludeFromAll()) {
  1001. installer["isExcludeFromAll"] = true;
  1002. }
  1003. if (gen->GetAllComponentsFlag()) {
  1004. installer["isForAllComponents"] = true;
  1005. }
  1006. this->AddBacktrace(installer, gen->GetBacktrace());
  1007. return installer;
  1008. }
  1009. Json::Value DirectoryObject::DumpInstallerExportTargets(cmExportSet* exp)
  1010. {
  1011. Json::Value targets = Json::arrayValue;
  1012. for (auto const& targetExport : exp->GetTargetExports()) {
  1013. Json::Value target = Json::objectValue;
  1014. target["id"] = TargetId(targetExport->Target, this->TopBuild);
  1015. target["index"] = this->TargetIndexMap[targetExport->Target];
  1016. targets.append(std::move(target)); // NOLINT(*)
  1017. }
  1018. return targets;
  1019. }
  1020. Json::Value DirectoryObject::DumpInstallerPath(std::string const& top,
  1021. std::string const& fromPathIn,
  1022. std::string const& toPath)
  1023. {
  1024. Json::Value installPath;
  1025. std::string fromPath = RelativeIfUnder(top, fromPathIn);
  1026. // If toPath is the last component of fromPath, use just fromPath.
  1027. if (toPath.find_first_of('/') == std::string::npos &&
  1028. cmHasSuffix(fromPath, toPath) &&
  1029. (fromPath.size() == toPath.size() ||
  1030. fromPath[fromPath.size() - toPath.size() - 1] == '/')) {
  1031. installPath = fromPath;
  1032. } else {
  1033. installPath = Json::objectValue;
  1034. installPath["from"] = fromPath;
  1035. installPath["to"] = toPath;
  1036. }
  1037. return installPath;
  1038. }
  1039. Target::Target(cmGeneratorTarget* gt, std::string const& config)
  1040. : GT(gt)
  1041. , Config(config)
  1042. , TopSource(gt->GetGlobalGenerator()->GetCMakeInstance()->GetHomeDirectory())
  1043. , TopBuild(
  1044. gt->GetGlobalGenerator()->GetCMakeInstance()->GetHomeOutputDirectory())
  1045. , SourceGroupsLocal(this->GT->Makefile->GetSourceGroups())
  1046. , Backtraces(this->TopSource)
  1047. {
  1048. }
  1049. Json::Value Target::Dump()
  1050. {
  1051. Json::Value target = Json::objectValue;
  1052. cmStateEnums::TargetType const type = this->GT->GetType();
  1053. target["name"] = this->GT->GetName();
  1054. target["type"] = cmState::GetTargetTypeName(type);
  1055. target["id"] = TargetId(this->GT, this->TopBuild);
  1056. target["paths"] = this->DumpPaths();
  1057. if (this->GT->Target->GetIsGeneratorProvided()) {
  1058. target["isGeneratorProvided"] = true;
  1059. }
  1060. this->AddBacktrace(target, this->GT->GetBacktrace());
  1061. if (this->GT->Target->GetHaveInstallRule()) {
  1062. target["install"] = this->DumpInstall();
  1063. }
  1064. if (this->GT->HaveWellDefinedOutputFiles()) {
  1065. Json::Value artifacts = this->DumpArtifacts();
  1066. if (!artifacts.empty()) {
  1067. target["artifacts"] = std::move(artifacts);
  1068. }
  1069. }
  1070. if (type == cmStateEnums::EXECUTABLE ||
  1071. type == cmStateEnums::SHARED_LIBRARY ||
  1072. type == cmStateEnums::MODULE_LIBRARY) {
  1073. target["nameOnDisk"] = this->GT->GetFullName(this->Config);
  1074. target["link"] = this->DumpLink();
  1075. } else if (type == cmStateEnums::STATIC_LIBRARY) {
  1076. target["nameOnDisk"] = this->GT->GetFullName(this->Config);
  1077. target["archive"] = this->DumpArchive();
  1078. }
  1079. if (type == cmStateEnums::EXECUTABLE) {
  1080. Json::Value launchers = this->DumpLaunchers();
  1081. if (!launchers.empty()) {
  1082. target["launchers"] = std::move(launchers);
  1083. }
  1084. }
  1085. Json::Value dependencies = this->DumpDependencies();
  1086. if (!dependencies.empty()) {
  1087. target["dependencies"] = dependencies;
  1088. }
  1089. {
  1090. this->ProcessLanguages();
  1091. auto fileSetInfo = this->DumpFileSets();
  1092. if (!fileSetInfo.first.isNull()) {
  1093. target["fileSets"] = fileSetInfo.first;
  1094. }
  1095. target["sources"] = this->DumpSources(fileSetInfo.second);
  1096. Json::Value folder = this->DumpFolder();
  1097. if (!folder.isNull()) {
  1098. target["folder"] = std::move(folder);
  1099. }
  1100. Json::Value sourceGroups = this->DumpSourceGroups();
  1101. if (!sourceGroups.empty()) {
  1102. target["sourceGroups"] = std::move(sourceGroups);
  1103. }
  1104. Json::Value compileGroups = this->DumpCompileGroups();
  1105. if (!compileGroups.empty()) {
  1106. target["compileGroups"] = std::move(compileGroups);
  1107. }
  1108. }
  1109. target["backtraceGraph"] = this->Backtraces.Dump();
  1110. return target;
  1111. }
  1112. void Target::ProcessLanguages()
  1113. {
  1114. std::set<std::string> languages;
  1115. this->GT->GetLanguages(languages, this->Config);
  1116. for (std::string const& lang : languages) {
  1117. this->ProcessLanguage(lang);
  1118. }
  1119. }
  1120. void Target::ProcessLanguage(std::string const& lang)
  1121. {
  1122. CompileData& cd = this->CompileDataMap[lang];
  1123. cd.Language = lang;
  1124. if (cmValue sysrootCompile =
  1125. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT_COMPILE")) {
  1126. cd.Sysroot = *sysrootCompile;
  1127. } else if (cmValue sysroot =
  1128. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT")) {
  1129. cd.Sysroot = *sysroot;
  1130. }
  1131. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  1132. {
  1133. // FIXME: Add flags from end section of ExpandRuleVariable,
  1134. // which may need to be factored out.
  1135. std::vector<BT<std::string>> flags =
  1136. lg->GetTargetCompileFlags(this->GT, this->Config, lang);
  1137. cd.Flags.reserve(flags.size());
  1138. for (const BT<std::string>& f : flags) {
  1139. cd.Flags.emplace_back(this->ToJBT(f));
  1140. }
  1141. }
  1142. std::set<BT<std::string>> defines =
  1143. lg->GetTargetDefines(this->GT, this->Config, lang);
  1144. cd.Defines.reserve(defines.size());
  1145. for (BT<std::string> const& d : defines) {
  1146. cd.Defines.emplace_back(this->ToJBT(d));
  1147. }
  1148. std::vector<BT<std::string>> includePathList =
  1149. lg->GetIncludeDirectories(this->GT, lang, this->Config);
  1150. for (BT<std::string> const& i : includePathList) {
  1151. if (this->GT->IsApple() && cmSystemTools::IsPathToFramework(i.Value)) {
  1152. cd.Frameworks.emplace_back(
  1153. this->ToJBT(i),
  1154. this->GT->IsSystemIncludeDirectory(i.Value, this->Config, lang));
  1155. } else {
  1156. cd.Includes.emplace_back(
  1157. this->ToJBT(i),
  1158. this->GT->IsSystemIncludeDirectory(i.Value, this->Config, lang));
  1159. }
  1160. }
  1161. std::vector<BT<std::string>> precompileHeaders =
  1162. this->GT->GetPrecompileHeaders(this->Config, lang);
  1163. for (BT<std::string> const& pch : precompileHeaders) {
  1164. cd.PrecompileHeaders.emplace_back(this->ToJBT(pch));
  1165. }
  1166. BTs<std::string> const* languageStandard =
  1167. this->GT->GetLanguageStandardProperty(lang, this->Config);
  1168. if (languageStandard) {
  1169. cd.LanguageStandard = this->ToJBTs(*languageStandard);
  1170. }
  1171. }
  1172. Json::ArrayIndex Target::AddSourceGroup(cmSourceGroup* sg, Json::ArrayIndex si)
  1173. {
  1174. auto i = this->SourceGroupsMap.find(sg);
  1175. if (i == this->SourceGroupsMap.end()) {
  1176. auto sgIndex = static_cast<Json::ArrayIndex>(this->SourceGroups.size());
  1177. i = this->SourceGroupsMap.emplace(sg, sgIndex).first;
  1178. SourceGroup g;
  1179. g.Name = sg->GetFullName();
  1180. this->SourceGroups.push_back(std::move(g));
  1181. }
  1182. this->SourceGroups[i->second].SourceIndexes.append(si);
  1183. return i->second;
  1184. }
  1185. CompileData Target::BuildCompileData(cmSourceFile* sf)
  1186. {
  1187. CompileData fd;
  1188. fd.Language = sf->GetOrDetermineLanguage();
  1189. if (fd.Language.empty()) {
  1190. return fd;
  1191. }
  1192. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  1193. cmGeneratorExpressionInterpreter genexInterpreter(lg, this->Config, this->GT,
  1194. fd.Language);
  1195. const std::string COMPILE_FLAGS("COMPILE_FLAGS");
  1196. if (cmValue cflags = sf->GetProperty(COMPILE_FLAGS)) {
  1197. std::string flags = genexInterpreter.Evaluate(*cflags, COMPILE_FLAGS);
  1198. fd.Flags.emplace_back(std::move(flags), JBTIndex());
  1199. }
  1200. const std::string COMPILE_OPTIONS("COMPILE_OPTIONS");
  1201. for (BT<std::string> tmpOpt : sf->GetCompileOptions()) {
  1202. tmpOpt.Value = genexInterpreter.Evaluate(tmpOpt.Value, COMPILE_OPTIONS);
  1203. // After generator evaluation we need to use the AppendCompileOptions
  1204. // method so we handle situations where backtrace entries have lists
  1205. // and properly escape flags.
  1206. std::string tmp;
  1207. lg->AppendCompileOptions(tmp, tmpOpt.Value);
  1208. BT<std::string> opt(tmp, tmpOpt.Backtrace);
  1209. fd.Flags.emplace_back(this->ToJBT(opt));
  1210. }
  1211. // Add precompile headers compile options.
  1212. std::vector<std::string> architectures =
  1213. this->GT->GetAppleArchs(this->Config, fd.Language);
  1214. if (architectures.empty()) {
  1215. architectures.emplace_back();
  1216. }
  1217. std::unordered_map<std::string, std::string> pchSources;
  1218. for (const std::string& arch : architectures) {
  1219. const std::string pchSource =
  1220. this->GT->GetPchSource(this->Config, fd.Language, arch);
  1221. if (!pchSource.empty()) {
  1222. pchSources.insert(std::make_pair(pchSource, arch));
  1223. }
  1224. }
  1225. if (!pchSources.empty() && !sf->GetProperty("SKIP_PRECOMPILE_HEADERS")) {
  1226. std::string pchOptions;
  1227. auto pchIt = pchSources.find(sf->ResolveFullPath());
  1228. if (pchIt != pchSources.end()) {
  1229. pchOptions = this->GT->GetPchCreateCompileOptions(
  1230. this->Config, fd.Language, pchIt->second);
  1231. } else {
  1232. pchOptions =
  1233. this->GT->GetPchUseCompileOptions(this->Config, fd.Language);
  1234. }
  1235. BT<std::string> tmpOpt(pchOptions);
  1236. tmpOpt.Value = genexInterpreter.Evaluate(tmpOpt.Value, COMPILE_OPTIONS);
  1237. // After generator evaluation we need to use the AppendCompileOptions
  1238. // method so we handle situations where backtrace entries have lists
  1239. // and properly escape flags.
  1240. std::string tmp;
  1241. lg->AppendCompileOptions(tmp, tmpOpt.Value);
  1242. BT<std::string> opt(tmp, tmpOpt.Backtrace);
  1243. fd.Flags.emplace_back(this->ToJBT(opt));
  1244. }
  1245. // Add include directories from source file properties.
  1246. {
  1247. const std::string INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
  1248. for (BT<std::string> tmpInclude : sf->GetIncludeDirectories()) {
  1249. tmpInclude.Value =
  1250. genexInterpreter.Evaluate(tmpInclude.Value, INCLUDE_DIRECTORIES);
  1251. // After generator evaluation we need to use the AppendIncludeDirectories
  1252. // method so we handle situations where backtrace entries have lists.
  1253. std::vector<std::string> tmp;
  1254. lg->AppendIncludeDirectories(tmp, tmpInclude.Value, *sf);
  1255. for (std::string& i : tmp) {
  1256. bool const isSystemInclude =
  1257. this->GT->IsSystemIncludeDirectory(i, this->Config, fd.Language);
  1258. BT<std::string> include(i, tmpInclude.Backtrace);
  1259. if (this->GT->IsApple() && cmSystemTools::IsPathToFramework(i)) {
  1260. fd.Frameworks.emplace_back(this->ToJBT(include), isSystemInclude);
  1261. } else {
  1262. fd.Includes.emplace_back(this->ToJBT(include), isSystemInclude);
  1263. }
  1264. }
  1265. }
  1266. }
  1267. const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
  1268. std::set<BT<std::string>> fileDefines;
  1269. for (BT<std::string> tmpDef : sf->GetCompileDefinitions()) {
  1270. tmpDef.Value =
  1271. genexInterpreter.Evaluate(tmpDef.Value, COMPILE_DEFINITIONS);
  1272. // After generator evaluation we need to use the AppendDefines method
  1273. // so we handle situations where backtrace entries have lists.
  1274. std::set<std::string> tmp;
  1275. lg->AppendDefines(tmp, tmpDef.Value);
  1276. for (const std::string& i : tmp) {
  1277. BT<std::string> def(i, tmpDef.Backtrace);
  1278. fileDefines.insert(def);
  1279. }
  1280. }
  1281. std::set<std::string> configFileDefines;
  1282. const std::string defPropName =
  1283. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(this->Config);
  1284. if (cmValue config_defs = sf->GetProperty(defPropName)) {
  1285. lg->AppendDefines(
  1286. configFileDefines,
  1287. genexInterpreter.Evaluate(*config_defs, COMPILE_DEFINITIONS));
  1288. }
  1289. fd.Defines.reserve(fileDefines.size() + configFileDefines.size());
  1290. for (BT<std::string> const& def : fileDefines) {
  1291. fd.Defines.emplace_back(this->ToJBT(def));
  1292. }
  1293. for (std::string const& d : configFileDefines) {
  1294. fd.Defines.emplace_back(d, JBTIndex());
  1295. }
  1296. return fd;
  1297. }
  1298. CompileData Target::MergeCompileData(CompileData const& fd)
  1299. {
  1300. CompileData cd;
  1301. cd.Language = fd.Language;
  1302. if (cd.Language.empty()) {
  1303. return cd;
  1304. }
  1305. CompileData const& td = this->CompileDataMap.at(cd.Language);
  1306. // All compile groups share the sysroot of the target.
  1307. cd.Sysroot = td.Sysroot;
  1308. // All compile groups share the precompile headers of the target.
  1309. cd.PrecompileHeaders = td.PrecompileHeaders;
  1310. // All compile groups share the language standard of the target.
  1311. cd.LanguageStandard = td.LanguageStandard;
  1312. // Use target-wide flags followed by source-specific flags.
  1313. cd.Flags.reserve(td.Flags.size() + fd.Flags.size());
  1314. cd.Flags.insert(cd.Flags.end(), td.Flags.begin(), td.Flags.end());
  1315. cd.Flags.insert(cd.Flags.end(), fd.Flags.begin(), fd.Flags.end());
  1316. // Use source-specific includes followed by target-wide includes.
  1317. cd.Includes.reserve(fd.Includes.size() + td.Includes.size());
  1318. cd.Includes.insert(cd.Includes.end(), fd.Includes.begin(),
  1319. fd.Includes.end());
  1320. cd.Includes.insert(cd.Includes.end(), td.Includes.begin(),
  1321. td.Includes.end());
  1322. // Use source-specific frameworks followed by target-wide frameworks.
  1323. cd.Frameworks.reserve(fd.Frameworks.size() + td.Frameworks.size());
  1324. cd.Frameworks.insert(cd.Frameworks.end(), fd.Frameworks.begin(),
  1325. fd.Frameworks.end());
  1326. cd.Frameworks.insert(cd.Frameworks.end(), td.Frameworks.begin(),
  1327. td.Frameworks.end());
  1328. // Use target-wide defines followed by source-specific defines.
  1329. cd.Defines.reserve(td.Defines.size() + fd.Defines.size());
  1330. cd.Defines.insert(cd.Defines.end(), td.Defines.begin(), td.Defines.end());
  1331. cd.Defines.insert(cd.Defines.end(), fd.Defines.begin(), fd.Defines.end());
  1332. // De-duplicate defines.
  1333. std::stable_sort(cd.Defines.begin(), cd.Defines.end(),
  1334. JBT<std::string>::ValueLess);
  1335. auto end = std::unique(cd.Defines.begin(), cd.Defines.end(),
  1336. JBT<std::string>::ValueEq);
  1337. cd.Defines.erase(end, cd.Defines.end());
  1338. return cd;
  1339. }
  1340. Json::ArrayIndex Target::AddSourceCompileGroup(cmSourceFile* sf,
  1341. Json::ArrayIndex si)
  1342. {
  1343. CompileData compileData = this->BuildCompileData(sf);
  1344. auto i = this->CompileGroupMap.find(compileData);
  1345. if (i == this->CompileGroupMap.end()) {
  1346. Json::ArrayIndex cgIndex =
  1347. static_cast<Json::ArrayIndex>(this->CompileGroups.size());
  1348. i = this->CompileGroupMap.emplace(std::move(compileData), cgIndex).first;
  1349. CompileGroup g;
  1350. g.Entry = i;
  1351. this->CompileGroups.push_back(std::move(g));
  1352. }
  1353. this->CompileGroups[i->second].SourceIndexes.append(si);
  1354. return i->second;
  1355. }
  1356. void Target::AddBacktrace(Json::Value& object, cmListFileBacktrace const& bt)
  1357. {
  1358. if (JBTIndex backtrace = this->Backtraces.Add(bt)) {
  1359. object["backtrace"] = backtrace.Index;
  1360. }
  1361. }
  1362. void Target::AddBacktrace(Json::Value& object, JBTIndex bt)
  1363. {
  1364. if (bt) {
  1365. object["backtrace"] = bt.Index;
  1366. }
  1367. }
  1368. Json::Value Target::DumpPaths()
  1369. {
  1370. Json::Value paths = Json::objectValue;
  1371. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  1372. std::string const& sourceDir = lg->GetCurrentSourceDirectory();
  1373. paths["source"] = RelativeIfUnder(this->TopSource, sourceDir);
  1374. std::string const& buildDir = lg->GetCurrentBinaryDirectory();
  1375. paths["build"] = RelativeIfUnder(this->TopBuild, buildDir);
  1376. return paths;
  1377. }
  1378. std::pair<Json::Value, Target::FileSetDatabase> Target::DumpFileSets()
  1379. {
  1380. Json::Value fsJson = Json::nullValue;
  1381. FileSetDatabase fsdb;
  1382. // Build the fileset database.
  1383. auto const* tgt = this->GT->Target;
  1384. auto const& fs_names = tgt->GetAllFileSetNames();
  1385. if (!fs_names.empty()) {
  1386. fsJson = Json::arrayValue;
  1387. size_t fsIndex = 0;
  1388. for (auto const& fs_name : fs_names) {
  1389. auto const* fs = tgt->GetFileSet(fs_name);
  1390. if (!fs) {
  1391. this->GT->Makefile->IssueMessage(
  1392. MessageType::INTERNAL_ERROR,
  1393. cmStrCat("Target \"", tgt->GetName(),
  1394. "\" is tracked to have file set \"", fs_name,
  1395. "\", but it was not found."));
  1396. continue;
  1397. }
  1398. auto fileEntries = fs->CompileFileEntries();
  1399. auto directoryEntries = fs->CompileDirectoryEntries();
  1400. auto directories = fs->EvaluateDirectoryEntries(
  1401. directoryEntries, this->GT->LocalGenerator, this->Config, this->GT);
  1402. fsJson.append(this->DumpFileSet(fs, directories));
  1403. std::map<std::string, std::vector<std::string>> files_per_dirs;
  1404. for (auto const& entry : fileEntries) {
  1405. fs->EvaluateFileEntry(directories, files_per_dirs, entry,
  1406. this->GT->LocalGenerator, this->Config,
  1407. this->GT);
  1408. }
  1409. for (auto const& files_per_dir : files_per_dirs) {
  1410. auto const& dir = files_per_dir.first;
  1411. for (auto const& file : files_per_dir.second) {
  1412. std::string sf_path;
  1413. if (dir.empty()) {
  1414. sf_path = file;
  1415. } else {
  1416. sf_path = cmStrCat(dir, '/', file);
  1417. }
  1418. fsdb[sf_path] = static_cast<Json::ArrayIndex>(fsIndex);
  1419. }
  1420. }
  1421. ++fsIndex;
  1422. }
  1423. }
  1424. return std::make_pair(fsJson, fsdb);
  1425. }
  1426. Json::Value Target::DumpFileSet(cmFileSet const* fs,
  1427. std::vector<std::string> const& directories)
  1428. {
  1429. Json::Value fileSet = Json::objectValue;
  1430. fileSet["name"] = fs->GetName();
  1431. fileSet["type"] = fs->GetType();
  1432. fileSet["visibility"] =
  1433. std::string(cmFileSetVisibilityToName(fs->GetVisibility()));
  1434. Json::Value baseDirs = Json::arrayValue;
  1435. for (auto const& directory : directories) {
  1436. baseDirs.append(RelativeIfUnder(this->TopSource, directory));
  1437. }
  1438. fileSet["baseDirectories"] = baseDirs;
  1439. return fileSet;
  1440. }
  1441. Json::Value Target::DumpSources(FileSetDatabase const& fsdb)
  1442. {
  1443. Json::Value sources = Json::arrayValue;
  1444. cmGeneratorTarget::KindedSources const& kinded =
  1445. this->GT->GetKindedSources(this->Config);
  1446. for (cmGeneratorTarget::SourceAndKind const& sk : kinded.Sources) {
  1447. sources.append(this->DumpSource(sk, sources.size(), fsdb));
  1448. }
  1449. return sources;
  1450. }
  1451. Json::Value Target::DumpSource(cmGeneratorTarget::SourceAndKind const& sk,
  1452. Json::ArrayIndex si,
  1453. FileSetDatabase const& fsdb)
  1454. {
  1455. Json::Value source = Json::objectValue;
  1456. cmSourceFile* sf = sk.Source.Value;
  1457. std::string const path = sf->ResolveFullPath();
  1458. source["path"] = RelativeIfUnder(this->TopSource, path);
  1459. if (sk.Source.Value->GetIsGenerated()) {
  1460. source["isGenerated"] = true;
  1461. }
  1462. this->AddBacktrace(source, sk.Source.Backtrace);
  1463. auto fsit = fsdb.find(path);
  1464. if (fsit != fsdb.end()) {
  1465. source["fileSetIndex"] = fsit->second;
  1466. }
  1467. if (cmSourceGroup* sg =
  1468. this->GT->Makefile->FindSourceGroup(path, this->SourceGroupsLocal)) {
  1469. source["sourceGroupIndex"] = this->AddSourceGroup(sg, si);
  1470. }
  1471. switch (sk.Kind) {
  1472. case cmGeneratorTarget::SourceKindCxxModuleSource:
  1473. case cmGeneratorTarget::SourceKindObjectSource: {
  1474. source["compileGroupIndex"] =
  1475. this->AddSourceCompileGroup(sk.Source.Value, si);
  1476. } break;
  1477. case cmGeneratorTarget::SourceKindAppManifest:
  1478. case cmGeneratorTarget::SourceKindCertificate:
  1479. case cmGeneratorTarget::SourceKindCustomCommand:
  1480. case cmGeneratorTarget::SourceKindExternalObject:
  1481. case cmGeneratorTarget::SourceKindExtra:
  1482. case cmGeneratorTarget::SourceKindHeader:
  1483. case cmGeneratorTarget::SourceKindIDL:
  1484. case cmGeneratorTarget::SourceKindManifest:
  1485. case cmGeneratorTarget::SourceKindModuleDefinition:
  1486. case cmGeneratorTarget::SourceKindResx:
  1487. case cmGeneratorTarget::SourceKindXaml:
  1488. case cmGeneratorTarget::SourceKindUnityBatched:
  1489. break;
  1490. }
  1491. return source;
  1492. }
  1493. Json::Value Target::DumpCompileData(CompileData const& cd)
  1494. {
  1495. Json::Value result = Json::objectValue;
  1496. if (!cd.Language.empty()) {
  1497. result["language"] = cd.Language;
  1498. }
  1499. if (!cd.Sysroot.empty()) {
  1500. result["sysroot"] = this->DumpSysroot(cd.Sysroot);
  1501. }
  1502. if (!cd.Flags.empty()) {
  1503. result["compileCommandFragments"] = this->DumpCommandFragments(cd.Flags);
  1504. }
  1505. if (!cd.Includes.empty()) {
  1506. Json::Value includes = Json::arrayValue;
  1507. for (auto const& i : cd.Includes) {
  1508. includes.append(this->DumpInclude(i));
  1509. }
  1510. result["includes"] = includes;
  1511. }
  1512. if (!cd.Frameworks.empty()) {
  1513. Json::Value frameworks = Json::arrayValue;
  1514. for (auto const& i : cd.Frameworks) {
  1515. frameworks.append(this->DumpFramework(i));
  1516. }
  1517. result["frameworks"] = frameworks;
  1518. }
  1519. if (!cd.Defines.empty()) {
  1520. Json::Value defines = Json::arrayValue;
  1521. for (JBT<std::string> const& d : cd.Defines) {
  1522. defines.append(this->DumpDefine(d));
  1523. }
  1524. result["defines"] = std::move(defines);
  1525. }
  1526. if (!cd.PrecompileHeaders.empty()) {
  1527. Json::Value precompileHeaders = Json::arrayValue;
  1528. for (JBT<std::string> const& pch : cd.PrecompileHeaders) {
  1529. precompileHeaders.append(this->DumpPrecompileHeader(pch));
  1530. }
  1531. result["precompileHeaders"] = std::move(precompileHeaders);
  1532. }
  1533. if (!cd.LanguageStandard.Value.empty()) {
  1534. result["languageStandard"] =
  1535. this->DumpLanguageStandard(cd.LanguageStandard);
  1536. }
  1537. return result;
  1538. }
  1539. Json::Value Target::DumpInclude(CompileData::IncludeEntry const& inc)
  1540. {
  1541. Json::Value include = Json::objectValue;
  1542. include["path"] = inc.Path.Value;
  1543. if (inc.IsSystem) {
  1544. include["isSystem"] = true;
  1545. }
  1546. this->AddBacktrace(include, inc.Path.Backtrace);
  1547. return include;
  1548. }
  1549. Json::Value Target::DumpFramework(CompileData::IncludeEntry const& fw)
  1550. {
  1551. // for now, idem as include
  1552. return this->DumpInclude(fw);
  1553. }
  1554. Json::Value Target::DumpPrecompileHeader(JBT<std::string> const& header)
  1555. {
  1556. Json::Value precompileHeader = Json::objectValue;
  1557. precompileHeader["header"] = header.Value;
  1558. this->AddBacktrace(precompileHeader, header.Backtrace);
  1559. return precompileHeader;
  1560. }
  1561. Json::Value Target::DumpLanguageStandard(JBTs<std::string> const& standard)
  1562. {
  1563. Json::Value languageStandard = Json::objectValue;
  1564. languageStandard["standard"] = standard.Value;
  1565. if (!standard.Backtraces.empty()) {
  1566. Json::Value backtraces = Json::arrayValue;
  1567. for (JBTIndex backtrace : standard.Backtraces) {
  1568. backtraces.append(backtrace.Index);
  1569. }
  1570. languageStandard["backtraces"] = backtraces;
  1571. }
  1572. return languageStandard;
  1573. }
  1574. Json::Value Target::DumpDefine(JBT<std::string> const& def)
  1575. {
  1576. Json::Value define = Json::objectValue;
  1577. define["define"] = def.Value;
  1578. this->AddBacktrace(define, def.Backtrace);
  1579. return define;
  1580. }
  1581. Json::Value Target::DumpSourceGroups()
  1582. {
  1583. Json::Value sourceGroups = Json::arrayValue;
  1584. for (auto& sg : this->SourceGroups) {
  1585. sourceGroups.append(this->DumpSourceGroup(sg));
  1586. }
  1587. return sourceGroups;
  1588. }
  1589. Json::Value Target::DumpSourceGroup(SourceGroup& sg)
  1590. {
  1591. Json::Value group = Json::objectValue;
  1592. group["name"] = sg.Name;
  1593. group["sourceIndexes"] = std::move(sg.SourceIndexes);
  1594. return group;
  1595. }
  1596. Json::Value Target::DumpCompileGroups()
  1597. {
  1598. Json::Value compileGroups = Json::arrayValue;
  1599. for (auto& cg : this->CompileGroups) {
  1600. compileGroups.append(this->DumpCompileGroup(cg));
  1601. }
  1602. return compileGroups;
  1603. }
  1604. Json::Value Target::DumpCompileGroup(CompileGroup& cg)
  1605. {
  1606. Json::Value group =
  1607. this->DumpCompileData(this->MergeCompileData(cg.Entry->first));
  1608. group["sourceIndexes"] = std::move(cg.SourceIndexes);
  1609. return group;
  1610. }
  1611. Json::Value Target::DumpSysroot(std::string const& path)
  1612. {
  1613. Json::Value sysroot = Json::objectValue;
  1614. sysroot["path"] = path;
  1615. return sysroot;
  1616. }
  1617. Json::Value Target::DumpInstall()
  1618. {
  1619. Json::Value install = Json::objectValue;
  1620. install["prefix"] = this->DumpInstallPrefix();
  1621. install["destinations"] = this->DumpInstallDestinations();
  1622. return install;
  1623. }
  1624. Json::Value Target::DumpInstallPrefix()
  1625. {
  1626. Json::Value prefix = Json::objectValue;
  1627. std::string p =
  1628. this->GT->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  1629. cmSystemTools::ConvertToUnixSlashes(p);
  1630. prefix["path"] = p;
  1631. return prefix;
  1632. }
  1633. Json::Value Target::DumpInstallDestinations()
  1634. {
  1635. Json::Value destinations = Json::arrayValue;
  1636. auto installGens = this->GT->Target->GetInstallGenerators();
  1637. for (auto* itGen : installGens) {
  1638. destinations.append(this->DumpInstallDestination(itGen));
  1639. }
  1640. return destinations;
  1641. }
  1642. Json::Value Target::DumpInstallDestination(cmInstallTargetGenerator* itGen)
  1643. {
  1644. Json::Value destination = Json::objectValue;
  1645. destination["path"] = itGen->GetDestination(this->Config);
  1646. this->AddBacktrace(destination, itGen->GetBacktrace());
  1647. return destination;
  1648. }
  1649. Json::Value Target::DumpArtifacts()
  1650. {
  1651. Json::Value artifacts = Json::arrayValue;
  1652. // Object libraries have only object files as artifacts.
  1653. if (this->GT->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1654. if (!this->GT->Target->HasKnownObjectFileLocation(nullptr)) {
  1655. return artifacts;
  1656. }
  1657. std::vector<cmSourceFile const*> objectSources;
  1658. this->GT->GetObjectSources(objectSources, this->Config);
  1659. std::string const obj_dir = this->GT->GetObjectDirectory(this->Config);
  1660. for (cmSourceFile const* sf : objectSources) {
  1661. const std::string& obj = this->GT->GetObjectName(sf);
  1662. Json::Value artifact = Json::objectValue;
  1663. artifact["path"] = RelativeIfUnder(this->TopBuild, obj_dir + obj);
  1664. artifacts.append(std::move(artifact)); // NOLINT(*)
  1665. }
  1666. return artifacts;
  1667. }
  1668. // Other target types always have a "main" artifact.
  1669. {
  1670. Json::Value artifact = Json::objectValue;
  1671. artifact["path"] =
  1672. RelativeIfUnder(this->TopBuild,
  1673. this->GT->GetFullPath(
  1674. this->Config, cmStateEnums::RuntimeBinaryArtifact));
  1675. artifacts.append(std::move(artifact)); // NOLINT(*)
  1676. }
  1677. // Add Windows-specific artifacts produced by the linker.
  1678. if (this->GT->HasImportLibrary(this->Config)) {
  1679. Json::Value artifact = Json::objectValue;
  1680. artifact["path"] =
  1681. RelativeIfUnder(this->TopBuild,
  1682. this->GT->GetFullPath(
  1683. this->Config, cmStateEnums::ImportLibraryArtifact));
  1684. artifacts.append(std::move(artifact)); // NOLINT(*)
  1685. }
  1686. if (this->GT->IsDLLPlatform() &&
  1687. this->GT->GetType() != cmStateEnums::STATIC_LIBRARY) {
  1688. cmGeneratorTarget::OutputInfo const* output =
  1689. this->GT->GetOutputInfo(this->Config);
  1690. if (output && !output->PdbDir.empty()) {
  1691. Json::Value artifact = Json::objectValue;
  1692. artifact["path"] = RelativeIfUnder(this->TopBuild,
  1693. output->PdbDir + '/' +
  1694. this->GT->GetPDBName(this->Config));
  1695. artifacts.append(std::move(artifact)); // NOLINT(*)
  1696. }
  1697. }
  1698. return artifacts;
  1699. }
  1700. Json::Value Target::DumpLink()
  1701. {
  1702. Json::Value link = Json::objectValue;
  1703. std::string lang = this->GT->GetLinkerLanguage(this->Config);
  1704. link["language"] = lang;
  1705. {
  1706. Json::Value commandFragments = this->DumpLinkCommandFragments();
  1707. if (!commandFragments.empty()) {
  1708. link["commandFragments"] = std::move(commandFragments);
  1709. }
  1710. }
  1711. if (cmValue sysrootLink =
  1712. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT_LINK")) {
  1713. link["sysroot"] = this->DumpSysroot(*sysrootLink);
  1714. } else if (cmValue sysroot =
  1715. this->GT->Makefile->GetDefinition("CMAKE_SYSROOT")) {
  1716. link["sysroot"] = this->DumpSysroot(*sysroot);
  1717. }
  1718. if (this->GT->IsIPOEnabled(lang, this->Config)) {
  1719. link["lto"] = true;
  1720. }
  1721. return link;
  1722. }
  1723. Json::Value Target::DumpArchive()
  1724. {
  1725. Json::Value archive = Json::objectValue;
  1726. {
  1727. // The "link" fragments not relevant to static libraries are empty.
  1728. Json::Value commandFragments = this->DumpLinkCommandFragments();
  1729. if (!commandFragments.empty()) {
  1730. archive["commandFragments"] = std::move(commandFragments);
  1731. }
  1732. }
  1733. std::string lang = this->GT->GetLinkerLanguage(this->Config);
  1734. if (this->GT->IsIPOEnabled(lang, this->Config)) {
  1735. archive["lto"] = true;
  1736. }
  1737. return archive;
  1738. }
  1739. Json::Value Target::DumpLinkCommandFragments()
  1740. {
  1741. Json::Value linkFragments = Json::arrayValue;
  1742. std::string linkLanguageFlags;
  1743. std::vector<BT<std::string>> linkFlags;
  1744. std::string frameworkPath;
  1745. std::vector<BT<std::string>> linkPath;
  1746. std::vector<BT<std::string>> linkLibs;
  1747. cmLocalGenerator* lg = this->GT->GetLocalGenerator();
  1748. cmGlobalGenerator* gg = this->GT->GetGlobalGenerator();
  1749. std::unique_ptr<cmLinkLineComputer> linkLineComputer =
  1750. gg->CreateLinkLineComputer(lg, lg->GetStateSnapshot().GetDirectory());
  1751. lg->GetTargetFlags(linkLineComputer.get(), this->Config, linkLibs,
  1752. linkLanguageFlags, linkFlags, frameworkPath, linkPath,
  1753. this->GT);
  1754. linkLanguageFlags = cmTrimWhitespace(linkLanguageFlags);
  1755. frameworkPath = cmTrimWhitespace(frameworkPath);
  1756. if (!linkLanguageFlags.empty()) {
  1757. linkFragments.append(
  1758. this->DumpCommandFragment(std::move(linkLanguageFlags), "flags"));
  1759. }
  1760. if (!linkFlags.empty()) {
  1761. for (BT<std::string> frag : linkFlags) {
  1762. frag.Value = cmTrimWhitespace(frag.Value);
  1763. linkFragments.append(
  1764. this->DumpCommandFragment(this->ToJBT(frag), "flags"));
  1765. }
  1766. }
  1767. if (!frameworkPath.empty()) {
  1768. linkFragments.append(
  1769. this->DumpCommandFragment(std::move(frameworkPath), "frameworkPath"));
  1770. }
  1771. if (!linkPath.empty()) {
  1772. for (BT<std::string> frag : linkPath) {
  1773. frag.Value = cmTrimWhitespace(frag.Value);
  1774. linkFragments.append(
  1775. this->DumpCommandFragment(this->ToJBT(frag), "libraryPath"));
  1776. }
  1777. }
  1778. if (!linkLibs.empty()) {
  1779. for (BT<std::string> frag : linkLibs) {
  1780. frag.Value = cmTrimWhitespace(frag.Value);
  1781. linkFragments.append(
  1782. this->DumpCommandFragment(this->ToJBT(frag), "libraries"));
  1783. }
  1784. }
  1785. return linkFragments;
  1786. }
  1787. Json::Value Target::DumpCommandFragments(
  1788. std::vector<JBT<std::string>> const& frags)
  1789. {
  1790. Json::Value commandFragments = Json::arrayValue;
  1791. for (JBT<std::string> const& f : frags) {
  1792. commandFragments.append(this->DumpCommandFragment(f));
  1793. }
  1794. return commandFragments;
  1795. }
  1796. Json::Value Target::DumpCommandFragment(JBT<std::string> const& frag,
  1797. std::string const& role)
  1798. {
  1799. Json::Value fragment = Json::objectValue;
  1800. fragment["fragment"] = frag.Value;
  1801. if (!role.empty()) {
  1802. fragment["role"] = role;
  1803. }
  1804. this->AddBacktrace(fragment, frag.Backtrace);
  1805. return fragment;
  1806. }
  1807. Json::Value Target::DumpDependencies()
  1808. {
  1809. Json::Value dependencies = Json::arrayValue;
  1810. cmGlobalGenerator* gg = this->GT->GetGlobalGenerator();
  1811. for (cmTargetDepend const& td : gg->GetTargetDirectDepends(this->GT)) {
  1812. dependencies.append(this->DumpDependency(td));
  1813. }
  1814. return dependencies;
  1815. }
  1816. Json::Value Target::DumpDependency(cmTargetDepend const& td)
  1817. {
  1818. Json::Value dependency = Json::objectValue;
  1819. dependency["id"] = TargetId(td, this->TopBuild);
  1820. this->AddBacktrace(dependency, td.GetBacktrace());
  1821. return dependency;
  1822. }
  1823. Json::Value Target::DumpFolder()
  1824. {
  1825. Json::Value folder;
  1826. if (cmValue f = this->GT->GetProperty("FOLDER")) {
  1827. folder = Json::objectValue;
  1828. folder["name"] = *f;
  1829. }
  1830. return folder;
  1831. }
  1832. Json::Value Target::DumpLauncher(const char* name, const char* type)
  1833. {
  1834. cmValue property = this->GT->GetProperty(name);
  1835. Json::Value launcher;
  1836. if (property) {
  1837. cmList commandWithArgs{ *property };
  1838. std::string command(commandWithArgs[0]);
  1839. cmSystemTools::ConvertToUnixSlashes(command);
  1840. launcher = Json::objectValue;
  1841. launcher["command"] = RelativeIfUnder(this->TopSource, command);
  1842. launcher["type"] = type;
  1843. Json::Value args;
  1844. for (std::string const& arg : cmMakeRange(commandWithArgs).advance(1)) {
  1845. args.append(arg);
  1846. }
  1847. if (!args.empty()) {
  1848. launcher["arguments"] = std::move(args);
  1849. }
  1850. }
  1851. return launcher;
  1852. }
  1853. Json::Value Target::DumpLaunchers()
  1854. {
  1855. Json::Value launchers;
  1856. bool allow =
  1857. this->GT->Makefile->GetDefinition("CMAKE_CROSSCOMPILING").IsOn();
  1858. Json::Value launcher;
  1859. if (allow) {
  1860. launcher = DumpLauncher("CROSSCOMPILING_EMULATOR", "emulator");
  1861. if (!launcher.empty()) {
  1862. launchers.append(std::move(launcher));
  1863. }
  1864. }
  1865. return launchers;
  1866. }
  1867. }
  1868. Json::Value cmFileAPICodemodelDump(cmFileAPI& fileAPI, unsigned long version)
  1869. {
  1870. Codemodel codemodel(fileAPI, version);
  1871. return codemodel.Dump();
  1872. }