cmFileAPICodemodel.cxx 46 KB

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