cmGraphVizWriter.cxx 20 KB

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