cmFileAPICodemodel.cxx 46 KB

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