use2dot.scm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. ;;; use2dot --- Display module dependencies as a DOT specification
  2. ;; Copyright (C) 2001, 2006, 2011, 2020 Free Software Foundation, Inc.
  3. ;;
  4. ;; This program is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public License
  6. ;; as published by the Free Software Foundation; either version 3, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this software; see the file COPYING.LESSER. If
  16. ;; not, write to the Free Software Foundation, Inc., 51 Franklin
  17. ;; Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;; Author: Thien-Thi Nguyen
  19. ;;; Commentary:
  20. ;; Usage: use2dot [OPTIONS] [FILE ...]
  21. ;; Display to stdout a DOT specification that describes module dependencies
  22. ;; in FILEs.
  23. ;;
  24. ;; A top-level `use-modules' form or a `:use-module' `define-module'-component
  25. ;; results in a "solid" style edge.
  26. ;;
  27. ;; An `:autoload' `define-module'-component results in a "dotted" style edge
  28. ;; with label "N" indicating that N names are responsible for triggering the
  29. ;; autoload. [The "N" label is not implemented.]
  30. ;;
  31. ;; A top-level `load' or `primitive-load' form results in a a "bold" style
  32. ;; edge to a node named with either the file name if the `load' argument is a
  33. ;; string, or "[computed in FILE]" otherwise.
  34. ;;
  35. ;; Options:
  36. ;; -m, --default-module MOD -- Set MOD as the default module (for top-level
  37. ;; `use-modules' forms that do not follow some
  38. ;; `define-module' form in a file). MOD should be
  39. ;; be a list or `#f', in which case such top-level
  40. ;; `use-modules' forms are effectively ignored.
  41. ;; Default value: `(guile-user)'.
  42. ;;; Code:
  43. (define-module (scripts use2dot)
  44. #:use-module (ice-9 getopt-long)
  45. #:use-module ((srfi srfi-13) #:select (string-join))
  46. #:use-module ((scripts frisk)
  47. #:select (make-frisker edge-type edge-up edge-down))
  48. #:export (use2dot))
  49. (define %summary "Print a module's dependencies in graphviz format.")
  50. (define *default-module* '(guile-user))
  51. (define (q s) ; quote
  52. (format #f "~S" s))
  53. (define (vv pairs) ; => ("var=val" ...)
  54. (map (lambda (pair)
  55. (format #f "~A=~A" (car pair) (cdr pair)))
  56. pairs))
  57. (define (>>header)
  58. (format #t "digraph use2dot {\n")
  59. (for-each (lambda (s) (format #t " ~A;\n" s))
  60. (vv `((label . ,(q "Guile Module Dependencies"))
  61. ;;(rankdir . LR)
  62. ;;(size . ,(q "7.5,10"))
  63. (ratio . fill)
  64. ;;(nodesep . ,(q "0.05"))
  65. ))))
  66. (define (>>body edges)
  67. (for-each
  68. (lambda (edge)
  69. (format #t " \"~A\" -> \"~A\"" (edge-down edge) (edge-up edge))
  70. (cond ((case (edge-type edge)
  71. ((autoload) '((style . dotted) (fontsize . 5)))
  72. ((computed) '((style . bold)))
  73. (else #f))
  74. => (lambda (etc)
  75. (format #t " [~A]" (string-join (vv etc) ",")))))
  76. (format #t ";\n"))
  77. edges))
  78. (define (>>footer)
  79. (format #t "}"))
  80. (define (>> edges)
  81. (>>header)
  82. (>>body edges)
  83. (>>footer))
  84. (define (use2dot . args)
  85. (let* ((parsed-args (getopt-long (cons "use2dot" args) ;;; kludge
  86. '((default-module
  87. (single-char #\m) (value #t)))))
  88. (=m (option-ref parsed-args 'default-module *default-module*))
  89. (scan (make-frisker `(default-module . ,=m)))
  90. (files (option-ref parsed-args '() '())))
  91. (>> (reverse ((scan files) 'edges)))))
  92. (define main use2dot)
  93. ;;; use2dot ends here