cmake/Source/cmGraphVizWriter.cxx

606 lines
20 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2011-01-16 11:35:12 +01:00
#include "cmGraphVizWriter.h"
2021-09-14 00:13:48 +02:00
#include <algorithm>
2020-08-30 11:54:41 +02:00
#include <cctype>
2017-04-14 19:02:05 +02:00
#include <iostream>
2020-02-01 23:06:01 +01:00
#include <memory>
2020-08-30 11:54:41 +02:00
#include <set>
2017-04-14 19:02:05 +02:00
#include <utility>
2020-08-30 11:54:41 +02:00
#include <cm/memory>
2016-07-09 11:21:54 +02:00
#include "cmGeneratedFileStream.h"
2016-10-30 18:24:19 +01:00
#include "cmGeneratorTarget.h"
2016-07-09 11:21:54 +02:00
#include "cmGlobalGenerator.h"
2020-08-30 11:54:41 +02:00
#include "cmLinkItem.h"
2016-07-09 11:21:54 +02:00
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
2019-11-11 23:01:05 +01:00
#include "cmState.h"
2017-04-14 19:02:05 +02:00
#include "cmStateSnapshot.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2016-10-30 18:24:19 +01:00
#include "cmSystemTools.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
2016-10-30 18:24:19 +01:00
#include "cmake.h"
2018-01-26 17:06:56 +01:00
namespace {
2020-08-30 11:54:41 +02:00
char const* const GRAPHVIZ_EDGE_STYLE_PUBLIC = "solid";
char const* const GRAPHVIZ_EDGE_STYLE_INTERFACE = "dashed";
char const* const GRAPHVIZ_EDGE_STYLE_PRIVATE = "dotted";
2018-01-26 17:06:56 +01:00
2020-08-30 11:54:41 +02:00
char const* const GRAPHVIZ_NODE_SHAPE_EXECUTABLE = "egg"; // egg-xecutable
// Normal libraries.
char const* const GRAPHVIZ_NODE_SHAPE_LIBRARY_STATIC = "octagon";
char const* const GRAPHVIZ_NODE_SHAPE_LIBRARY_SHARED = "doubleoctagon";
char const* const GRAPHVIZ_NODE_SHAPE_LIBRARY_MODULE = "tripleoctagon";
2018-01-26 17:06:56 +01:00
2020-08-30 11:54:41 +02:00
char const* const GRAPHVIZ_NODE_SHAPE_LIBRARY_INTERFACE = "pentagon";
char const* const GRAPHVIZ_NODE_SHAPE_LIBRARY_OBJECT = "hexagon";
char const* const GRAPHVIZ_NODE_SHAPE_LIBRARY_UNKNOWN = "septagon";
char const* const GRAPHVIZ_NODE_SHAPE_UTILITY = "box";
const char* getShapeForTarget(const cmLinkItem& item)
2011-01-16 11:35:12 +01:00
{
2020-08-30 11:54:41 +02:00
if (item.Target == nullptr) {
return GRAPHVIZ_NODE_SHAPE_LIBRARY_UNKNOWN;
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
2020-08-30 11:54:41 +02:00
switch (item.Target->GetType()) {
2017-04-14 19:02:05 +02:00
case cmStateEnums::EXECUTABLE:
2020-08-30 11:54:41 +02:00
return GRAPHVIZ_NODE_SHAPE_EXECUTABLE;
2017-04-14 19:02:05 +02:00
case cmStateEnums::STATIC_LIBRARY:
2020-08-30 11:54:41 +02:00
return GRAPHVIZ_NODE_SHAPE_LIBRARY_STATIC;
2017-04-14 19:02:05 +02:00
case cmStateEnums::SHARED_LIBRARY:
2020-08-30 11:54:41 +02:00
return GRAPHVIZ_NODE_SHAPE_LIBRARY_SHARED;
2017-04-14 19:02:05 +02:00
case cmStateEnums::MODULE_LIBRARY:
2020-08-30 11:54:41 +02:00
return GRAPHVIZ_NODE_SHAPE_LIBRARY_MODULE;
case cmStateEnums::OBJECT_LIBRARY:
return GRAPHVIZ_NODE_SHAPE_LIBRARY_OBJECT;
case cmStateEnums::UTILITY:
return GRAPHVIZ_NODE_SHAPE_UTILITY;
case cmStateEnums::INTERFACE_LIBRARY:
return GRAPHVIZ_NODE_SHAPE_LIBRARY_INTERFACE;
case cmStateEnums::UNKNOWN_LIBRARY:
2011-01-16 11:35:12 +01:00
default:
2020-08-30 11:54:41 +02:00
return GRAPHVIZ_NODE_SHAPE_LIBRARY_UNKNOWN;
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
}
2020-08-30 11:54:41 +02:00
struct DependeesDir
2018-01-26 17:06:56 +01:00
{
2020-08-30 11:54:41 +02:00
template <typename T>
static const cmLinkItem& src(const T& con)
{
return con.src;
}
2018-01-26 17:06:56 +01:00
2020-08-30 11:54:41 +02:00
template <typename T>
static const cmLinkItem& dst(const T& con)
{
return con.dst;
}
};
2018-01-26 17:06:56 +01:00
2020-08-30 11:54:41 +02:00
struct DependersDir
{
template <typename T>
static const cmLinkItem& src(const T& con)
{
return con.dst;
2018-01-26 17:06:56 +01:00
}
2020-08-30 11:54:41 +02:00
template <typename T>
static const cmLinkItem& dst(const T& con)
{
return con.src;
}
};
}
2018-01-26 17:06:56 +01:00
2020-08-30 11:54:41 +02:00
cmGraphVizWriter::cmGraphVizWriter(std::string const& fileName,
const cmGlobalGenerator* globalGenerator)
: FileName(fileName)
, GlobalFileStream(fileName)
, GraphName(globalGenerator->GetSafeGlobalSetting("CMAKE_PROJECT_NAME"))
, GraphHeader("node [\n fontsize = \"12\"\n];")
, GraphNodePrefix("node")
, GlobalGenerator(globalGenerator)
{
}
2018-01-26 17:06:56 +01:00
2020-08-30 11:54:41 +02:00
cmGraphVizWriter::~cmGraphVizWriter()
{
this->WriteFooter(this->GlobalFileStream);
}
void cmGraphVizWriter::VisitGraph(std::string const&)
{
2021-09-14 00:13:48 +02:00
this->WriteHeader(this->GlobalFileStream, this->GraphName);
this->WriteLegend(this->GlobalFileStream);
2020-08-30 11:54:41 +02:00
}
2018-01-26 17:06:56 +01:00
2020-08-30 11:54:41 +02:00
void cmGraphVizWriter::OnItem(cmLinkItem const& item)
{
if (this->ItemExcluded(item)) {
return;
}
2018-01-26 17:06:56 +01:00
2021-09-14 00:13:48 +02:00
this->NodeNames[item.AsStr()] =
cmStrCat(this->GraphNodePrefix, this->NextNodeId);
++this->NextNodeId;
2018-01-26 17:06:56 +01:00
2020-08-30 11:54:41 +02:00
this->WriteNode(this->GlobalFileStream, item);
}
2021-09-14 00:13:48 +02:00
std::unique_ptr<cmGeneratedFileStream> cmGraphVizWriter::CreateTargetFile(
cmLinkItem const& item, std::string const& fileNameSuffix)
2020-08-30 11:54:41 +02:00
{
auto const pathSafeItemName = PathSafeString(item.AsStr());
auto const perTargetFileName =
cmStrCat(this->FileName, '.', pathSafeItemName, fileNameSuffix);
auto perTargetFileStream =
cm::make_unique<cmGeneratedFileStream>(perTargetFileName);
this->WriteHeader(*perTargetFileStream, item.AsStr());
this->WriteNode(*perTargetFileStream, item);
2018-01-26 17:06:56 +01:00
2021-09-14 00:13:48 +02:00
return perTargetFileStream;
2018-01-26 17:06:56 +01:00
}
2020-08-30 11:54:41 +02:00
void cmGraphVizWriter::OnDirectLink(cmLinkItem const& depender,
cmLinkItem const& dependee,
DependencyType dt)
{
this->VisitLink(depender, dependee, true, GetEdgeStyle(dt));
2018-01-26 17:06:56 +01:00
}
2020-08-30 11:54:41 +02:00
void cmGraphVizWriter::OnIndirectLink(cmLinkItem const& depender,
cmLinkItem const& dependee)
2011-01-16 11:35:12 +01:00
{
2020-08-30 11:54:41 +02:00
this->VisitLink(depender, dependee, false);
}
void cmGraphVizWriter::VisitLink(cmLinkItem const& depender,
cmLinkItem const& dependee, bool isDirectLink,
std::string const& scopeType)
{
if (this->ItemExcluded(depender) || this->ItemExcluded(dependee)) {
return;
}
if (!isDirectLink) {
return;
}
// write global data directly
this->WriteConnection(this->GlobalFileStream, depender, dependee, scopeType);
if (this->GeneratePerTarget) {
2021-09-14 00:13:48 +02:00
this->PerTargetConnections[depender].emplace_back(depender, dependee,
scopeType);
2020-08-30 11:54:41 +02:00
}
if (this->GenerateDependers) {
2021-09-14 00:13:48 +02:00
this->TargetDependersConnections[dependee].emplace_back(dependee, depender,
scopeType);
2020-08-30 11:54:41 +02:00
}
2011-01-16 11:35:12 +01:00
}
2019-11-11 23:01:05 +01:00
void cmGraphVizWriter::ReadSettings(
const std::string& settingsFileName,
const std::string& fallbackSettingsFileName)
2011-01-16 11:35:12 +01:00
{
2019-11-11 23:01:05 +01:00
cmake cm(cmake::RoleScript, cmState::Unknown);
2015-08-17 11:37:30 +02:00
cm.SetHomeDirectory("");
cm.SetHomeOutputDirectory("");
2016-03-13 13:35:51 +01:00
cm.GetCurrentSnapshot().SetDefaultDefinitions();
2015-08-17 11:37:30 +02:00
cmGlobalGenerator ggi(&cm);
2018-01-26 17:06:56 +01:00
cmMakefile mf(&ggi, cm.GetCurrentSnapshot());
std::unique_ptr<cmLocalGenerator> lg(ggi.CreateLocalGenerator(&mf));
2011-01-16 11:35:12 +01:00
2019-11-11 23:01:05 +01:00
std::string inFileName = settingsFileName;
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::FileExists(inFileName)) {
2011-01-16 11:35:12 +01:00
inFileName = fallbackSettingsFileName;
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::FileExists(inFileName)) {
2011-01-16 11:35:12 +01:00
return;
}
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
2018-01-26 17:06:56 +01:00
if (!mf.ReadListFile(inFileName)) {
2019-11-11 23:01:05 +01:00
cmSystemTools::Error("Problem opening GraphViz options file: " +
2011-01-16 11:35:12 +01:00
inFileName);
return;
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
std::cout << "Reading GraphViz options file: " << inFileName << std::endl;
2021-09-14 00:13:48 +02:00
#define set_if_set(var, cmakeDefinition) \
2019-11-11 23:01:05 +01:00
do { \
2021-11-20 13:41:27 +01:00
cmValue value = mf.GetDefinition(cmakeDefinition); \
2016-07-09 11:21:54 +02:00
if (value) { \
2021-09-14 00:13:48 +02:00
(var) = *value; \
2016-07-09 11:21:54 +02:00
} \
2019-11-11 23:01:05 +01:00
} while (false)
2011-01-16 11:35:12 +01:00
2021-09-14 00:13:48 +02:00
set_if_set(this->GraphName, "GRAPHVIZ_GRAPH_NAME");
set_if_set(this->GraphHeader, "GRAPHVIZ_GRAPH_HEADER");
set_if_set(this->GraphNodePrefix, "GRAPHVIZ_NODE_PREFIX");
2011-01-16 11:35:12 +01:00
2021-09-14 00:13:48 +02:00
#define set_bool_if_set(var, cmakeDefinition) \
2019-11-11 23:01:05 +01:00
do { \
2021-11-20 13:41:27 +01:00
cmValue value = mf.GetDefinition(cmakeDefinition); \
2016-07-09 11:21:54 +02:00
if (value) { \
2021-09-14 00:13:48 +02:00
(var) = cmIsOn(*value); \
2016-07-09 11:21:54 +02:00
} \
2019-11-11 23:01:05 +01:00
} while (false)
2011-01-16 11:35:12 +01:00
2021-09-14 00:13:48 +02:00
set_bool_if_set(this->GenerateForExecutables, "GRAPHVIZ_EXECUTABLES");
set_bool_if_set(this->GenerateForStaticLibs, "GRAPHVIZ_STATIC_LIBS");
set_bool_if_set(this->GenerateForSharedLibs, "GRAPHVIZ_SHARED_LIBS");
set_bool_if_set(this->GenerateForModuleLibs, "GRAPHVIZ_MODULE_LIBS");
set_bool_if_set(this->GenerateForInterfaceLibs, "GRAPHVIZ_INTERFACE_LIBS");
set_bool_if_set(this->GenerateForObjectLibs, "GRAPHVIZ_OBJECT_LIBS");
set_bool_if_set(this->GenerateForUnknownLibs, "GRAPHVIZ_UNKNOWN_LIBS");
set_bool_if_set(this->GenerateForCustomTargets, "GRAPHVIZ_CUSTOM_TARGETS");
set_bool_if_set(this->GenerateForExternals, "GRAPHVIZ_EXTERNAL_LIBS");
set_bool_if_set(this->GeneratePerTarget, "GRAPHVIZ_GENERATE_PER_TARGET");
set_bool_if_set(this->GenerateDependers, "GRAPHVIZ_GENERATE_DEPENDERS");
2011-01-16 11:35:12 +01:00
2015-04-27 22:25:09 +02:00
std::string ignoreTargetsRegexes;
2021-09-14 00:13:48 +02:00
set_if_set(ignoreTargetsRegexes, "GRAPHVIZ_IGNORE_TARGETS");
2011-06-19 15:41:06 +03:00
this->TargetsToIgnoreRegex.clear();
2016-07-09 11:21:54 +02:00
if (!ignoreTargetsRegexes.empty()) {
2020-02-01 23:06:01 +01:00
std::vector<std::string> ignoreTargetsRegExVector =
cmExpandedList(ignoreTargetsRegexes);
2018-01-26 17:06:56 +01:00
for (std::string const& currentRegexString : ignoreTargetsRegExVector) {
2011-06-19 15:41:06 +03:00
cmsys::RegularExpression currentRegex;
2018-04-23 21:13:27 +02:00
if (!currentRegex.compile(currentRegexString)) {
2011-06-19 15:41:06 +03:00
std::cerr << "Could not compile bad regex \"" << currentRegexString
<< "\"" << std::endl;
2011-01-16 11:35:12 +01:00
}
2018-04-23 21:13:27 +02:00
this->TargetsToIgnoreRegex.push_back(std::move(currentRegex));
2011-01-16 11:35:12 +01:00
}
2016-07-09 11:21:54 +02:00
}
2011-06-19 15:41:06 +03:00
}
2020-08-30 11:54:41 +02:00
void cmGraphVizWriter::Write()
2011-06-19 15:41:06 +03:00
{
2021-09-14 00:13:48 +02:00
const auto* gg = this->GlobalGenerator;
2020-08-30 11:54:41 +02:00
this->VisitGraph(gg->GetName());
// We want to traverse in a determined order, such that the output is always
// the same for a given project (this makes tests reproducible, etc.)
std::set<cmGeneratorTarget const*, cmGeneratorTarget::StrictTargetComparison>
sortedGeneratorTargets;
for (const auto& lg : gg->GetLocalGenerators()) {
for (const auto& gt : lg->GetGeneratorTargets()) {
// Reserved targets have inconsistent names across platforms (e.g. 'all'
// vs. 'ALL_BUILD'), which can disrupt the traversal ordering.
// We don't need or want them anyway.
if (!cmGlobalGenerator::IsReservedTarget(gt->GetName())) {
sortedGeneratorTargets.insert(gt.get());
}
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
}
2011-06-19 15:41:06 +03:00
2020-08-30 11:54:41 +02:00
// write global data and collect all connection data for per target graphs
2021-09-14 00:13:48 +02:00
for (const auto* const gt : sortedGeneratorTargets) {
2020-08-30 11:54:41 +02:00
auto item = cmLinkItem(gt, false, gt->GetBacktrace());
this->VisitItem(item);
}
2011-06-19 15:41:06 +03:00
2020-08-30 11:54:41 +02:00
if (this->GeneratePerTarget) {
2021-09-14 00:13:48 +02:00
this->WritePerTargetConnections<DependeesDir>(this->PerTargetConnections);
2020-08-30 11:54:41 +02:00
}
2011-06-19 15:41:06 +03:00
2020-08-30 11:54:41 +02:00
if (this->GenerateDependers) {
2021-09-14 00:13:48 +02:00
this->WritePerTargetConnections<DependersDir>(
this->TargetDependersConnections, ".dependers");
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
}
2020-08-30 11:54:41 +02:00
void cmGraphVizWriter::FindAllConnections(const ConnectionsMap& connectionMap,
const cmLinkItem& rootItem,
Connections& extendedCons,
std::set<cmLinkItem>& visitedItems)
2011-01-16 11:35:12 +01:00
{
2020-08-30 11:54:41 +02:00
// some "targets" are not in map, e.g. linker flags as -lm or
// targets without dependency.
// in both cases we are finished with traversing the graph
if (connectionMap.find(rootItem) == connectionMap.cend()) {
2014-08-03 19:52:23 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2020-08-30 11:54:41 +02:00
const Connections& origCons = connectionMap.at(rootItem);
2011-01-16 11:35:12 +01:00
2020-08-30 11:54:41 +02:00
for (const Connection& con : origCons) {
extendedCons.emplace_back(con);
const cmLinkItem& dstItem = con.dst;
bool const visited = visitedItems.find(dstItem) != visitedItems.cend();
if (!visited) {
visitedItems.insert(dstItem);
2021-09-14 00:13:48 +02:00
this->FindAllConnections(connectionMap, dstItem, extendedCons,
visitedItems);
2016-07-09 11:21:54 +02:00
}
}
2011-01-16 11:35:12 +01:00
}
2020-08-30 11:54:41 +02:00
void cmGraphVizWriter::FindAllConnections(const ConnectionsMap& connectionMap,
const cmLinkItem& rootItem,
Connections& extendedCons)
2011-01-16 11:35:12 +01:00
{
2020-08-30 11:54:41 +02:00
std::set<cmLinkItem> visitedItems = { rootItem };
2021-09-14 00:13:48 +02:00
this->FindAllConnections(connectionMap, rootItem, extendedCons,
visitedItems);
2020-08-30 11:54:41 +02:00
}
2011-01-16 11:35:12 +01:00
2020-08-30 11:54:41 +02:00
template <typename DirFunc>
void cmGraphVizWriter::WritePerTargetConnections(
2021-09-14 00:13:48 +02:00
const ConnectionsMap& connections, const std::string& fileNameSuffix)
2020-08-30 11:54:41 +02:00
{
// the per target connections must be extended by indirect dependencies
ConnectionsMap extendedConnections;
for (auto const& conPerTarget : connections) {
const cmLinkItem& rootItem = conPerTarget.first;
Connections& extendedCons = extendedConnections[conPerTarget.first];
2021-09-14 00:13:48 +02:00
this->FindAllConnections(connections, rootItem, extendedCons);
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
2020-08-30 11:54:41 +02:00
for (auto const& conPerTarget : extendedConnections) {
const cmLinkItem& rootItem = conPerTarget.first;
2011-01-16 11:35:12 +01:00
2020-08-30 11:54:41 +02:00
// some of the nodes are excluded completely and are not written
if (this->ItemExcluded(rootItem)) {
2011-01-16 11:35:12 +01:00
continue;
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
2020-08-30 11:54:41 +02:00
const Connections& cons = conPerTarget.second;
2021-09-14 00:13:48 +02:00
std::unique_ptr<cmGeneratedFileStream> fileStream =
this->CreateTargetFile(rootItem, fileNameSuffix);
2016-07-09 11:21:54 +02:00
2020-08-30 11:54:41 +02:00
for (const Connection& con : cons) {
const cmLinkItem& src = DirFunc::src(con);
const cmLinkItem& dst = DirFunc::dst(con);
this->WriteNode(*fileStream, con.dst);
this->WriteConnection(*fileStream, src, dst, con.scopeType);
}
2021-09-14 00:13:48 +02:00
this->WriteFooter(*fileStream);
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
}
2020-08-30 11:54:41 +02:00
void cmGraphVizWriter::WriteHeader(cmGeneratedFileStream& fs,
const std::string& name)
2011-01-16 11:35:12 +01:00
{
2020-08-30 11:54:41 +02:00
auto const escapedGraphName = EscapeForDotFile(name);
fs << "digraph \"" << escapedGraphName << "\" {\n"
<< this->GraphHeader << '\n';
2011-01-16 11:35:12 +01:00
}
2020-08-30 11:54:41 +02:00
void cmGraphVizWriter::WriteFooter(cmGeneratedFileStream& fs)
2011-01-16 11:35:12 +01:00
{
2020-08-30 11:54:41 +02:00
fs << "}\n";
2011-01-16 11:35:12 +01:00
}
2020-08-30 11:54:41 +02:00
void cmGraphVizWriter::WriteLegend(cmGeneratedFileStream& fs)
2011-01-16 11:35:12 +01:00
{
2020-08-30 11:54:41 +02:00
// Note that the subgraph name must start with "cluster", as done here, to
// make Graphviz layout engines do the right thing and keep the nodes
// together.
/* clang-format off */
fs << "subgraph clusterLegend {\n"
" label = \"Legend\";\n"
// Set the color of the box surrounding the legend.
" color = black;\n"
// We use invisible edges just to enforce the layout.
" edge [ style = invis ];\n"
// Nodes.
" legendNode0 [ label = \"Executable\", shape = "
<< GRAPHVIZ_NODE_SHAPE_EXECUTABLE << " ];\n"
" legendNode1 [ label = \"Static Library\", shape = "
<< GRAPHVIZ_NODE_SHAPE_LIBRARY_STATIC << " ];\n"
" legendNode2 [ label = \"Shared Library\", shape = "
<< GRAPHVIZ_NODE_SHAPE_LIBRARY_SHARED << " ];\n"
" legendNode3 [ label = \"Module Library\", shape = "
<< GRAPHVIZ_NODE_SHAPE_LIBRARY_MODULE << " ];\n"
" legendNode4 [ label = \"Interface Library\", shape = "
<< GRAPHVIZ_NODE_SHAPE_LIBRARY_INTERFACE << " ];\n"
" legendNode5 [ label = \"Object Library\", shape = "
<< GRAPHVIZ_NODE_SHAPE_LIBRARY_OBJECT << " ];\n"
" legendNode6 [ label = \"Unknown Library\", shape = "
<< GRAPHVIZ_NODE_SHAPE_LIBRARY_UNKNOWN << " ];\n"
" legendNode7 [ label = \"Custom Target\", shape = "
<< GRAPHVIZ_NODE_SHAPE_UTILITY << " ];\n"
// Edges.
// Some of those are dummy (invisible) edges to enforce a layout.
" legendNode0 -> legendNode1 [ style = "
<< GRAPHVIZ_EDGE_STYLE_PUBLIC << " ];\n"
" legendNode0 -> legendNode2 [ style = "
<< GRAPHVIZ_EDGE_STYLE_PUBLIC << " ];\n"
" legendNode0 -> legendNode3;\n"
" legendNode1 -> legendNode4 [ label = \"Interface\", style = "
<< GRAPHVIZ_EDGE_STYLE_INTERFACE << " ];\n"
" legendNode2 -> legendNode5 [ label = \"Private\", style = "
<< GRAPHVIZ_EDGE_STYLE_PRIVATE << " ];\n"
" legendNode3 -> legendNode6 [ style = "
<< GRAPHVIZ_EDGE_STYLE_PUBLIC << " ];\n"
" legendNode0 -> legendNode7;\n"
"}\n";
/* clang-format off */
}
2011-01-16 11:35:12 +01:00
2020-08-30 11:54:41 +02:00
void cmGraphVizWriter::WriteNode(cmGeneratedFileStream& fs,
cmLinkItem const& item)
{
auto const& itemName = item.AsStr();
auto const& nodeName = this->NodeNames[itemName];
2011-02-19 15:09:36 +02:00
2021-09-14 00:13:48 +02:00
auto const itemNameWithAliases = this->ItemNameWithAliases(itemName);
2020-08-30 11:54:41 +02:00
auto const escapedLabel = EscapeForDotFile(itemNameWithAliases);
2011-01-16 11:35:12 +01:00
2020-08-30 11:54:41 +02:00
fs << " \"" << nodeName << "\" [ label = \"" << escapedLabel
<< "\", shape = " << getShapeForTarget(item) << " ];\n";
}
2018-01-26 17:06:56 +01:00
2020-08-30 11:54:41 +02:00
void cmGraphVizWriter::WriteConnection(cmGeneratedFileStream& fs,
cmLinkItem const& depender,
cmLinkItem const& dependee,
std::string const& edgeStyle)
{
auto const& dependerName = depender.AsStr();
auto const& dependeeName = dependee.AsStr();
2018-01-26 17:06:56 +01:00
2020-08-30 11:54:41 +02:00
fs << " \"" << this->NodeNames[dependerName] << "\" -> \""
<< this->NodeNames[dependeeName] << "\" "
<< edgeStyle
<< " // " << dependerName << " -> " << dependeeName << '\n';
2011-01-16 11:35:12 +01:00
}
2020-08-30 11:54:41 +02:00
bool cmGraphVizWriter::ItemExcluded(cmLinkItem const& item)
2011-06-19 15:41:06 +03:00
{
2020-08-30 11:54:41 +02:00
auto const itemName = item.AsStr();
2011-06-19 15:41:06 +03:00
2020-08-30 11:54:41 +02:00
if (this->ItemNameFilteredOut(itemName)) {
return true;
2016-07-09 11:21:54 +02:00
}
2011-06-19 15:41:06 +03:00
2020-08-30 11:54:41 +02:00
if (item.Target == nullptr) {
return !this->GenerateForExternals;
2016-07-09 11:21:54 +02:00
}
2011-06-19 15:41:06 +03:00
2020-08-30 11:54:41 +02:00
if (item.Target->GetType() == cmStateEnums::UTILITY) {
if (cmHasLiteralPrefix(itemName, "Nightly") ||
cmHasLiteralPrefix(itemName, "Continuous") ||
cmHasLiteralPrefix(itemName, "Experimental")) {
return true;
2011-06-19 15:41:06 +03:00
}
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
2020-08-30 11:54:41 +02:00
if (item.Target->IsImported() && !this->GenerateForExternals) {
return true;
2011-01-16 11:35:12 +01:00
}
2020-08-30 11:54:41 +02:00
return !this->TargetTypeEnabled(item.Target->GetType());
2011-01-16 11:35:12 +01:00
}
2020-08-30 11:54:41 +02:00
bool cmGraphVizWriter::ItemNameFilteredOut(std::string const& itemName)
2011-01-16 11:35:12 +01:00
{
2020-08-30 11:54:41 +02:00
if (itemName == ">") {
// FIXME: why do we even receive such a target here?
return true;
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
2020-08-30 11:54:41 +02:00
if (cmGlobalGenerator::IsReservedTarget(itemName)) {
return true;
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
2018-01-26 17:06:56 +01:00
for (cmsys::RegularExpression& regEx : this->TargetsToIgnoreRegex) {
2016-07-09 11:21:54 +02:00
if (regEx.is_valid()) {
2020-08-30 11:54:41 +02:00
if (regEx.find(itemName)) {
2011-06-19 15:41:06 +03:00
return true;
2011-01-16 11:35:12 +01:00
}
}
2016-07-09 11:21:54 +02:00
}
2011-06-19 15:41:06 +03:00
return false;
2011-01-16 11:35:12 +01:00
}
2020-08-30 11:54:41 +02:00
bool cmGraphVizWriter::TargetTypeEnabled(
2017-04-14 19:02:05 +02:00
cmStateEnums::TargetType targetType) const
2011-01-16 11:35:12 +01:00
{
2016-07-09 11:21:54 +02:00
switch (targetType) {
2017-04-14 19:02:05 +02:00
case cmStateEnums::EXECUTABLE:
2011-01-16 11:35:12 +01:00
return this->GenerateForExecutables;
2017-04-14 19:02:05 +02:00
case cmStateEnums::STATIC_LIBRARY:
2011-01-16 11:35:12 +01:00
return this->GenerateForStaticLibs;
2017-04-14 19:02:05 +02:00
case cmStateEnums::SHARED_LIBRARY:
2011-01-16 11:35:12 +01:00
return this->GenerateForSharedLibs;
2017-04-14 19:02:05 +02:00
case cmStateEnums::MODULE_LIBRARY:
2011-01-16 11:35:12 +01:00
return this->GenerateForModuleLibs;
2018-01-26 17:06:56 +01:00
case cmStateEnums::INTERFACE_LIBRARY:
2020-08-30 11:54:41 +02:00
return this->GenerateForInterfaceLibs;
case cmStateEnums::OBJECT_LIBRARY:
return this->GenerateForObjectLibs;
case cmStateEnums::UNKNOWN_LIBRARY:
return this->GenerateForUnknownLibs;
case cmStateEnums::UTILITY:
return this->GenerateForCustomTargets;
case cmStateEnums::GLOBAL_TARGET:
// Built-in targets like edit_cache, etc.
// We don't need/want those in the dot file.
return false;
2011-01-16 11:35:12 +01:00
default:
break;
}
return false;
}
2020-08-30 11:54:41 +02:00
std::string cmGraphVizWriter::ItemNameWithAliases(
std::string const& itemName) const
{
2021-09-14 00:13:48 +02:00
std::vector<std::string> items;
2020-08-30 11:54:41 +02:00
for (auto const& lg : this->GlobalGenerator->GetLocalGenerators()) {
for (auto const& aliasTargets : lg->GetMakefile()->GetAliasTargets()) {
if (aliasTargets.second == itemName) {
2021-09-14 00:13:48 +02:00
items.push_back(aliasTargets.first);
2020-08-30 11:54:41 +02:00
}
}
}
2021-09-14 00:13:48 +02:00
std::sort(items.begin(), items.end());
items.erase(std::unique(items.begin(), items.end()), items.end());
auto nameWithAliases = itemName;
for(auto const& item : items) {
nameWithAliases += "\\n(" + item + ")";
}
2020-08-30 11:54:41 +02:00
return nameWithAliases;
}
std::string cmGraphVizWriter::GetEdgeStyle(DependencyType dt)
{
std::string style;
switch (dt) {
case DependencyType::LinkPrivate:
style = "[ style = " + std::string(GRAPHVIZ_EDGE_STYLE_PRIVATE) + " ]";
break;
case DependencyType::LinkInterface:
style = "[ style = " + std::string(GRAPHVIZ_EDGE_STYLE_INTERFACE) + " ]";
break;
default:
break;
}
return style;
}
std::string cmGraphVizWriter::EscapeForDotFile(std::string const& str)
{
return cmSystemTools::EscapeChars(str.data(), "\"");
}
std::string cmGraphVizWriter::PathSafeString(std::string const& str)
{
std::string pathSafeStr;
// We'll only keep alphanumerical characters, plus the following ones that
// are common, and safe on all platforms:
auto const extra_chars = std::set<char>{ '.', '-', '_' };
for (char c : str) {
if (std::isalnum(c) || extra_chars.find(c) != extra_chars.cend()) {
pathSafeStr += c;
}
}
return pathSafeStr;
}