cmGraphVizWriter.cxx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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 == nullptr) {
  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. sortedGeneratorTargets.insert(gt.get());
  244. }
  245. }
  246. }
  247. // write global data and collect all connection data for per target graphs
  248. for (const auto* const gt : sortedGeneratorTargets) {
  249. auto item = cmLinkItem(gt, false, gt->GetBacktrace());
  250. this->VisitItem(item);
  251. }
  252. if (this->GeneratePerTarget) {
  253. this->WritePerTargetConnections<DependeesDir>(this->PerTargetConnections);
  254. }
  255. if (this->GenerateDependers) {
  256. this->WritePerTargetConnections<DependersDir>(
  257. this->TargetDependersConnections, ".dependers");
  258. }
  259. }
  260. void cmGraphVizWriter::FindAllConnections(const ConnectionsMap& connectionMap,
  261. const cmLinkItem& rootItem,
  262. Connections& extendedCons,
  263. std::set<cmLinkItem>& visitedItems)
  264. {
  265. // some "targets" are not in map, e.g. linker flags as -lm or
  266. // targets without dependency.
  267. // in both cases we are finished with traversing the graph
  268. if (connectionMap.find(rootItem) == connectionMap.cend()) {
  269. return;
  270. }
  271. const Connections& origCons = connectionMap.at(rootItem);
  272. for (const Connection& con : origCons) {
  273. extendedCons.emplace_back(con);
  274. const cmLinkItem& dstItem = con.dst;
  275. bool const visited = visitedItems.find(dstItem) != visitedItems.cend();
  276. if (!visited) {
  277. visitedItems.insert(dstItem);
  278. this->FindAllConnections(connectionMap, dstItem, extendedCons,
  279. visitedItems);
  280. }
  281. }
  282. }
  283. void cmGraphVizWriter::FindAllConnections(const ConnectionsMap& connectionMap,
  284. const cmLinkItem& rootItem,
  285. Connections& extendedCons)
  286. {
  287. std::set<cmLinkItem> visitedItems = { rootItem };
  288. this->FindAllConnections(connectionMap, rootItem, extendedCons,
  289. visitedItems);
  290. }
  291. template <typename DirFunc>
  292. void cmGraphVizWriter::WritePerTargetConnections(
  293. const ConnectionsMap& connections, const std::string& fileNameSuffix)
  294. {
  295. // the per target connections must be extended by indirect dependencies
  296. ConnectionsMap extendedConnections;
  297. for (auto const& conPerTarget : connections) {
  298. const cmLinkItem& rootItem = conPerTarget.first;
  299. Connections& extendedCons = extendedConnections[conPerTarget.first];
  300. this->FindAllConnections(connections, rootItem, extendedCons);
  301. }
  302. for (auto const& conPerTarget : extendedConnections) {
  303. const cmLinkItem& rootItem = conPerTarget.first;
  304. // some of the nodes are excluded completely and are not written
  305. if (this->ItemExcluded(rootItem)) {
  306. continue;
  307. }
  308. const Connections& cons = conPerTarget.second;
  309. std::unique_ptr<cmGeneratedFileStream> fileStream =
  310. this->CreateTargetFile(rootItem, fileNameSuffix);
  311. for (const Connection& con : cons) {
  312. const cmLinkItem& src = DirFunc::src(con);
  313. const cmLinkItem& dst = DirFunc::dst(con);
  314. this->WriteNode(*fileStream, con.dst);
  315. this->WriteConnection(*fileStream, src, dst, con.scopeType);
  316. }
  317. this->WriteFooter(*fileStream);
  318. }
  319. }
  320. void cmGraphVizWriter::WriteHeader(cmGeneratedFileStream& fs,
  321. const std::string& name)
  322. {
  323. auto const escapedGraphName = EscapeForDotFile(name);
  324. fs << "digraph \"" << escapedGraphName << "\" {\n"
  325. << this->GraphHeader << '\n';
  326. }
  327. void cmGraphVizWriter::WriteFooter(cmGeneratedFileStream& fs)
  328. {
  329. fs << "}\n";
  330. }
  331. void cmGraphVizWriter::WriteLegend(cmGeneratedFileStream& fs)
  332. {
  333. // Note that the subgraph name must start with "cluster", as done here, to
  334. // make Graphviz layout engines do the right thing and keep the nodes
  335. // together.
  336. /* clang-format off */
  337. fs << "subgraph clusterLegend {\n"
  338. " label = \"Legend\";\n"
  339. // Set the color of the box surrounding the legend.
  340. " color = black;\n"
  341. // We use invisible edges just to enforce the layout.
  342. " edge [ style = invis ];\n"
  343. // Nodes.
  344. " legendNode0 [ label = \"Executable\", shape = "
  345. << GRAPHVIZ_NODE_SHAPE_EXECUTABLE << " ];\n"
  346. " legendNode1 [ label = \"Static Library\", shape = "
  347. << GRAPHVIZ_NODE_SHAPE_LIBRARY_STATIC << " ];\n"
  348. " legendNode2 [ label = \"Shared Library\", shape = "
  349. << GRAPHVIZ_NODE_SHAPE_LIBRARY_SHARED << " ];\n"
  350. " legendNode3 [ label = \"Module Library\", shape = "
  351. << GRAPHVIZ_NODE_SHAPE_LIBRARY_MODULE << " ];\n"
  352. " legendNode4 [ label = \"Interface Library\", shape = "
  353. << GRAPHVIZ_NODE_SHAPE_LIBRARY_INTERFACE << " ];\n"
  354. " legendNode5 [ label = \"Object Library\", shape = "
  355. << GRAPHVIZ_NODE_SHAPE_LIBRARY_OBJECT << " ];\n"
  356. " legendNode6 [ label = \"Unknown Library\", shape = "
  357. << GRAPHVIZ_NODE_SHAPE_LIBRARY_UNKNOWN << " ];\n"
  358. " legendNode7 [ label = \"Custom Target\", shape = "
  359. << GRAPHVIZ_NODE_SHAPE_UTILITY << " ];\n"
  360. // Edges.
  361. // Some of those are dummy (invisible) edges to enforce a layout.
  362. " legendNode0 -> legendNode1 [ style = "
  363. << GRAPHVIZ_EDGE_STYLE_PUBLIC << " ];\n"
  364. " legendNode0 -> legendNode2 [ style = "
  365. << GRAPHVIZ_EDGE_STYLE_PUBLIC << " ];\n"
  366. " legendNode0 -> legendNode3;\n"
  367. " legendNode1 -> legendNode4 [ label = \"Interface\", style = "
  368. << GRAPHVIZ_EDGE_STYLE_INTERFACE << " ];\n"
  369. " legendNode2 -> legendNode5 [ label = \"Private\", style = "
  370. << GRAPHVIZ_EDGE_STYLE_PRIVATE << " ];\n"
  371. " legendNode3 -> legendNode6 [ style = "
  372. << GRAPHVIZ_EDGE_STYLE_PUBLIC << " ];\n"
  373. " legendNode0 -> legendNode7;\n"
  374. "}\n";
  375. /* clang-format off */
  376. }
  377. void cmGraphVizWriter::WriteNode(cmGeneratedFileStream& fs,
  378. cmLinkItem const& item)
  379. {
  380. auto const& itemName = item.AsStr();
  381. auto const& nodeName = this->NodeNames[itemName];
  382. auto const itemNameWithAliases = this->ItemNameWithAliases(itemName);
  383. auto const escapedLabel = EscapeForDotFile(itemNameWithAliases);
  384. fs << " \"" << nodeName << "\" [ label = \"" << escapedLabel
  385. << "\", shape = " << getShapeForTarget(item) << " ];\n";
  386. }
  387. void cmGraphVizWriter::WriteConnection(cmGeneratedFileStream& fs,
  388. cmLinkItem const& depender,
  389. cmLinkItem const& dependee,
  390. std::string const& edgeStyle)
  391. {
  392. auto const& dependerName = depender.AsStr();
  393. auto const& dependeeName = dependee.AsStr();
  394. fs << " \"" << this->NodeNames[dependerName] << "\" -> \""
  395. << this->NodeNames[dependeeName] << "\" "
  396. << edgeStyle
  397. << " // " << dependerName << " -> " << dependeeName << '\n';
  398. }
  399. bool cmGraphVizWriter::ItemExcluded(cmLinkItem const& item)
  400. {
  401. auto const itemName = item.AsStr();
  402. if (this->ItemNameFilteredOut(itemName)) {
  403. return true;
  404. }
  405. if (item.Target == nullptr) {
  406. return !this->GenerateForExternals;
  407. }
  408. if (item.Target->GetType() == cmStateEnums::UTILITY) {
  409. if (cmHasLiteralPrefix(itemName, "Nightly") ||
  410. cmHasLiteralPrefix(itemName, "Continuous") ||
  411. cmHasLiteralPrefix(itemName, "Experimental")) {
  412. return true;
  413. }
  414. }
  415. if (item.Target->IsImported() && !this->GenerateForExternals) {
  416. return true;
  417. }
  418. return !this->TargetTypeEnabled(item.Target->GetType());
  419. }
  420. bool cmGraphVizWriter::ItemNameFilteredOut(std::string const& itemName)
  421. {
  422. if (itemName == ">") {
  423. // FIXME: why do we even receive such a target here?
  424. return true;
  425. }
  426. if (cmGlobalGenerator::IsReservedTarget(itemName)) {
  427. return true;
  428. }
  429. for (cmsys::RegularExpression& regEx : this->TargetsToIgnoreRegex) {
  430. if (regEx.is_valid()) {
  431. if (regEx.find(itemName)) {
  432. return true;
  433. }
  434. }
  435. }
  436. return false;
  437. }
  438. bool cmGraphVizWriter::TargetTypeEnabled(
  439. cmStateEnums::TargetType targetType) const
  440. {
  441. switch (targetType) {
  442. case cmStateEnums::EXECUTABLE:
  443. return this->GenerateForExecutables;
  444. case cmStateEnums::STATIC_LIBRARY:
  445. return this->GenerateForStaticLibs;
  446. case cmStateEnums::SHARED_LIBRARY:
  447. return this->GenerateForSharedLibs;
  448. case cmStateEnums::MODULE_LIBRARY:
  449. return this->GenerateForModuleLibs;
  450. case cmStateEnums::INTERFACE_LIBRARY:
  451. return this->GenerateForInterfaceLibs;
  452. case cmStateEnums::OBJECT_LIBRARY:
  453. return this->GenerateForObjectLibs;
  454. case cmStateEnums::UNKNOWN_LIBRARY:
  455. return this->GenerateForUnknownLibs;
  456. case cmStateEnums::UTILITY:
  457. return this->GenerateForCustomTargets;
  458. case cmStateEnums::GLOBAL_TARGET:
  459. // Built-in targets like edit_cache, etc.
  460. // We don't need/want those in the dot file.
  461. return false;
  462. default:
  463. break;
  464. }
  465. return false;
  466. }
  467. std::string cmGraphVizWriter::ItemNameWithAliases(
  468. std::string const& itemName) const
  469. {
  470. std::vector<std::string> items;
  471. for (auto const& lg : this->GlobalGenerator->GetLocalGenerators()) {
  472. for (auto const& aliasTargets : lg->GetMakefile()->GetAliasTargets()) {
  473. if (aliasTargets.second == itemName) {
  474. items.push_back(aliasTargets.first);
  475. }
  476. }
  477. }
  478. std::sort(items.begin(), items.end());
  479. items.erase(std::unique(items.begin(), items.end()), items.end());
  480. auto nameWithAliases = itemName;
  481. for(auto const& item : items) {
  482. nameWithAliases += "\\n(" + item + ")";
  483. }
  484. return nameWithAliases;
  485. }
  486. std::string cmGraphVizWriter::GetEdgeStyle(DependencyType dt)
  487. {
  488. std::string style;
  489. switch (dt) {
  490. case DependencyType::LinkPrivate:
  491. style = "[ style = " + std::string(GRAPHVIZ_EDGE_STYLE_PRIVATE) + " ]";
  492. break;
  493. case DependencyType::LinkInterface:
  494. style = "[ style = " + std::string(GRAPHVIZ_EDGE_STYLE_INTERFACE) + " ]";
  495. break;
  496. default:
  497. break;
  498. }
  499. return style;
  500. }
  501. std::string cmGraphVizWriter::EscapeForDotFile(std::string const& str)
  502. {
  503. return cmSystemTools::EscapeChars(str.data(), "\"");
  504. }
  505. std::string cmGraphVizWriter::PathSafeString(std::string const& str)
  506. {
  507. std::string pathSafeStr;
  508. // We'll only keep alphanumerical characters, plus the following ones that
  509. // are common, and safe on all platforms:
  510. auto const extra_chars = std::set<char>{ '.', '-', '_' };
  511. for (char c : str) {
  512. if (std::isalnum(c) || extra_chars.find(c) != extra_chars.cend()) {
  513. pathSafeStr += c;
  514. }
  515. }
  516. return pathSafeStr;
  517. }