cmGraphVizWriter.cxx 20 KB

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