cmGraphVizWriter.cxx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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 "cmGraphVizWriter.h"
  4. #include <algorithm>
  5. #include <cctype>
  6. #include <iostream>
  7. #include <memory>
  8. #include <set>
  9. #include <utility>
  10. #include <cm/memory>
  11. #include "cmGeneratedFileStream.h"
  12. #include "cmGeneratorTarget.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmLinkItem.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmProperty.h"
  18. #include "cmState.h"
  19. #include "cmStateSnapshot.h"
  20. #include "cmStringAlgorithms.h"
  21. #include "cmSystemTools.h"
  22. #include "cmake.h"
  23. namespace {
  24. char const* const GRAPHVIZ_EDGE_STYLE_PUBLIC = "solid";
  25. char const* const GRAPHVIZ_EDGE_STYLE_INTERFACE = "dashed";
  26. char const* const GRAPHVIZ_EDGE_STYLE_PRIVATE = "dotted";
  27. char const* const GRAPHVIZ_NODE_SHAPE_EXECUTABLE = "egg"; // egg-xecutable
  28. // Normal libraries.
  29. char const* const GRAPHVIZ_NODE_SHAPE_LIBRARY_STATIC = "octagon";
  30. char const* const GRAPHVIZ_NODE_SHAPE_LIBRARY_SHARED = "doubleoctagon";
  31. char const* const GRAPHVIZ_NODE_SHAPE_LIBRARY_MODULE = "tripleoctagon";
  32. char const* const GRAPHVIZ_NODE_SHAPE_LIBRARY_INTERFACE = "pentagon";
  33. char const* const GRAPHVIZ_NODE_SHAPE_LIBRARY_OBJECT = "hexagon";
  34. char const* const GRAPHVIZ_NODE_SHAPE_LIBRARY_UNKNOWN = "septagon";
  35. char const* const GRAPHVIZ_NODE_SHAPE_UTILITY = "box";
  36. const char* getShapeForTarget(const cmLinkItem& item)
  37. {
  38. if (item.Target == nullptr) {
  39. return GRAPHVIZ_NODE_SHAPE_LIBRARY_UNKNOWN;
  40. }
  41. switch (item.Target->GetType()) {
  42. case cmStateEnums::EXECUTABLE:
  43. return GRAPHVIZ_NODE_SHAPE_EXECUTABLE;
  44. case cmStateEnums::STATIC_LIBRARY:
  45. return GRAPHVIZ_NODE_SHAPE_LIBRARY_STATIC;
  46. case cmStateEnums::SHARED_LIBRARY:
  47. return GRAPHVIZ_NODE_SHAPE_LIBRARY_SHARED;
  48. case cmStateEnums::MODULE_LIBRARY:
  49. return GRAPHVIZ_NODE_SHAPE_LIBRARY_MODULE;
  50. case cmStateEnums::OBJECT_LIBRARY:
  51. return GRAPHVIZ_NODE_SHAPE_LIBRARY_OBJECT;
  52. case cmStateEnums::UTILITY:
  53. return GRAPHVIZ_NODE_SHAPE_UTILITY;
  54. case cmStateEnums::INTERFACE_LIBRARY:
  55. return GRAPHVIZ_NODE_SHAPE_LIBRARY_INTERFACE;
  56. case cmStateEnums::UNKNOWN_LIBRARY:
  57. default:
  58. return GRAPHVIZ_NODE_SHAPE_LIBRARY_UNKNOWN;
  59. }
  60. }
  61. struct DependeesDir
  62. {
  63. template <typename T>
  64. static const cmLinkItem& src(const T& con)
  65. {
  66. return con.src;
  67. }
  68. template <typename T>
  69. static const cmLinkItem& dst(const T& con)
  70. {
  71. return con.dst;
  72. }
  73. };
  74. struct DependersDir
  75. {
  76. template <typename T>
  77. static const cmLinkItem& src(const T& con)
  78. {
  79. return con.dst;
  80. }
  81. template <typename T>
  82. static const cmLinkItem& dst(const T& con)
  83. {
  84. return con.src;
  85. }
  86. };
  87. }
  88. cmGraphVizWriter::cmGraphVizWriter(std::string const& fileName,
  89. const cmGlobalGenerator* globalGenerator)
  90. : FileName(fileName)
  91. , GlobalFileStream(fileName)
  92. , GraphName(globalGenerator->GetSafeGlobalSetting("CMAKE_PROJECT_NAME"))
  93. , GraphHeader("node [\n fontsize = \"12\"\n];")
  94. , GraphNodePrefix("node")
  95. , GlobalGenerator(globalGenerator)
  96. , NextNodeId(0)
  97. , GenerateForExecutables(true)
  98. , GenerateForStaticLibs(true)
  99. , GenerateForSharedLibs(true)
  100. , GenerateForModuleLibs(true)
  101. , GenerateForInterfaceLibs(true)
  102. , GenerateForObjectLibs(true)
  103. , GenerateForUnknownLibs(true)
  104. , GenerateForCustomTargets(false)
  105. , GenerateForExternals(true)
  106. , GeneratePerTarget(true)
  107. , GenerateDependers(true)
  108. {
  109. }
  110. cmGraphVizWriter::~cmGraphVizWriter()
  111. {
  112. this->WriteFooter(this->GlobalFileStream);
  113. }
  114. void cmGraphVizWriter::VisitGraph(std::string const&)
  115. {
  116. this->WriteHeader(GlobalFileStream, this->GraphName);
  117. this->WriteLegend(GlobalFileStream);
  118. }
  119. void cmGraphVizWriter::OnItem(cmLinkItem const& item)
  120. {
  121. if (this->ItemExcluded(item)) {
  122. return;
  123. }
  124. NodeNames[item.AsStr()] = cmStrCat(GraphNodePrefix, NextNodeId);
  125. ++NextNodeId;
  126. this->WriteNode(this->GlobalFileStream, item);
  127. }
  128. std::unique_ptr<cmGeneratedFileStream> cmGraphVizWriter::CreateTargetFile(
  129. cmLinkItem const& item, std::string const& fileNameSuffix)
  130. {
  131. auto const pathSafeItemName = PathSafeString(item.AsStr());
  132. auto const perTargetFileName =
  133. cmStrCat(this->FileName, '.', pathSafeItemName, fileNameSuffix);
  134. auto perTargetFileStream =
  135. cm::make_unique<cmGeneratedFileStream>(perTargetFileName);
  136. this->WriteHeader(*perTargetFileStream, item.AsStr());
  137. this->WriteNode(*perTargetFileStream, item);
  138. return perTargetFileStream;
  139. }
  140. void cmGraphVizWriter::OnDirectLink(cmLinkItem const& depender,
  141. cmLinkItem const& dependee,
  142. DependencyType dt)
  143. {
  144. this->VisitLink(depender, dependee, true, GetEdgeStyle(dt));
  145. }
  146. void cmGraphVizWriter::OnIndirectLink(cmLinkItem const& depender,
  147. cmLinkItem const& dependee)
  148. {
  149. this->VisitLink(depender, dependee, false);
  150. }
  151. void cmGraphVizWriter::VisitLink(cmLinkItem const& depender,
  152. cmLinkItem const& dependee, bool isDirectLink,
  153. std::string const& scopeType)
  154. {
  155. if (this->ItemExcluded(depender) || this->ItemExcluded(dependee)) {
  156. return;
  157. }
  158. if (!isDirectLink) {
  159. return;
  160. }
  161. // write global data directly
  162. this->WriteConnection(this->GlobalFileStream, depender, dependee, scopeType);
  163. if (this->GeneratePerTarget) {
  164. PerTargetConnections[depender].emplace_back(depender, dependee, scopeType);
  165. }
  166. if (this->GenerateDependers) {
  167. TargetDependersConnections[dependee].emplace_back(dependee, depender,
  168. scopeType);
  169. }
  170. }
  171. void cmGraphVizWriter::ReadSettings(
  172. const std::string& settingsFileName,
  173. const std::string& fallbackSettingsFileName)
  174. {
  175. cmake cm(cmake::RoleScript, cmState::Unknown);
  176. cm.SetHomeDirectory("");
  177. cm.SetHomeOutputDirectory("");
  178. cm.GetCurrentSnapshot().SetDefaultDefinitions();
  179. cmGlobalGenerator ggi(&cm);
  180. cmMakefile mf(&ggi, cm.GetCurrentSnapshot());
  181. std::unique_ptr<cmLocalGenerator> lg(ggi.CreateLocalGenerator(&mf));
  182. std::string inFileName = settingsFileName;
  183. if (!cmSystemTools::FileExists(inFileName)) {
  184. inFileName = fallbackSettingsFileName;
  185. if (!cmSystemTools::FileExists(inFileName)) {
  186. return;
  187. }
  188. }
  189. if (!mf.ReadListFile(inFileName)) {
  190. cmSystemTools::Error("Problem opening GraphViz options file: " +
  191. inFileName);
  192. return;
  193. }
  194. std::cout << "Reading GraphViz options file: " << inFileName << std::endl;
  195. #define __set_if_set(var, cmakeDefinition) \
  196. do { \
  197. cmProp value = mf.GetDefinition(cmakeDefinition); \
  198. if (value) { \
  199. (var) = *value; \
  200. } \
  201. } while (false)
  202. __set_if_set(this->GraphName, "GRAPHVIZ_GRAPH_NAME");
  203. __set_if_set(this->GraphHeader, "GRAPHVIZ_GRAPH_HEADER");
  204. __set_if_set(this->GraphNodePrefix, "GRAPHVIZ_NODE_PREFIX");
  205. #define __set_bool_if_set(var, cmakeDefinition) \
  206. do { \
  207. cmProp value = mf.GetDefinition(cmakeDefinition); \
  208. if (value) { \
  209. (var) = cmIsOn(*value); \
  210. } \
  211. } while (false)
  212. __set_bool_if_set(this->GenerateForExecutables, "GRAPHVIZ_EXECUTABLES");
  213. __set_bool_if_set(this->GenerateForStaticLibs, "GRAPHVIZ_STATIC_LIBS");
  214. __set_bool_if_set(this->GenerateForSharedLibs, "GRAPHVIZ_SHARED_LIBS");
  215. __set_bool_if_set(this->GenerateForModuleLibs, "GRAPHVIZ_MODULE_LIBS");
  216. __set_bool_if_set(this->GenerateForInterfaceLibs, "GRAPHVIZ_INTERFACE_LIBS");
  217. __set_bool_if_set(this->GenerateForObjectLibs, "GRAPHVIZ_OBJECT_LIBS");
  218. __set_bool_if_set(this->GenerateForUnknownLibs, "GRAPHVIZ_UNKNOWN_LIBS");
  219. __set_bool_if_set(this->GenerateForCustomTargets, "GRAPHVIZ_CUSTOM_TARGETS");
  220. __set_bool_if_set(this->GenerateForExternals, "GRAPHVIZ_EXTERNAL_LIBS");
  221. __set_bool_if_set(this->GeneratePerTarget, "GRAPHVIZ_GENERATE_PER_TARGET");
  222. __set_bool_if_set(this->GenerateDependers, "GRAPHVIZ_GENERATE_DEPENDERS");
  223. std::string ignoreTargetsRegexes;
  224. __set_if_set(ignoreTargetsRegexes, "GRAPHVIZ_IGNORE_TARGETS");
  225. this->TargetsToIgnoreRegex.clear();
  226. if (!ignoreTargetsRegexes.empty()) {
  227. std::vector<std::string> ignoreTargetsRegExVector =
  228. cmExpandedList(ignoreTargetsRegexes);
  229. for (std::string const& currentRegexString : ignoreTargetsRegExVector) {
  230. cmsys::RegularExpression currentRegex;
  231. if (!currentRegex.compile(currentRegexString)) {
  232. std::cerr << "Could not compile bad regex \"" << currentRegexString
  233. << "\"" << std::endl;
  234. }
  235. this->TargetsToIgnoreRegex.push_back(std::move(currentRegex));
  236. }
  237. }
  238. }
  239. void cmGraphVizWriter::Write()
  240. {
  241. auto gg = this->GlobalGenerator;
  242. this->VisitGraph(gg->GetName());
  243. // We want to traverse in a determined order, such that the output is always
  244. // the same for a given project (this makes tests reproducible, etc.)
  245. std::set<cmGeneratorTarget const*, cmGeneratorTarget::StrictTargetComparison>
  246. sortedGeneratorTargets;
  247. for (const auto& lg : gg->GetLocalGenerators()) {
  248. for (const auto& gt : lg->GetGeneratorTargets()) {
  249. // Reserved targets have inconsistent names across platforms (e.g. 'all'
  250. // vs. 'ALL_BUILD'), which can disrupt the traversal ordering.
  251. // We don't need or want them anyway.
  252. if (!cmGlobalGenerator::IsReservedTarget(gt->GetName())) {
  253. sortedGeneratorTargets.insert(gt.get());
  254. }
  255. }
  256. }
  257. // write global data and collect all connection data for per target graphs
  258. for (auto const gt : sortedGeneratorTargets) {
  259. auto item = cmLinkItem(gt, false, gt->GetBacktrace());
  260. this->VisitItem(item);
  261. }
  262. if (this->GeneratePerTarget) {
  263. WritePerTargetConnections<DependeesDir>(PerTargetConnections);
  264. }
  265. if (this->GenerateDependers) {
  266. WritePerTargetConnections<DependersDir>(TargetDependersConnections,
  267. ".dependers");
  268. }
  269. }
  270. void cmGraphVizWriter::FindAllConnections(const ConnectionsMap& connectionMap,
  271. const cmLinkItem& rootItem,
  272. Connections& extendedCons,
  273. std::set<cmLinkItem>& visitedItems)
  274. {
  275. // some "targets" are not in map, e.g. linker flags as -lm or
  276. // targets without dependency.
  277. // in both cases we are finished with traversing the graph
  278. if (connectionMap.find(rootItem) == connectionMap.cend()) {
  279. return;
  280. }
  281. const Connections& origCons = connectionMap.at(rootItem);
  282. for (const Connection& con : origCons) {
  283. extendedCons.emplace_back(con);
  284. const cmLinkItem& dstItem = con.dst;
  285. bool const visited = visitedItems.find(dstItem) != visitedItems.cend();
  286. if (!visited) {
  287. visitedItems.insert(dstItem);
  288. FindAllConnections(connectionMap, dstItem, extendedCons, visitedItems);
  289. }
  290. }
  291. }
  292. void cmGraphVizWriter::FindAllConnections(const ConnectionsMap& connectionMap,
  293. const cmLinkItem& rootItem,
  294. Connections& extendedCons)
  295. {
  296. std::set<cmLinkItem> visitedItems = { rootItem };
  297. FindAllConnections(connectionMap, rootItem, extendedCons, visitedItems);
  298. }
  299. template <typename DirFunc>
  300. void cmGraphVizWriter::WritePerTargetConnections(
  301. const ConnectionsMap& connections, const std::string& fileNameSuffix)
  302. {
  303. // the per target connections must be extended by indirect dependencies
  304. ConnectionsMap extendedConnections;
  305. for (auto const& conPerTarget : connections) {
  306. const cmLinkItem& rootItem = conPerTarget.first;
  307. Connections& extendedCons = extendedConnections[conPerTarget.first];
  308. FindAllConnections(connections, rootItem, extendedCons);
  309. }
  310. for (auto const& conPerTarget : extendedConnections) {
  311. const cmLinkItem& rootItem = conPerTarget.first;
  312. // some of the nodes are excluded completely and are not written
  313. if (this->ItemExcluded(rootItem)) {
  314. continue;
  315. }
  316. const Connections& cons = conPerTarget.second;
  317. std::unique_ptr<cmGeneratedFileStream> fileStream =
  318. this->CreateTargetFile(rootItem, fileNameSuffix);
  319. for (const Connection& con : cons) {
  320. const cmLinkItem& src = DirFunc::src(con);
  321. const cmLinkItem& dst = DirFunc::dst(con);
  322. this->WriteNode(*fileStream, con.dst);
  323. this->WriteConnection(*fileStream, src, dst, con.scopeType);
  324. }
  325. this->WriteFooter(*fileStream);
  326. }
  327. }
  328. void cmGraphVizWriter::WriteHeader(cmGeneratedFileStream& fs,
  329. const std::string& name)
  330. {
  331. auto const escapedGraphName = EscapeForDotFile(name);
  332. fs << "digraph \"" << escapedGraphName << "\" {\n"
  333. << this->GraphHeader << '\n';
  334. }
  335. void cmGraphVizWriter::WriteFooter(cmGeneratedFileStream& fs)
  336. {
  337. fs << "}\n";
  338. }
  339. void cmGraphVizWriter::WriteLegend(cmGeneratedFileStream& fs)
  340. {
  341. // Note that the subgraph name must start with "cluster", as done here, to
  342. // make Graphviz layout engines do the right thing and keep the nodes
  343. // together.
  344. /* clang-format off */
  345. fs << "subgraph clusterLegend {\n"
  346. " label = \"Legend\";\n"
  347. // Set the color of the box surrounding the legend.
  348. " color = black;\n"
  349. // We use invisible edges just to enforce the layout.
  350. " edge [ style = invis ];\n"
  351. // Nodes.
  352. " legendNode0 [ label = \"Executable\", shape = "
  353. << GRAPHVIZ_NODE_SHAPE_EXECUTABLE << " ];\n"
  354. " legendNode1 [ label = \"Static Library\", shape = "
  355. << GRAPHVIZ_NODE_SHAPE_LIBRARY_STATIC << " ];\n"
  356. " legendNode2 [ label = \"Shared Library\", shape = "
  357. << GRAPHVIZ_NODE_SHAPE_LIBRARY_SHARED << " ];\n"
  358. " legendNode3 [ label = \"Module Library\", shape = "
  359. << GRAPHVIZ_NODE_SHAPE_LIBRARY_MODULE << " ];\n"
  360. " legendNode4 [ label = \"Interface Library\", shape = "
  361. << GRAPHVIZ_NODE_SHAPE_LIBRARY_INTERFACE << " ];\n"
  362. " legendNode5 [ label = \"Object Library\", shape = "
  363. << GRAPHVIZ_NODE_SHAPE_LIBRARY_OBJECT << " ];\n"
  364. " legendNode6 [ label = \"Unknown Library\", shape = "
  365. << GRAPHVIZ_NODE_SHAPE_LIBRARY_UNKNOWN << " ];\n"
  366. " legendNode7 [ label = \"Custom Target\", shape = "
  367. << GRAPHVIZ_NODE_SHAPE_UTILITY << " ];\n"
  368. // Edges.
  369. // Some of those are dummy (invisible) edges to enforce a layout.
  370. " legendNode0 -> legendNode1 [ style = "
  371. << GRAPHVIZ_EDGE_STYLE_PUBLIC << " ];\n"
  372. " legendNode0 -> legendNode2 [ style = "
  373. << GRAPHVIZ_EDGE_STYLE_PUBLIC << " ];\n"
  374. " legendNode0 -> legendNode3;\n"
  375. " legendNode1 -> legendNode4 [ label = \"Interface\", style = "
  376. << GRAPHVIZ_EDGE_STYLE_INTERFACE << " ];\n"
  377. " legendNode2 -> legendNode5 [ label = \"Private\", style = "
  378. << GRAPHVIZ_EDGE_STYLE_PRIVATE << " ];\n"
  379. " legendNode3 -> legendNode6 [ style = "
  380. << GRAPHVIZ_EDGE_STYLE_PUBLIC << " ];\n"
  381. " legendNode0 -> legendNode7;\n"
  382. "}\n";
  383. /* clang-format off */
  384. }
  385. void cmGraphVizWriter::WriteNode(cmGeneratedFileStream& fs,
  386. cmLinkItem const& item)
  387. {
  388. auto const& itemName = item.AsStr();
  389. auto const& nodeName = this->NodeNames[itemName];
  390. auto const itemNameWithAliases = ItemNameWithAliases(itemName);
  391. auto const escapedLabel = EscapeForDotFile(itemNameWithAliases);
  392. fs << " \"" << nodeName << "\" [ label = \"" << escapedLabel
  393. << "\", shape = " << getShapeForTarget(item) << " ];\n";
  394. }
  395. void cmGraphVizWriter::WriteConnection(cmGeneratedFileStream& fs,
  396. cmLinkItem const& depender,
  397. cmLinkItem const& dependee,
  398. std::string const& edgeStyle)
  399. {
  400. auto const& dependerName = depender.AsStr();
  401. auto const& dependeeName = dependee.AsStr();
  402. fs << " \"" << this->NodeNames[dependerName] << "\" -> \""
  403. << this->NodeNames[dependeeName] << "\" "
  404. << edgeStyle
  405. << " // " << dependerName << " -> " << dependeeName << '\n';
  406. }
  407. bool cmGraphVizWriter::ItemExcluded(cmLinkItem const& item)
  408. {
  409. auto const itemName = item.AsStr();
  410. if (this->ItemNameFilteredOut(itemName)) {
  411. return true;
  412. }
  413. if (item.Target == nullptr) {
  414. return !this->GenerateForExternals;
  415. }
  416. if (item.Target->GetType() == cmStateEnums::UTILITY) {
  417. if (cmHasLiteralPrefix(itemName, "Nightly") ||
  418. cmHasLiteralPrefix(itemName, "Continuous") ||
  419. cmHasLiteralPrefix(itemName, "Experimental")) {
  420. return true;
  421. }
  422. }
  423. if (item.Target->IsImported() && !this->GenerateForExternals) {
  424. return true;
  425. }
  426. return !this->TargetTypeEnabled(item.Target->GetType());
  427. }
  428. bool cmGraphVizWriter::ItemNameFilteredOut(std::string const& itemName)
  429. {
  430. if (itemName == ">") {
  431. // FIXME: why do we even receive such a target here?
  432. return true;
  433. }
  434. if (cmGlobalGenerator::IsReservedTarget(itemName)) {
  435. return true;
  436. }
  437. for (cmsys::RegularExpression& regEx : this->TargetsToIgnoreRegex) {
  438. if (regEx.is_valid()) {
  439. if (regEx.find(itemName)) {
  440. return true;
  441. }
  442. }
  443. }
  444. return false;
  445. }
  446. bool cmGraphVizWriter::TargetTypeEnabled(
  447. cmStateEnums::TargetType targetType) const
  448. {
  449. switch (targetType) {
  450. case cmStateEnums::EXECUTABLE:
  451. return this->GenerateForExecutables;
  452. case cmStateEnums::STATIC_LIBRARY:
  453. return this->GenerateForStaticLibs;
  454. case cmStateEnums::SHARED_LIBRARY:
  455. return this->GenerateForSharedLibs;
  456. case cmStateEnums::MODULE_LIBRARY:
  457. return this->GenerateForModuleLibs;
  458. case cmStateEnums::INTERFACE_LIBRARY:
  459. return this->GenerateForInterfaceLibs;
  460. case cmStateEnums::OBJECT_LIBRARY:
  461. return this->GenerateForObjectLibs;
  462. case cmStateEnums::UNKNOWN_LIBRARY:
  463. return this->GenerateForUnknownLibs;
  464. case cmStateEnums::UTILITY:
  465. return this->GenerateForCustomTargets;
  466. case cmStateEnums::GLOBAL_TARGET:
  467. // Built-in targets like edit_cache, etc.
  468. // We don't need/want those in the dot file.
  469. return false;
  470. default:
  471. break;
  472. }
  473. return false;
  474. }
  475. std::string cmGraphVizWriter::ItemNameWithAliases(
  476. std::string const& itemName) const
  477. {
  478. std::vector<std::string> items;
  479. for (auto const& lg : this->GlobalGenerator->GetLocalGenerators()) {
  480. for (auto const& aliasTargets : lg->GetMakefile()->GetAliasTargets()) {
  481. if (aliasTargets.second == itemName) {
  482. items.push_back(aliasTargets.first);
  483. }
  484. }
  485. }
  486. std::sort(items.begin(), items.end());
  487. items.erase(std::unique(items.begin(), items.end()), items.end());
  488. auto nameWithAliases = itemName;
  489. for(auto const& item : items) {
  490. nameWithAliases += "\\n(" + item + ")";
  491. }
  492. return nameWithAliases;
  493. }
  494. std::string cmGraphVizWriter::GetEdgeStyle(DependencyType dt)
  495. {
  496. std::string style;
  497. switch (dt) {
  498. case DependencyType::LinkPrivate:
  499. style = "[ style = " + std::string(GRAPHVIZ_EDGE_STYLE_PRIVATE) + " ]";
  500. break;
  501. case DependencyType::LinkInterface:
  502. style = "[ style = " + std::string(GRAPHVIZ_EDGE_STYLE_INTERFACE) + " ]";
  503. break;
  504. default:
  505. break;
  506. }
  507. return style;
  508. }
  509. std::string cmGraphVizWriter::EscapeForDotFile(std::string const& str)
  510. {
  511. return cmSystemTools::EscapeChars(str.data(), "\"");
  512. }
  513. std::string cmGraphVizWriter::PathSafeString(std::string const& str)
  514. {
  515. std::string pathSafeStr;
  516. // We'll only keep alphanumerical characters, plus the following ones that
  517. // are common, and safe on all platforms:
  518. auto const extra_chars = std::set<char>{ '.', '-', '_' };
  519. for (char c : str) {
  520. if (std::isalnum(c) || extra_chars.find(c) != extra_chars.cend()) {
  521. pathSafeStr += c;
  522. }
  523. }
  524. return pathSafeStr;
  525. }