cmFileAPICodemodel.cxx 44 KB

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