cmGraphVizWriter.cxx 20 KB

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