use2dot 4.0 KB

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