cmFileAPICodemodel.cxx 58 KB

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