cmFileAPICodemodel.cxx 44 KB

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