summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/depends.nim25
-rw-r--r--compiler/main.nim2
2 files changed, 17 insertions, 10 deletions
diff --git a/compiler/depends.nim b/compiler/depends.nim
index 38b84aafc..34ab5b157 100644
--- a/compiler/depends.nim
+++ b/compiler/depends.nim
@@ -16,35 +16,39 @@ from modulegraphs import ModuleGraph
 
 type
   TGen = object of TPassContext
-    module*: PSym
+    module: PSym
     config: ConfigRef
+    graph: ModuleGraph
   PGen = ref TGen
 
-var gDotGraph: Rope # the generated DOT file; we need a global variable
+  Backend = ref object of RootRef
+    dotGraph: Rope
 
-proc addDependencyAux(importing, imported: string) =
-  addf(gDotGraph, "$1 -> \"$2\";$n", [rope(importing), rope(imported)])
+proc addDependencyAux(b: Backend; importing, imported: string) =
+  addf(b.dotGraph, "$1 -> \"$2\";$n", [rope(importing), rope(imported)])
   # s1 -> s2_4[label="[0-9]"];
 
 proc addDotDependency(c: PPassContext, n: PNode): PNode =
   result = n
-  var g = PGen(c)
+  let g = PGen(c)
+  let b = Backend(g.graph.backend)
   case n.kind
   of nkImportStmt:
     for i in countup(0, sonsLen(n) - 1):
       var imported = getModuleName(g.config, n.sons[i])
-      addDependencyAux(g.module.name.s, imported)
+      addDependencyAux(b, g.module.name.s, imported)
   of nkFromStmt, nkImportExceptStmt:
     var imported = getModuleName(g.config, n.sons[0])
-    addDependencyAux(g.module.name.s, imported)
+    addDependencyAux(b, g.module.name.s, imported)
   of nkStmtList, nkBlockStmt, nkStmtListExpr, nkBlockExpr:
     for i in countup(0, sonsLen(n) - 1): discard addDotDependency(c, n.sons[i])
   else:
     discard
 
-proc generateDot*(project: string) =
+proc generateDot*(graph: ModuleGraph; project: string) =
+  let b = Backend(graph.backend)
   discard writeRope("digraph $1 {$n$2}$n" % [
-      rope(changeFileExt(extractFilename(project), "")), gDotGraph],
+      rope(changeFileExt(extractFilename(project), "")), b.dotGraph],
             changeFileExt(project, "dot"))
 
 proc myOpen(graph: ModuleGraph; module: PSym; cache: IdentCache): PPassContext =
@@ -52,6 +56,9 @@ proc myOpen(graph: ModuleGraph; module: PSym; cache: IdentCache): PPassContext =
   new(g)
   g.module = module
   g.config = graph.config
+  g.graph = graph
+  if graph.backend == nil:
+    graph.backend = Backend(dotGraph: nil)
   result = g
 
 const gendependPass* = makePass(open = myOpen, process = addDotDependency)
diff --git a/compiler/main.nim b/compiler/main.nim
index 3b5370fa5..2489b1c50 100644
--- a/compiler/main.nim
+++ b/compiler/main.nim
@@ -48,7 +48,7 @@ proc commandGenDepend(graph: ModuleGraph; cache: IdentCache) =
   compileProject(graph, cache)
   let project = graph.config.projectFull
   writeDepsFile(graph, project)
-  generateDot(project)
+  generateDot(graph, project)
   execExternalProgram(graph.config, "dot -Tpng -o" & changeFileExt(project, "png") &
       ' ' & changeFileExt(project, "dot"))