depends.nim 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # This module implements a dependency file generator.
  10. import
  11. os, options, ast, astalgo, msgs, ropes, idents, passes, importer
  12. from modulegraphs import ModuleGraph
  13. proc generateDot*(project: string)
  14. type
  15. TGen = object of TPassContext
  16. module*: PSym
  17. PGen = ref TGen
  18. var gDotGraph: Rope # the generated DOT file; we need a global variable
  19. proc addDependencyAux(importing, imported: string) =
  20. addf(gDotGraph, "$1 -> \"$2\";$n", [rope(importing), rope(imported)])
  21. # s1 -> s2_4[label="[0-9]"];
  22. proc addDotDependency(c: PPassContext, n: PNode): PNode =
  23. result = n
  24. var g = PGen(c)
  25. case n.kind
  26. of nkImportStmt:
  27. for i in countup(0, sonsLen(n) - 1):
  28. var imported = getModuleName(n.sons[i])
  29. addDependencyAux(g.module.name.s, imported)
  30. of nkFromStmt, nkImportExceptStmt:
  31. var imported = getModuleName(n.sons[0])
  32. addDependencyAux(g.module.name.s, imported)
  33. of nkStmtList, nkBlockStmt, nkStmtListExpr, nkBlockExpr:
  34. for i in countup(0, sonsLen(n) - 1): discard addDotDependency(c, n.sons[i])
  35. else:
  36. discard
  37. proc generateDot(project: string) =
  38. writeRope("digraph $1 {$n$2}$n" % [
  39. rope(changeFileExt(extractFilename(project), "")), gDotGraph],
  40. changeFileExt(project, "dot"))
  41. proc myOpen(graph: ModuleGraph; module: PSym; cache: IdentCache): PPassContext =
  42. var g: PGen
  43. new(g)
  44. g.module = module
  45. result = g
  46. const gendependPass* = makePass(open = myOpen, process = addDotDependency)