generate-autoload 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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} -l $0 -c "(apply $main (cdr (command-line)))" "$@"
  5. !#
  6. ;;; generate-autoload --- Display define-module form with autoload info
  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: generate-autoload [OPTIONS] FILE1 FILE2 ...
  26. ;;
  27. ;; The autoload form is displayed to standard output:
  28. ;;
  29. ;; (define-module (guile-user)
  30. ;; :autoload (ZAR FOO) (FOO-1 FOO-2 ...)
  31. ;; :
  32. ;; :
  33. ;; :autoload (ZAR BAR) (BAR-1 BAR-2 ...))
  34. ;;
  35. ;; For each file, a symbol triggers an autoload if it is found in one
  36. ;; of these situations:
  37. ;; - in the `:export' clause of a `define-module' form
  38. ;; - in a top-level `export' or `export-syntax' form
  39. ;; - in a `define-public' form
  40. ;; - in a `defmacro-public' form
  41. ;;
  42. ;; The module name is inferred from the `define-module' form. If either the
  43. ;; module name or the exports list cannot be determined, no autoload entry is
  44. ;; generated for that file.
  45. ;;
  46. ;; Options:
  47. ;; --target MODULE-NAME -- Use MODULE-NAME instead of `(guile-user)'.
  48. ;; Note that some shells may require you to
  49. ;; quote the argument to handle parentheses
  50. ;; and spaces.
  51. ;;
  52. ;; Usage examples from Scheme code as a module:
  53. ;; (use-modules (scripts generate-autoload))
  54. ;; (generate-autoload "generate-autoload")
  55. ;; (generate-autoload "--target" "(my module)" "generate-autoload")
  56. ;; (apply generate-autoload "--target" "(my module)" '("foo" "bar" "baz"))
  57. ;;; Code:
  58. (define-module (scripts generate-autoload)
  59. :export (generate-autoload))
  60. (define (autoload-info file)
  61. (let ((p (open-input-file file)))
  62. (let loop ((form (read p)) (module-name #f) (exports '()))
  63. (if (eof-object? form)
  64. (and module-name
  65. (not (null? exports))
  66. (list module-name exports)) ; ret
  67. (cond ((and (list? form)
  68. (< 1 (length form))
  69. (eq? 'define-module (car form)))
  70. (loop (read p)
  71. (cadr form)
  72. (cond ((member ':export form)
  73. => (lambda (val)
  74. (append (cadr val) exports)))
  75. (else exports))))
  76. ((and (list? form)
  77. (< 1 (length form))
  78. (memq (car form) '(export export-syntax)))
  79. (loop (read p)
  80. module-name
  81. (append (cdr form) exports)))
  82. ((and (list? form)
  83. (< 2 (length form))
  84. (eq? 'define-public (car form))
  85. (list? (cadr form))
  86. (symbol? (caadr form)))
  87. (loop (read p)
  88. module-name
  89. (cons (caadr form) exports)))
  90. ((and (list? form)
  91. (< 2 (length form))
  92. (eq? 'define-public (car form))
  93. (symbol? (cadr form)))
  94. (loop (read p)
  95. module-name
  96. (cons (cadr form) exports)))
  97. ((and (list? form)
  98. (< 3 (length form))
  99. (eq? 'defmacro-public (car form))
  100. (symbol? (cadr form)))
  101. (loop (read p)
  102. module-name
  103. (cons (cadr form) exports)))
  104. (else (loop (read p) module-name exports)))))))
  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. (define main generate-autoload)
  136. ;;; generate-autoload ends here