depends.nim 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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, modulepaths,
  12. pathutils
  13. from modulegraphs import ModuleGraph, PPassContext
  14. type
  15. TGen = object of PPassContext
  16. module: PSym
  17. config: ConfigRef
  18. graph: ModuleGraph
  19. PGen = ref TGen
  20. Backend = ref object of RootRef
  21. dotGraph: Rope
  22. proc addDependencyAux(b: Backend; importing, imported: string) =
  23. addf(b.dotGraph, "$1 -> \"$2\";$n", [rope(importing), rope(imported)])
  24. # s1 -> s2_4[label="[0-9]"];
  25. proc addDotDependency(c: PPassContext, n: PNode): PNode =
  26. result = n
  27. let g = PGen(c)
  28. let b = Backend(g.graph.backend)
  29. case n.kind
  30. of nkImportStmt:
  31. for i in countup(0, sonsLen(n) - 1):
  32. var imported = getModuleName(g.config, n.sons[i])
  33. addDependencyAux(b, g.module.name.s, imported)
  34. of nkFromStmt, nkImportExceptStmt:
  35. var imported = getModuleName(g.config, n.sons[0])
  36. addDependencyAux(b, g.module.name.s, imported)
  37. of nkStmtList, nkBlockStmt, nkStmtListExpr, nkBlockExpr:
  38. for i in countup(0, sonsLen(n) - 1): discard addDotDependency(c, n.sons[i])
  39. else:
  40. discard
  41. proc generateDot*(graph: ModuleGraph; project: AbsoluteFile) =
  42. let b = Backend(graph.backend)
  43. discard writeRope("digraph $1 {$n$2}$n" % [
  44. rope(project.splitFile.name), b.dotGraph],
  45. changeFileExt(project, "dot"))
  46. proc myOpen(graph: ModuleGraph; module: PSym): PPassContext =
  47. var g: PGen
  48. new(g)
  49. g.module = module
  50. g.config = graph.config
  51. g.graph = graph
  52. if graph.backend == nil:
  53. graph.backend = Backend(dotGraph: nil)
  54. result = g
  55. const gendependPass* = makePass(open = myOpen, process = addDotDependency)