1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
#!/bin/sh
# aside from this initial boilerplate, this is actually -*- scheme -*- code
main='(module-ref (resolve-module '\''(scripts use2dot)) '\'main')'
exec ${GUILE-guile} -l $0 -c "(apply $main (cdr (command-line)))" "$@"
!#
;;; use2dot --- Display module dependencies as a DOT specification
;; Copyright (C) 2001 Free Software Foundation, Inc.
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this software; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
;; Boston, MA 02111-1307 USA
;;; Author: Thien-Thi Nguyen
;;; Commentary:
;; Usage: use2dot [OPTIONS] [FILE ...]
;; Display to stdout a DOT specification that describes module dependencies
;; in FILEs.
;;
;; A top-level `use-modules' form or a `:use-module' `define-module'-component
;; results in a "solid" style edge.
;;
;; An `:autoload' `define-module'-component results in a "dotted" style edge
;; with label "N" indicating that N names are responsible for triggering the
;; autoload.
;;
;; A top-level `load' or `primitive-load' form results in a a "bold" style
;; edge to a node named with either the file name if the `load' argument is a
;; string, or "[computed in FILE]" otherwise.
;;
;; Options:
;; --default-module MOD -- Set MOD as the default module (for top-level
;; `use-modules' forms that do not follow some
;; `define-module' form in a file). MOD should be
;; be a list or `#f', in which case such top-level
;; `use-modules' forms are effectively ignored.
;; Default value: `(guile)'.
;;
;; TODO
;; - add `--load-synonyms' option
;; - add `--ignore-module' option
;; - handle arbitrary command-line key/value configuration
;;; Code:
(define-module (scripts use2dot)
:use-module (ice-9 regex))
(define (string-append/separator separator strings)
;; from (ttn stringutils) -- todo: use srfi-13
;; "Append w/ SEPARATOR a list of STRINGS.
;; SEPARATOR can be a character or a string."
(let ((rev (reverse strings))
(sep (if (char? separator)
(make-string 1 separator)
separator)))
(apply string-append
(let loop ((s (cdr rev))
(acc (list (car rev))))
(if (null? s)
acc
(loop (cdr s)
(cons (car s)
(cons sep acc))))))))
(define (mapconcat proc ls sep)
;; from (ttn stringutils) -- todo: use srfi-13
;; "Map PROC over LS, concatening resulting strings with separator SEP."
(string-append/separator sep (map proc ls)))
(define default-module '(guile))
(define (q s) ; quote
(format #f "~S" s))
(define (vv pair) ; var=val
(format #f "~A=~A" (car pair) (cdr pair)))
(define (spew module use . etc)
(and module
(let ((etc-spec (if (null? etc)
""
(format #f " [~A]" (mapconcat vv etc ",")))))
(format #t " \"~A\" -> \"~A\"~A;\n" module use etc-spec))))
(define (header)
(format #t "digraph use2dot {")
(for-each (lambda (s) (format #t " ~A;\n" s))
(map vv `((label . ,(q "Guile Module Dependencies"))
;(rankdir . LR)
;(size . ,(q "7.5,10"))
(ratio . fill)
;(nodesep . ,(q "0.05"))
))))
(define (grok filename)
(let* ((p (open-file filename "r"))
(next (lambda () (read p)))
(curmod #f))
(let loop ((form (next)))
(cond ((eof-object? form))
((not (list? form)) (loop (next)))
(else (case (car form)
((define-module)
(let ((module (cadr form)))
(set! curmod module)
(let loop ((ls form))
(or (null? ls)
(case (car ls)
((:use-module)
(spew module (cadr ls))
(loop (cddr ls)))
((:autoload)
(spew module (cadr ls)
'(style . dotted)
'(fontsize . 5)
(let ((len (length (caddr ls))))
`(label . ,(q (number->string len)))))
(loop (cdddr ls)))
(else (loop (cdr ls))))))))
((use-modules)
(for-each (lambda (use)
(spew (or curmod default-module) use))
(cdr form)))
((load primitive-load)
(spew (or curmod default-module)
(let ((file (cadr form)))
(if (string? file)
file
(format #f "[computed in ~A]" filename)))
'(style . bold))))
(loop (next)))))))
(define (body files)
(for-each grok files))
(define (footer)
(format #t "}"))
(define (use2dot . args)
(header)
(let* ((override (cond ((member "--default-module" args) => cadr)
(else #f)))
(files (if override (cddr args) args)))
(and override
(set! default-module
(with-input-from-string override (lambda () (read)))))
(body files))
(footer))
(define main use2dot)
;;; use2dot ends here
|