cmGraphVizWriter.cxx 20 KB

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