generate-autoload.scm 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. ;;; generate-autoload --- Display define-module form with autoload info
  2. ;; Copyright (C) 2001, 2006, 2011 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 %include-in-guild-list #f)
  56. (define %summary "Generate #:autoload clauses for a module.")
  57. (define (autoload-info file)
  58. (let ((p (open-input-file file)))
  59. (let loop ((form (read p)) (module-name #f) (exports '()))
  60. (if (eof-object? form)
  61. (and module-name
  62. (not (null? exports))
  63. (list module-name exports)) ; ret
  64. (cond ((and (list? form)
  65. (< 1 (length form))
  66. (eq? 'define-module (car form)))
  67. (loop (read p)
  68. (cadr form)
  69. (cond ((member ':export form)
  70. => (lambda (val)
  71. (append (cadr val) exports)))
  72. (else exports))))
  73. ((and (list? form)
  74. (< 1 (length form))
  75. (memq (car form) '(export export-syntax)))
  76. (loop (read p)
  77. module-name
  78. (append (cdr form) exports)))
  79. ((and (list? form)
  80. (< 2 (length form))
  81. (eq? 'define-public (car form))
  82. (list? (cadr form))
  83. (symbol? (caadr form)))
  84. (loop (read p)
  85. module-name
  86. (cons (caadr form) exports)))
  87. ((and (list? form)
  88. (< 2 (length form))
  89. (eq? 'define-public (car form))
  90. (symbol? (cadr form)))
  91. (loop (read p)
  92. module-name
  93. (cons (cadr form) exports)))
  94. ((and (list? form)
  95. (< 3 (length form))
  96. (eq? 'defmacro-public (car form))
  97. (symbol? (cadr form)))
  98. (loop (read p)
  99. module-name
  100. (cons (cadr form) exports)))
  101. (else (loop (read p) module-name exports)))))))
  102. (define (generate-autoload . args)
  103. (let* ((module-count 0)
  104. (syms-count 0)
  105. (target-override (cond ((member "--target" args) => cadr)
  106. (else #f)))
  107. (files (if target-override (cddr args) (cdr args))))
  108. (display ";;; do not edit --- generated ")
  109. (display (strftime "%Y-%m-%d %H:%M:%S" (localtime (current-time))))
  110. (newline)
  111. (display "(define-module ")
  112. (display (or target-override "(guile-user)"))
  113. (for-each (lambda (file)
  114. (cond ((autoload-info file)
  115. => (lambda (info)
  116. (and info
  117. (apply (lambda (module-name exports)
  118. (set! module-count (1+ module-count))
  119. (set! syms-count (+ (length exports)
  120. syms-count))
  121. (for-each display
  122. (list "\n :autoload "
  123. module-name " "
  124. exports)))
  125. info))))))
  126. files)
  127. (display ")")
  128. (newline)
  129. (for-each display (list " ;;; "
  130. syms-count " symbols in "
  131. module-count " modules\n"))))
  132. (define main generate-autoload)
  133. ;;; generate-autoload ends here