generate-autoload.scm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. ;;; generate-autoload --- Display define-module form with autoload info
  2. ;; Copyright (C) 2001, 2006 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: generate-autoload [OPTIONS] FILE1 FILE2 ...
  21. ;;
  22. ;; The autoload form is displayed to standard output:
  23. ;;
  24. ;; (define-module (guile-user)
  25. ;; :autoload (ZAR FOO) (FOO-1 FOO-2 ...)
  26. ;; :
  27. ;; :
  28. ;; :autoload (ZAR BAR) (BAR-1 BAR-2 ...))
  29. ;;
  30. ;; For each file, a symbol triggers an autoload if it is found in one
  31. ;; of these situations:
  32. ;; - in the `:export' clause of a `define-module' form
  33. ;; - in a top-level `export' or `export-syntax' form
  34. ;; - in a `define-public' form
  35. ;; - in a `defmacro-public' form
  36. ;;
  37. ;; The module name is inferred from the `define-module' form. If either the
  38. ;; module name or the exports list cannot be determined, no autoload entry is
  39. ;; generated for that file.
  40. ;;
  41. ;; Options:
  42. ;; --target MODULE-NAME -- Use MODULE-NAME instead of `(guile-user)'.
  43. ;; Note that some shells may require you to
  44. ;; quote the argument to handle parentheses
  45. ;; and spaces.
  46. ;;
  47. ;; Usage examples from Scheme code as a module:
  48. ;; (use-modules (scripts generate-autoload))
  49. ;; (generate-autoload "generate-autoload")
  50. ;; (generate-autoload "--target" "(my module)" "generate-autoload")
  51. ;; (apply generate-autoload "--target" "(my module)" '("foo" "bar" "baz"))
  52. ;;; Code:
  53. (define-module (scripts generate-autoload)
  54. :export (generate-autoload))
  55. (define (autoload-info file)
  56. (let ((p (open-input-file file)))
  57. (let loop ((form (read p)) (module-name #f) (exports '()))
  58. (if (eof-object? form)
  59. (and module-name
  60. (not (null? exports))
  61. (list module-name exports)) ; ret
  62. (cond ((and (list? form)
  63. (< 1 (length form))
  64. (eq? 'define-module (car form)))
  65. (loop (read p)
  66. (cadr form)
  67. (cond ((member ':export form)
  68. => (lambda (val)
  69. (append (cadr val) exports)))
  70. (else exports))))
  71. ((and (list? form)
  72. (< 1 (length form))
  73. (memq (car form) '(export export-syntax)))
  74. (loop (read p)
  75. module-name
  76. (append (cdr form) exports)))
  77. ((and (list? form)
  78. (< 2 (length form))
  79. (eq? 'define-public (car form))
  80. (list? (cadr form))
  81. (symbol? (caadr form)))
  82. (loop (read p)
  83. module-name
  84. (cons (caadr form) exports)))
  85. ((and (list? form)
  86. (< 2 (length form))
  87. (eq? 'define-public (car form))
  88. (symbol? (cadr form)))
  89. (loop (read p)
  90. module-name
  91. (cons (cadr form) exports)))
  92. ((and (list? form)
  93. (< 3 (length form))
  94. (eq? 'defmacro-public (car form))
  95. (symbol? (cadr form)))
  96. (loop (read p)
  97. module-name
  98. (cons (cadr form) exports)))
  99. (else (loop (read p) module-name exports)))))))
  100. (define (generate-autoload . args)
  101. (let* ((module-count 0)
  102. (syms-count 0)
  103. (target-override (cond ((member "--target" args) => cadr)
  104. (else #f)))
  105. (files (if target-override (cddr args) (cdr args))))
  106. (display ";;; do not edit --- generated ")
  107. (display (strftime "%Y-%m-%d %H:%M:%S" (localtime (current-time))))
  108. (newline)
  109. (display "(define-module ")
  110. (display (or target-override "(guile-user)"))
  111. (for-each (lambda (file)
  112. (cond ((autoload-info file)
  113. => (lambda (info)
  114. (and info
  115. (apply (lambda (module-name exports)
  116. (set! module-count (1+ module-count))
  117. (set! syms-count (+ (length exports)
  118. syms-count))
  119. (for-each display
  120. (list "\n :autoload "
  121. module-name " "
  122. exports)))
  123. info))))))
  124. files)
  125. (display ")")
  126. (newline)
  127. (for-each display (list " ;;; "
  128. syms-count " symbols in "
  129. module-count " modules\n"))))
  130. (define main generate-autoload)
  131. ;;; generate-autoload ends here