* Added Jost to THANKS and AUTHORS list.
[bpt/guile.git] / scripts / generate-autoload
1 #!/bin/sh
2 # aside from this initial boilerplate, this is actually -*- scheme -*- code
3 main='(module-ref (resolve-module '\''(scripts generate-autoload)) '\'main')'
4 exec ${GUILE-guile} -c "(apply $main (cdr (command-line)))" "$@"
5 !#
6 ;;; generate-autoload --- Display define-module form with autoload info
7
8 ;; Copyright (C) 2001 Free Software Foundation, Inc.
9 ;;
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or
13 ;; (at your option) any later version.
14 ;;
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
19 ;;
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this software; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23 ;; Boston, MA 02111-1307 USA
24
25 ;;; Author: Thien-Thi Nguyen
26
27 ;;; Commentary:
28
29 ;; Usage: generate-autoload [OPTIONS] FILE1 FILE2 ...
30 ;;
31 ;; The autoload form is displayed to standard output:
32 ;;
33 ;; (define-module (guile-user)
34 ;; :autoload (ZAR FOO) (FOO-1 FOO-2 ...)
35 ;; :
36 ;; :
37 ;; :autoload (ZAR BAR) (BAR-1 BAR-2 ...))
38 ;;
39 ;; For each file, a symbol triggers an autoload if it is found in one
40 ;; of these situations:
41 ;; - in the `:export' clause of a `define-module' form;
42 ;; - in a top-level `export' or `export-syntax' form;
43 ;; - in a `define-public' form.
44 ;;
45 ;; The module name is inferred from the `define-module' form. If either the
46 ;; module name or the exports list cannot be determined, no autoload entry is
47 ;; generated for that file.
48 ;;
49 ;; Options:
50 ;; --target MODULE-NAME -- Use MODULE-NAME instead of `(guile-user)'.
51 ;; Note that some shells may require you to
52 ;; quote the argument to handle parentheses
53 ;; and spaces.
54 ;;
55 ;; Usage examples from Scheme code as a module:
56 ;; (use-modules (scripts generate-autoload))
57 ;; (generate-autoload "generate-autoload")
58 ;; (generate-autoload "--target" "(my module)" "generate-autoload")
59 ;; (apply generate-autoload "--target" "(my module)" '("foo" "bar" "baz"))
60
61 ;;; Code:
62
63 (define-module (scripts generate-autoload)
64 :export (generate-autoload))
65
66 (define (autoload-info file)
67 (let ((p (open-input-file file)))
68 (let loop ((form (read p)) (module-name #f) (exports '()))
69 (if (eof-object? form)
70 (and module-name
71 (not (null? exports))
72 (list module-name exports)) ; ret
73 (cond ((and (list? form)
74 (< 1 (length form))
75 (eq? 'define-module (car form)))
76 (loop (read p)
77 (cadr form)
78 (cond ((member ':export form)
79 => (lambda (val)
80 (append (cadr val) exports)))
81 (else exports))))
82 ((and (list? form)
83 (< 1 (length form))
84 (memq (car form) '(export export-syntax)))
85 (loop (read p)
86 module-name
87 (append (cdr form) exports)))
88 ((and (list? form)
89 (< 2 (length form))
90 (eq? 'define-public (car form))
91 (list? (cadr form))
92 (symbol? (caadr form)))
93 (loop (read p)
94 module-name
95 (cons (caadr form) exports)))
96 ((and (list? form)
97 (< 2 (length form))
98 (eq? 'define-public (car form))
99 (symbol? (cadr form)))
100 (loop (read p)
101 module-name
102 (cons (cadr form) exports)))
103 (else (loop (read p) module-name exports)))))))
104
105 (define (generate-autoload . args)
106 (let* ((module-count 0)
107 (syms-count 0)
108 (target-override (cond ((member "--target" args) => cadr)
109 (else #f)))
110 (files (if target-override (cddr args) (cdr args))))
111 (display ";;; do not edit --- generated ")
112 (display (strftime "%Y-%m-%d %H:%M:%S" (localtime (current-time))))
113 (newline)
114 (display "(define-module ")
115 (display (or target-override "(guile-user)"))
116 (for-each (lambda (file)
117 (cond ((autoload-info file)
118 => (lambda (info)
119 (and info
120 (apply (lambda (module-name exports)
121 (set! module-count (1+ module-count))
122 (set! syms-count (+ (length exports)
123 syms-count))
124 (for-each display
125 (list "\n :autoload "
126 module-name " "
127 exports)))
128 info))))))
129 files)
130 (display ")")
131 (newline)
132 (for-each display (list " ;;; "
133 syms-count " symbols in "
134 module-count " modules\n"))))
135
136 (define main generate-autoload)
137
138 ;;; generate-autoload ends here