Skip to content

Commit 5268b70

Browse files
[Util] Rewrite loading Graphviz symbols
We don't need the `graphviz/gvc.h` anymore. We now declare all required functions ourselves.
1 parent 2ce65d8 commit 5268b70

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/util/DotTool.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
#include <mutable/util/fn.hpp>
44
#include <mutable/util/macro.hpp>
5-
#include <cstdio>
65
#include <fstream>
7-
#include <graphviz/gvc.h>
86
#include <iostream>
97
#include <sstream>
108

@@ -19,6 +17,19 @@
1917

2018
using namespace m;
2119

20+
// POD type forward declarations
21+
struct GVC_t;
22+
struct Agraph_t;
23+
struct graph_t;
24+
25+
// function declarations: add the functions that you need here and to the `SYMBOLS` X macro
26+
static int(*agclose)(Agraph_t*);
27+
static Agraph_t*(*agmemread)(const char*);
28+
static GVC_t*(*gvContext)();
29+
static int(*gvFreeContext)(GVC_t*);
30+
static int(*gvFreeLayout)(GVC_t*, graph_t*);
31+
static int(*gvLayout)(GVC_t*, graph_t*, const char*);
32+
static int(*gvRenderFilename)(GVC_t*, graph_t*, const char*, const char*);
2233

2334
#define SYMBOLS(X) \
2435
X(agclose) \
@@ -29,18 +40,16 @@ using namespace m;
2940
X(gvLayout) \
3041
X(gvRenderFilename)
3142

32-
#define DECLSYM(SYM) static decltype(SYM) *sym_##SYM;
33-
#define LOADSYM(SYM) { \
34-
sym_##SYM = (decltype(SYM)*)(dlsym(libgraphviz, #SYM)); \
35-
}
43+
#define LOADSYM(SYM) SYM = (decltype(SYM))(dlsym(libgraphviz, #SYM));
44+
3645
#if __linux
3746
static constexpr const char * LIB_GRAPHVIZ = "libgvc.so";
3847
#elif __APPLE__
3948
static constexpr const char * LIB_GRAPHVIZ = "libgvc.dylib";
4049
#endif
50+
4151
static void *libgraphviz;
4252
static GVC_t *gvc;
43-
SYMBOLS(DECLSYM);
4453

4554
DotTool::DotTool(Diagnostic &diag)
4655
: diag(diag)
@@ -54,7 +63,7 @@ DotTool::DotTool(Diagnostic &diag)
5463
if (libgraphviz) {
5564
/* Load the required symbols from the shared object. */
5665
SYMBOLS(LOADSYM);
57-
gvc = sym_gvContext();
66+
gvc = gvContext();
5867
}
5968
}
6069
#endif
@@ -64,11 +73,11 @@ int DotTool::render_to_pdf(const char *path_to_pdf, const char *algo)
6473
{
6574
/*----- Render the dot graph with graphviz. ----------------------------------------------------------------------*/
6675
auto dotstr = stream_.str();
67-
Agraph_t *G = M_notnull(sym_agmemread(dotstr.c_str()));
68-
sym_gvLayout(gvc, G, algo);
69-
auto ret = sym_gvRenderFilename(gvc, G, "pdf", path_to_pdf);
70-
sym_gvFreeLayout(gvc, G);
71-
sym_agclose(G);
76+
Agraph_t *G = M_notnull(agmemread(dotstr.c_str()));
77+
gvLayout(gvc, (graph_t*) G, algo);
78+
auto ret = gvRenderFilename(gvc, (graph_t*) G, "pdf", path_to_pdf);
79+
gvFreeLayout(gvc, (graph_t*) G);
80+
agclose(G);
7281
return ret;
7382
}
7483

0 commit comments

Comments
 (0)